Skip to content

Commit 9a0c01d

Browse files
committed
- Fixed an issue with challenge generation in Keccak's verifier_challenge.
- Moved the remaining tests to Keccak in sage_test_vectors.
1 parent 0911345 commit 9a0c01d

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/toolbox/sigma/transcript/keccak_transcript.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ where
181181
let scalar_byte_length = <<G as Group>::Scalar as PrimeField>::Repr::default().as_ref().len();
182182

183183
let uniform_bytes = self.hasher.squeeze(scalar_byte_length + 16);
184-
println!("big : {:?}", &self.order);
185184
let scalar = BigUint::from_bytes_be(&uniform_bytes);
186185
let reduced = scalar % self.order.clone();
187186

188187
let mut bytes = vec![0u8; scalar_byte_length];
189188
let reduced_bytes = reduced.to_bytes_be();
190189
let start = bytes.len() - reduced_bytes.len();
191190
bytes[start..].copy_from_slice(&reduced_bytes);
191+
bytes.reverse();
192192

193193
let mut repr = <<G as Group>::Scalar as PrimeField>::Repr::default();
194194
repr.as_mut().copy_from_slice(&bytes);

tests/sage_test_vectors.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ use sigma_rs::toolbox::sigma::sage_test::custom_schnorr_proof::SchnorrProofCusto
77
use sigma_rs::toolbox::sigma::transcript::KeccakDuplexSponge;
88
use sigma_rs::toolbox::sigma::{
99
GroupMorphismPreimage,
10-
transcript::{ShakeTranscript, ByteSchnorrCodec},
10+
transcript::ByteSchnorrCodec,
1111
NISigmaProtocol,
1212
};
1313

1414
type Gp = G1Projective;
15+
type Codec = ByteSchnorrCodec::<Gp, KeccakDuplexSponge>;
16+
type SigmaP = SchnorrProofCustom<Gp>;
1517

