From 2d6e1498ee99ef991d20b281ac28da708a5b3e73 Mon Sep 17 00:00:00 2001 From: kilic Date: Mon, 5 Oct 2020 13:54:38 +0300 Subject: [PATCH] parameterise test cases --- examples/export_test_keys/main.rs | 3 +- src/circuit/bench.rs | 14 ++++---- src/circuit/poseidon.rs | 2 +- src/circuit/rln.rs | 59 +++++++++++++++++++++++++++---- src/merkle.rs | 4 +-- src/poseidon.rs | 47 ++++++++++++------------ 6 files changed, 85 insertions(+), 44 deletions(-) diff --git a/examples/export_test_keys/main.rs b/examples/export_test_keys/main.rs index 9ebd5cf..14d352d 100644 --- a/examples/export_test_keys/main.rs +++ b/examples/export_test_keys/main.rs @@ -20,7 +20,8 @@ mod test_keys { use rln::poseidon::PoseidonParams; use sapling_crypto::bellman::groth16::generate_random_parameters; use std::fs::File; - let poseidon_params = PoseidonParams::::default(); + + let poseidon_params = PoseidonParams::::new(8, 55, 3, None, None, None); let mut rng = XorShiftRng::from_seed([0x3dbe6258, 0x8d313d76, 0x3237db17, 0xe5bc0654]); let hasher = PoseidonCircuit::new(poseidon_params.clone()); let circuit = RLNCircuit:: { diff --git a/src/circuit/bench.rs b/src/circuit/bench.rs index 1a010bf..79608af 100644 --- a/src/circuit/bench.rs +++ b/src/circuit/bench.rs @@ -34,12 +34,11 @@ impl BenchResult { } } -// TODO make it composable: curve, poseidon, merkle -pub fn run_rln_bn(merkle_depth: usize) -> BenchResult { - use sapling_crypto::bellman::pairing::bn256::Bn256; - let poseidon_params = PoseidonParams::::default(); - let rln_test = RLNTest::new(poseidon_params, merkle_depth); - rln_test.run() +pub fn run_rln_bench( + merkle_depth: usize, + poseidon_params: PoseidonParams, +) -> BenchResult { + RLNTest::new(merkle_depth, poseidon_params).run() } pub struct RLNTest @@ -54,8 +53,7 @@ impl RLNTest where E: Engine, { - pub fn new(poseidon_params: PoseidonParams, merkle_depth: usize) -> RLNTest { - // let cs = TestConstraintSystem::::new(); + pub fn new(merkle_depth: usize, poseidon_params: PoseidonParams) -> RLNTest { RLNTest:: { poseidon_params, merkle_depth, diff --git a/src/circuit/poseidon.rs b/src/circuit/poseidon.rs index 1777933..c86cbfd 100644 --- a/src/circuit/poseidon.rs +++ b/src/circuit/poseidon.rs @@ -367,7 +367,7 @@ fn test_poseidon_circuit() { use sapling_crypto::circuit::test::TestConstraintSystem; let mut cs = TestConstraintSystem::::new(); - let params = PoseidonParams::default(); + let params = PoseidonParams::new(8, 55, 3, None, None, None); let inputs: Vec = ["0", "0"] .iter() diff --git a/src/circuit/rln.rs b/src/circuit/rln.rs index 5c2d3a3..ed440b8 100644 --- a/src/circuit/rln.rs +++ b/src/circuit/rln.rs @@ -374,17 +374,62 @@ mod test { use super::RLNInputs; use crate::circuit::bench; + use crate::poseidon::PoseidonParams; + use sapling_crypto::bellman::pairing::bls12_381::Bls12; + use sapling_crypto::bellman::pairing::bn256::Bn256; + use sapling_crypto::bellman::pairing::Engine; - #[test] - fn test_rln_bn_24() { - let result = bench::run_rln_bn(24); - result.print(); + struct TestSuite { + merkle_depth: usize, + poseidon_parameters: PoseidonParams, + } + + fn cases() -> Vec> { + vec![ + TestSuite { + merkle_depth: 16, + poseidon_parameters: PoseidonParams::new(8, 55, 3, None, None, None), + }, + TestSuite { + merkle_depth: 24, + poseidon_parameters: PoseidonParams::new(8, 55, 3, None, None, None), + }, + TestSuite { + merkle_depth: 32, + poseidon_parameters: PoseidonParams::new(8, 55, 3, None, None, None), + }, + TestSuite { + merkle_depth: 16, + poseidon_parameters: PoseidonParams::new(8, 33, 3, None, None, None), + }, + TestSuite { + merkle_depth: 24, + poseidon_parameters: PoseidonParams::new(8, 33, 3, None, None, None), + }, + TestSuite { + merkle_depth: 32, + poseidon_parameters: PoseidonParams::new(8, 33, 3, None, None, None), + }, + ] } #[test] - fn test_rln_bn_32() { - let result = bench::run_rln_bn(32); - result.print(); + fn test_rln_bn() { + use sapling_crypto::bellman::pairing::bn256::Bn256; + let cases = cases::(); + for case in cases.iter() { + let rln_test = + bench::RLNTest::::new(case.merkle_depth, case.poseidon_parameters.clone()); + let result = rln_test.run(); + println!( + "bn256, t: {}, rf: {}, rp: {}, merkle depth: {}", + case.poseidon_parameters.width(), + case.poseidon_parameters.full_round_half_len() * 2, + case.poseidon_parameters.partial_round_len(), + case.merkle_depth, + ); + result.print(); + } } #[test] diff --git a/src/merkle.rs b/src/merkle.rs index 57354f1..9964511 100644 --- a/src/merkle.rs +++ b/src/merkle.rs @@ -132,7 +132,7 @@ fn test_merkle_set() { .map(|s| Fr::from_str(&format!("{}", s)).unwrap()) .collect(); use sapling_crypto::bellman::pairing::bn256::{Bn256, Fr, FrRepr}; - let params = PoseidonParams::::default(); + let params = PoseidonParams::::new(8, 55, 3, None, None, None); let hasher = Hasher::new(params); let mut set = MerkleTree::empty(hasher, 3); let leaf_index = 6; @@ -144,7 +144,7 @@ fn test_merkle_set() { #[test] fn test_merkle_zeros() { use sapling_crypto::bellman::pairing::bn256::{Bn256, Fr, FrRepr}; - let params = PoseidonParams::::default(); + let params = PoseidonParams::::new(8, 55, 3, None, None, None); let hasher = Hasher::new(params); let mut set = MerkleTree::empty(hasher, 32); set.insert(5, Fr::from_str("1").unwrap(), Some(Fr::zero())); diff --git a/src/poseidon.rs b/src/poseidon.rs index 184a980..dadd1ee 100644 --- a/src/poseidon.rs +++ b/src/poseidon.rs @@ -24,28 +24,34 @@ impl PoseidonParams { rf: usize, rp: usize, t: usize, - round_constants: Vec, - mds_matrix: Vec, + round_constants: Option>, + mds_matrix: Option>, + seed: Option>, ) -> PoseidonParams { - assert_eq!(rf + rp, round_constants.len()); + let seed = match seed { + Some(seed) => seed, + None => b"".to_vec(), + }; + + let _round_constants = match round_constants { + Some(round_constants) => round_constants, + None => PoseidonParams::::generate_constants(b"drlnhdsc", seed.clone(), rf + rp), + }; + assert_eq!(rf + rp, _round_constants.len()); + + let _mds_matrix = match mds_matrix { + Some(mds_matrix) => mds_matrix, + None => PoseidonParams::::generate_mds_matrix(b"drlnhdsm", seed.clone(), t), + }; PoseidonParams { rf, rp, t, - round_constants, - mds_matrix, + round_constants: _round_constants, + mds_matrix: _mds_matrix, } } - pub fn default() -> PoseidonParams { - let (t, rf, rp) = (3usize, 8usize, 55usize); - let seed = b"".to_vec(); - let round_constants = - PoseidonParams::::generate_constants(b"drlnhdsc", seed.clone(), rf + rp); - let mds_matrix = PoseidonParams::::generate_mds_matrix(b"drlnhdsm", seed.clone(), t); - PoseidonParams::new(rf, rp, t, round_constants, mds_matrix) - } - pub fn width(&self) -> usize { return self.t; } @@ -111,16 +117,6 @@ impl PoseidonParams { } impl Poseidon { - pub fn new_with_params( - rf: usize, - rp: usize, - t: usize, - round_constants: Vec, - mds_matrix: Vec, - ) -> Poseidon { - let params = PoseidonParams::new(rf, rp, t, round_constants, mds_matrix); - Poseidon::new(params) - } pub fn new(params: PoseidonParams) -> Poseidon { Poseidon { round: 0, @@ -235,7 +231,8 @@ impl Poseidon { fn test_poseidon_hash() { use sapling_crypto::bellman::pairing::bn256; use sapling_crypto::bellman::pairing::bn256::{Bn256, Fr}; - let mut hasher = Poseidon::::new(PoseidonParams::default()); + let params = PoseidonParams::::new(8, 55, 3, None, None, None); + let mut hasher = Poseidon::::new(params); let input1: Vec = ["0"].iter().map(|e| Fr::from_str(e).unwrap()).collect(); let r1: Fr = hasher.hash(input1.to_vec()); let input2: Vec = ["0", "0"]