Skip to content

Commit 73e4cdf

Browse files
committed
fix unit-tests and update test vectors
1 parent a962c24 commit 73e4cdf

File tree

8 files changed

+51
-52
lines changed

8 files changed

+51
-52
lines changed

src/codec.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ pub trait Codec {
2323
type Challenge;
2424

2525
/// Generates an empty codec that can be identified by a domain separator.
26-
fn new(protocol_identifier: &[u8], session_identifier: &[u8], instance_label: &[u8]) -> Self;
26+
fn new(
27+
protocol_identifier: &[u8; 64],
28+
session_identifier: &[u8],
29+
instance_label: &[u8],
30+
) -> Self;
2731

2832
/// Allows for precomputed initialization of the codec with a specific IV.
2933
fn from_iv(iv: [u8; 64]) -> Self;
@@ -65,12 +69,11 @@ fn length_to_bytes(x: usize) -> [u8; WORD_SIZE] {
6569
/// This function computes a deterministic IV from the protocol identifier,
6670
/// session identifier, and instance label using the specified duplex sponge.
6771
pub fn compute_iv<H: DuplexSpongeInterface>(
68-
protocol_id: &[u8],
72+
protocol_id: &[u8; 64],
6973
session_id: &[u8],
7074
instance_label: &[u8],
7175
) -> [u8; 64] {
7276
let mut tmp = H::new([0u8; 64]);
73-
tmp.absorb(&length_to_bytes(protocol_id.len()));
7477
tmp.absorb(protocol_id);
7578
tmp.absorb(&length_to_bytes(session_id.len()));
7679
tmp.absorb(session_id);
@@ -86,9 +89,16 @@ where
8689
{
8790
type Challenge = G::Scalar;
8891

89-
fn new(protocol_id: &[u8], session_id: &[u8], instance_label: &[u8]) -> Self {
90-
let iv = compute_iv::<H>(protocol_id, session_id, instance_label);
91-
Self::from_iv(iv)
92+
fn new(protocol_id: &[u8; 64], session_id: &[u8], instance_label: &[u8]) -> Self {
93+
let mut hasher = H::new(*protocol_id);
94+
hasher.absorb(&length_to_bytes(session_id.len()));
95+
hasher.absorb(session_id);
96+
hasher.absorb(&length_to_bytes(instance_label.len()));
97+
hasher.absorb(instance_label);
98+
Self {
99+
hasher,
100+
_marker: core::marker::PhantomData,
101+
}
92102
}
93103

94104
fn from_iv(iv: [u8; 64]) -> Self {

src/composition.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ impl<G: PrimeGroup + ConstantTimeEq + ConditionallySelectable> SigmaProtocol
556556
}
557557
}
558558

559-
fn protocol_identifier(&self) -> impl AsRef<[u8]> {
559+
fn protocol_identifier(&self) -> [u8; 64] {
560560
let mut hasher = Sha3_256::new();
561561

562562
match self {
@@ -569,19 +569,21 @@ impl<G: PrimeGroup + ConstantTimeEq + ConditionallySelectable> SigmaProtocol
569569
let mut hasher = Sha3_256::new();
570570
hasher.update([1u8; 32]);
571571
for p in protocols {
572-
hasher.update(p.protocol_identifier());
572+
hasher.update(p.protocol_identifier().as_ref());
573573
}
574574
}
575575
ComposedRelation::Or(protocols) => {
576576
let mut hasher = Sha3_256::new();
577577
hasher.update([2u8; 32]);
578578
for p in protocols {
579-
hasher.update(p.protocol_identifier());
579+
hasher.update(p.protocol_identifier().as_ref());
580580
}
581581
}
582582
}
583583

584-
hasher.finalize()
584+
let mut protocol_id = [0u8; 64];
585+
(&mut protocol_id[..32]).clone_from_slice(&hasher.finalize());
586+
protocol_id
585587
}
586588

587589
fn serialize_response(&self, response: &Self::Response) -> Vec<u8> {

src/fiat_shamir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
/// A new [`Nizk`] that can generate and verify non-interactive proofs.
6969
pub fn new(session_identifier: &[u8], interactive_proof: P) -> Self {
7070
let hash_state = C::new(
71-
interactive_proof.protocol_identifier().as_ref(),
71+
&interactive_proof.protocol_identifier(),
7272
session_identifier,
7373
interactive_proof.instance_label().as_ref(),
7474
);

src/schnorr_protocol.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,11 @@ impl<G: PrimeGroup> SigmaProtocol for CanonicalLinearRelation<G> {
233233
self.label()
234234
}
235235

236-
fn protocol_identifier(&self) -> impl AsRef<[u8]> {
237-
b"draft-zkproof-fiat-shamir"
236+
fn protocol_identifier(&self) -> [u8; 64] {
237+
const PROTOCOL_ID: &[u8; 32] = b"ietf sigma proof linear relation";
238+
let mut protocol_id = [0; 64];
239+
protocol_id[..32].clone_from_slice(PROTOCOL_ID);
240+
protocol_id
238241
}
239242
}
240243

src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub trait SigmaProtocol {
9898
/// Deserializes a response from bytes.
9999
fn deserialize_response(&self, data: &[u8]) -> Result<Self::Response, Error>;
100100

101-
fn protocol_identifier(&self) -> impl AsRef<[u8]>;
101+
fn protocol_identifier(&self) -> [u8; 64];
102102

103103
fn instance_label(&self) -> impl AsRef<[u8]>;
104104
}

tests/relations/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,27 +182,25 @@ pub fn range_instance_generation<G: PrimeGroup, R: RngCore>(
182182
let [var_G, var_H] = instance.allocate_elements();
183183
let [var_x, var_r] = instance.allocate_scalars();
184184
let vars_b = instance.allocate_scalars_vec(bases.len());
185-
let vars_s = instance.allocate_scalars_vec(bases.len() - 1);
185+
let vars_s = instance.allocate_scalars_vec(bases.len());
186186
let var_s2 = instance.allocate_scalars_vec(bases.len());
187187
let var_Ds = instance.allocate_elements_vec(bases.len());
188188

189189
// `var_C` is a Pedersen commitment to `var_x`.
190190
let var_C = instance.allocate_eq(var_x * var_G + var_r * var_H);
191191
// `var_Ds[i]` are bit commitments...
192-
for i in 1..bases.len() {
192+
for i in 0..bases.len() {
193193
instance.append_equation(var_Ds[i], vars_b[i] * var_G + vars_s[i] * var_H);
194194
instance.append_equation(var_Ds[i], vars_b[i] * var_Ds[i] + var_s2[i] * var_H);
195195
}
196196
// ... satisfying that sum(Ds[i] * bases[i]) = C
197197
instance.append_equation(
198-
var_Ds[0],
199-
var_C
200-
- var_G * G::Scalar::from(range.start)
201-
- (1..bases.len())
198+
var_C,
199+
var_G * G::Scalar::from(range.start)
200+
+ (0..bases.len())
202201
.map(|i| var_Ds[i] * G::Scalar::from(bases[i]))
203202
.sum::<Sum<_>>(),
204203
);
205-
instance.append_equation(var_Ds[0], vars_b[0] * var_Ds[0] + var_s2[0] * var_H);
206204

207205
// Compute the witness
208206
let r = G::Scalar::random(&mut rng);
@@ -492,24 +490,26 @@ pub fn elgamal_subtraction<G: PrimeGroup, R: RngCore>(
492490
let mut instance = LinearRelation::new();
493491
let [dk, a, r] = instance.allocate_scalars();
494492
let [ek, C, D, H, G] = instance.allocate_elements();
495-
let v = G::Scalar::from(100);
496493

497494
instance.append_equation(ek, dk * H);
498495

499496
instance.append_equation(D, r * H);
500497
instance.append_equation(C, r * ek + a * G);
501498

502-
instance.append_equation(C, G * v + dk * D + a * G);
499+
instance.append_equation(C, dk * D + a * G);
503500

504-
// set dk for testing to
505-
let witness = vec![
506-
G::Scalar::from(4242),
507-
G::Scalar::from(1000),
508-
G::Scalar::random(&mut *rng),
509-
];
501+
let witness_dk = G::Scalar::from(4242);
502+
let witness_a = G::Scalar::from(1000);
503+
let witness_r = G::Scalar::random(&mut *rng);
504+
let witness = vec![witness_dk, witness_a, witness_r];
505+
506+
// Assign group elements consistent with the witness so compute_image is unnecessary.
510507
let alt_gen = G::random(&mut *rng);
511508
instance.set_elements([(G, G::generator()), (H, alt_gen)]);
512-
instance.compute_image(&witness).unwrap();
509+
let ek_val = alt_gen * witness_dk;
510+
let D_val = alt_gen * witness_r;
511+
let C_val = ek_val * witness_r + G::generator() * witness_a;
512+
instance.set_elements([(ek, ek_val), (D, D_val), (C, C_val)]);
513513

514514
(instance.canonical().unwrap(), witness)
515515
}

tests/spec/custom_schnorr_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<G: SRandom + PrimeGroup> SigmaProtocol for DeterministicSchnorrProof<G> {
8888
self.0.instance_label()
8989
}
9090

91-
fn protocol_identifier(&self) -> impl AsRef<[u8]> {
91+
fn protocol_identifier(&self) -> [u8; 64] {
9292
self.0.protocol_identifier()
9393
}
9494
}

tests/spec_vectors.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ struct TestVector {
1818
session_id: Vec<u8>,
1919
statement: Vec<u8>,
2020
witness: Vec<u8>,
21-
iv: Vec<u8>,
2221
proof: Vec<u8>,
2322
}
2423

@@ -78,17 +77,10 @@ fn test_spec_testvectors() {
7877
let nizk = SchnorrNizk::new(&vector.session_id, protocol);
7978

8079
// Verify that the computed IV matches the test vector IV
81-
let protocol_id = b"draft-zkproof-fiat-shamir";
82-
let instance_label = parsed_instance.label();
83-
let computed_iv = sigma_proofs::codec::compute_iv::<sigma_proofs::KeccakDuplexSponge>(
84-
protocol_id,
85-
&vector.session_id,
86-
&instance_label,
87-
);
88-
assert_eq!(
89-
computed_iv,
90-
vector.iv.as_slice(),
91-
"Computed IV doesn't match test vector IV for {test_name}"
80+
// Ensure the provided test vector proof verifies.
81+
assert!(
82+
nizk.verify_batchable(&vector.proof).is_ok(),
83+
"Fiat-Shamir Schnorr proof from vectors did not verify for {test_name}"
9284
);
9385

9486
// Generate proof with the proof generation RNG
@@ -145,15 +137,8 @@ fn extract_vectors_new() -> Result<HashMap<String, TestVector>, String> {
145137
)
146138
.map_err(|e| format!("Invalid hex in Witness for {name}: {e}"))?;
147139

148-
let iv = Vec::from_hex(
149-
obj["IV"]
150-
.as_str()
151-
.ok_or_else(|| format!("IV field not found for {name}"))?,
152-
)
153-
.map_err(|e| format!("Invalid hex in IV for {name}: {e}"))?;
154-
155140
let proof = Vec::from_hex(
156-
obj["Proof"]
141+
obj["Batchable Proof"]
157142
.as_str()
158143
.ok_or_else(|| format!("Proof field not found for {name}"))?,
159144
)
@@ -166,7 +151,6 @@ fn extract_vectors_new() -> Result<HashMap<String, TestVector>, String> {
166151
session_id,
167152
statement,
168153
witness,
169-
iv,
170154
proof,
171155
},
172156
);

0 commit comments

Comments
 (0)