diff --git a/arrabbiata/src/constraints.rs b/arrabbiata/src/constraints.rs index af08765c98..8665ca7265 100644 --- a/arrabbiata/src/constraints.rs +++ b/arrabbiata/src/constraints.rs @@ -17,7 +17,10 @@ use o1_utils::FieldHelpers; use poly_commitment::commitment::CommitmentCurve; #[derive(Clone, Debug)] -pub struct Env { +pub struct Env +where + C::BaseField: PrimeField, +{ /// The parameter a is the coefficients of the elliptic curve in affine /// coordinates. pub a: BigInt, @@ -31,10 +34,12 @@ pub struct Env { impl Env where C::BaseField: PrimeField, + <::Params as CurveConfig>::BaseField: PrimeField, { pub fn new() -> Self { // This check might not be useful - let a: BigInt = ::Params::COEFF_A.to_biguint().into(); + let a = ::Params::COEFF_A; + let a: BigInt = a.to_biguint().into(); assert!( a < C::ScalarField::modulus_biguint().into(), "a is too large" @@ -55,7 +60,10 @@ where /// proof. /// The constraint environment must be instantiated only once, at the last step /// of the computation. -impl InterpreterEnv for Env { +impl InterpreterEnv for Env +where + C::BaseField: PrimeField, +{ type Position = (Column, CurrOrNext); type Variable = E; @@ -311,7 +319,10 @@ impl InterpreterEnv for Env { } } -impl Env { +impl Env +where + C::BaseField: PrimeField, +{ /// Get all the constraints for the IVC circuit, only. /// /// The following gadgets are used in the IVC circuit: @@ -380,6 +391,7 @@ impl Env { impl Default for Env where + C::BaseField: PrimeField, <::Params as CurveConfig>::BaseField: PrimeField, { fn default() -> Self { diff --git a/arrabbiata/src/witness.rs b/arrabbiata/src/witness.rs index d574108b03..0f149e1fbd 100644 --- a/arrabbiata/src/witness.rs +++ b/arrabbiata/src/witness.rs @@ -56,10 +56,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, @@ -209,6 +209,8 @@ impl< where ::BaseField: PrimeField, ::BaseField: PrimeField, + E1::BaseField: PrimeField, + E2::BaseField: PrimeField, { type Position = (Column, CurrOrNext); @@ -258,9 +260,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 { @@ -280,9 +282,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] = v.clone(); @@ -297,9 +299,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)); @@ -431,10 +433,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) } } @@ -650,14 +652,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() @@ -677,9 +679,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) @@ -725,9 +727,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 @@ -773,6 +775,13 @@ impl< E1: ArrabbiataCurve, E2: ArrabbiataCurve, > Env +where + ::BaseField: PrimeField, + ::BaseField: PrimeField, + ::BigInt: Into<::BigInt>, + ::BigInt: Into<::BigInt>, + E1::BaseField: PrimeField, + E2::BaseField: PrimeField, { pub fn new( srs_log2_size: usize, @@ -781,16 +790,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)) @@ -799,8 +808,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 = { @@ -942,9 +951,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 @@ -963,9 +972,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 @@ -979,17 +988,19 @@ impl< fn absorb_state(&mut self) { if self.current_iteration % 2 == 0 { let sponge: DefaultFqSponge = - DefaultFqSponge::new(E1::other_curve_sponge_params()); + DefaultFqSponge::new(E2::other_curve_sponge_params()); self.sponge_e1.iter().for_each(|v| { - let v = Fq::from_biguint(&v.to_biguint().unwrap()).unwrap(); - sponge.absorb(v); + // let v = Fq::from_biguint(&v.to_biguint().unwrap()).unwrap(); + let v = E1::BaseField::zero(); + sponge.absorb_fq(&[v]); }); } else { let sponge: DefaultFqSponge = - DefaultFqSponge::new(E2::other_curve_sponge_params()); + DefaultFqSponge::new(E1::other_curve_sponge_params()); self.sponge_e2.iter().for_each(|v| { - let v = Fp::from_biguint(&v.to_biguint().unwrap()).unwrap(); - sponge.absorb(v); + let v = E2::BaseField::zero(); + // let v = Fp::from_biguint(&v.to_biguint().unwrap()).unwrap(); + sponge.absorb_fq(&[v]); }); } }