Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- Add `UInt::{from_bytes_le, from_bytes_be, to_bytes_be}`.
- [\#143](https://github.com/arkworks-rs/r1cs-std/pull/143) Add `AllocVar::new_variable_with_inferred_mode`.
- [\#144](https://github.com/arkworks-rs/r1cs-std/pull/144) Add `ToConstraintFieldGadget` bounds to `CurveVar` and `FieldVar`
- [\#190](https://github.com/arkworks-rs/r1cs-std/pull/190) Add `affine_xy, affine_x, affine_y` functions to `CurveVar`

### Improvements

Expand Down
7 changes: 6 additions & 1 deletion src/groups/curves/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ where
}
}

impl<P, F> CurveVar<SWProjective<P>, BasePrimeField<P>> for ProjectiveVar<P, F>
impl<P, F> CurveVar<SWProjective<P>, BasePrimeField<P>, F> for ProjectiveVar<P, F>
where
P: SWCurveConfig,
F: FieldVar<P::BaseField, BasePrimeField<P>>,
Expand Down Expand Up @@ -573,6 +573,11 @@ where
*self += Self::constant(base).scalar_mul_le(bits.iter())?;
Ok(())
}

fn affine_xy(&self) -> Result<(F, F), SynthesisError> {
let self_affine = self.to_affine()?;
Ok((self_affine.x, self_affine.y))
}
}

impl<P, F> ToConstraintFieldGadget<BasePrimeField<P>> for ProjectiveVar<P, F>
Expand Down
6 changes: 5 additions & 1 deletion src/groups/curves/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ where
}
}

impl<P, F> CurveVar<TEProjective<P>, BasePrimeField<P>> for AffineVar<P, F>
impl<P, F> CurveVar<TEProjective<P>, BasePrimeField<P>, F> for AffineVar<P, F>
where
P: TECurveConfig,
F: FieldVar<P::BaseField, BasePrimeField<P>>
Expand Down Expand Up @@ -547,6 +547,10 @@ where

Ok(())
}

fn affine_xy(&self) -> Result<(F, F), SynthesisError> {
Ok((self.x.clone(), self.y.clone()))
}
}

impl<P, F> AllocVar<TEProjective<P>, BasePrimeField<P>> for AffineVar<P, F>
Expand Down
15 changes: 14 additions & 1 deletion src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub trait GroupOpsBounds<'a, G, T: 'a>:

/// A variable that represents a curve point for
/// the curve `C`.
pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField>:
pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField, F: FieldVar<C::BaseField, ConstraintF>>:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to add this as an associated type, as it would avoid updating every usage of CurveVar.

'static
+ Sized
+ Clone
Expand Down Expand Up @@ -68,6 +68,19 @@ pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField>:
/// This *should not* allocate any variables.
fn constant(other: C) -> Self;

/// Returns the x and y coordinates in Affine representation.
fn affine_xy(&self) -> Result<(F, F), SynthesisError>;

/// Returns the x coordinate in Affine representation.
fn affine_x(&self) -> Result<F, SynthesisError> {
self.affine_xy().map(|(x, _)| x)
}

/// Returns the y coordinate in Affine representation.
fn affine_y(&self) -> Result<F, SynthesisError> {
self.affine_xy().map(|(_, y)| y)
}

/// Allocates a variable in the subgroup without checking if it's in the
/// prime-order subgroup.
fn new_variable_omit_prime_order_check(
Expand Down
5 changes: 4 additions & 1 deletion src/pairing/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ impl<P: Bls12Config> PairingVar<P> {
}
}

impl<P: Bls12Config> PG<Bls12<P>> for PairingVar<P> {
impl<P: Bls12Config>
PG<Bls12<P>, FpVar<<P as Bls12Config>::Fp>, Fp2Var<<P as Bls12Config>::Fp2Config>>
for PairingVar<P>
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
4 changes: 3 additions & 1 deletion src/pairing/mnt4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ impl<P: MNT4Config> PairingVar<P> {
}
}

impl<P: MNT4Config> PG<MNT4<P>> for PairingVar<P> {
impl<P: MNT4Config> PG<MNT4<P>, FpVar<<P as MNT4Config>::Fp>, Fp2Var<<P as MNT4Config>::Fp2Config>>
for PairingVar<P>
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
4 changes: 3 additions & 1 deletion src/pairing/mnt6/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ impl<P: MNT6Config> PairingVar<P> {
}
}

impl<P: MNT6Config> PG<MNT6<P>> for PairingVar<P> {
impl<P: MNT6Config> PG<MNT6<P>, FpVar<<P as MNT6Config>::Fp>, Fp3Var<<P as MNT6Config>::Fp3Config>>
for PairingVar<P>
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
11 changes: 8 additions & 3 deletions src/pairing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ type BasePrimeField<E> = <<E as Pairing>::BaseField as ark_ff::Field>::BasePrime

/// Specifies the constraints for computing a pairing in the yybilinear group
/// `E`.
pub trait PairingVar<E: Pairing> {
pub trait PairingVar<
E: Pairing,
F1: FieldVar<<E::G1 as ark_ec::CurveGroup>::BaseField, E::BaseField>,
F2: FieldVar<<E::G2 as ark_ec::CurveGroup>::BaseField, E::BaseField>,
>
{
/// An variable representing an element of `G1`.
/// This is the R1CS equivalent of `E::G1Projective`.
type G1Var: CurveVar<E::G1, BasePrimeField<E>>;
type G1Var: CurveVar<E::G1, BasePrimeField<E>, F1>;

/// An variable representing an element of `G2`.
/// This is the R1CS equivalent of `E::G2Projective`.
type G2Var: CurveVar<E::G2, BasePrimeField<E>>;
type G2Var: CurveVar<E::G2, BasePrimeField<E>, F2>;

/// An variable representing an element of `GT`.
/// This is the R1CS equivalent of `E::GT`.
Expand Down