Skip to content

Commit 37f7000

Browse files
committed
- Replaced the useless Modulable trait with a generic cardinal<G>() function for G:PrimeField
- Restructured the spec tests
1 parent d8d7968 commit 37f7000

File tree

9 files changed

+98
-121
lines changed

9 files changed

+98
-121
lines changed

src/toolbox/sigma/transcript/keccak_transcript.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use group::{Group, GroupEncoding};
55
use num_bigint::BigUint;
66
use std::convert::TryInto;
77
use tiny_keccak::keccakf;
8+
use num_traits::identities::One;
89

910
const R: usize = 136;
1011
const N: usize = 136 + 64;
@@ -145,32 +146,28 @@ impl DuplexSpongeInterface for KeccakDuplexSponge {
145146
}
146147
}
147148

148-
pub trait Modulable: PrimeField {
149-
fn cardinal() -> BigUint;
149+
fn cardinal<F: PrimeField>() -> BigUint {
150+
let bytes = (F::ZERO - F::ONE).to_repr();
151+
BigUint::from_bytes_le(bytes.as_ref()) + BigUint::one()
150152
}
151153

152154
pub struct ByteSchnorrCodec<G, H>
153155
where
154156
G: Group + GroupEncoding + GroupSerialisation,
155-
G::Scalar: Modulable,
156157
H: DuplexSpongeInterface,
157158
{
158-
order: BigUint,
159159
hasher: H,
160160
_marker: core::marker::PhantomData<G>,
161161
}
162162

163163
impl<G, H> TranscriptCodec<G> for ByteSchnorrCodec<G, H>
164164
where
165165
G: Group + GroupEncoding + GroupSerialisation,
166-
G::Scalar: Modulable,
167166
H: DuplexSpongeInterface,
168167
{
169168
fn new(domain_sep: &[u8]) -> Self {
170169
let hasher = H::new(domain_sep);
171-
let order = G::Scalar::cardinal();
172170
Self {
173-
order,
174171
hasher,
175172
_marker: Default::default(),
176173
}
@@ -190,7 +187,7 @@ where
190187

191188
let uniform_bytes = self.hasher.squeeze(scalar_byte_length + 16);
192189
let scalar = BigUint::from_bytes_be(&uniform_bytes);
193-
let reduced = scalar % self.order.clone();
190+
let reduced = scalar % cardinal::<G::Scalar>();
194191

195192
let mut bytes = vec![0u8; scalar_byte_length];
196193
let reduced_bytes = reduced.to_bytes_be();

src/toolbox/sigma/transcript/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ pub mod keccak_transcript;
22
pub mod shake_transcript;
33
pub mod r#trait;
44

5-
pub use keccak_transcript::{ByteSchnorrCodec, KeccakDuplexSponge, Modulable};
5+
pub use keccak_transcript::{ByteSchnorrCodec, KeccakDuplexSponge};
66
pub use r#trait::TranscriptCodec;
77
pub use shake_transcript::ShakeTranscript;

tests/spec/bls12_381.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use sigma_rs::tests::spec::random::{SInput, SRandom};
2-
use sigma_rs::toolbox::sigma::transcript::keccak_transcript::Modulable;
31
use group::Group;
42
use ff::PrimeField;
5-
use bls12_381::{G1Projective, Scalar};
3+
use bls12_381::G1Projective;
64
use rand::{Rng, CryptoRng};
75
use subtle::CtOption;
86
use num_bigint::BigUint;
97
use hex::FromHex;
108
use num_traits::One;
119

10+
use crate::random::{SInput, SRandom};
11+
1212
impl SInput for G1Projective {
1313
fn scalar_from_hex_be(
1414
hex_str: &str
@@ -65,10 +65,4 @@ impl SRandom for G1Projective {
6565
}
6666
G1Projective::scalar_from_hex_be(&hex_string).unwrap()
6767
}
68-
}
69-
70-
impl Modulable for Scalar {
71-
fn cardinal() -> BigUint {
72-
BigUint::parse_bytes(b"111001111101101101001110101001100101001100111010111110101001000001100110011100111011000000010000000100110100001110110000000010101010011101111011010010000000010111111111111111001011011111111101111111111111111111111111111111100000000000000000000000000000001", 2).unwrap()
73-
}
7468
}

tests/spec/custom_schnorr_proof.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use rand::{CryptoRng, Rng};
22
use group::{Group, GroupEncoding};
33
use ff::PrimeField;
4-
use crate::toolbox::sigma::{GroupMorphismPreimage, SigmaProtocol, GroupSerialisation};
5-
use crate::errors::ProofError;
4+
use sigma_rs::toolbox::sigma::{GroupMorphismPreimage, SigmaProtocol, GroupSerialisation};
5+
use sigma_rs::ProofError;
66

7-
use crate::tests::spec::random::SRandom;
7+
use crate::random::SRandom;
88

99
pub struct SchnorrProofCustom<G>
1010
where
@@ -13,11 +13,6 @@ where
1313
pub morphismp: GroupMorphismPreimage<G>
1414
}
1515

16-
pub struct SchnorrState<S> {
17-
pub nonces: Vec<S>,
18-
pub witness: Vec<S>,
19-
}
20-
2116
impl<G> SigmaProtocol for SchnorrProofCustom<G>
2217
where
2318
G: SRandom + GroupEncoding + GroupSerialisation

tests/spec/low_level_tests.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use rand::RngCore;
2+
use group::{Group, ff::Field};
3+
use bls12_381::{G1Projective, G1Affine};
4+
use num_bigint::BigUint;
5+
6+
use crate::test_drng::TestDRNG;
7+
8+
type Gp = G1Projective;
9+
type Ga = G1Affine;
10+
11+
#[allow(non_snake_case)]
12+
#[test]
13+
fn DRNG_testing() {
14+
let mut rng = TestDRNG::new(b"hello world");
15+
println!("Next u32 : {}", rng.next_u32());
16+
println!("randint : {}", rng.randint(0, 1000000000));
17+
// println!("randint : {}", rng.randint(0, 52435875175126190479447740508185965837690552500527637822603658699938581184513));
18+
let low = BigUint::parse_bytes(b"0", 10).unwrap();
19+
let high = BigUint::parse_bytes(b"73EDA753299D7C00000000000000000000000000000000000000000000000000", 16).unwrap();
20+
let rand = rng.randint_big(&low, &high);
21+
println!("{}", rand);
22+
}
23+
24+
25+
#[allow(non_snake_case)]
26+
#[test]
27+
fn Scalar_test() {
28+
let rng = TestDRNG::new(b"hello world");
29+
let y = <Gp as Group>::Scalar::random(rng);
30+
let ZERO = <Gp as Group>::Scalar::ZERO;
31+
let ONE = y * y.invert().unwrap();
32+
let ONE_inv = ONE.invert().unwrap();
33+
let TWO = ONE + ONE;
34+
let TWO_INV = TWO.invert().unwrap();
35+
println!("y = {}", y);
36+
println!("ZERO = {}", ZERO);
37+
println!("ONE = {}", ONE);
38+
println!("ONE_inv = {}", ONE_inv);
39+
println!("TWO = {}", TWO);
40+
println!("TWO_INV = {}", TWO_INV);
41+
}
42+
43+
#[allow(non_snake_case)]
44+
#[test]
45+
fn DRNG_test_on_Scalar() {
46+
let mut rng = TestDRNG::new(b"hello world");
47+
let x = G1Projective::random(&mut rng);
48+
let y = G1Projective::random(&mut rng);
49+
println!("x = {}", x);
50+
println!("y = {}", y);
51+
}
52+
53+
54+
#[allow(non_snake_case)]
55+
#[test]
56+
fn DRNG_test_on_Group() {
57+
let mut _rng = TestDRNG::new(b"hello world");
58+
let H = Ga::identity();
59+
let _bytes = H.to_uncompressed();
60+
println!("H : {}", H);
61+
}

tests/spec/random.rs

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -20,75 +20,3 @@ pub trait SRandom: Group {
2020
) -> Self::Scalar;
2121
}
2222

23-
24-
use num_bigint::BigUint;
25-
26-
use rand::RngCore;
27-
use group::{Group, ff::Field};
28-
use bls12_381::{G1Projective, G1Affine};
29-
30-
31-
use spec::{TestDRNG, SInput, SRandom};
32-
33-
type Gp = G1Projective;
34-
type Ga = G1Affine;
35-
36-
#[allow(non_snake_case)]
37-
#[test]
38-
fn DRNG_testing() {
39-
let mut rng = TestDRNG::new(b"hello world");
40-
println!("Next u32 : {}", rng.next_u32());
41-
println!("randint : {}", rng.randint(0, 1000000000));
42-
// println!("randint : {}", rng.randint(0, 52435875175126190479447740508185965837690552500527637822603658699938581184513));
43-
let low = BigUint::parse_bytes(b"0", 10).unwrap();
44-
let high = BigUint::parse_bytes(b"73EDA753299D7C00000000000000000000000000000000000000000000000000", 16).unwrap();
45-
let rand = rng.randint_big(&low, &high);
46-
println!("{}", rand);
47-
}
48-
49-
50-
#[allow(non_snake_case)]
51-
#[test]
52-
fn Scalar_test() {
53-
let rng = TestDRNG::new(b"hello world");
54-
let y = <Gp as Group>::Scalar::random(rng);
55-
let ZERO = <Gp as Group>::Scalar::ZERO;
56-
let ONE = y * y.invert().unwrap();
57-
let ONE_inv = ONE.invert().unwrap();
58-
let TWO = ONE + ONE;
59-
let TWO_INV = TWO.invert().unwrap();
60-
let ch = hex::decode("26a48d1bb889d46d66689d580335f2ac713f36abaaaa1eaa5555555500000003").into().unwrap();
61-
let Z = <Gp as Group>::Scalar::from_bytes(ch).unwrap();
62-
let Z_inv = Z.invert().unwrap();
63-
let _W = <Gp as Group>::Scalar::from_bytes(&Z_inv.to_bytes()).unwrap();
64-
println!("y = {}", y);
65-
println!("ZERO = {}", ZERO);
66-
println!("ONE = {}", ONE);
67-
println!("ONE_inv = {}", ONE_inv);
68-
println!("TWO = {}", TWO);
69-
println!("TWO_INV = {}", TWO_INV);
70-
println!("Z = {}", Z);
71-
println!("Z_inv = {}", Z_inv);
72-
println!("W = {}", TWO * TWO);
73-
}
74-
75-
#[allow(non_snake_case)]
76-
#[test]
77-
fn DRNG_test_on_Scalar() {
78-
let mut rng = TestDRNG::new(b"hello world");
79-
let x = G1Projective::random(&mut rng);
80-
let y = G1Projective::random(&mut rng);
81-
println!("x = {}", x);
82-
println!("y = {}", y);
83-
}
84-
85-
86-
#[allow(non_snake_case)]
87-
#[test]
88-
fn DRNG_test_on_Group() {
89-
let mut _rng = TestDRNG::new(b"hello world");
90-
let H = Ga::identity();
91-
let _bytes = H.to_uncompressed();
92-
println!("Voici H : {}", H);
93-
}
94-

tests/spec/sage_test_vectors.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
use bls12_381::G1Projective;
22
use rand::{Rng, CryptoRng};
33
use group::{Group, GroupEncoding};
4-
use random::{SRandom, TestDRNG};
5-
use custom_schnorr_proof::SchnorrProofCustom;
64

7-
use sigma_rs::toolbox::sigma::transcript::KeccakDuplexSponge;
85
use sigma_rs::toolbox::sigma::{
6+
transcript::{ByteSchnorrCodec, KeccakDuplexSponge},
97
GroupMorphismPreimage,
10-
transcript::ByteSchnorrCodec,
118
NISigmaProtocol,
129
};
1310

11+
use crate::{
12+
random::SRandom,
13+
test_drng::TestDRNG,
14+
custom_schnorr_proof::SchnorrProofCustom,
15+
};
16+
1417
type Gp = G1Projective;
1518
type Codec = ByteSchnorrCodec::<Gp, KeccakDuplexSponge>;
1619
type SigmaP = SchnorrProofCustom<Gp>;
1720

21+
1822
fn msm_pr<G: Group>(scalars: &[G::Scalar], bases: &[G]) -> G {
1923
let mut acc = G::identity();
2024
for (s, p) in scalars.iter().zip(bases.iter()) {

tests/spec/test_sponge.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

tests/spec_tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[path = "spec/bls12_381.rs"]
2+
mod bls12_381;
3+
4+
#[path = "spec/custom_schnorr_proof.rs"]
5+
mod custom_schnorr_proof;
6+
7+
#[path = "spec/low_level_tests.rs"]
8+
mod low_level_tests;
9+
10+
#[path = "spec/random.rs"]
11+
mod random;
12+
13+
#[path = "spec/sage_test_vectors.rs"]
14+
mod sage_test_vectors;
15+
16+
#[path = "spec/test_drng.rs"]
17+
mod test_drng;

0 commit comments

Comments
 (0)