diff --git a/ring/src/lib.rs b/ring/src/lib.rs index ba4a385..a95488f 100644 --- a/ring/src/lib.rs +++ b/ring/src/lib.rs @@ -2,7 +2,7 @@ use ark_ec::AffineRepr; use ark_ec::short_weierstrass::{Affine, SWCurveConfig}; -use ark_ff::{One, Zero}; +use ark_ff::{One, Zero, Field}; use ark_std::rand; use fflonk::pcs::PCS; @@ -38,6 +38,17 @@ pub fn find_complement_point() -> Affine { } } +pub fn find_random_point>() -> P { + let mut x: u8 = 0; + loop { + let p = P::from_random_bytes(&[x]); + if p.is_some() && !p.unwrap().is_zero(){// && !p.unwrap().is_in_correct_subgroup_assuming_on_curve() { + return p.unwrap().clear_cofactor(); + } + x = x + 1; + } +} + // TODO: switch to better hash to curve when available pub fn hash_to_curve(message: &[u8]) -> A { use blake2::Digest; @@ -52,7 +63,7 @@ pub fn hash_to_curve(message: &[u8]) -> A { mod tests { use ark_bls12_381::Bls12_381; use ark_ec::CurveGroup; - use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, Fq, Fr, SWAffine}; + use ark_ed_on_bls12_381_bandersnatch::{BandersnatchConfig, Fq, Fr, SWAffine, EdwardsAffine}; use ark_ff::MontFp; use ark_std::{end_timer, start_timer, test_rng, UniformRand}; use ark_std::ops::Mul; @@ -62,6 +73,10 @@ mod tests { use common::test_helpers::random_vec; + use common::gadgets::sw_cond_add::{SwCondAdd, SwCondAddValues}; + use common::gadgets::te_cond_add::{TeCondAdd, TeCondAddValues}; + use common::gadgets::cond_add::CondAdd; + use common::gadgets::ProverGadget; use crate::piop::FixedColumnsCommitted; use crate::ring::{Ring, RingBuilderKey}; use crate::ring_prover::RingProver; @@ -69,14 +84,14 @@ mod tests { use super::*; - fn _test_ring_proof>(domain_size: usize) { + fn _test_ring_proof, P: AffineRepr, CondAddT: CondAdd + ProverGadget>(domain_size: usize) { let rng = &mut test_rng(); - let (pcs_params, piop_params) = setup::<_, CS>(rng, domain_size); + let (pcs_params, piop_params) = setup::<_, CS, P>(rng, domain_size); let max_keyset_size = piop_params.keyset_part_size; let keyset_size: usize = rng.gen_range(0..max_keyset_size); - let pks = random_vec::(keyset_size, rng); + let pks = random_vec::(keyset_size, rng); let k = rng.gen_range(0..keyset_size); // prover's secret index let pk = pks[k].clone(); @@ -87,12 +102,12 @@ mod tests { let result = piop_params.h.mul(secret) + pk; let ring_prover = RingProver::init(prover_key, piop_params.clone(), k, Transcript::new(b"ring-vrf-test")); let t_prove = start_timer!(|| "Prove"); - let proof = ring_prover.prove(secret); + let proof = ring_prover.prove::(secret); end_timer!(t_prove); let ring_verifier = RingVerifier::init(verifier_key, piop_params, Transcript::new(b"ring-vrf-test")); let t_verify = start_timer!(|| "Verify"); - let res = ring_verifier.verify_ring_proof(proof, result.into_affine()); + let res = ring_verifier.verify_ring_proof::>(proof, result.into_affine()); end_timer!(t_verify); assert!(res); } @@ -103,7 +118,7 @@ mod tests { let domain_size = 2usize.pow(9); - let (pcs_params, piop_params) = setup::<_, KZG>(rng, domain_size); + let (pcs_params, piop_params) = setup::<_, KZG, SWAffine>(rng, domain_size); let ring_builder_key = RingBuilderKey::from_srs(&pcs_params, domain_size); let max_keyset_size = piop_params.keyset_part_size; @@ -118,13 +133,13 @@ mod tests { assert_eq!(fixed_columns_committed, verifier_key.fixed_columns_committed); } - fn setup>(rng: &mut R, domain_size: usize) -> (CS::Params, PiopParams) { + fn setup, P: AffineRepr>(rng: &mut R, domain_size: usize) -> (CS::Params, PiopParams) { let setup_degree = 3 * domain_size; let pcs_params = CS::setup(setup_degree, rng); let domain = Domain::new(domain_size, true); - let h = SWAffine::rand(rng); - let seed = find_complement_point::(); + let h = P::rand(rng); + let seed = find_random_point::(); let piop_params = PiopParams::setup(domain, h, seed); (pcs_params, piop_params) @@ -139,12 +154,17 @@ mod tests { } #[test] - fn test_ring_proof_kzg() { - _test_ring_proof::>(2usize.pow(10)); + fn test_ring_proof_kzg_sw() { + _test_ring_proof::, SWAffine, SwCondAdd>(2usize.pow(10)); + } + + #[test] + fn test_ring_proof_kzg_te() { + _test_ring_proof::, EdwardsAffine, TeCondAdd>(2usize.pow(10)); } #[test] fn test_ring_proof_id() { - _test_ring_proof::(2usize.pow(10)); + _test_ring_proof::>(2usize.pow(10)); } }