Skip to content

Commit

Permalink
Pass all test except for test_ring_proof_kzg_te
Browse files Browse the repository at this point in the history
  • Loading branch information
drskalman committed Sep 24, 2024
1 parent 5b87474 commit a196581
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions ring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,6 +38,17 @@ pub fn find_complement_point<Curve: SWCurveConfig>() -> Affine<Curve> {
}
}

pub fn find_random_point<F: Field, P: AffineRepr<BaseField=F>>() -> 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<A: AffineRepr>(message: &[u8]) -> A {
use blake2::Digest;
Expand All @@ -52,7 +63,7 @@ pub fn hash_to_curve<A: AffineRepr>(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;
Expand All @@ -62,21 +73,25 @@ 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;
use crate::ring_verifier::RingVerifier;

use super::*;

fn _test_ring_proof<CS: PCS<Fq>>(domain_size: usize) {
fn _test_ring_proof<CS: PCS<Fq>, P: AffineRepr<BaseField=Fq, ScalarField=Fr>, CondAddT: CondAdd<Fq, P> + ProverGadget<Fq>>(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::<SWAffine, _>(keyset_size, rng);
let pks = random_vec::<P, _>(keyset_size, rng);
let k = rng.gen_range(0..keyset_size); // prover's secret index
let pk = pks[k].clone();

Expand All @@ -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::<CondAddT>(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::<SwCondAddValues<Fq>>(proof, result.into_affine());
end_timer!(t_verify);
assert!(res);
}
Expand All @@ -103,7 +118,7 @@ mod tests {

let domain_size = 2usize.pow(9);

let (pcs_params, piop_params) = setup::<_, KZG<Bls12_381>>(rng, domain_size);
let (pcs_params, piop_params) = setup::<_, KZG<Bls12_381>, SWAffine>(rng, domain_size);
let ring_builder_key = RingBuilderKey::from_srs(&pcs_params, domain_size);

let max_keyset_size = piop_params.keyset_part_size;
Expand All @@ -118,13 +133,13 @@ mod tests {
assert_eq!(fixed_columns_committed, verifier_key.fixed_columns_committed);
}

fn setup<R: Rng, CS: PCS<Fq>>(rng: &mut R, domain_size: usize) -> (CS::Params, PiopParams<Fq, BandersnatchConfig>) {
fn setup<R: Rng, CS: PCS<Fq>, P: AffineRepr<BaseField=Fq>>(rng: &mut R, domain_size: usize) -> (CS::Params, PiopParams<Fq, P>) {
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::<BandersnatchConfig>();
let h = P::rand(rng);
let seed = find_random_point::<Fq, P>();
let piop_params = PiopParams::setup(domain, h, seed);

(pcs_params, piop_params)
Expand All @@ -139,12 +154,17 @@ mod tests {
}

#[test]
fn test_ring_proof_kzg() {
_test_ring_proof::<KZG<Bls12_381>>(2usize.pow(10));
fn test_ring_proof_kzg_sw() {
_test_ring_proof::<KZG<Bls12_381>, SWAffine, SwCondAdd<Fq, SWAffine>>(2usize.pow(10));
}

#[test]
fn test_ring_proof_kzg_te() {
_test_ring_proof::<KZG<Bls12_381>, EdwardsAffine, TeCondAdd<Fq, EdwardsAffine>>(2usize.pow(10));
}

#[test]
fn test_ring_proof_id() {
_test_ring_proof::<fflonk::pcs::IdentityCommitment>(2usize.pow(10));
_test_ring_proof::<fflonk::pcs::IdentityCommitment, SWAffine, SwCondAdd<Fq, SWAffine>>(2usize.pow(10));
}
}

0 comments on commit a196581

Please sign in to comment.