diff --git a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
index 7494b3b..d189d1e 100644
--- a/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
+++ b/r1cs-std/src/groups/curves/short_weierstrass/bls12/mod.rs
@@ -1,234 +1,195 @@
use algebra::{
- curves::bls12::{Bls12Parameters, G1Prepared, G2Prepared, TwistType},
+ curves::{
+ bls12::{Bls12Parameters, G1Prepared, G2Prepared, TwistType},
+ short_weierstrass_jacobian::GroupAffine,
+ },
fields::Field,
- BitIterator, One, ProjectiveCurve,
+ BitIterator, One,
};
-use r1cs_core::{ConstraintSystem, SynthesisError};
+use r1cs_core::SynthesisError;
use crate::{
- fields::{fp::FpGadget, fp2::Fp2Gadget, FieldGadget},
- groups::curves::short_weierstrass::AffineGadget,
- prelude::*,
+ fields::{fp::FpVar, fp2::Fp2Var, FieldVar},
+ groups::curves::short_weierstrass::*,
Vec,
};
-use core::{borrow::Borrow, fmt::Debug, ops::Mul};
+use core::fmt::Debug;
-pub type G1Gadget
= AffineGadget<
-
::G1Parameters,
-
::Fp,
- FpGadget<
::Fp>,
->;
+pub type G1Var
=
+ ProjectiveVar<
::G1Parameters, FpVar<
::Fp>>;
+pub type G1AffineVar
=
+ AffineVar<
::G1Parameters, FpVar<
::Fp>>;
-pub type G2Gadget
=
- AffineGadget<
::G2Parameters,
::Fp, Fp2G
>;
+pub type G2Var
= ProjectiveVar<
::G2Parameters, Fp2G
>;
+pub type G2AffineVar
= AffineVar<
::G2Parameters, Fp2G
>;
#[derive(Derivative)]
-#[derivative(
- Clone(bound = "G1Gadget
: Clone"),
- Debug(bound = "G1Gadget
: Debug")
-)]
-pub struct G1PreparedGadget(pub G1Gadget);
-
-impl AllocGadget, P::Fp> for G1PreparedGadget {
- fn alloc_constant>(
- mut cs: CS,
- t: T,
- ) -> Result
- where
- T: Borrow>,
- {
- let obj = t.borrow();
-
- Ok(Self(G1Gadget::::alloc_constant(
- &mut cs.ns(|| "g1"),
- &obj.0.into(),
- )?))
- }
-
- fn alloc>(_cs: CS, _f: F) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
+#[derivative(Clone(bound = "G1Var: Clone"), Debug(bound = "G1Var
: Debug"))]
+pub struct G1PreparedVar(pub AffineVar>);
+
+impl G1PreparedVar {
+ pub fn value(&self) -> Result, SynthesisError> {
+ let x = self.0.x.value()?;
+ let y = self.0.y.value()?;
+ let infinity = self.0.infinity.value()?;
+ let g = GroupAffine::new(x, y, infinity);
+ Ok(g.into())
}
- fn alloc_input>(
- _cs: CS,
- _f: F,
- ) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
+ pub fn from_group_var(q: &G1Var) -> Result {
+ let g = q.to_affine()?;
+ Ok(Self(g))
}
}
-impl G1PreparedGadget {
- pub fn get_value(&self) -> Option> {
- Some(G1Prepared::from(self.0.get_value().unwrap().into_affine()))
- }
-
- pub fn from_affine>(
- _cs: CS,
- q: &G1Gadget,
+impl AllocVar, P::Fp> for G1PreparedVar {
+ fn new_variable>>(
+ cs: impl Into>,
+ f: impl FnOnce() -> Result,
+ mode: AllocationMode,
) -> Result {
- Ok(G1PreparedGadget(q.clone()))
+ let ns = cs.into();
+ let cs = ns.cs();
+ let g1_prep = f().map(|b| b.borrow().0);
+
+ let x = FpVar::new_variable(cs.ns("x"), || g1_prep.map(|g| g.x), mode)?;
+ let y = FpVar::new_variable(cs.ns("y"), || g1_prep.map(|g| g.y), mode)?;
+ let infinity = Boolean::new_variable(cs.ns("inf"), || g1_prep.map(|g| g.infinity), mode)?;
+ let g = AffineVar::new(x, y, infinity);
+ Ok(Self(g))
}
}
-impl ToBytesGadget for G1PreparedGadget {
+impl ToBytesGadget for G1PreparedVar {
#[inline]
- fn to_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- self.0.to_bytes(&mut cs.ns(|| "g_alpha to bytes"))
+ fn to_bytes(&self) -> Result>, SynthesisError> {
+ let mut bytes = self.0.x.to_bytes()?;
+ let y_bytes = self.0.y.to_bytes()?;
+ let inf_bytes = self.0.infinity.to_bytes()?;
+ bytes.extend_from_slice(&y_bytes);
+ bytes.extend_from_slice(&inf_bytes);
+ Ok(bytes)
}
- fn to_non_unique_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- self.0
- .to_non_unique_bytes(&mut cs.ns(|| "g_alpha to bytes"))
+ fn to_non_unique_bytes(&self) -> Result>, SynthesisError> {
+ let mut bytes = self.0.x.to_bytes()?;
+ let y_bytes = self.0.y.to_bytes()?;
+ let inf_bytes = self.0.infinity.to_bytes()?;
+ bytes.extend_from_slice(&y_bytes);
+ bytes.extend_from_slice(&inf_bytes);
+ Ok(bytes)
}
}
-type Fp2G = Fp2Gadget<
::Fp2Params,
::Fp>;
+type Fp2G
= Fp2Var<
::Fp2Params>;
type LCoeff
= (Fp2G
, Fp2G
);
#[derive(Derivative)]
#[derivative(
- Clone(bound = "Fp2Gadget: Clone"),
- Debug(bound = "Fp2Gadget: Debug")
+ Clone(bound = "Fp2Var: Clone"),
+ Debug(bound = "Fp2Var: Debug")
)]
-pub struct G2PreparedGadget {
+pub struct G2PreparedVar {
pub ell_coeffs: Vec>,
}
-impl AllocGadget, P::Fp> for G2PreparedGadget {
- fn alloc_constant>(
- mut cs: CS,
- t: T,
- ) -> Result
- where
- T: Borrow>,
- {
- let obj = t.borrow();
- let mut res = Vec::>::new();
-
- for (i, (x, y, z)) in obj.ell_coeffs.iter().enumerate() {
- let z_inverse = z.inverse().unwrap();
-
- let x_normalized = x.mul(&z_inverse);
- let y_normalized = y.mul(&z_inverse);
-
- let x_gadget =
- Fp2Gadget::alloc_constant(&mut cs.ns(|| format!("alloc_x#{}", i)), x_normalized)?;
- let y_gadget =
- Fp2Gadget::alloc_constant(&mut cs.ns(|| format!("alloc_y#{}", i)), y_normalized)?;
-
- res.push((x_gadget, y_gadget));
- }
-
- Ok(Self { ell_coeffs: res })
- }
-
- fn alloc>(_cs: CS, _f: F) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
- }
-
- fn alloc_input>(
- _cs: CS,
- _f: F,
- ) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
+impl AllocVar, P::Fp> for G2PreparedVar {
+ fn new_variable>>(
+ cs: impl Into>,
+ f: impl FnOnce() -> Result,
+ mode: AllocationMode,
+ ) -> Result {
+ let ns = cs.into();
+ let cs = ns.cs();
+ let g2_prep = f().map(|b| {
+ let projective_coeffs = &b.borrow().ell_coeffs;
+ let mut z_s = projective_coeffs
+ .iter()
+ .map(|(_, _, z)| *z)
+ .collect::>();
+ algebra::fields::batch_inversion(&mut z_s);
+ projective_coeffs
+ .iter()
+ .zip(z_s)
+ .map(|((x, y, _), z_inv)| (*x * &z_inv, *y * &z_inv))
+ .collect::>()
+ });
+
+ let l = Vec::new_variable(
+ cs.ns("l"),
+ || {
+ g2_prep
+ .clone()
+ .map(|c| c.iter().map(|(l, _)| *l).collect::>())
+ },
+ mode,
+ )?;
+ let r = Vec::new_variable(
+ cs.ns("r"),
+ || g2_prep.map(|c| c.iter().map(|(_, r)| *r).collect::>()),
+ mode,
+ )?;
+ let ell_coeffs = l.into_iter().zip(r).collect();
+ Ok(Self { ell_coeffs })
}
}
-impl ToBytesGadget for G2PreparedGadget {
+impl ToBytesGadget for G2PreparedVar {
#[inline]
- fn to_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
+ fn to_bytes(&self) -> Result>, SynthesisError> {
let mut bytes = Vec::new();
- for (i, coeffs) in self.ell_coeffs.iter().enumerate() {
- let mut cs = cs.ns(|| format!("Iteration {}", i));
- bytes.extend_from_slice(&coeffs.0.to_bytes(&mut cs.ns(|| "c0"))?);
- bytes.extend_from_slice(&coeffs.1.to_bytes(&mut cs.ns(|| "c1"))?);
+ for coeffs in &self.ell_coeffs {
+ bytes.extend_from_slice(&coeffs.0.to_bytes()?);
+ bytes.extend_from_slice(&coeffs.1.to_bytes()?);
}
Ok(bytes)
}
- fn to_non_unique_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
+ fn to_non_unique_bytes(&self) -> Result>, SynthesisError> {
let mut bytes = Vec::new();
- for (i, coeffs) in self.ell_coeffs.iter().enumerate() {
- let mut cs = cs.ns(|| format!("Iteration {}", i));
- bytes.extend_from_slice(&coeffs.0.to_non_unique_bytes(&mut cs.ns(|| "c0"))?);
- bytes.extend_from_slice(&coeffs.1.to_non_unique_bytes(&mut cs.ns(|| "c1"))?);
+ for coeffs in &self.ell_coeffs {
+ bytes.extend_from_slice(&coeffs.0.to_non_unique_bytes()?);
+ bytes.extend_from_slice(&coeffs.1.to_non_unique_bytes()?);
}
Ok(bytes)
}
}
-impl G2PreparedGadget {
- pub fn from_affine>(
- mut cs: CS,
- q: &G2Gadget,
- ) -> Result {
+impl G2PreparedVar {
+ pub fn from_group_var(q: &G2Var
) -> Result {
+ let q = q.to_affine()?;
let two_inv = P::Fp::one().double().inverse().unwrap();
- let zero = G2Gadget::::zero(cs.ns(|| "zero"))?;
- q.enforce_not_equal(cs.ns(|| "enforce not zero"), &zero)?;
+ // Enforce that `q` is not the point at infinity.
+ q.infinity.enforce_not_equal(&Boolean::Constant(true))?;
let mut ell_coeffs = vec![];
let mut r = q.clone();
- for (j, i) in BitIterator::new(P::X).skip(1).enumerate() {
- let mut cs = cs.ns(|| format!("Iteration {}", j));
- ell_coeffs.push(Self::double(cs.ns(|| "double"), &mut r, &two_inv)?);
+ for i in BitIterator::new(P::X).skip(1) {
+ ell_coeffs.push(Self::double(&mut r, &two_inv)?);
if i {
- ell_coeffs.push(Self::add(cs.ns(|| "add"), &mut r, &q)?);
+ ell_coeffs.push(Self::add(&mut r, &q)?);
}
}
Ok(Self { ell_coeffs })
}
- fn double>(
- mut cs: CS,
- r: &mut G2Gadget,
- two_inv: &P::Fp,
- ) -> Result, SynthesisError> {
- let a = r.y.inverse(cs.ns(|| "Inverse"))?;
- let mut b = r.x.square(cs.ns(|| "square x"))?;
+ fn double(r: &mut G2AffineVar, two_inv: &P::Fp) -> Result, SynthesisError> {
+ let a = r.y.inverse()?;
+ let mut b = r.x.square()?;
let b_tmp = b.clone();
- b.mul_by_fp_constant_in_place(cs.ns(|| "mul by two_inv"), two_inv)?;
- b.add_in_place(cs.ns(|| "compute b"), &b_tmp)?;
-
- let c = a.mul(cs.ns(|| "compute c"), &b)?;
- let d = r.x.double(cs.ns(|| "compute d"))?;
- let x3 = c.square(cs.ns(|| "c^2"))?.sub(cs.ns(|| "sub d"), &d)?;
- let e = c
- .mul(cs.ns(|| "c*r.x"), &r.x)?
- .sub(cs.ns(|| "sub r.y"), &r.y)?;
- let c_x3 = c.mul(cs.ns(|| "c*x_3"), &x3)?;
- let y3 = e.sub(cs.ns(|| "e = c * x3"), &c_x3)?;
+ b.mul_assign_by_base_field_constant(*two_inv);
+ b += &b_tmp;
+
+ let c = &a * &b;
+ let d = r.x.double()?;
+ let x3 = c.square()? - &d;
+ let e = &c * &r.x - &r.y;
+ let c_x3 = &c * &x3;
+ let y3 = &e - &c_x3;
let mut f = c;
- f.negate_in_place(cs.ns(|| "c = -c"))?;
+ f.negate_in_place()?;
r.x = x3;
r.y = y3;
match P::TWIST_TYPE {
@@ -237,28 +198,18 @@ impl G2PreparedGadget {
}
}
- fn add>(
- mut cs: CS,
- r: &mut G2Gadget,
- q: &G2Gadget
,
- ) -> Result, SynthesisError> {
- let a =
- q.x.sub(cs.ns(|| "q.x - r.x"), &r.x)?
- .inverse(cs.ns(|| "calc a"))?;
- let b = q.y.sub(cs.ns(|| "q.y - r.y"), &r.y)?;
- let c = a.mul(cs.ns(|| "compute c"), &b)?;
- let d = r.x.add(cs.ns(|| "r.x + q.x"), &q.x)?;
- let x3 = c.square(cs.ns(|| "c^2"))?.sub(cs.ns(|| "sub d"), &d)?;
+ fn add(r: &mut G2AffineVar, q: &G2AffineVar
) -> Result, SynthesisError> {
+ let a = (&q.x - &r.x).inverse()?;
+ let b = &q.y - &r.y;
+ let c = &a * &b;
+ let d = &r.x + &q.x;
+ let x3 = c.square()? - &d;
- let e =
- r.x.sub(cs.ns(|| "r.x - x3"), &x3)?
- .mul(cs.ns(|| "c * (r.x - x3)"), &c)?;
- let y3 = e.sub(cs.ns(|| "calc y3"), &r.y)?;
- let g = c
- .mul(cs.ns(|| "c*r.x"), &r.x)?
- .sub(cs.ns(|| "calc g"), &r.y)?;
+ let e = (&r.x - &x3) * &c;
+ let y3 = e - &r.y;
+ let g = &c * &r.x - &r.y;
let mut f = c;
- f.negate_in_place(cs.ns(|| "c = -c"))?;
+ f.negate_in_place()?;
r.x = x3;
r.y = y3;
match P::TWIST_TYPE {
diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mnt4/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mnt4/mod.rs
index def96b4..6f515af 100644
--- a/r1cs-std/src/groups/curves/short_weierstrass/mnt4/mod.rs
+++ b/r1cs-std/src/groups/curves/short_weierstrass/mnt4/mod.rs
@@ -5,94 +5,64 @@ use algebra::{
},
Field,
};
-use r1cs_core::{ConstraintSystem, SynthesisError};
+use r1cs_core::{Namespace, SynthesisError};
use crate::{
- fields::{fp::FpGadget, fp2::Fp2Gadget, FieldGadget},
- groups::curves::short_weierstrass::AffineGadget,
- pairing::mnt4::PairingGadget,
+ fields::{fp::FpVar, fp2::Fp2Var, FieldVar},
+ groups::curves::short_weierstrass::ProjectiveVar,
+ pairing::mnt4::PairingVar,
prelude::*,
Vec,
};
use core::borrow::Borrow;
-pub type G1Gadget = AffineGadget<
-
::G1Parameters,
-
::Fp,
- FpGadget<
::Fp>,
->;
+pub type G1Var
=
+ ProjectiveVar<
::G1Parameters, FpVar<
::Fp>>;
-pub type G2Gadget
=
- AffineGadget<
::G2Parameters,
::Fp, Fp2G
>;
+pub type G2Var
= ProjectiveVar<
::G2Parameters, Fp2G
>;
#[derive(Derivative)]
#[derivative(Clone(bound = "P: MNT4Parameters"), Debug(bound = "P: MNT4Parameters"))]
-pub struct G1PreparedGadget {
- pub x: FpGadget,
- pub y: FpGadget,
- pub x_twist: Fp2Gadget,
- pub y_twist: Fp2Gadget,
+pub struct G1PreparedVar {
+ pub x: FpVar,
+ pub y: FpVar,
+ pub x_twist: Fp2Var,
+ pub y_twist: Fp2Var,
}
-impl G1PreparedGadget {
- pub fn get_value(&self) -> Option> {
- match (
- self.x.get_value(),
- self.y.get_value(),
- self.x_twist.get_value(),
- self.y_twist.get_value(),
- ) {
- (Some(x), Some(y), Some(x_twist), Some(y_twist)) => Some(G1Prepared {
- x,
- y,
- x_twist,
- y_twist,
- }),
- _ => None,
- }
- }
-
- pub fn from_affine>(
- mut cs: CS,
- q: &G1Gadget,
+impl AllocVar, P::Fp> for G1PreparedVar {
+ fn new_variable>>(
+ cs: impl Into>,
+ f: impl FnOnce() -> Result,
+ mode: AllocationMode,
) -> Result {
- let x_twist = Fp2Gadget::new(
- q.x.mul_by_constant(cs.ns(|| "g1.x * twist.c0"), &P::TWIST.c0)?,
- q.x.mul_by_constant(cs.ns(|| "g1.x * twist.c1"), &P::TWIST.c1)?,
- );
- let y_twist = Fp2Gadget::new(
- q.y.mul_by_constant(cs.ns(|| "g1.y * twist.c0"), &P::TWIST.c0)?,
- q.y.mul_by_constant(cs.ns(|| "g1.y * twist.c1"), &P::TWIST.c1)?,
- );
- Ok(G1PreparedGadget {
- x: q.x.clone(),
- y: q.y.clone(),
+ let ns = cs.into();
+ let cs = ns.cs();
+
+ let g1_prep = f().map(|b| *b.borrow());
+
+ let x = FpVar::new_variable(cs.ns("x"), || g1_prep.map(|g| g.x), mode)?;
+ let y = FpVar::new_variable(cs.ns("y"), || g1_prep.map(|g| g.y), mode)?;
+ let x_twist = Fp2Var::new_variable(cs.ns("x_twist"), || g1_prep.map(|g| g.x_twist), mode)?;
+ let y_twist = Fp2Var::new_variable(cs.ns("y_twist"), || g1_prep.map(|g| g.y_twist), mode)?;
+ Ok(Self {
+ x,
+ y,
x_twist,
y_twist,
})
}
}
-impl AllocGadget, P::Fp> for G1PreparedGadget {
- fn alloc_constant>(
- mut cs: CS,
- t: T,
- ) -> Result
- where
- T: Borrow>,
- {
- let obj = t.borrow();
- let x = FpGadget::::alloc_constant(&mut cs.ns(|| "alloc_x"), &obj.x)?;
- let y = FpGadget::::alloc_constant(&mut cs.ns(|| "alloc_y"), &obj.y)?;
- let x_twist = Fp2Gadget::::alloc_constant(
- &mut cs.ns(|| "alloc_x_twist"),
- &obj.x_twist,
- )?;
- let y_twist = Fp2Gadget::::alloc_constant(
- &mut cs.ns(|| "alloc_y_twist"),
- &obj.y_twist,
- )?;
- Ok(G1PreparedGadget {
+impl G1PreparedVar {
+ pub fn value(&self) -> Result, SynthesisError> {
+ let (x, y, x_twist, y_twist) = (
+ self.x.value()?,
+ self.y.value()?,
+ self.x_twist.value()?,
+ self.y_twist.value()?,
+ );
+ Ok(G1Prepared {
x,
y,
x_twist,
@@ -100,36 +70,26 @@ impl AllocGadget, P::Fp> for G1PreparedGadget>(_cs: CS, _f: F) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
- }
-
- fn alloc_input>(
- _cs: CS,
- _f: F,
- ) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
+ pub fn from_group_var(q: &G1Var) -> Result {
+ let q = q.to_affine()?;
+ let x_twist = Fp2Var::new(&q.x * P::TWIST.c0, &q.x * P::TWIST.c1);
+ let y_twist = Fp2Var::new(&q.y * P::TWIST.c0, &q.y * P::TWIST.c1);
+ Ok(G1PreparedVar {
+ x: q.x.clone(),
+ y: q.y.clone(),
+ x_twist,
+ y_twist,
+ })
}
}
-impl ToBytesGadget for G1PreparedGadget {
+impl ToBytesGadget for G1PreparedVar {
#[inline]
- fn to_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- let mut x = self.x.to_bytes(&mut cs.ns(|| "x to bytes"))?;
- let mut y = self.y.to_bytes(&mut cs.ns(|| "y to bytes"))?;
- let mut x_twist = self.x_twist.to_bytes(&mut cs.ns(|| "x_twist to bytes"))?;
- let mut y_twist = self.y_twist.to_bytes(&mut cs.ns(|| "y_twist to bytes"))?;
+ fn to_bytes(&self) -> Result>, SynthesisError> {
+ let mut x = self.x.to_bytes()?;
+ let mut y = self.y.to_bytes()?;
+ let mut x_twist = self.x_twist.to_bytes()?;
+ let mut y_twist = self.y_twist.to_bytes()?;
x.append(&mut y);
x.append(&mut x_twist);
@@ -137,18 +97,11 @@ impl ToBytesGadget for G1PreparedGadget {
Ok(x)
}
- fn to_non_unique_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- let mut x = self.x.to_non_unique_bytes(&mut cs.ns(|| "x to bytes"))?;
- let mut y = self.y.to_non_unique_bytes(&mut cs.ns(|| "y to bytes"))?;
- let mut x_twist = self
- .x_twist
- .to_non_unique_bytes(&mut cs.ns(|| "x_twist to bytes"))?;
- let mut y_twist = self
- .y_twist
- .to_non_unique_bytes(&mut cs.ns(|| "y_twist to bytes"))?;
+ fn to_non_unique_bytes(&self) -> Result>, SynthesisError> {
+ let mut x = self.x.to_non_unique_bytes()?;
+ let mut y = self.y.to_non_unique_bytes()?;
+ let mut x_twist = self.x_twist.to_non_unique_bytes()?;
+ let mut y_twist = self.y_twist.to_non_unique_bytes()?;
x.append(&mut y);
x.append(&mut x_twist);
@@ -157,53 +110,48 @@ impl ToBytesGadget for G1PreparedGadget {
}
}
-type Fp2G
= Fp2Gadget<
::Fp2Params,
::Fp>;
+type Fp2G
= Fp2Var<
::Fp2Params>;
+
#[derive(Derivative)]
#[derivative(Clone(bound = "P: MNT4Parameters"), Debug(bound = "P: MNT4Parameters"))]
-pub struct G2PreparedGadget {
- pub x: Fp2Gadget,
- pub y: Fp2Gadget,
- pub x_over_twist: Fp2Gadget,
- pub y_over_twist: Fp2Gadget,
- pub double_coefficients: Vec>,
- pub addition_coefficients: Vec>,
+pub struct G2PreparedVar {
+ pub x: Fp2Var,
+ pub y: Fp2Var,
+ pub x_over_twist: Fp2Var,
+ pub y_over_twist: Fp2Var,
+ pub double_coefficients: Vec>,
+ pub addition_coefficients: Vec>,
}
-impl AllocGadget, P::Fp> for G2PreparedGadget {
- fn alloc_constant>(
- mut cs: CS,
- t: T,
- ) -> Result
- where
- T: Borrow>,
- {
- let obj = t.borrow();
- let x = Fp2Gadget::::alloc_constant(&mut cs.ns(|| "alloc_x"), &obj.x)?;
- let y = Fp2Gadget::::alloc_constant(&mut cs.ns(|| "alloc_y"), &obj.y)?;
- let x_over_twist = Fp2Gadget::::alloc_constant(
- &mut cs.ns(|| "alloc_x_over_twist"),
- &obj.x_over_twist,
+impl AllocVar, P::Fp> for G2PreparedVar {
+ fn new_variable>>(
+ cs: impl Into>,
+ f: impl FnOnce() -> Result,
+ mode: AllocationMode,
+ ) -> Result {
+ let ns = cs.into();
+ let cs = ns.cs();
+
+ let g2_prep = f().map(|b| b.borrow().clone());
+ let g2 = g2_prep.as_ref().map_err(|e| *e);
+
+ let x = Fp2Var::new_variable(cs.ns("x"), || g2.map(|g| g.x), mode)?;
+ let y = Fp2Var::new_variable(cs.ns("y"), || g2.map(|g| g.y), mode)?;
+ let x_over_twist =
+ Fp2Var::new_variable(cs.ns("x_over_twist"), || g2.map(|g| g.x_over_twist), mode)?;
+ let y_over_twist =
+ Fp2Var::new_variable(cs.ns("y_over_twist"), || g2.map(|g| g.y_over_twist), mode)?;
+ let double_coefficients = Vec::new_variable(
+ cs.ns("double coeffs"),
+ || g2.map(|g| g.double_coefficients.clone()),
+ mode,
)?;
- let y_over_twist = Fp2Gadget::::alloc_constant(
- &mut cs.ns(|| "alloc_y_over_twist"),
- &obj.y_over_twist,
+ let addition_coefficients = Vec::new_variable(
+ cs.ns("add coeffs"),
+ || g2.map(|g| g.addition_coefficients.clone()),
+ mode,
)?;
- let mut double_coefficients = Vec::>::new();
- for (i, item) in obj.double_coefficients.iter().enumerate() {
- double_coefficients.push(AteDoubleCoefficientsGadget::::alloc_constant(
- &mut cs.ns(|| format!("alloc_double_coefficients_{}", i)),
- item,
- )?);
- }
- let mut addition_coefficients = Vec::>::new();
- for (i, item) in obj.addition_coefficients.iter().enumerate() {
- addition_coefficients.push(AteAdditionCoefficientsGadget::::alloc_constant(
- &mut cs.ns(|| format!("alloc_addition_coefficients_{}", i)),
- item,
- )?);
- }
-
- Ok(G2PreparedGadget {
+ Ok(Self {
x,
y,
x_over_twist,
@@ -212,142 +160,93 @@ impl AllocGadget, P::Fp> for G2PreparedGadget>(_cs: CS, _f: F) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
- }
-
- fn alloc_input>(
- _cs: CS,
- _f: F,
- ) -> Result
- where
- F: FnOnce() -> Result,
- T: Borrow>,
- {
- todo!()
- }
}
-impl ToBytesGadget for G2PreparedGadget {
+impl ToBytesGadget for G2PreparedVar {
#[inline]
- fn to_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- let mut x = self.x.to_bytes(&mut cs.ns(|| "x to bytes"))?;
- let mut y = self.y.to_bytes(&mut cs.ns(|| "y to bytes"))?;
- let mut x_over_twist = self
- .x_over_twist
- .to_bytes(&mut cs.ns(|| "x_over_twist to bytes"))?;
- let mut y_over_twist = self
- .y_over_twist
- .to_bytes(&mut cs.ns(|| "y_over_twist to bytes"))?;
+ fn to_bytes(&self) -> Result>, SynthesisError> {
+ let mut x = self.x.to_bytes()?;
+ let mut y = self.y.to_bytes()?;
+ let mut x_over_twist = self.x_over_twist.to_bytes()?;
+ let mut y_over_twist = self.y_over_twist.to_bytes()?;
x.append(&mut y);
x.append(&mut x_over_twist);
x.append(&mut y_over_twist);
- for (i, coeff) in self.double_coefficients.iter().enumerate() {
- x.extend_from_slice(&coeff.to_bytes(cs.ns(|| format!("double_coefficients {}", i)))?);
+ for coeff in &self.double_coefficients {
+ x.extend_from_slice(&coeff.to_bytes()?);
}
- for (i, coeff) in self.addition_coefficients.iter().enumerate() {
- x.extend_from_slice(&coeff.to_bytes(cs.ns(|| format!("addition_coefficients {}", i)))?);
+ for coeff in &self.addition_coefficients {
+ x.extend_from_slice(&coeff.to_bytes()?);
}
Ok(x)
}
- fn to_non_unique_bytes>(
- &self,
- mut cs: CS,
- ) -> Result, SynthesisError> {
- let mut x = self.x.to_non_unique_bytes(&mut cs.ns(|| "x to bytes"))?;
- let mut y = self.y.to_non_unique_bytes(&mut cs.ns(|| "y to bytes"))?;
- let mut x_over_twist = self
- .x_over_twist
- .to_non_unique_bytes(&mut cs.ns(|| "x_over_twist to bytes"))?;
- let mut y_over_twist = self
- .y_over_twist
- .to_non_unique_bytes(&mut cs.ns(|| "y_over_twist to bytes"))?;
+ fn to_non_unique_bytes(&self) -> Result>, SynthesisError> {
+ let mut x = self.x.to_non_unique_bytes()?;
+ let mut y = self.y.to_non_unique_bytes()?;
+ let mut x_over_twist = self.x_over_twist.to_non_unique_bytes()?;
+ let mut y_over_twist = self.y_over_twist.to_non_unique_bytes()?;
x.append(&mut y);
x.append(&mut x_over_twist);
x.append(&mut y_over_twist);
- for (i, coeff) in self.double_coefficients.iter().enumerate() {
- x.extend_from_slice(
- &coeff.to_non_unique_bytes(cs.ns(|| format!("double_coefficients {}", i)))?,
- );
+ for coeff in &self.double_coefficients {
+ x.extend_from_slice(&coeff.to_non_unique_bytes()?);
}
- for (i, coeff) in self.addition_coefficients.iter().enumerate() {
- x.extend_from_slice(
- &coeff.to_non_unique_bytes(cs.ns(|| format!("addition_coefficients {}", i)))?,
- );
+ for coeff in &self.addition_coefficients {
+ x.extend_from_slice(&coeff.to_non_unique_bytes()?);
}
Ok(x)
}
}
-impl G2PreparedGadget {
- pub fn get_value(&self) -> Option> {
- match (
- self.x.get_value(),
- self.y.get_value(),
- self.x_over_twist.get_value(),
- self.y_over_twist.get_value(),
- self.double_coefficients
- .iter()
- .map(|coeff| coeff.get_value())
- .collect::