Skip to content

Commit

Permalink
Merge pull request #2981 from o1-labs/arrabbiata/use-curve-instead-of…
Browse files Browse the repository at this point in the history
…-field

Use E1/E2::ScalarField instead of Fp/Fq
  • Loading branch information
dannywillems authored Jan 30, 2025
2 parents 33ebe13 + a88fc63 commit 5df3d41
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions arrabbiata/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ pub struct Env<
// ----------------
// Setup related (domains + SRS)
/// Domain for Fp
pub domain_fp: EvaluationDomains<Fp>,
pub domain_fp: EvaluationDomains<E1::ScalarField>,

/// Domain for Fq
pub domain_fq: EvaluationDomains<Fq>,
pub domain_fq: EvaluationDomains<E2::ScalarField>,

/// SRS for the first curve
pub srs_e1: SRS<E1>,
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -776,6 +776,9 @@ impl<
E1: ArrabbiataCurve<ScalarField = Fp, BaseField = Fq>,
E2: ArrabbiataCurve<ScalarField = Fq, BaseField = Fp>,
> Env<Fp, Fq, E1, E2>
where
E1::BaseField: PrimeField,
E2::BaseField: PrimeField,
{
pub fn new(
srs_log2_size: usize,
Expand All @@ -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))
Expand All @@ -802,8 +805,8 @@ impl<
);
}
let srs_size = 1 << srs_log2_size;
let domain_fp = EvaluationDomains::<Fp>::create(srs_size).unwrap();
let domain_fq = EvaluationDomains::<Fq>::create(srs_size).unwrap();
let domain_fp = EvaluationDomains::<E1::ScalarField>::create(srs_size).unwrap();
let domain_fq = EvaluationDomains::<E2::ScalarField>::create(srs_size).unwrap();

info!("Create an SRS of size {srs_log2_size} for the first curve");
let srs_e1: SRS<E1> = {
Expand Down Expand Up @@ -945,9 +948,9 @@ impl<
.witness
.par_iter()
.map(|evals| {
let evals: Vec<Fp> = evals
let evals: Vec<E1::ScalarField> = 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
Expand All @@ -966,9 +969,9 @@ impl<
.witness
.iter()
.map(|evals| {
let evals: Vec<Fq> = evals
let evals: Vec<E2::ScalarField> = 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
Expand Down

0 comments on commit 5df3d41

Please sign in to comment.