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: 0 additions & 1 deletion Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ compiler_version = ">=1.0.0"
[dependencies]
bignum = { git = "https://github.com/noir-lang/noir-bignum", tag = "v0.8.2" }
poseidon = { git = "https://github.com/noir-lang/poseidon", tag = "v0.1.1" }

14 changes: 7 additions & 7 deletions src/curve_jac.nr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct CurveJ<B, BigCurve> {
pub(crate) x: B,
pub(crate) y: B,
pub(crate) z: B,
pub(crate) is_infinity: bool,
pub is_infinity: bool,
}

/// A transcript of a group operation in Jacobian coordinates.
Expand Down Expand Up @@ -67,9 +67,9 @@ where
/// # Optimization
/// If you have an array of `JTranscript` objects, you can convert them into `AffineTranscript` objects using only one modular inverse.
pub struct AffineTranscript<B> {
pub(crate) lambda: B,
pub(crate) x3: B,
pub(crate) y3: B,
pub lambda: B,
pub x3: B,
pub y3: B,
}

impl<B> AffineTranscript<B>
Expand All @@ -80,7 +80,7 @@ where
AffineTranscript { lambda: B::zero(), x3: B::zero(), y3: B::zero() }
}

pub(crate) unconstrained fn from_j(j_tx: JTranscript<B>) -> Self {
pub unconstrained fn from_j(j_tx: JTranscript<B>) -> Self {
AffineTranscript::from_jacobian_transcript([j_tx])[0]
}

Expand Down Expand Up @@ -279,11 +279,11 @@ where
CurveJ { x: B::zero(), y: B::zero(), z: B::zero(), is_infinity: true }
}