1618
fn msm_pr<G: Group>(scalars: &[G::Scalar], bases: &[G]) -> G {
1719
let mut acc = G::identity();
@@ -46,14 +48,14 @@ fn discrete_logarithm<G: SRandom + Group + GroupEncoding>(
4648

4749

4850
#[allow(non_snake_case)]
49-
fn dleq<G: Group + GroupEncoding>(
51+
fn dleq<G: Group + GroupEncoding + SRandom>(
5052
rng: &mut (impl Rng + CryptoRng)
5153
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
5254
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
5355

5456
let G = G::generator();
57+
let x = G::srandom(&mut *rng);
5558
let H = G::random(&mut *rng);
56-
let x = G::Scalar::random(&mut *rng);
5759
let X = G * x;
5860
let Y = H * x;
5961

@@ -71,15 +73,15 @@ fn dleq<G: Group + GroupEncoding>(
7173

7274

7375
#[allow(non_snake_case)]
74-
fn pedersen_commitment<G: Group + GroupEncoding>(
76+
fn pedersen_commitment<G: Group + GroupEncoding + SRandom>(
7577
rng: &mut (impl Rng + CryptoRng)
7678
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
7779
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
7880

7981
let G = G::generator();
8082
let H = G::random(&mut *rng);
81-
let x = G::Scalar::random(&mut *rng);
82-
let r = G::Scalar::random(&mut *rng);
83+
let x = G::srandom(&mut *rng);
84+
let r = G::srandom(&mut *rng);
8385
let witness = vec![x, r];
8486

8587
let C = G*x + H*r;
@@ -97,7 +99,7 @@ fn pedersen_commitment<G: Group + GroupEncoding>(
9799

98100

99101
#[allow(non_snake_case)]
100-
fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
102+
fn pedersen_commitment_dleq<G: Group + GroupEncoding + SRandom>(
101103
rng: &mut (impl Rng + CryptoRng)
102104
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
103105
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
@@ -109,8 +111,8 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
109111
generators.push(G::random(&mut *rng));
110112

111113
let mut witness = Vec::<G::Scalar>::new();
112-
witness.push(G::Scalar::random(&mut *rng));
113-
witness.push(G::Scalar::random(&mut *rng));
114+
witness.push(G::srandom(&mut *rng));
115+
witness.push(G::srandom(&mut *rng));
114116

115117
let X = msm_pr::<G>(&witness, &[generators[0], generators[1]]);
116118
let Y = msm_pr::<G>(&witness, &[generators[2], generators[3]]);
@@ -134,7 +136,7 @@ fn pedersen_commitment_dleq<G: Group + GroupEncoding>(
134136

135137

136138
#[allow(non_snake_case)]
137-
fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
139+
fn bbs_blind_commitment_computation<G: Group + GroupEncoding + SRandom>(
138140
rng: &mut (impl Rng + CryptoRng)
139141
) -> (GroupMorphismPreimage<G>, Vec<G::Scalar>) {
140142
let mut morphismp: GroupMorphismPreimage<G> = GroupMorphismPreimage::new();
@@ -144,10 +146,10 @@ fn bbs_blind_commitment_computation<G: Group + GroupEncoding>(
144146
// BBS.create_generators(M + 1, "BLIND_" || api_id)
145147
let (Q_2, J_1, J_2, J_3) = (G::random(&mut *rng), G::random(&mut *rng), G::random(&mut *rng), G::random(&mut *rng));
146148
// BBS.messages_to_scalars(committed_messages, api_id)
147-
let (msg_1, msg_2, msg_3) = (G::Scalar::random(&mut *rng), G::Scalar::random(&mut *rng), G::Scalar::random(&mut *rng));
149+
let (msg_1, msg_2, msg_3) = (G::srandom(&mut *rng), G::srandom(&mut *rng), G::srandom(&mut *rng));
148150

149151
// these are computed before the proof in the specification
150-
let secret_prover_blind = G::Scalar::random(&mut *rng);
152+
let secret_prover_blind = G::srandom(&mut *rng);
151153
let C = Q_2*secret_prover_blind + J_1*msg_1 + J_2*msg_2 + J_3*msg_3;
152154

153155
// This is the part that needs to be changed in the specification of blind bbs.
@@ -181,7 +183,7 @@ fn NI_discrete_logarithm() {
181183

182184
let protocol = SchnorrProofCustom { morphismp };
183185
let domain_sep: Vec<u8> = b"yellow submarineyellow submarine".to_vec();
184-
let mut nizk = NISigmaProtocol::<SchnorrProofCustom<Gp>, ByteSchnorrCodec::<Gp, KeccakDuplexSponge>, Gp>::new(&domain_sep, protocol);
186+
let mut nizk = NISigmaProtocol::<SigmaP, Codec, Gp>::new(&domain_sep, protocol);
185187

186188
let proof_bytes = nizk.prove(&witness, &mut rng);
187189
let verified = nizk.verify(&proof_bytes).is_ok();
@@ -192,14 +194,14 @@ fn NI_discrete_logarithm() {
192194
#[allow(non_snake_case)]
193195
#[test]
194196
fn NI_dleq() {
195-
let mut rng = TestDRNG::new(b"79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65");
197+
let mut rng = TestDRNG::new(b"hello world");
196198
let (morphismp, witness) = dleq::<Gp>(&mut rng);
197199

198200
println!("witness: {:?}", witness);
199201

200202
let protocol = SchnorrProofCustom { morphismp };
201203
let domain_sep: Vec<u8> = b"yellow submarineyellow submarine".to_vec();
202-
let mut nizk = NISigmaProtocol::<SchnorrProofCustom<Gp>, ShakeTranscript<Gp>, Gp>::new(&domain_sep, protocol);
204+
let mut nizk = NISigmaProtocol::<SigmaP, Codec, Gp>::new(&domain_sep, protocol);
203205

204206
let proof_bytes = nizk.prove(&witness, &mut rng);
205207
let verified = nizk.verify(&proof_bytes).is_ok();
@@ -210,14 +212,14 @@ fn NI_dleq() {
210212
#[allow(non_snake_case)]
211213
#[test]
212214
fn NI_pedersen_commitment() {
213-
let mut rng = TestDRNG::new(b"79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65");
215+
let mut rng = TestDRNG::new(b"hello world");
214216
let (morphismp, witness) = pedersen_commitment::<Gp>(&mut rng);
215217

216218
println!("witness: {:?}", witness);
217219

218220
let protocol = SchnorrProofCustom { morphismp };
219221
let domain_sep: Vec<u8> = b"yellow submarineyellow submarine".to_vec();
220-
let mut nizk = NISigmaProtocol::<SchnorrProofCustom<Gp>, ShakeTranscript<Gp>, Gp>::new(&domain_sep, protocol);
222+
let mut nizk = NISigmaProtocol::<SigmaP, Codec, Gp>::new(&domain_sep, protocol);
221223

222224
let proof_bytes = nizk.prove(&witness, &mut rng);
223225
let verified = nizk.verify(&proof_bytes).is_ok();
@@ -228,14 +230,14 @@ fn NI_pedersen_commitment() {
228230
#[allow(non_snake_case)]
229231
#[test]
230232
fn NI_pedersen_commitment_dleq() {
231-
let mut rng = TestDRNG::new(b"79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65");
233+
let mut rng = TestDRNG::new(b"hello world");
232234
let (morphismp, witness) = pedersen_commitment_dleq::<Gp>(&mut rng);
233235

234236
println!("witness: {:?}", witness);
235237

236238
let protocol = SchnorrProofCustom { morphismp };
237239
let domain_sep: Vec<u8> = b"yellow submarineyellow submarine".to_vec();
238-
let mut nizk = NISigmaProtocol::<SchnorrProofCustom<Gp>, ShakeTranscript<Gp>, Gp>::new(&domain_sep, protocol);
240+
let mut nizk = NISigmaProtocol::<SigmaP, Codec, Gp>::new(&domain_sep, protocol);
239241

240242
let proof_bytes = nizk.prove(&witness, &mut rng);
241243
let verified = nizk.verify(&proof_bytes).is_ok();
@@ -246,14 +248,14 @@ fn NI_pedersen_commitment_dleq() {
246248
#[allow(non_snake_case)]
247249
#[test]
248250
fn NI_bbs_blind_commitment_computation() {
249-
let mut rng = TestDRNG::new(b"79656c6c6f77207375626d6172696e6579656c6c6f77207375626d6172696e65");
251+
let mut rng = TestDRNG::new(b"hello world");
250252
let (morphismp, witness) = bbs_blind_commitment_computation::<Gp>(&mut rng);
251253

252254
println!("witness: {:?}", witness);
253255

254256
let protocol = SchnorrProofCustom { morphismp };
255257
let domain_sep: Vec<u8> = b"yellow submarineyellow submarine".to_vec();
256-
let mut nizk = NISigmaProtocol::<SchnorrProofCustom<Gp>, ShakeTranscript<Gp>, Gp>::new(&domain_sep, protocol);
258+
let mut nizk = NISigmaProtocol::<SigmaP, Codec, Gp>::new(&domain_sep, protocol);
257259

258260
let proof_bytes = nizk.prove(&witness, &mut rng);
259261
let verified = nizk.verify(&proof_bytes).is_ok();

0 commit comments

Comments
 (0)