Skip to content

Commit

Permalink
refactor: simplify interface
Browse files Browse the repository at this point in the history
  • Loading branch information
BasileiosKal committed Jan 15, 2024
1 parent 1fc4412 commit d941eef
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 433 deletions.
27 changes: 5 additions & 22 deletions src/common/hash_param/h2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,19 @@ use crate::{
Error,
};

use super::{
constant::{DEFAULT_DST_SUFFIX_H2S, XOF_NO_OF_BYTES},
ExpandMessageParameter,
};
use super::{constant::XOF_NO_OF_BYTES, ExpandMessageParameter};

pub(crate) trait HashToScalarParameter: ExpandMessageParameter {
/// Default domain separation tag for `hash_to_scalar` operation.
fn default_hash_to_scalar_dst() -> Vec<u8> {
[Self::ID.as_octets(), DEFAULT_DST_SUFFIX_H2S.as_bytes()].concat()
}

/// Hash arbitrary data to `n` number of scalars as specified in BBS
/// specification.
fn hash_to_scalar(
msg_octets: &[u8],
dst: Option<&[u8]>,
) -> Result<Scalar, Error> {
let default_hash_to_scalar_dst = Self::default_hash_to_scalar_dst();
let dst_octets = dst.unwrap_or(&default_hash_to_scalar_dst);

if !dst_octets.is_ascii() {
fn hash_to_scalar(msg_octets: &[u8], dst: &[u8]) -> Result<Scalar, Error> {
if !dst.is_ascii() {
return Err(Error::BadParams {
cause: "non-ascii dst".to_owned(),
});
}
let mut expander = Self::Expander::init_expand(
msg_octets,
dst_octets,
XOF_NO_OF_BYTES,
);
let mut expander =
Self::Expander::init_expand(msg_octets, dst, XOF_NO_OF_BYTES);

let mut buf = [0u8; 64];
expander.read_into(&mut buf[16..]);
Expand Down
12 changes: 4 additions & 8 deletions src/schemes/bbs/api/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ where
let verify_signature = request.verify_signature.unwrap_or(true);
if verify_signature {
// Verify the signature to check the messages supplied are valid
if !(signature.verify::<_, _, _, I::Ciphersuite>(
if !(signature.verify::<_, _, _, I>(
&pk,
request.header.as_ref(),
&generators,
&digested_messages,
Some(I::api_id()),
)?) {
return Err(Error::SignatureVerification);
}
Expand All @@ -91,14 +90,13 @@ where
_parse_request_helper::<T, I>(request)?;

// Generate the proof
let proof = Proof::new::<_, _, I::Ciphersuite>(
let proof = Proof::new::<_, _, I>(
&pk,
&signature,
request.header.as_ref(),
request.presentation_header.as_ref(),
&generators,
&proof_messages,
Some(I::api_id()),
)?;

Ok(proof.to_octets())
Expand Down Expand Up @@ -132,13 +130,12 @@ where
let generators =
MemoryCachedGenerators::<I>::new(total_message_count, None)?;

proof.verify::<_, _, I::Ciphersuite>(
proof.verify::<_, _, I>(
&public_key,
request.header.as_ref(),
request.presentation_header.as_ref(),
&generators,
&messages,
Some(I::api_id()),
)
}

Expand All @@ -157,14 +154,13 @@ where
_parse_request_helper::<T, I>(request)?;

// Generate the proof
let proof = Proof::new_with_rng::<_, _, _, I::Ciphersuite>(
let proof = Proof::new_with_rng::<_, _, _, I>(
&pk,
&signature,
request.header.as_ref(),
request.presentation_header.as_ref(),
&generators,
&proof_messages,
Some(I::api_id()),
rng,
)?;

Expand Down
6 changes: 2 additions & 4 deletions src/schemes/bbs/api/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ where
let generators = MemoryCachedGenerators::<I>::new(messages.len(), None)?;

// Produce the signature and return
Signature::new::<_, _, _, I::Ciphersuite>(
Signature::new::<_, _, _, I>(
&sk,
&pk,
request.header.as_ref(),
&generators,
&messages,
Some(I::api_id()),
)
.map(|sig| sig.to_octets())
}
Expand All @@ -68,11 +67,10 @@ where
// Parse signature from request
let signature = Signature::from_octets(request.signature)?;

signature.verify::<_, _, _, I::Ciphersuite>(
signature.verify::<_, _, _, I>(
&pk,
request.header.as_ref(),
&generators,
&messages,
Some(I::api_id()),
)
}
9 changes: 2 additions & 7 deletions src/schemes/bbs/ciphersuites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
common::{
ciphersuite::CipherSuiteId,
hash_param::{
constant::{DEFAULT_DST_SUFFIX_H2S, XOF_NO_OF_BYTES},
constant::XOF_NO_OF_BYTES,
h2c::HashToCurveParameter,
h2s::HashToScalarParameter,
},
Expand All @@ -16,7 +16,7 @@ use crate::{
},
Error,
};
use blstrs::Scalar;

use group::Group;

/// BBS BLS12-381 ciphersuites.
Expand Down Expand Up @@ -88,9 +88,4 @@ pub(crate) trait BbsCiphersuiteParameters:
fn p2() -> G2Projective {
G2Projective::generator()
}

fn hash_to_e(data_to_hash: &[u8], api_id: &[u8]) -> Result<Scalar, Error> {
let e_dst = [api_id, DEFAULT_DST_SUFFIX_H2S.as_bytes()].concat();
Self::hash_to_scalar(data_to_hash, Some(&e_dst))
}
}
4 changes: 2 additions & 2 deletions src/schemes/bbs/ciphersuites/bls12_381_g1_sha_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub const SCALAR_OCTETS_LENGTH: usize = OCTET_SCALAR_LENGTH;
#[cfg_attr(docsrs, doc(cfg(feature = "__private_bbs_fixtures_generator_api")))]
pub fn hash_to_scalar(
msg_octets: &[u8],
dst: Option<&[u8]>,
dst: &[u8],
) -> Result<[u8; OCTET_SCALAR_LENGTH], Error> {
let scalars =
Bls12381Sha256CipherSuiteParameter::hash_to_scalar(msg_octets, dst);
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn map_message_to_scalar_as_hash(
#[cfg(feature = "__private_bbs_fixtures_generator_api")]
#[cfg_attr(docsrs, doc(cfg(feature = "__private_bbs_fixtures_generator_api")))]
pub fn default_hash_to_scalar_dst() -> Vec<u8> {
Bls12381Sha256CipherSuiteParameter::default_hash_to_scalar_dst()
Bls12381Sha256InterfaceParameter::default_hash_to_scalar_dst()
}

/// Return the default map message to scalar as hash dst.
Expand Down
4 changes: 2 additions & 2 deletions src/schemes/bbs/ciphersuites/bls12_381_g1_shake_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub const SCALAR_OCTETS_LENGTH: usize = OCTET_SCALAR_LENGTH;
#[cfg_attr(docsrs, doc(cfg(feature = "__private_bbs_fixtures_generator_api")))]
pub fn hash_to_scalar(
msg_octets: &[u8],
dst: Option<&[u8]>,
dst: &[u8],
) -> Result<[u8; OCTET_SCALAR_LENGTH], Error> {
let scalars: Result<blstrs::Scalar, Error> =
Bls12381Shake256CipherSuiteParameter::hash_to_scalar(msg_octets, dst);
Expand Down Expand Up @@ -188,7 +188,7 @@ pub fn map_message_to_scalar_as_hash(
#[cfg(feature = "__private_bbs_fixtures_generator_api")]
#[cfg_attr(docsrs, doc(cfg(feature = "__private_bbs_fixtures_generator_api")))]
pub fn default_hash_to_scalar_dst() -> Vec<u8> {
Bls12381Shake256CipherSuiteParameter::default_hash_to_scalar_dst()
Bls12381Shake256InterfaceParameter::default_hash_to_scalar_dst()
}

/// Return the default map message to scalar as hash dst.
Expand Down
Loading

0 comments on commit d941eef

Please sign in to comment.