From a88fc636015059f3aec1ec5118499560f0b93132 Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Sun, 12 Jan 2025 08:52:50 +0100 Subject: [PATCH] Use E1/E2::ScalarField instead of Fp/Fq --- arrabbiata/src/witness.rs | 53 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/arrabbiata/src/witness.rs b/arrabbiata/src/witness.rs index d3b854f5dd..6982e60218 100644 --- a/arrabbiata/src/witness.rs +++ b/arrabbiata/src/witness.rs @@ -55,10 +55,10 @@ pub struct Env< // ---------------- // Setup related (domains + SRS) /// Domain for Fp - pub domain_fp: EvaluationDomains, + pub domain_fp: EvaluationDomains, /// Domain for Fq - pub domain_fq: EvaluationDomains, + pub domain_fq: EvaluationDomains, /// SRS for the first curve pub srs_e1: SRS, @@ -257,9 +257,9 @@ where unimplemented!("Only works for private inputs") }; let modulus: BigInt = if self.current_iteration % 2 == 0 { - Fp::modulus_biguint().into() + E1::ScalarField::modulus_biguint().into() } else { - Fq::modulus_biguint().into() + E2::ScalarField::modulus_biguint().into() }; let v = v.mod_floor(&modulus); match row { @@ -279,9 +279,9 @@ where unimplemented!("Only works for public input columns") }; let modulus: BigInt = if self.current_iteration % 2 == 0 { - Fp::modulus_biguint().into() + E1::ScalarField::modulus_biguint().into() } else { - Fq::modulus_biguint().into() + E2::ScalarField::modulus_biguint().into() }; let v = v.mod_floor(&modulus); self.public_state[idx].clone_from(&v); @@ -296,9 +296,9 @@ where fn constrain_boolean(&mut self, x: Self::Variable) { let modulus: BigInt = if self.current_iteration % 2 == 0 { - Fp::modulus_biguint().into() + E1::ScalarField::modulus_biguint().into() } else { - Fq::modulus_biguint().into() + E2::ScalarField::modulus_biguint().into() }; let x = x.mod_floor(&modulus); assert!(x == BigInt::from(0_usize) || x == BigInt::from(1_usize)); @@ -430,10 +430,10 @@ where unsafe fn save_poseidon_state(&mut self, x: Self::Variable, i: usize) { if self.current_iteration % 2 == 0 { - let modulus: BigInt = Fp::modulus_biguint().into(); + let modulus: BigInt = E1::ScalarField::modulus_biguint().into(); self.sponge_e1[i] = x.mod_floor(&modulus) } else { - let modulus: BigInt = Fq::modulus_biguint().into(); + let modulus: BigInt = E2::ScalarField::modulus_biguint().into(); self.sponge_e2[i] = x.mod_floor(&modulus) } } @@ -649,14 +649,14 @@ where /// Zero is not allowed as an input. unsafe fn inverse(&mut self, pos: Self::Position, x: Self::Variable) -> Self::Variable { let res = if self.current_iteration % 2 == 0 { - Fp::from_biguint(&x.to_biguint().unwrap()) + E1::ScalarField::from_biguint(&x.to_biguint().unwrap()) .unwrap() .inverse() .unwrap() .to_biguint() .into() } else { - Fq::from_biguint(&x.to_biguint().unwrap()) + E2::ScalarField::from_biguint(&x.to_biguint().unwrap()) .unwrap() .inverse() .unwrap() @@ -676,9 +676,9 @@ where y2: Self::Variable, ) -> Self::Variable { let modulus: BigInt = if self.current_iteration % 2 == 0 { - Fp::modulus_biguint().into() + E1::ScalarField::modulus_biguint().into() } else { - Fq::modulus_biguint().into() + E2::ScalarField::modulus_biguint().into() }; // If it is not the same point, we compute lambda as: // - λ = (Y1 - Y2) / (X1 - X2) @@ -726,9 +726,9 @@ where y1: Self::Variable, ) -> (Self::Variable, Self::Variable) { let modulus: BigInt = if self.current_iteration % 2 == 0 { - Fp::modulus_biguint().into() + E1::ScalarField::modulus_biguint().into() } else { - Fq::modulus_biguint().into() + E2::ScalarField::modulus_biguint().into() }; // - λ = (3X1^2 + a) / (2Y1) // We compute λ and use an additional column as a temporary value @@ -776,6 +776,9 @@ impl< E1: ArrabbiataCurve, E2: ArrabbiataCurve, > Env +where + E1::BaseField: PrimeField, + E2::BaseField: PrimeField, { pub fn new( srs_log2_size: usize, @@ -784,16 +787,16 @@ impl< sponge_e2: [BigInt; PlonkSpongeConstants::SPONGE_WIDTH], ) -> Self { { - assert!(Fp::MODULUS_BIT_SIZE <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fp is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}"); + assert!(E1::ScalarField::MODULUS_BIT_SIZE <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fp is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}"); assert!(Fq::MODULUS_BIT_SIZE <= MAXIMUM_FIELD_SIZE_IN_BITS.try_into().unwrap(), "The size of the field Fq is too large, it should be less than {MAXIMUM_FIELD_SIZE_IN_BITS}"); - let modulus_fp = Fp::modulus_biguint(); + let modulus_fp = E1::ScalarField::modulus_biguint(); let alpha = PlonkSpongeConstants::PERM_SBOX; assert!( (modulus_fp - BigUint::from(1_u64)).gcd(&BigUint::from(alpha)) == BigUint::from(1_u64), "The modulus of Fp should be coprime with {alpha}" ); - let modulus_fq = Fq::modulus_biguint(); + let modulus_fq = E2::ScalarField::modulus_biguint(); let alpha = PlonkSpongeConstants::PERM_SBOX; assert!( (modulus_fq - BigUint::from(1_u64)).gcd(&BigUint::from(alpha)) @@ -802,8 +805,8 @@ impl< ); } let srs_size = 1 << srs_log2_size; - let domain_fp = EvaluationDomains::::create(srs_size).unwrap(); - let domain_fq = EvaluationDomains::::create(srs_size).unwrap(); + let domain_fp = EvaluationDomains::::create(srs_size).unwrap(); + let domain_fq = EvaluationDomains::::create(srs_size).unwrap(); info!("Create an SRS of size {srs_log2_size} for the first curve"); let srs_e1: SRS = { @@ -945,9 +948,9 @@ impl< .witness .par_iter() .map(|evals| { - let evals: Vec = evals + let evals: Vec = evals .par_iter() - .map(|x| Fp::from_biguint(&x.to_biguint().unwrap()).unwrap()) + .map(|x| E1::ScalarField::from_biguint(&x.to_biguint().unwrap()).unwrap()) .collect(); let evals = Evaluations::from_vec_and_domain(evals.to_vec(), self.domain_fp.d1); self.srs_e1 @@ -966,9 +969,9 @@ impl< .witness .iter() .map(|evals| { - let evals: Vec = evals + let evals: Vec = evals .par_iter() - .map(|x| Fq::from_biguint(&x.to_biguint().unwrap()).unwrap()) + .map(|x| E2::ScalarField::from_biguint(&x.to_biguint().unwrap()).unwrap()) .collect(); let evals = Evaluations::from_vec_and_domain(evals.to_vec(), self.domain_fq.d1); self.srs_e2