Skip to content

Commit ba7e1dd

Browse files
committed
refactor: updates and cleanups
1 parent bd611bc commit ba7e1dd

File tree

10 files changed

+111
-99
lines changed

10 files changed

+111
-99
lines changed

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub enum Error {
4747
/// Public key is not valid.
4848
InvalidPublicKey,
4949

50+
/// Pseudonym is not valid
51+
InvalidPseudonym,
52+
5053
/// Signature is malformed.
5154
MalformedSignature {
5255
/// Detailed cause.
@@ -126,6 +129,9 @@ impl core::fmt::Debug for Error {
126129
Error::InvalidPublicKey => {
127130
write!(f, "public key is invalid.")
128131
}
132+
Error::InvalidPseudonym => {
133+
write!(f, "pseudonym is invalid")
134+
}
129135
Error::MalformedSignature { ref cause } => {
130136
write!(f, "signature is malformed: cause: {cause}")
131137
}

src/schemes/bbs/core/proof.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,24 @@ macro_rules! slicer {
5252
};
5353
}
5454

55-
#[derive(Default)]
5655
pub(crate) struct RandomScalars {
5756
pub r1: Scalar,
5857
pub r2_tilde: Scalar,
5958
pub z_tilde: Scalar,
6059
pub m_tilde_scalars: Vec<Scalar>,
6160
}
6261

62+
impl Default for RandomScalars {
63+
fn default() -> Self {
64+
Self {
65+
r1: Default::default(),
66+
r2_tilde: Default::default(),
67+
z_tilde: Default::default(),
68+
m_tilde_scalars: Vec::new() as Vec<Scalar>,
69+
}
70+
}
71+
}
72+
6373
impl RandomScalars {
6474
pub fn insert_m_tilde(&mut self, m_tilde: Scalar) {
6575
self.m_tilde_scalars.push(m_tilde);
@@ -144,21 +154,6 @@ impl Proof {
144154
G: Generators,
145155
I: BbsInterfaceParameter,
146156
{
147-
// Input parameter checks
148-
// Error out if there is no `header` and not any `ProofMessage`
149-
if header.is_none() && messages.is_empty() {
150-
return Err(Error::BadParams {
151-
cause: "nothing to prove".to_owned(),
152-
});
153-
}
154-
// Error out if length of messages and generators are not equal
155-
if messages.len() != generators.message_generators_length() {
156-
return Err(Error::MessageGeneratorsLengthMismatch {
157-
generators: generators.message_generators_length(),
158-
messages: messages.len(),
159-
});
160-
}
161-
162157
// (r1, r2, r3, m~_j1, ..., m~_jU) = calculate_random_scalars(3+U)
163158
let mut random_scalars = RandomScalars {
164159
r1: create_random_scalar(&mut rng)?,
@@ -258,17 +253,12 @@ impl Proof {
258253
// cv_for_hash = encode_for_hash(cv_array)
259254
// if cv_for_hash is INVALID, return INVALID
260255
// cv = hash_to_scalar(cv_for_hash, 1)
261-
<<<<<<< HEAD
262-
let cv = compute_challenge::<_, I>(&init_res, disclosed_messages, ph)?;
263-
=======
264-
let cv = compute_challenge::<_, C>(
256+
let cv = compute_challenge::<_, I>(
265257
&init_res,
266258
disclosed_messages,
267259
ph,
268-
api_id,
269260
None,
270261
)?;
271-
>>>>>>> 8a6d8d2 (feat: add pseudonyms)
272262

273263
// Check the selective disclosure proof
274264
// if c != cv, return INVALID
@@ -316,6 +306,19 @@ impl Proof {
316306
let total_no_of_messages = message_scalars.len();
317307

318308
// Check input sizes.
309+
// Error out if there is no `header` and not any `ProofMessage`
310+
if header.is_none() && message_scalars.is_empty() {
311+
return Err(Error::BadParams {
312+
cause: "nothing to prove".to_owned(),
313+
});
314+
}
315+
// Error out if length of messages and generators are not equal
316+
if total_no_of_messages != generators.message_generators_length() {
317+
return Err(Error::MessageGeneratorsLengthMismatch {
318+
generators: generators.message_generators_length(),
319+
messages: total_no_of_messages,
320+
});
321+
}
319322
// Number of message generators == number of messages is checked in
320323
// compute_domain. Checking that all the indexes are in the [0,
321324
// length(messages)) range is done before get_message_generator
@@ -330,7 +333,7 @@ impl Proof {
330333

331334
// Checking that number of undisclosed messages (/indexes) <= number of
332335
// messages
333-
if undisclosed_indexes.len() > message_scalars.len() {
336+
if undisclosed_indexes.len() > total_no_of_messages {
334337
return Err(Error::BadParams {
335338
cause: format!(
336339
"Not disclosed messages number is invalid. Maximum \
@@ -345,7 +348,7 @@ impl Proof {
345348
let domain = compute_domain::<_, _, I>(
346349
PK,
347350
header,
348-
message_scalars.len(),
351+
total_no_of_messages,
349352
generators,
350353
)?;
351354

src/schemes/pseudonym/api/dtos.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct BbsSignRequest<'a, T: AsRef<[u8]> + Default> {
1717
/// Public key
1818
pub public_key: &'a [u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
1919
/// Prover unique identifier
20-
pub pid: T,
20+
pub prover_id: T,
2121
/// Header containing context and application specific information
2222
pub header: Option<T>,
2323
/// Vector of messages to sign
@@ -29,7 +29,7 @@ impl<'a, T: AsRef<[u8]> + Default> Default for BbsSignRequest<'a, T> {
2929
Self {
3030
secret_key: &[0u8; BBS_BLS12381G1_SECRET_KEY_LENGTH],
3131
public_key: &[0u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
32-
pid: Default::default(),
32+
prover_id: Default::default(),
3333
header: Default::default(),
3434
messages: Default::default(),
3535
}
@@ -42,7 +42,7 @@ pub struct BbsVerifyRequest<'a, T: AsRef<[u8]> + Default> {
4242
/// Public key
4343
pub public_key: &'a [u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
4444
/// Prover unique identifier
45-
pub pid: T,
45+
pub prover_id: T,
4646
/// Header containing context and application specific information
4747
pub header: Option<T>,
4848
/// Vector of messages to verify against a signature
@@ -55,7 +55,7 @@ impl<'a, T: AsRef<[u8]> + Default> Default for BbsVerifyRequest<'a, T> {
5555
fn default() -> Self {
5656
Self {
5757
public_key: &[0u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
58-
pid: Default::default(),
58+
prover_id: Default::default(),
5959
header: Default::default(),
6060
messages: Default::default(),
6161
signature: &[0u8; BBS_BLS12381G1_SIGNATURE_LENGTH],
@@ -70,7 +70,7 @@ pub struct BbsProofGenRequest<'a, T: AsRef<[u8]> + Default> {
7070
/// Public key associated to the BBS signature
7171
pub public_key: &'a [u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
7272
/// The Prover's unique identifier
73-
pub pid: T,
73+
pub prover_id: T,
7474
/// The Verifier's unique Identifier
7575
pub verifier_id: T,
7676
/// Point of G1 used by a Verifier to link multiple proof presentations
@@ -94,7 +94,7 @@ impl<'a, T: AsRef<[u8]> + Default> Default for BbsProofGenRequest<'a, T> {
9494
fn default() -> Self {
9595
Self {
9696
public_key: &[0u8; BBS_BLS12381G1_PUBLIC_KEY_LENGTH],
97-
pid: Default::default(),
97+
prover_id: Default::default(),
9898
verifier_id: Default::default(),
9999
pseudonym: &[0u8; OCTET_POINT_G1_LENGTH],
100100
header: Default::default(),

src/schemes/pseudonym/api/proof.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ where
4242
let pseudonym = Pseudonym::from_octets(request.pseudonym)?;
4343

4444
// digest the pid message
45-
let pid = pid_to_message::<_, I>(&request.pid)?;
45+
let pid = pid_to_message::<_, I>(&request.prover_id)?;
4646
digested_messages.push(pid);
47-
// proof_messages.push(ProofMessage::Hidden(pid));
4847

4948
let verify_signature = request.verify_signature.unwrap_or(true);
5049
if verify_signature

src/schemes/pseudonym/api/signature.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
let pk = PublicKey::from_octets(request.public_key)?;
2626

2727
let mut messages = digest_messages::<_, I>(request.messages)?;
28-
let pid_msg = digest_messages::<_, I>(Some(&[&request.pid]))?;
28+
let pid_msg = digest_messages::<_, I>(Some(&[&request.prover_id]))?;
2929
messages.push(pid_msg[0]);
3030

3131
let generators = MemoryCachedGenerators::<I>::new(messages.len(), None)?;
@@ -51,7 +51,7 @@ where
5151
let pk = PublicKey::from_octets(request.public_key)?;
5252

5353
let mut messages = digest_messages::<_, I>(request.messages)?;
54-
let pid_msg = digest_messages::<_, I>(Some(&[&request.pid]))?;
54+
let pid_msg = digest_messages::<_, I>(Some(&[&request.prover_id]))?;
5555
messages.push(pid_msg[0]);
5656

5757
let generators = MemoryCachedGenerators::<I>::new(messages.len(), None)?;

src/schemes/pseudonym/ciphersuites/bls12_381_g1_sha_256.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
#![allow(dead_code)]
2-
#![allow(unused)]
3-
#![allow(non_snake_case)]
4-
5-
// use crate::common::ciphersuite::{
6-
// CipherSuiteParameter,
7-
// CipherSuiteId,
8-
// };
9-
101
use crate::{
112
bbs::{
123
ciphersuites::{

src/schemes/pseudonym/core/proof.rs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#![allow(dead_code)]
2-
#![allow(unused)]
31
#![allow(non_snake_case)]
2+
43
use std::collections::BTreeMap;
54

65
use blstrs::{G1Projective, Scalar};
@@ -41,9 +40,9 @@ impl ProofWithNym {
4140
pub fn new<T, G, C>(
4241
PK: &PublicKey,
4342
signature: &Signature,
44-
nym: &Pseudonym,
43+
pseudonym: &Pseudonym,
4544
verifier_id: T,
46-
pid: Message,
45+
prover_id: Message,
4746
header: Option<T>,
4847
ph: Option<T>,
4948
generators: &G,
@@ -58,9 +57,9 @@ impl ProofWithNym {
5857
Self::new_with_rng::<_, _, _, C>(
5958
PK,
6059
signature,
61-
nym,
60+
pseudonym,
6261
verifier_id,
63-
pid,
62+
prover_id,
6463
header,
6564
ph,
6665
generators,
@@ -75,9 +74,9 @@ impl ProofWithNym {
7574
pub fn new_with_rng<T, R, G, C>(
7675
PK: &PublicKey,
7776
signature: &Signature,
78-
nym: &Pseudonym,
77+
pseudonym: &Pseudonym,
7978
verifier_id: T,
80-
pid: Message,
79+
prover_id: Message,
8180
header: Option<T>,
8281
ph: Option<T>,
8382
generators: &G,
@@ -91,24 +90,6 @@ impl ProofWithNym {
9190
G: Generators,
9291
C: BbsCiphersuiteParameters,
9392
{
94-
if header.is_none() && messages.is_empty() {
95-
return Err(Error::BadParams {
96-
cause: "nothing to prove".to_owned(),
97-
});
98-
}
99-
// Error out if length of messages and generators are not equal
100-
if messages.len() + 1 != generators.message_generators_length() {
101-
println!("messages.len() + 1 = {:?}", messages.len() + 1);
102-
println!(
103-
"generators.message_generators_length() = {:?}",
104-
generators.message_generators_length()
105-
);
106-
107-
return Err(Error::MessageGeneratorsLengthMismatch {
108-
generators: generators.message_generators_length(),
109-
messages: messages.len(),
110-
});
111-
}
11293
let api_id = api_id.unwrap_or([].to_vec());
11394

11495
// (r1, r2, r3, m~_j1, ..., m~_jU) = calculate_random_scalars(3+U)
@@ -124,12 +105,12 @@ impl ProofWithNym {
124105
//
125106
// Deserialization:
126107
// ...(implicit steps)...
127-
// 4. messages.push(pid)
108+
// 4. messages.push(prover_id)
128109
// ...(implicit steps)...
129110
// 10. undisclosed_indexes = range(1, L) \ disclosed_indexes
130111
// 11. disclosed_messages = (messages[i1], ..., messages[iR])
131112
let mut messages_vec = messages.to_vec();
132-
messages_vec.push(ProofMessage::Hidden(pid));
113+
messages_vec.push(ProofMessage::Hidden(prover_id));
133114

134115
let message_scalars: Vec<Scalar> =
135116
messages_vec.iter().map(|m| m.get_message().0).collect();
@@ -169,7 +150,7 @@ impl ProofWithNym {
169150

170151
let pid_tilde = random_scalars.m_tilde_scalars.last().unwrap();
171152
let pseudonym_proof_init = CommitProofInitResult {
172-
commit: nym.as_point(),
153+
commit: pseudonym.as_point(),
173154
commit_base: OP,
174155
blind_commit: OP * pid_tilde,
175156
};
@@ -221,11 +202,35 @@ impl ProofWithNym {
221202
if PK.is_valid().unwrap_u8() == 0u8 {
222203
return Err(Error::InvalidPublicKey);
223204
}
205+
206+
// the pseudonym should be a point of G1 but not any of the constant
207+
// "reserved" points (i.e., the identity of G1 or the base
208+
// generator and the base point of G1).
209+
if pseudonym.is_valid::<C>().unwrap_u8() == 0u8 {
210+
return Err(Error::InvalidPseudonym);
211+
}
212+
213+
// Check that the m_hat_list is not empty (the prover_id
214+
// should always be undisclosed).
215+
if self.0.m_hat_list.is_empty() {
216+
return Err(Error::BadParams {
217+
cause: "At least on message must be undisclosed".to_owned(),
218+
});
219+
}
220+
221+
// Check that the last message (the prover_id) is not revealed
222+
if let Some(val) = disclosed_messages.last_key_value() {
223+
if *val.0 == self.0.m_hat_list.len() + disclosed_messages.len() {
224+
return Err(Error::BadParams {
225+
cause: "The last signed message should not be revealed"
226+
.to_owned(),
227+
});
228+
}
229+
}
230+
224231
let api_id = api_id.unwrap_or([].to_vec());
225232

226233
// initialize the proof verification procedure
227-
// TODO: Check that the last message is not revealed
228-
// TODO: Check that the m_hat_list is not empty.
229234
let init_res = self.0.proof_verify_init::<T, G, C>(
230235
PK,
231236
header,
@@ -236,14 +241,13 @@ impl ProofWithNym {
236241

237242
// initialize the pseudonym correctness proof verification procedure
238243
let OP = C::hash_to_curve(verifier_id.as_ref(), &api_id)?;
239-
240-
// unwrap() is safe here is we check that m_hat_list is non empty (TODO)
241-
let pid_hat = self.0.m_hat_list.last().unwrap();
242244
let pseudonym_point = pseudonym.as_point();
243245
let proof_challenge = self.0.c;
246+
247+
// unwrap() is safe here since we check that m_hat_list is non empty
244248
let Uv = G1Projective::multi_exp(
245249
&[OP, pseudonym_point],
246-
&[pid_hat.0, -proof_challenge.0],
250+
&[self.0.m_hat_list.last().unwrap().0, -proof_challenge.0],
247251
);
248252

249253
let pseudonym_proof_verify_init = CommitProofInitResult {

0 commit comments

Comments
 (0)