Skip to content

Commit

Permalink
Removes CurveAffineExt (privacy-scaling-explorations#67)
Browse files Browse the repository at this point in the history
remove `CurveAffineExt`
  • Loading branch information
kilic authored Jul 14, 2023
1 parent 1ab41bb commit 5c24fbd
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 243 deletions.
18 changes: 0 additions & 18 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ pub trait CurveEndo: CurveExt {
fn decompose_scalar(e: &Self::ScalarExt) -> (u128, bool, u128, bool);
}

pub trait CurveAffineExt: pasta_curves::arithmetic::CurveAffine {
fn batch_add<const COMPLETE: bool, const LOAD_POINTS: bool>(
points: &mut [Self],
output_indices: &[u32],
num_points: usize,
offset: usize,
bases: &[Self],
base_positions: &[u32],
);

/// Unlike the `Coordinates` trait, this just returns the raw affine coordinates without checking `is_on_curve`
fn into_coordinates(self) -> (Self::Base, Self::Base) {
// fallback implementation
let coordinates = self.coordinates().unwrap();
(*coordinates.x(), *coordinates.y())
}
}

/// Compute a + b + carry, returning the result and the new carry over.
#[inline(always)]
pub(crate) const fn adc(a: u64, b: u64, carry: u64) -> (u64, u64) {
Expand Down
24 changes: 4 additions & 20 deletions src/bn256/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::group::Curve;
use crate::group::{cofactor::CofactorGroup, prime::PrimeCurveAffine, Group, GroupEncoding};
use crate::hash_to_curve::svdw_hash_to_curve;
use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, new_curve_impl,
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
new_curve_impl,
};
use crate::{Coordinates, CurveAffine, CurveAffineExt, CurveExt};
use crate::{Coordinates, CurveAffine, CurveExt};
use core::cmp;
use core::fmt::Debug;
use core::iter::Sum;
Expand Down Expand Up @@ -56,22 +56,6 @@ new_curve_impl!(
|_, _| unimplemented!(),
);

impl CurveAffineExt for G1Affine {
batch_add!();

fn into_coordinates(self) -> (Self::Base, Self::Base) {
(self.x, self.y)
}
}

impl CurveAffineExt for G2Affine {
batch_add!();

fn into_coordinates(self) -> (Self::Base, Self::Base) {
(self.x, self.y)
}
}

const G1_GENERATOR_X: Fq = Fq::one();
const G1_GENERATOR_Y: Fq = Fq::from_raw([2, 0, 0, 0]);
const G1_A: Fq = Fq::from_raw([0, 0, 0, 0]);
Expand Down
142 changes: 0 additions & 142 deletions src/derive/curve.rs
Original file line number Diff line number Diff line change
@@ -1,145 +1,3 @@
#[macro_export]
macro_rules! batch_add {
() => {
fn batch_add<const COMPLETE: bool, const LOAD_POINTS: bool>(
points: &mut [Self],
output_indices: &[u32],
num_points: usize,
offset: usize,
bases: &[Self],
base_positions: &[u32],
) {
// assert!(Self::constant_a().is_zero());

let get_point = |point_data: u32| -> Self {
let negate = point_data & 0x80000000 != 0;
let base_idx = (point_data & 0x7FFFFFFF) as usize;
if negate {
bases[base_idx].neg()
} else {
bases[base_idx]
}
};

// Affine addition formula (P != Q):
// - lambda = (y_2 - y_1) / (x_2 - x_1)
// - x_3 = lambda^2 - (x_2 + x_1)
// - y_3 = lambda * (x_1 - x_3) - y_1

// Batch invert accumulator
let mut acc = Self::Base::one();

for i in (0..num_points).step_by(2) {
// Where that result of the point addition will be stored
let out_idx = output_indices[i >> 1] as usize - offset;

#[cfg(all(feature = "prefetch", target_arch = "x86_64"))]
if i < num_points - 2 {
if LOAD_POINTS {
$crate::prefetch::<Self>(bases, base_positions[i + 2] as usize);
$crate::prefetch::<Self>(bases, base_positions[i + 3] as usize);
}
$crate::prefetch::<Self>(
points,
output_indices[(i >> 1) + 1] as usize - offset,
);
}
if LOAD_POINTS {
points[i] = get_point(base_positions[i]);
points[i + 1] = get_point(base_positions[i + 1]);
}

if COMPLETE {
// Nothing to do here if one of the points is zero
if (points[i].is_identity() | points[i + 1].is_identity()).into() {
continue;
}

if points[i].x == points[i + 1].x {
if points[i].y == points[i + 1].y {
// Point doubling (P == Q)
// - s = (3 * x^2) / (2 * y)
// - x_2 = s^2 - (2 * x)
// - y_2 = s * (x - x_2) - y

// (2 * x)
points[out_idx].x = points[i].x + points[i].x;
// x^2
let xx = points[i].x.square();
// (2 * y)
points[i + 1].x = points[i].y + points[i].y;
// (3 * x^2) * acc
points[i + 1].y = (xx + xx + xx) * acc;
// acc * (2 * y)
acc *= points[i + 1].x;
continue;
} else {
// Zero
points[i] = Self::identity();
points[i + 1] = Self::identity();
continue;
}
}
}

// (x_2 + x_1)
points[out_idx].x = points[i].x + points[i + 1].x;
// (x_2 - x_1)
points[i + 1].x -= points[i].x;
// (y2 - y1) * acc
points[i + 1].y = (points[i + 1].y - points[i].y) * acc;
// acc * (x_2 - x_1)
acc *= points[i + 1].x;
}

// Batch invert
if COMPLETE {
if (!acc.is_zero()).into() {
acc = acc.invert().unwrap();
}
} else {
acc = acc.invert().unwrap();
}

for i in (0..num_points).step_by(2).rev() {
// Where that result of the point addition will be stored
let out_idx = output_indices[i >> 1] as usize - offset;

#[cfg(all(feature = "prefetch", target_arch = "x86_64"))]
if i > 0 {
$crate::prefetch::<Self>(
points,
output_indices[(i >> 1) - 1] as usize - offset,
);
}

if COMPLETE {
// points[i] is zero so the sum is points[i + 1]
if points[i].is_identity().into() {
points[out_idx] = points[i + 1];
continue;
}
// points[i + 1] is zero so the sum is points[i]
if points[i + 1].is_identity().into() {
points[out_idx] = points[i];
continue;
}
}

// lambda
points[i + 1].y *= acc;
// acc * (x_2 - x_1)
acc *= points[i + 1].x;
// x_3 = lambda^2 - (x_2 + x_1)
points[out_idx].x = points[i + 1].y.square() - points[out_idx].x;
// y_3 = lambda * (x_1 - x_3) - y_1
points[out_idx].y =
points[i + 1].y * (points[i].x - points[out_idx].x) - points[i].y;
}
}
};
}

#[macro_export]
macro_rules! endo {
($name:ident, $field:ident, $params:expr) => {
Expand Down
16 changes: 4 additions & 12 deletions src/grumpkin/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::grumpkin::Fq;
use crate::grumpkin::Fr;
use crate::hash_to_curve::svdw_hash_to_curve;
use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, new_curve_impl,
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
new_curve_impl,
};
use crate::{Coordinates, CurveAffine, CurveAffineExt, CurveExt};
use crate::{Coordinates, CurveAffine, CurveExt};
use core::cmp;
use core::fmt::Debug;
use core::iter::Sum;
Expand All @@ -35,14 +35,6 @@ new_curve_impl!(
|curve_id, domain_prefix| svdw_hash_to_curve(curve_id, domain_prefix, G1::SVDW_Z),
);

impl CurveAffineExt for G1Affine {
batch_add!();

fn into_coordinates(self) -> (Self::Base, Self::Base) {
(self.x, self.y)
}
}

// Parameters in montgomery form taken from
// https://github.com/AztecProtocol/barretenberg/blob/97ccf76c42db581a8b8f8bfbcffe8ca015a3dd22/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp#L14
const G1_GENERATOR_X: Fq = Fq::one();
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod secp256r1;

#[macro_use]
mod derive;
pub use arithmetic::CurveAffineExt;
pub use pasta_curves::arithmetic::{Coordinates, CurveAffine, CurveExt};

// Re-export ff and group to simplify down stream dependencies
Expand Down
26 changes: 0 additions & 26 deletions src/pasta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@ use ff::WithSmallOrderMulGroup;
pub use pasta_curves::{pallas, vesta, Ep, EpAffine, Eq, EqAffine, Fp, Fq};
use std::convert::TryInto;

impl crate::CurveAffineExt for EpAffine {
fn batch_add<const COMPLETE: bool, const LOAD_POINTS: bool>(
_: &mut [Self],
_: &[u32],
_: usize,
_: usize,
_: &[Self],
_: &[u32],
) {
unimplemented!();
}
}

impl crate::CurveAffineExt for EqAffine {
fn batch_add<const COMPLETE: bool, const LOAD_POINTS: bool>(
_: &mut [Self],
_: &[u32],
_: usize,
_: usize,
_: &[Self],
_: &[u32],
) {
unimplemented!();
}
}

// Generated using https://github.com/ConsenSys/gnark-crypto/blob/master/ecc/utils.go
// with `pasta_curves::Fp::ZETA`
// See https://github.com/demining/Endomorphism-Secp256k1/blob/main/README.md
Expand Down
16 changes: 4 additions & 12 deletions src/secp256k1/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ff::{Field, PrimeField};
use crate::group::{prime::PrimeCurveAffine, Curve, Group as _, GroupEncoding};
use crate::secp256k1::Fp;
use crate::secp256k1::Fq;
use crate::{Coordinates, CurveAffine, CurveAffineExt, CurveExt};
use crate::{Coordinates, CurveAffine, CurveExt};
use core::cmp;
use core::fmt::Debug;
use core::iter::Sum;
Expand Down Expand Up @@ -48,9 +48,9 @@ const SECP_A: Fp = Fp::from_raw([0, 0, 0, 0]);
const SECP_B: Fp = Fp::from_raw([7, 0, 0, 0]);

use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, new_curve_impl,
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
new_curve_impl,
};

new_curve_impl!(
Expand All @@ -67,14 +67,6 @@ new_curve_impl!(
|_, _| unimplemented!(),
);

impl CurveAffineExt for Secp256k1Affine {
batch_add!();

fn into_coordinates(self) -> (Self::Base, Self::Base) {
(self.x, self.y)
}
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256k1>();
Expand Down
16 changes: 4 additions & 12 deletions src/secp256r1/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ff::{Field, PrimeField};
use crate::group::{prime::PrimeCurveAffine, Curve, Group as _, GroupEncoding};
use crate::secp256r1::Fp;
use crate::secp256r1::Fq;
use crate::{Coordinates, CurveAffine, CurveAffineExt, CurveExt};
use crate::{Coordinates, CurveAffine, CurveExt};
use core::cmp;
use core::fmt::Debug;
use core::iter::Sum;
Expand Down Expand Up @@ -59,9 +59,9 @@ const SECP_B: Fp = Fp::from_raw([
]);

use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
impl_binops_additive_specify_output, impl_binops_multiplicative,
impl_binops_multiplicative_mixed, impl_sub_binop_specify_output, new_curve_impl,
impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output,
impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_sub_binop_specify_output,
new_curve_impl,
};

new_curve_impl!(
Expand All @@ -78,14 +78,6 @@ new_curve_impl!(
|_, _| unimplemented!(),
);

impl CurveAffineExt for Secp256r1Affine {
batch_add!();

fn into_coordinates(self) -> (Self::Base, Self::Base) {
(self.x, self.y)
}
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256r1>();
Expand Down

0 comments on commit 5c24fbd

Please sign in to comment.