From cf97165da6f9d7d3920f5ef984dc996f7318518b Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 13 Jan 2025 16:42:40 +0000 Subject: [PATCH 1/6] feat: bump bignum to `v0.5.0` --- Nargo.toml | 2 +- src/bigcurve_test.nr | 8 ++++--- src/curve_jac.nr | 14 +++++------ src/curves/bls12_377.nr | 2 +- src/curves/bls12_381.nr | 2 +- src/curves/bn254.nr | 2 +- src/curves/mnt4_753.nr | 2 +- src/curves/mnt6_753.nr | 2 +- src/curves/pallas.nr | 2 +- src/curves/secp256k1.nr | 2 +- src/curves/secp256r1.nr | 2 +- src/curves/secp384r1.nr | 4 ++-- src/curves/vesta.nr | 2 +- src/lib.nr | 34 ++++++++++++++------------- src/utils/derive_offset_generators.nr | 6 +++-- src/utils/hash_to_curve.nr | 4 ++-- 16 files changed, 48 insertions(+), 42 deletions(-) diff --git a/Nargo.toml b/Nargo.toml index eeb3d97..c3433b5 100644 --- a/Nargo.toml +++ b/Nargo.toml @@ -5,5 +5,5 @@ authors = [""] compiler_version = ">=0.37.0" [dependencies] -bignum = {tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum"} +bignum = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir-bignum"} sort = {tag = "v0.2.1", git = "https://github.com/noir-lang/noir_sort"} diff --git a/src/bigcurve_test.nr b/src/bigcurve_test.nr index d8577e5..b538ba0 100644 --- a/src/bigcurve_test.nr +++ b/src/bigcurve_test.nr @@ -1,7 +1,9 @@ -use dep::bignum::BigNum; +use std::ops::{Add, Neg, Sub}; + +use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::bn254Fq::BN254_Fq_Params; -use crate::BigCurve; +use crate::{BigCurve, BigCurveTrait}; use crate::curve_jac; use crate::curve_jac::AffineTranscript; use crate::curve_jac::CurveJ; @@ -17,7 +19,7 @@ use crate::curves::secp256r1::{Secp256r1, Secp256r1Fr, Secp256r1Scalar}; use crate::curves::secp384r1::{Secp384r1, Secp384r1Fr, Secp384r1Scalar}; use crate::curves::vesta::{Vesta, VestaFr, VestaScalar}; use crate::PointTable; -use crate::scalar_field::ScalarField; +use crate::scalar_field::{ScalarField, ScalarFieldTrait}; use super::curves::mnt6_753::MNT6_753Fq; type Fq = BigNum<3, 254, BN254_Fq_Params>; diff --git a/src/curve_jac.nr b/src/curve_jac.nr index 6a4034d..5a13070 100644 --- a/src/curve_jac.nr +++ b/src/curve_jac.nr @@ -3,7 +3,7 @@ use dep::bignum::BigNumTrait; use crate::BigCurve; use crate::CurveParamsTrait; -use crate::scalar_field::ScalarField; +use crate::scalar_field::{ScalarField, ScalarFieldTrait}; /** * @brief CurveJ represents a Short Weierstrass elliptic curve using Jacobian coordinates. * representation in Jacobian form is X, Y, Z @@ -44,7 +44,7 @@ pub struct JTranscript { impl JTranscript where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { unconstrained fn new() -> Self { JTranscript { @@ -75,7 +75,7 @@ pub struct AffineTranscript { **/ impl AffineTranscript where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { pub(crate) fn new() -> Self { AffineTranscript { lambda: BigNum::new(), x3: BigNum::new(), y3: BigNum::new() } @@ -144,7 +144,7 @@ pub struct PointTable { impl PointTable where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { pub(crate) fn empty() -> Self { PointTable { @@ -214,7 +214,7 @@ where **/ impl std::convert::From> for CurveJ where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, CurveParams: CurveParamsTrait, { fn from(affine_point: BigCurve) -> Self { @@ -233,7 +233,7 @@ where **/ impl std::cmp::Eq for CurveJ where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, CurveParams: CurveParamsTrait, { fn eq(self, other: Self) -> bool { @@ -269,7 +269,7 @@ where impl CurveJ where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, CurveParams: CurveParamsTrait, { /** diff --git a/src/curves/bls12_377.nr b/src/curves/bls12_377.nr index 17ae51b..b94f327 100644 --- a/src/curves/bls12_377.nr +++ b/src/curves/bls12_377.nr @@ -84,7 +84,7 @@ pub type BLS12_377Fr = BigNum<3, 253, BLS12_377_Fr_Params>; mod test { use crate::curves::bls12_377::BLS12_377_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::bls12_377Fr::BLS12_377_Fr_Params; #[test] diff --git a/src/curves/bls12_381.nr b/src/curves/bls12_381.nr index 7e88b44..8ab6add 100644 --- a/src/curves/bls12_381.nr +++ b/src/curves/bls12_381.nr @@ -84,7 +84,7 @@ pub type BLS12_381Fr = BigNum<3, 255, BLS12_381_Fr_Params>; mod test { use crate::curves::bls12_381::BLS12_381_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::bls12_381Fr::BLS12_381_Fr_Params; #[test] diff --git a/src/curves/bn254.nr b/src/curves/bn254.nr index e0dc178..1447226 100644 --- a/src/curves/bn254.nr +++ b/src/curves/bn254.nr @@ -44,7 +44,7 @@ pub type BN254Fq = BigNum<3, 254, BN254_Fq_Params>; // pub type Secp256r1Fr = BigNum<3, Secp256r1_Fr_Params>; // mod test { -// use dep::bignum::BigNum; +// use dep::bignum::{BigNum, BigNumTrait}; // use crate::curves::secp256r1::SECP256r1_SCALAR_SLICES; // #[test] // fn test_bn254_bits() { diff --git a/src/curves/mnt4_753.nr b/src/curves/mnt4_753.nr index c7bfd51..011a2ef 100644 --- a/src/curves/mnt4_753.nr +++ b/src/curves/mnt4_753.nr @@ -112,7 +112,7 @@ pub type MNT4_753Fr = BigNum<7, 753, MNT4_753_Fr_Params>; mod test { use crate::curves::mnt4_753::MNT4_753_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::mnt4_753Fr::MNT4_753_Fr_Params; #[test] diff --git a/src/curves/mnt6_753.nr b/src/curves/mnt6_753.nr index bdfc356..da224e8 100644 --- a/src/curves/mnt6_753.nr +++ b/src/curves/mnt6_753.nr @@ -112,7 +112,7 @@ pub type MNT6_753Fr = BigNum<7, 753, MNT6_753_Fr_Params>; mod test { use crate::curves::mnt6_753::MNT6_753_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::mnt6_753Fr::MNT6_753_Fr_Params; #[test] diff --git a/src/curves/pallas.nr b/src/curves/pallas.nr index 255888e..3fe63d8 100644 --- a/src/curves/pallas.nr +++ b/src/curves/pallas.nr @@ -51,7 +51,7 @@ pub type PallasFr = BigNum<3, 255, Pallas_Fr_Params>; mod test { use crate::curves::pallas::PALLAS_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::pallasFr::Pallas_Fr_Params; #[test] diff --git a/src/curves/secp256k1.nr b/src/curves/secp256k1.nr index 4e4f18b..83dd931 100644 --- a/src/curves/secp256k1.nr +++ b/src/curves/secp256k1.nr @@ -53,7 +53,7 @@ pub type Secp256k1Fr = BigNum<3, 256, Secp256k1_Fr_Params>; mod test { use crate::curves::secp256k1::SECP256k1_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::secp256k1Fr::Secp256k1_Fr_Params; #[test] diff --git a/src/curves/secp256r1.nr b/src/curves/secp256r1.nr index c4a9ba6..048d2a9 100644 --- a/src/curves/secp256r1.nr +++ b/src/curves/secp256r1.nr @@ -56,7 +56,7 @@ pub type Secp256r1Fr = BigNum<3, 256, Secp256r1_Fr_Params>; mod test { use crate::curves::secp256r1::SECP256r1_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::secp256r1Fr::Secp256r1_Fr_Params; #[test] diff --git a/src/curves/secp384r1.nr b/src/curves/secp384r1.nr index d02e8f8..5bdd509 100644 --- a/src/curves/secp384r1.nr +++ b/src/curves/secp384r1.nr @@ -98,8 +98,8 @@ pub type Secp384r1Fr = BigNum<4, 384, Secp384r1_Fr_Params>; mod test { use crate::curves::secp384r1::Secp384r1Scalar; - use crate::scalar_field::ScalarField; - use dep::bignum::BigNum; + use crate::scalar_field::{ScalarField, ScalarFieldTrait}; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::secp384r1Fr::Secp384r1_Fr_Params; #[test] diff --git a/src/curves/vesta.nr b/src/curves/vesta.nr index e6ee71a..541f125 100644 --- a/src/curves/vesta.nr +++ b/src/curves/vesta.nr @@ -50,7 +50,7 @@ pub type VestaFr = BigNum<3, 255, Vesta_Fr_Params>; mod test { use crate::curves::vesta::VESTA_SCALAR_SLICES; - use dep::bignum::BigNum; + use dep::bignum::{BigNum, BigNumTrait}; use dep::bignum::fields::vestaFr::Vesta_Fr_Params; #[test] diff --git a/src/lib.nr b/src/lib.nr index 81463f0..b168f5e 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -15,6 +15,8 @@ use crate::utils::hash_to_curve::hash_to_curve; use dep::sort::sort_advanced; +use scalar_field::ScalarFieldTrait; + fn __sort_field_as_u32(lhs: Field, rhs: Field) -> bool { lhs as u32 < rhs as u32 } @@ -174,9 +176,9 @@ where { fn offset_generator() -> [BigNum; 2]; fn offset_generator_final() -> [BigNum; 2]; - pub fn one() -> [BigNum; 2]; - pub fn b() -> BigNum; - pub fn a() -> BigNum; + fn one() -> [BigNum; 2]; + fn b() -> BigNum; + fn a() -> BigNum; } /** @@ -196,7 +198,7 @@ struct PointTable { impl PointTable where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { pub(crate) fn empty() -> Self { PointTable { x: [BigNum::new(); 16], y: [BigNum::new(); 16] } @@ -244,21 +246,21 @@ where } trait BigCurveTrait { - pub fn neg(self) -> Self; - pub fn point_at_infinity() -> Self; + fn neg(self) -> Self; + fn point_at_infinity() -> Self; fn offset_generator() -> Self; fn offset_generator_final() -> Self; - pub fn one() -> Self; - pub fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self; - pub fn validate_on_curve(self); - pub fn mul(self, scalar: ScalarField) -> Self; - pub fn hash_to_curve(seed: [u8; N]) -> Self; + fn one() -> Self; + fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self; + fn validate_on_curve(self); + fn mul(self, scalar: ScalarField) -> Self; + fn hash_to_curve(seed: [u8; N]) -> Self; } impl BigCurveTrait for BigCurve where CurveParams: CurveParamsTrait, - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { fn hash_to_curve(seed: [u8; N]) -> Self { @@ -335,7 +337,7 @@ where impl BigCurve where CurveParams: CurveParamsTrait, - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { /** @@ -1004,7 +1006,7 @@ where impl std::ops::Add for BigCurve where CurveParams: CurveParamsTrait, - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { // Expensive witness generation! Avoid if possible fn add(self, other: Self) -> Self { @@ -1020,7 +1022,7 @@ where impl std::ops::Sub for BigCurve where CurveParams: CurveParamsTrait, - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { // Expensive witness generation! Avoid if possible fn sub(self, other: Self) -> Self { @@ -1038,7 +1040,7 @@ where **/ impl std::cmp::Eq for BigCurve where - BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq, + BigNum: BigNumTrait, { fn eq(self, other: Self) -> bool { let coords_equal = diff --git a/src/utils/derive_offset_generators.nr b/src/utils/derive_offset_generators.nr index fc8e8b7..f9c2423 100644 --- a/src/utils/derive_offset_generators.nr +++ b/src/utils/derive_offset_generators.nr @@ -1,4 +1,6 @@ -use crate::BigCurve; +use std::ops::Neg; + +use crate::{BigCurve, BigCurveTrait}; use crate::curve_jac::CurveJ; use crate::CurveParamsTrait; @@ -456,7 +458,7 @@ unconstrained fn compute_and_print_offset_generators, { let a = Curve::a(); diff --git a/src/utils/hash_to_curve.nr b/src/utils/hash_to_curve.nr index 528d5a1..cbdd834 100644 --- a/src/utils/hash_to_curve.nr +++ b/src/utils/hash_to_curve.nr @@ -7,7 +7,7 @@ unconstrained fn hash_to_curve_inner( b: Fq, ) -> (Fq, Fq, Field) where - Fq: BigNumTrait + std::ops::Mul + std::ops::Add + std::cmp::Eq, + Fq: BigNumTrait, { let seedhash = std::hash::poseidon2::Poseidon2::hash([seedbase, seed_counter], 2); // TODO: assert in field? @@ -63,7 +63,7 @@ fn poseidon_hash_bytes(seed: [u8; SeedBytes]) -> Field { } pub fn hash_to_curve(seed: [u8; SeedBytes], a: Fq, b: Fq) -> (Fq, Fq) where - Fq: BigNumTrait + std::ops::Mul + std::ops::Add + std::cmp::Eq, + Fq: BigNumTrait, { let (_, y, salt) = unsafe { __hash_to_curve_witgen(seed, a, b) }; let outer_hash: Field = poseidon_hash_bytes(seed); From ef20b6ee1b4ff72e151761bea37ff36416ba0d85 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 13 Jan 2025 16:47:30 +0000 Subject: [PATCH 2/6] . --- src/utils/hash_to_curve.nr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/hash_to_curve.nr b/src/utils/hash_to_curve.nr index cbdd834..069e11e 100644 --- a/src/utils/hash_to_curve.nr +++ b/src/utils/hash_to_curve.nr @@ -39,7 +39,7 @@ unconstrained fn __hash_to_curve_witgen( b: Fq, ) -> (Fq, Fq, Field) where - Fq: BigNumTrait + std::ops::Mul + std::ops::Add + std::cmp::Eq, + Fq: BigNumTrait, { let hashed_seed = poseidon_hash_bytes(seed); hash_to_curve_inner(hashed_seed, 0, a, b) From 9734686980853cba0804a6e611bff0e2590693f6 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 16 Jan 2025 10:49:24 +0000 Subject: [PATCH 3/6] . --- .github/workflows/test.yml | 33 ++++++++++++++++++++++++++------- Nargo.toml | 2 +- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec05635..ef99948 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,21 +2,41 @@ name: Noir tests on: push: - branches: - - main + branches: + - main pull_request: env: CARGO_TERM_COLOR: always + MINIMUM_NOIR_VERSION: v1.0.0-beta.0 jobs: + noir-version-list: + name: Query supported Noir versions + runs-on: ubuntu-latest + outputs: + noir_versions: ${{ steps.get_versions.outputs.versions }} + steps: + - name: Checkout sources + id: get_versions + run: | + # gh returns the Noir releases in reverse chronological order so we keep all releases published after the minimum supported version. + VERSIONS=$(gh release list -R noir-lang/noir --exclude-pre-releases --json tagName -q 'map(.tagName) | index(env.MINIMUM_NOIR_VERSION) as $index | if $index then .[0:$index+1] else [env.MINIMUM_NOIR_VERSION] end') + echo "versions=$VERSIONS" + echo "versions=$VERSIONS" >> $GITHUB_OUTPUT + env: + GH_TOKEN: ${{ github.token }} + test: + needs: [noir-version-list] name: Test on Nargo ${{matrix.toolchain}} runs-on: ubuntu-latest strategy: fail-fast: false matrix: - toolchain: [nightly, 0.37.0] + toolchain: ${{ fromJson( needs.noir-version-list.outputs.noir_versions )}} + include: + - toolchain: nightly steps: - name: Checkout sources uses: actions/checkout@v4 @@ -28,8 +48,6 @@ jobs: - name: Run Noir tests run: nargo test - env: - RAYON_NUM_THREADS: 1 format: runs-on: ubuntu-latest @@ -40,12 +58,13 @@ jobs: - name: Install Nargo uses: noir-lang/noirup@v0.1.3 with: - toolchain: 0.37.0 + toolchain: ${{ env.MINIMUM_NOIR_VERSION }} - name: Run formatter run: nargo fmt --check - # This is a job which depends on all test jobs and reports the overall status. + +# This is a job which depends on all test jobs and reports the overall status. # This allows us to add/remove test jobs without having to update the required workflows. tests-end: name: Noir End diff --git a/Nargo.toml b/Nargo.toml index c3433b5..b9ac09c 100644 --- a/Nargo.toml +++ b/Nargo.toml @@ -2,7 +2,7 @@ name = "noir_bigcurve" type = "lib" authors = [""] -compiler_version = ">=0.37.0" +compiler_version = ">=1.0.0" [dependencies] bignum = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir-bignum"} From 0bfcfd9a4205d42bcf143821063366e906047b99 Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 16 Jan 2025 10:50:54 +0000 Subject: [PATCH 4/6] . --- Nargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Nargo.toml b/Nargo.toml index b9ac09c..323e8be 100644 --- a/Nargo.toml +++ b/Nargo.toml @@ -6,4 +6,4 @@ compiler_version = ">=1.0.0" [dependencies] bignum = {tag = "v0.5.0", git = "https://github.com/noir-lang/noir-bignum"} -sort = {tag = "v0.2.1", git = "https://github.com/noir-lang/noir_sort"} +sort = {tag = "v0.2.2", git = "https://github.com/noir-lang/noir_sort"} From 334d67fa09846c02d364d5fdb333f88108ab513b Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 16 Jan 2025 10:56:01 +0000 Subject: [PATCH 5/6] . --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ef99948..cdfe406 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -48,6 +48,8 @@ jobs: - name: Run Noir tests run: nargo test + env: + RAYON_NUM_THREADS: 1 format: runs-on: ubuntu-latest From 924968d4d67d12c1b1395b667a00f48db53cb3db Mon Sep 17 00:00:00 2001 From: Tom French Date: Thu, 16 Jan 2025 10:56:32 +0000 Subject: [PATCH 6/6] . --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cdfe406..b9c4b3e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -66,7 +66,7 @@ jobs: run: nargo fmt --check -# This is a job which depends on all test jobs and reports the overall status. + # This is a job which depends on all test jobs and reports the overall status. # This allows us to add/remove test jobs without having to update the required workflows. tests-end: name: Noir End