Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: update to noir_bignum v0.4.0 #10

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
toolchain: [nightly, 0.35.0]
toolchain: [nightly, 0.36.0]
steps:
- name: Checkout sources
uses: actions/checkout@v4
Expand All @@ -38,7 +38,7 @@ jobs:
- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: 0.35.0
toolchain: 0.36.0

- name: Run formatter
run: nargo fmt --check
8 changes: 5 additions & 3 deletions Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
name = "noir_bigcurve"
type = "lib"
authors = [""]
compiler_version = ">=0.35.0"
compiler_version = ">=0.36.0"

[dependencies]
bignum = {tag = "v0.3.7", git = "https://github.com/noir-lang/noir-bignum"}
sort = {tag = "v0.1.0", git = "https://github.com/noir-lang/noir_sort"}
# bignum = {path = "../noir-bignum"}
# bignum = {tag = "v0.4.0", git = "https://github.com/noir-lang/noir-bignum"}
bignum = {tag = "mc/bignum-compatibility-changes", git = "https://github.com/noir-lang/noir-bignum"}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TODO, once the bignum PR is merged.

sort = {tag = "v0.2.0", git = "https://github.com/noir-lang/noir_sort"}
24 changes: 12 additions & 12 deletions src/bigcurve_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::curve_jac::CurveJ;
use crate::scalar_field::ScalarField;
use crate::PointTable;
use crate::curves::bn254::{BN254, BN254Scalar, BN254Params};
type Fq = BigNum<3, BNParams>;
type Fq = BigNum<3, 254, BN254_Fq_Params>;

type BN254J = CurveJ<Fq, BN254Params>;

Expand Down Expand Up @@ -73,8 +73,8 @@ fn test_mul() {
is_infinity: false
};
expected.y = BigNum::new() - expected.y;
assert(result.x.eq(expected.x));
assert(result.y.eq(expected.y));
assert(result.x == expected.x);
assert(result.y == expected.y);
}

#[test]
Expand Down Expand Up @@ -193,7 +193,7 @@ fn test_transcript() {
let lhs = (lambda.__add(lambda)).__mul(P.y);
let rhs = (P.x.__add(P.x).__add(P.x)).__mul(P.x);

assert(lhs.eq(rhs));
assert(lhs == rhs);

let X2 = P2.1.x3;
let Y2 = P2.1.y3;
Expand All @@ -217,7 +217,7 @@ fn test_transcript() {
let lhs = lambda.__mul(x2.__sub(x1));
let rhs = y2.__sub(y1);

assert(lhs.eq(rhs));
assert(lhs == rhs);
}
}

Expand Down Expand Up @@ -247,8 +247,8 @@ fn test_double_with_hint() {
let transcript: AffineTranscript<Fq> = AffineTranscript { lambda, x3, y3 };
let P2_affine = P_affine.double_with_hint(transcript);

assert(P2_affine.x.eq(x3));
assert(P2_affine.y.eq(y3));
assert(P2_affine.x == x3);
assert(P2_affine.y == y3);
}
}

Expand Down Expand Up @@ -285,8 +285,8 @@ fn test_incomplete_add_with_hint() {
let transcript: AffineTranscript<Fq> = AffineTranscript { lambda, x3, y3 };
let P2_affine = P_affine.incomplete_add_with_hint(Q_affine, transcript);

assert(P2_affine.x.eq(x3));
assert(P2_affine.y.eq(y3));
assert(P2_affine.x == x3);
assert(P2_affine.y == y3);

let P: BN254J = CurveJ::one();

Expand All @@ -296,7 +296,7 @@ fn test_incomplete_add_with_hint() {
let rhs = unsafe {
P.dbl().0.incomplete_add(P).0.incomplete_add(P).0
};
assert(lhs.eq(rhs));
assert(lhs == rhs);
}
}

Expand Down Expand Up @@ -462,7 +462,7 @@ fn test_make_table() {
}
}

use dep::bignum::fields::bn254Fq::BNParams;
use dep::bignum::fields::bn254Fq::BN254_Fq_Params;

use crate::curves::vesta::{Vesta, VestaFr, VestaScalar};
use crate::curves::pallas::{Pallas, PallasFr, PallasScalar};
Expand Down Expand Up @@ -547,7 +547,7 @@ fn test_msm() {
}
}

