Skip to content

Commit 45e4e26

Browse files
Add affine_xy, affine_x, affine_y functions to CurveVar (#190)
* Add xy, x, y functions to CurveVar * Refactor * Update CHANGELOG.md * Refactor * Refactor * Update mod.rs --------- Co-authored-by: Weikeng Chen <w.k@berkeley.edu>
1 parent 0d7272f commit 45e4e26

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- Add `UInt::{from_bytes_le, from_bytes_be, to_bytes_be}`.
3939
- [\#143](https://github.com/arkworks-rs/r1cs-std/pull/143) Add `AllocVar::new_variable_with_inferred_mode`.
4040
- [\#144](https://github.com/arkworks-rs/r1cs-std/pull/144) Add `ToConstraintFieldGadget` bounds to `CurveVar` and `FieldVar`
41+
- [\#190](https://github.com/arkworks-rs/r1cs-std/pull/190) Add `affine_xy, affine_x, affine_y` functions to `CurveVar`
4142

4243
### Improvements
4344

src/groups/curves/short_weierstrass/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ where
377377
F: FieldVar<P::BaseField, BasePrimeField<P>>,
378378
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
379379
{
380+
type BaseFieldVar = F;
381+
380382
fn constant(g: SWProjective<P>) -> Self {
381383
let cs = ConstraintSystemRef::None;
382384
Self::new_variable_omit_on_curve_check(cs, || Ok(g), AllocationMode::Constant).unwrap()
@@ -573,6 +575,11 @@ where
573575
*self += Self::constant(base).scalar_mul_le(bits.iter())?;
574576
Ok(())
575577
}
578+
579+
fn affine_xy(&self) -> Result<(F, F), SynthesisError> {
580+
let self_affine = self.to_affine()?;
581+
Ok((self_affine.x, self_affine.y))
582+
}
576583
}
577584

578585
impl<P, F> ToConstraintFieldGadget<BasePrimeField<P>> for ProjectiveVar<P, F>

src/groups/curves/twisted_edwards/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ where
402402
+ TwoBitLookupGadget<BasePrimeField<P>, TableConstant = P::BaseField>,
403403
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
404404
{
405+
type BaseFieldVar = F;
406+
405407
fn constant(g: TEProjective<P>) -> Self {
406408
let cs = ConstraintSystemRef::None;
407409
Self::new_variable_omit_on_curve_check(cs, || Ok(g), AllocationMode::Constant).unwrap()
@@ -547,6 +549,10 @@ where
547549

548550
Ok(())
549551
}
552+
553+
fn affine_xy(&self) -> Result<(F, F), SynthesisError> {
554+
Ok((self.x.clone(), self.y.clone()))
555+
}
550556
}
551557

552558
impl<P, F> AllocVar<TEProjective<P>, BasePrimeField<P>> for AffineVar<P, F>

src/groups/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField>:
5353
+ for<'a> Mul<&'a EmulatedFpVar<C::ScalarField, ConstraintF>, Output = Self>
5454
+ MulAssign<EmulatedFpVar<C::ScalarField, ConstraintF>>
5555
{
56+
/// The base field of the coordinates.
57+
type BaseFieldVar: FieldVar<C::BaseField, ConstraintF>;
58+
5659
/// Returns the constant `F::zero()`. This is the identity
5760
/// of the group.
5861
fn zero() -> Self;
@@ -68,6 +71,19 @@ pub trait CurveVar<C: CurveGroup, ConstraintF: PrimeField>:
6871
/// This *should not* allocate any variables.
6972
fn constant(other: C) -> Self;
7073

74+
/// Returns the x and y coordinates in Affine representation.
75+
fn affine_xy(&self) -> Result<(Self::BaseFieldVar, Self::BaseFieldVar), SynthesisError>;
76+
77+
/// Returns the x coordinate in Affine representation.
78+
fn affine_x(&self) -> Result<Self::BaseFieldVar, SynthesisError> {
79+
self.affine_xy().map(|(x, _)| x)
80+
}
81+
82+
/// Returns the y coordinate in Affine representation.
83+
fn affine_y(&self) -> Result<Self::BaseFieldVar, SynthesisError> {
84+
self.affine_xy().map(|(_, y)| y)
85+
}
86+
7187
/// Allocates a variable in the subgroup without checking if it's in the
7288
/// prime-order subgroup.
7389
fn new_variable_omit_prime_order_check(

0 commit comments

Comments
 (0)