parameterise test cases

This commit is contained in:
kilic 2020-10-05 13:54:38 +03:00
parent ecfc186b3a
commit 2d6e1498ee
6 changed files with 85 additions and 44 deletions

View File

@ -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::<E>::default();
let poseidon_params = PoseidonParams::<E>::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::<E> {

View File

@ -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::<Bn256>::default();
let rln_test = RLNTest::new(poseidon_params, merkle_depth);
rln_test.run()
pub fn run_rln_bench<E: Engine>(
merkle_depth: usize,
poseidon_params: PoseidonParams<E>,
) -> BenchResult {
RLNTest::new(merkle_depth, poseidon_params).run()
}
pub struct RLNTest<E>
@ -54,8 +53,7 @@ impl<E> RLNTest<E>
where
E: Engine,
{
pub fn new(poseidon_params: PoseidonParams<E>, merkle_depth: usize) -> RLNTest<E> {
// let cs = TestConstraintSystem::<E>::new();
pub fn new(merkle_depth: usize, poseidon_params: PoseidonParams<E>) -> RLNTest<E> {
RLNTest::<E> {
poseidon_params,
merkle_depth,

View File

@ -367,7 +367,7 @@ fn test_poseidon_circuit() {
use sapling_crypto::circuit::test::TestConstraintSystem;
let mut cs = TestConstraintSystem::<Bn256>::new();
let params = PoseidonParams::default();
let params = PoseidonParams::new(8, 55, 3, None, None, None);
let inputs: Vec<Fr> = ["0", "0"]
.iter()

View File

@ -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<E: Engine> {
merkle_depth: usize,
poseidon_parameters: PoseidonParams<E>,
}
fn cases<E: Engine>() -> Vec<TestSuite<E>> {
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::<Bn256>();
for case in cases.iter() {
let rln_test =
bench::RLNTest::<Bn256>::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]

View File

@ -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::<Bn256>::default();
let params = PoseidonParams::<Bn256>::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::<Bn256>::default();
let params = PoseidonParams::<Bn256>::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()));

View File

@ -24,28 +24,34 @@ impl<E: Engine> PoseidonParams<E> {
rf: usize,
rp: usize,
t: usize,
round_constants: Vec<E::Fr>,
mds_matrix: Vec<E::Fr>,
round_constants: Option<Vec<E::Fr>>,
mds_matrix: Option<Vec<E::Fr>>,
seed: Option<Vec<u8>>,
) -> PoseidonParams<E> {
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::<E>::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::<E>::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<E> {
let (t, rf, rp) = (3usize, 8usize, 55usize);
let seed = b"".to_vec();
let round_constants =
PoseidonParams::<E>::generate_constants(b"drlnhdsc", seed.clone(), rf + rp);
let mds_matrix = PoseidonParams::<E>::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<E: Engine> PoseidonParams<E> {
}
impl<E: Engine> Poseidon<E> {
pub fn new_with_params(
rf: usize,
rp: usize,
t: usize,
round_constants: Vec<E::Fr>,
mds_matrix: Vec<E::Fr>,
) -> Poseidon<E> {
let params = PoseidonParams::new(rf, rp, t, round_constants, mds_matrix);
Poseidon::new(params)
}
pub fn new(params: PoseidonParams<E>) -> Poseidon<E> {
Poseidon {
round: 0,
@ -235,7 +231,8 @@ impl<E: Engine> Poseidon<E> {
fn test_poseidon_hash() {
use sapling_crypto::bellman::pairing::bn256;
use sapling_crypto::bellman::pairing::bn256::{Bn256, Fr};
let mut hasher = Poseidon::<Bn256>::new(PoseidonParams::default());
let params = PoseidonParams::<Bn256>::new(8, 55, 3, None, None, None);
let mut hasher = Poseidon::<Bn256>::new(params);
let input1: Vec<Fr> = ["0"].iter().map(|e| Fr::from_str(e).unwrap()).collect();
let r1: Fr = hasher.hash(input1.to_vec());
let input2: Vec<Fr> = ["0", "0"]