Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 7 additions & 0 deletions src/groups/curves/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ where
F: FieldVar<P::BaseField, BasePrimeField<P>>,
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
{
type BaseFieldVar = F;

fn constant(g: SWProjective<P>) -> Self {
let cs = ConstraintSystemRef::None;
Self::new_variable_omit_on_curve_check(cs, || Ok(g), AllocationMode::Constant).unwrap()
Expand Down Expand Up @@ -573,6 +575,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: 6 additions & 0 deletions src/groups/curves/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ where
+ TwoBitLookupGadget<BasePrimeField<P>, TableConstant = P::BaseField>,
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
{
type BaseFieldVar = F;

fn constant(g: TEProjective<P>) -> Self {
let cs = ConstraintSystemRef::None;
Self::new_variable_omit_on_curve_check(cs, || Ok(g), AllocationMode::Constant).unwrap()
Expand Down Expand Up @@ -547,6 +549,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: 15 additions & 0 deletions src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField>:
+ for<'a> Mul<&'a EmulatedFpVar<C::ScalarField, ConstraintF>, Output = Self>
+ MulAssign<EmulatedFpVar<C::ScalarField, ConstraintF>>
{
type BaseFieldVar: FieldVar<C::BaseField, ConstraintF>;

/// Returns the constant `F::zero()`. This is the identity
/// of the group.
fn zero() -> Self;
Expand All @@ -68,6 +70,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<(Self::BaseFieldVar, Self::BaseFieldVar), SynthesisError>;

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

/// Returns the y coordinate in Affine representation.
fn affine_y(&self) -> Result<Self::BaseFieldVar, 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