pub(crate) unconstrained fn sub(self, p2: Self) -> (Self, JTranscript<B>) {
pub unconstrained fn sub(self, p2: Self) -> (Self, JTranscript<B>) {
self.add(p2.neg())
}

pub(crate) unconstrained fn add(self, p2: Self) -> (Self, JTranscript<B>) {
pub unconstrained fn add(self, p2: Self) -> (Self, JTranscript<B>) {
let X1 = self.x;
let X2 = p2.x;
let Y1 = self.y;
Expand Down
38 changes: 20 additions & 18 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ pub(crate) mod utils;
pub mod curves;

use bignum::BigNum;
use bignum::bignum::evaluate_quadratic_expression;

use crate::curve_jac::AffineTranscript;
use crate::scalar_field::ScalarField;
use std::ops::{Add, Neg, Sub};
mod benchmarks;

// Re-exports for derive_curve_impl macro
pub use bignum::bignum::evaluate_quadratic_expression;
pub use curve_jac::{AffineTranscript, CurveJ};
pub use utils::hash_to_curve::hash_to_curve;

/// Implements an elliptic curve over a prime field that is not the circuit's native field.

pub struct BigCurveParams<B> {
Expand Down Expand Up @@ -77,8 +80,7 @@ pub comptime fn derive_curve_impl(
params: Quoted,
) -> Quoted {
let typ = struct_def.as_type();
let CurveJ = quote { $crate::curve_jac::CurveJ };
let AffineTranscript = quote { $crate::curve_jac::AffineTranscript };
let AffineTranscript = quote { $crate::AffineTranscript };

quote {
impl $crate::BigCurve<$field_type> for $typ {
Expand Down Expand Up @@ -168,7 +170,7 @@ pub comptime fn derive_curve_impl(
}

fn hash_to_curve<let N: u32>(seed: [u8; N]) -> Self {
let r = $crate::utils::hash_to_curve::hash_to_curve::<$field_type, N>(seed, $params.a, $params.b);
let r = $crate::hash_to_curve::<$field_type, N>(seed, $params.a, $params.b);
Self { x: r.0, y: r.1, is_infinity: false }
}

Expand All @@ -183,15 +185,15 @@ pub comptime fn derive_curve_impl(
// Expensive witness generation! Avoid if possible
impl std::ops::Add for $typ {
fn add(self, other: Self) -> Self {
let lhsJ = $crate::curve_jac::CurveJ::<$field_type, $typ>::from(self);
let rhsJ = $crate::curve_jac::CurveJ::<$field_type, $typ>::from(other);
let lhsJ = $crate::CurveJ::<$field_type, $typ>::from(self);
let rhsJ = $crate::CurveJ::<$field_type, $typ>::from(other);
let (result_jac, j_transcript) = unsafe { lhsJ.add(rhsJ) };
let transcript = unsafe { $crate::curve_jac::AffineTranscript::from_j(j_transcript) };
let transcript = unsafe { $crate::AffineTranscript::from_j(j_transcript) };
if std::runtime::is_unconstrained() {
$typ::from_coordinates(transcript.x3, transcript.y3, result_jac.is_infinity)

} else {
$crate::add_with_hint::<$field_type, $typ>(self, other, transcript)
$crate::add_with_hint::<$field_type, $typ>(self, other, transcript)
}
}
}
Expand All @@ -207,12 +209,12 @@ pub comptime fn derive_curve_impl(

impl std::ops::Sub for $typ {
fn sub(self, other: Self) -> Self {
let lhsJ = $crate::curve_jac::CurveJ::<$field_type, $typ>::from(self);
let rhsJ = $crate::curve_jac::CurveJ::<$field_type, $typ>::from(other);
let lhsJ = $crate::CurveJ::<$field_type, $typ>::from(self);
let rhsJ = $crate::CurveJ::<$field_type, $typ>::from(other);
let (result_jac, j_transcript) = unsafe { lhsJ.sub(rhsJ) };

// Convert back to affine coordinates using the transcript
let transcript = unsafe { $crate::curve_jac::AffineTranscript::from_j(j_transcript) };
let transcript = unsafe { $crate::AffineTranscript::from_j(j_transcript) };
if std::runtime::is_unconstrained() {
$typ::from_coordinates(transcript.x3, transcript.y3, result_jac.is_infinity)
} else {
Expand Down Expand Up @@ -528,7 +530,7 @@ fn incomplete_subtract_with_hint<B: BigNum, P: BigCurve<B>>(
P::from_coordinates(x3, y3, false)
}

pub(crate) fn add_with_hint<B: BigNum, P: BigCurve<B>>(
pub fn add_with_hint<B: BigNum, P: BigCurve<B>>(
point: P,
other: P,
transcript: AffineTranscript<B>,
Expand Down Expand Up @@ -658,7 +660,7 @@ pub(crate) fn add_with_hint<B: BigNum, P: BigCurve<B>>(
result
}

pub(crate) fn sub_with_hint<B: BigNum, P: BigCurve<B>>(
pub fn sub_with_hint<B: BigNum, P: BigCurve<B>>(
point: P,
other: P,
transcript: AffineTranscript<B>,
Expand Down Expand Up @@ -807,7 +809,7 @@ pub(crate) fn sub_with_hint<B: BigNum, P: BigCurve<B>>(
/// # Note
///
/// This function assumes the transcript is generated using unconstrained functions.
pub(crate) fn mul_with_hint<let NScalarSlices: u32, let NTranscriptSlices: u32, B: BigNum, P: BigCurve<B>>(
pub fn mul_with_hint<let NScalarSlices: u32, let NTranscriptSlices: u32, B: BigNum, P: BigCurve<B>>(
point: P,
scalar: ScalarField<NScalarSlices>,
transcript: [AffineTranscript<B>; NTranscriptSlices],
Expand Down Expand Up @@ -888,7 +890,7 @@ fn msm_with_hint<let Size: u32, let NScalarSlices: u32, let NTranscriptSlices: u
accumulator
}

unconstrained fn get_mul_transcript<let NScalarSlices: u32, B: BigNum, P: BigCurve<B>>(
pub unconstrained fn get_mul_transcript<let NScalarSlices: u32, B: BigNum, P: BigCurve<B>>(
point: P,
scalar: ScalarField<NScalarSlices>,
) -> [AffineTranscript<B>; 6 + NScalarSlices * 5] {
Expand All @@ -897,7 +899,7 @@ unconstrained fn get_mul_transcript<let NScalarSlices: u32, B: BigNum, P: BigCur
transcript
}

fn evaluate_linear_expression<F: BigNum, Curve: BigCurve<F>, let NScalarSlices: u32, let NMuls: u32, let NAdds: u32>(
pub fn evaluate_linear_expression<F: BigNum, Curve: BigCurve<F>, let NScalarSlices: u32, let NMuls: u32, let NAdds: u32>(
mul_points: [Curve; NMuls],
mul_scalars: [ScalarField<NScalarSlices>; NMuls],
add_points: [Curve; NAdds],
Expand Down
Loading