#[make_test(quote{BN254}, quote{BigNum<3, BNParams>}, quote{BN254Scalar})]
#[make_test(quote{BN254}, quote{BigNum<3, 254, BN254_Fq_Params>}, quote{BN254Scalar})]
pub struct BN254GenTests{}
#[make_test(quote{Vesta}, quote{VestaFr}, quote{VestaScalar})]
pub struct VestaGenTests{}
Expand Down
63 changes: 32 additions & 31 deletions src/curve_jac.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dep::bignum::BigNum;
use dep::bignum::BigNumTrait;

use crate::scalar_field::ScalarField;
use crate::CurveParamsTrait;
use crate::BigCurve;
Expand All @@ -23,10 +24,10 @@ use crate::BigCurve;
* Yes, this is an extremely complex solution to a simple problem. Such is life. Inverses are expensive to generate witnesses for.
**/
pub struct CurveJ<BigNum, CurveParams> {
x: BigNum,
y: BigNum,
z: BigNum,
is_infinity: bool
pub(crate) x: BigNum,
pub(crate) y: BigNum,
pub(crate) z: BigNum,
pub(crate) is_infinity: bool
}

/**
Expand All @@ -35,10 +36,10 @@ pub struct CurveJ<BigNum, CurveParams> {
* lambda_numerator = numerator of the `lambda` term (the denominator is assumed to be z3)
**/
pub struct JTranscript<BigNum> {
lambda_numerator: BigNum,
x3: BigNum,
y3: BigNum,
z3: BigNum
pub(crate) lambda_numerator: BigNum,
pub(crate) x3: BigNum,
pub(crate) y3: BigNum,
pub(crate) z3: BigNum
}

impl<BigNum> JTranscript<BigNum> where BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq {
Expand All @@ -56,20 +57,20 @@ impl<BigNum> JTranscript<BigNum> where BigNum: BigNumTrait + std::ops::Add + std
* If we have an array of JTranscript objects, we can turn them into AffineTranscript objects with only 1 modular inverse
**/
pub struct AffineTranscript<BigNum> {
lambda: BigNum,
x3: BigNum,
y3: BigNum
pub(crate) lambda: BigNum,
pub(crate) x3: BigNum,
pub(crate) y3: BigNum
}

/**
* @brief construct a sequence of AffineTranscript objects from a sequence of Jacobian transcript objects
**/
impl<BigNum> AffineTranscript<BigNum> where BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq {
fn new() -> Self {
pub(crate) fn new() -> Self {
AffineTranscript { lambda: BigNum::new(), x3: BigNum::new(), y3: BigNum::new() }
}

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

Expand Down Expand Up @@ -121,11 +122,11 @@ pub struct PointTable<BigNum> {
x: [BigNum; 16],
y: [BigNum; 16],
z: [BigNum; 16],
transcript: [JTranscript<BigNum>; 8]
pub(crate) transcript: [JTranscript<BigNum>; 8]
}

impl<BigNum> PointTable<BigNum> where BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq {
fn empty() -> Self {
pub(crate) fn empty() -> Self {
PointTable {
x: [BigNum::new(); 16],
y: [BigNum::new(); 16],
Expand All @@ -143,7 +144,7 @@ impl<BigNum> PointTable<BigNum> where BigNum: BigNumTrait + std::ops::Add + std:
* [0, P, ..., 15P] requires 14 group operations.
* group operations are expensive!
**/
unconstrained fn new<CurveParams>(P: CurveJ<BigNum, CurveParams>) -> Self where CurveParams: CurveParamsTrait<BigNum> {
unconstrained pub(crate) fn new<CurveParams>(P: CurveJ<BigNum, CurveParams>) -> Self where CurveParams: CurveParamsTrait<BigNum> {
let mut result = PointTable {
x: [BigNum::new(); 16],
y: [BigNum::new(); 16],
Expand Down Expand Up @@ -179,7 +180,7 @@ impl<BigNum> PointTable<BigNum> where BigNum: BigNumTrait + std::ops::Add + std:
/**
* @brief get a value out of the lookup table
**/
unconstrained fn get<CurveParams>(self, idx: u8) -> CurveJ<BigNum, CurveParams> where CurveParams: CurveParamsTrait<BigNum> {
unconstrained pub(crate) fn get<CurveParams>(self, idx: u8) -> CurveJ<BigNum, CurveParams> where CurveParams: CurveParamsTrait<BigNum> {
CurveJ { x: self.x[idx], y: self.y[idx], z: self.z[idx], is_infinity: false }
}
}
Expand Down Expand Up @@ -233,23 +234,23 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
/**
* @brief negate a point
**/
fn neg(self) -> Self {
pub(crate) fn neg(self) -> Self {
CurveJ { x: self.x, y: self.y.neg(), z: self.z, is_infinity: self.is_infinity }
}

unconstrained fn new() -> Self {
unconstrained pub(crate) fn new() -> Self {
CurveJ { x: BigNum::new(), y: BigNum::new(), z: BigNum::new(), is_infinity: false }
}

unconstrained fn point_at_infinity() -> Self {
unconstrained pub(crate) fn point_at_infinity() -> Self {
CurveJ { x: BigNum::new(), y: BigNum::new(), z: BigNum::new(), is_infinity: true }
}

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

unconstrained fn add(self, p2: Self) -> (Self, JTranscript<BigNum>) {
unconstrained pub(crate) fn add(self, p2: Self) -> (Self, JTranscript<BigNum>) {
let X1 = self.x;
let X2 = p2.x;
let Y1 = self.y;
Expand Down Expand Up @@ -334,7 +335,7 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
* @note This method minimizes the number of calls to `compute_quadratic_expression`,
* which is NOT the same as minimizing the number of multiplications.
**/
unconstrained fn incomplete_add(self, p2: Self) -> (Self, JTranscript<BigNum>) {
unconstrained pub(crate) fn incomplete_add(self, p2: Self) -> (Self, JTranscript<BigNum>) {
let X1 = self.x;
let X2 = p2.x;
let Y1 = self.y;
Expand Down Expand Up @@ -401,7 +402,7 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
* @note This method minimizes the number of calls to `compute_quadratic_expression`,
* which is NOT the same as minimizing the number of multiplications.
**/
unconstrained fn dbl(self) -> (Self, JTranscript<BigNum>) {
unconstrained pub(crate) fn dbl(self) -> (Self, JTranscript<BigNum>) {
let X1 = self.x;
let Y1 = self.y;
let Z1 = self.z;
Expand Down Expand Up @@ -444,22 +445,22 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
)
}

fn offset_generator() -> Self {
pub(crate) fn offset_generator() -> Self {
let result = CurveParams::offset_generator();
Self { x: result[0], y: result[1], z: BigNum::one(), is_infinity: false }
}

fn offset_generator_final() -> Self {
pub(crate) fn offset_generator_final() -> Self {
let result = CurveParams::offset_generator_final();
Self { x: result[0], y: result[1], z: BigNum::one(), is_infinity: false }
}

fn one() -> Self {
pub(crate) fn one() -> Self {
let result = CurveParams::one();
Self { x: result[0], y: result[1], z: BigNum::one(), is_infinity: false }
}

fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self {
pub(crate) fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self {
let mut result = rhs;
if (predicate) {
result = lhs;
Expand All @@ -469,7 +470,7 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
/**
* @brief Perform an ecc scalar multiplication and output the generated AffineTranscript
**/
unconstrained fn mul<let NScalarSlices: u32>(self, scalar: ScalarField<NScalarSlices>) -> (Self, [AffineTranscript<BigNum>]) {
unconstrained pub(crate) fn mul<let NScalarSlices: u32>(self, scalar: ScalarField<NScalarSlices>) -> (Self, [AffineTranscript<BigNum>]) {
let mut transcript: [JTranscript<BigNum>; NScalarSlices * 5 + 6] = [JTranscript::new(); NScalarSlices * 5 + 6];

let input: Self = CurveJ::conditional_select(CurveJ::one(), self, self.is_infinity);
Expand Down Expand Up @@ -580,7 +581,7 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
/**
* @brief Perform an ecc scalar multiplication and output the generated AffineTranscript
**/
unconstrained fn msm<let Size: u32, let NScalarSlices: u32>(
unconstrained pub(crate) fn msm<let Size: u32, let NScalarSlices: u32>(
mut points: [Self; Size],
mut scalars: [ScalarField<NScalarSlices>; Size]
) -> (Self, [AffineTranscript<BigNum>; NScalarSlices * Size + NScalarSlices * 4 + Size * 9 - 3]) {
Expand All @@ -593,7 +594,7 @@ impl<BigNum, CurveParams> CurveJ<BigNum, CurveParams> where BigNum: BigNumTrait
(accumulator, affine_transcript)
}

unconstrained fn compute_linear_expression_transcript<let NScalarSlices: u32, let NMuls: u32, let NAdds: u32>(
unconstrained pub(crate) fn compute_linear_expression_transcript<let NScalarSlices: u32, let NMuls: u32, let NAdds: u32>(
mut mul_points: [BigCurve<BigNum, CurveParams>; NMuls],
mut scalars: [ScalarField<NScalarSlices>; NMuls],
mut add_points: [BigCurve<BigNum, CurveParams>; NAdds]
Expand Down
30 changes: 16 additions & 14 deletions src/curves/bls12_377.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use crate::scalar_field::ScalarField;

global BLS12_377_SCALAR_SLICES = 64;
pub struct BLS12_377_Params {}
impl CurveParamsTrait<BigNum<4, BLS12_377_Fq_Params>> for BLS12_377_Params {
fn a() -> BigNum<4, BLS12_377_Fq_Params> {
impl CurveParamsTrait<BigNum<4, 377, BLS12_377_Fq_Params>> for BLS12_377_Params {
fn a() -> BigNum<4, 377, BLS12_377_Fq_Params> {
BigNum { limbs: [0x00, 0x00, 0x00, 0x00] }
}
fn b() -> BigNum<4, BLS12_377_Fq_Params> {
fn b() -> BigNum<4, 377, BLS12_377_Fq_Params> {
BigNum { limbs: [0x01, 0x00, 0x00, 0x00] }
}
fn one() -> [BigNum<4, BLS12_377_Fq_Params>; 2] {
fn one() -> [BigNum<4, 377, BLS12_377_Fq_Params>; 2] {
[
BigNum {
limbs: [
Expand All @@ -27,7 +27,7 @@ impl CurveParamsTrait<BigNum<4, BLS12_377_Fq_Params>> for BLS12_377_Params {
}
]
}
fn offset_generator() -> [BigNum<4, BLS12_377_Fq_Params>; 2] {
fn offset_generator() -> [BigNum<4, 377, BLS12_377_Fq_Params>; 2] {
[
BigNum {
limbs: [
Expand All @@ -40,7 +40,7 @@ impl CurveParamsTrait<BigNum<4, BLS12_377_Fq_Params>> for BLS12_377_Params {
}
]
}
fn offset_generator_final() -> [BigNum<4, BLS12_377_Fq_Params>; 2] {
fn offset_generator_final() -> [BigNum<4, 377, BLS12_377_Fq_Params>; 2] {
[
BigNum {
limbs: [
Expand All @@ -55,18 +55,20 @@ impl CurveParamsTrait<BigNum<4, BLS12_377_Fq_Params>> for BLS12_377_Params {
}
}

pub type BLS12_377 = BigCurve<BigNum<4, BLS12_377_Fq_Params>, BLS12_377_Params>;
pub type BLS12_377 = BigCurve<BigNum<4, 377, BLS12_377_Fq_Params>, BLS12_377_Params>;
pub type BLS12_377Scalar = ScalarField<BLS12_377_SCALAR_SLICES>;
pub type BLS12_377Fq = BigNum<4, BLS12_377_Fq_Params>;
pub type BLS12_377Fr = BigNum<3, BLS12_377_Fr_Params>;
pub type BLS12_377Fq = BigNum<4, 377, BLS12_377_Fq_Params>;
pub type BLS12_377Fr = BigNum<3, 253, BLS12_377_Fr_Params>;

mod test {
use dep::bignum::BigNum;
use crate::curves::bls12_377::BLS12_377_SCALAR_SLICES;
use dep::bignum::fields::bls12_377Fr::BLS12_377_Fr_Params;
#[test]

use dep::bignum::BigNum;
use crate::curves::bls12_377::BLS12_377_SCALAR_SLICES;
use dep::bignum::fields::bls12_377Fr::BLS12_377_Fr_Params;

#[test]
fn test_bits() {
let x: BigNum<3, BLS12_377_Fr_Params> = BigNum::new();
let x: BigNum<3, 253, BLS12_377_Fr_Params> = BigNum::new();
let max_wnaf_bits: u32 = x.modulus_bits() + 1;

let scalar_slices = (max_wnaf_bits / 4) + (max_wnaf_bits % 4 != 0) as u32;
Expand Down
Loading