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

chore: update to target nargo 0.34.0 #3

Merged
merged 1 commit into from
Sep 16, 2024
Merged
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
8 changes: 8 additions & 0 deletions .github/NIGHTLY_CANARY_DIED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Tests fail on latest Nargo nightly release"
assignees: TomAFrench
---

The tests on this Noir project have started failing when using the latest nightly release of the Noir compiler. This likely means that there have been breaking changes for which this project needs to be updated to take into account.

Check the [{{env.WORKFLOW_NAME}}]({{env.WORKFLOW_URL}}) workflow for details.
43 changes: 43 additions & 0 deletions .github/workflows/nightly-canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Noir Nightly Canary

on:
schedule:
# Run a check at 9 AM UTC
- cron: "0 9 * * *"

env:
CARGO_TERM_COLOR: always

permissions:
issues: write

jobs:
test:
name: Test on Nargo ${{matrix.toolchain}}
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: nightly

- name: Run Noir tests
run: nargo test

- name: Run formatter
run: nargo fmt --check

- name: Alert on dead links
uses: JasonEtco/create-an-issue@v2
if: ${{ failure() }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WORKFLOW_NAME: ${{ github.workflow }}
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
update_existing: true
filename: .github/NIGHTLY_CANARY_DIED.md

33 changes: 18 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,42 @@ name: Noir tests
on:
push:
branches:
- master
- main
pull_request:

env:
CARGO_TERM_COLOR: always
NARGO_VERSION: 0.32.0

jobs:
test:
name: Test on Nargo ${{matrix.toolchain}}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
toolchain: [nightly, 0.34.0]
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: $NARGO_VERSION
toolchain: ${{ matrix.toolchain }}

- name: Run Noir tests
run: nargo test

# Disabled as the formatter panics
# format:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout sources
# uses: actions/checkout@v4
format:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

# - name: Install Nargo
# uses: noir-lang/[email protected]
# with:
# toolchain: $NARGO_VERSION
- name: Install Nargo
uses: noir-lang/[email protected]
with:
toolchain: 0.34.0

# - name: Run formatter
# run: nargo fmt --check
- name: Run formatter
run: nargo fmt --check
4 changes: 2 additions & 2 deletions Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
name = "edwards"
type = "lib"
authors = [""]
compiler_version = ">=0.32.0"
compiler_version = ">=0.34.0"

[dependencies]
[dependencies]
16 changes: 12 additions & 4 deletions src/bjj.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ use crate::Curve;

struct BabyJubJubParams {}
impl TECurveParameterTrait for BabyJubJubParams {
fn a() -> Field { 168700 }
fn d() -> Field { 168696 }
fn gen() -> (Field, Field) { (0x0bb77a6ad63e739b4eacb2e09d6277c12ab8d8010534e0b62893f3f6bb957051, 0x25797203f7a0b24925572e1cd16bf9edfce0051fb9e133774b3c257a872d7d8b)}
fn a() -> Field {
168700
}
fn d() -> Field {
168696
}
fn gen() -> (Field, Field) {
(
0x0bb77a6ad63e739b4eacb2e09d6277c12ab8d8010534e0b62893f3f6bb957051, 0x25797203f7a0b24925572e1cd16bf9edfce0051fb9e133774b3c257a872d7d8b
)
}
}
type BabyJubJub = Curve<BabyJubJubParams>;
type BabyJubJub = Curve<BabyJubJubParams>;
58 changes: 29 additions & 29 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod scalar_field;
mod test;
mod bjj;

use dep::std;
use crate::scalar_field::ScalarField;

struct Curve<Params> {
Expand Down Expand Up @@ -70,7 +69,7 @@ impl<Params> std::ops::Add for Curve<Params> where Params: TECurveParameterTrait
*
* Cost: 7 gates
**/
fn add(self, other:Self) -> Self {
fn add(self, other: Self) -> Self {
Curve::add_internal(self, other, Params::a(), Params::d())
}
}
Expand All @@ -82,7 +81,7 @@ impl<Params> std::ops::Neg for Curve<Params> where Params: TECurveParameterTrait
* Cost: usually 0, will cost 1 gate if the `x` coordinate needs to be converted into a witness
**/
fn neg(self) -> Self {
Curve{ x: -self.x, y: self.y }
Curve { x: -self.x, y: self.y }
}
}

Expand All @@ -92,7 +91,7 @@ impl<Params> std::ops::Sub for Curve<Params> where Params: TECurveParameterTrait
*
* Cost: 7 gates
**/
fn sub(self, other:Self) -> Self {
fn sub(self, other: Self) -> Self {
Curve::add_internal(self, other.neg(), Params::a(), Params::d())
}
}
Expand All @@ -103,7 +102,7 @@ impl<Params> std::cmp::Eq for Curve<Params> where Params: TECurveParameterTrait
*
* Cost: 6 gates
**/
fn eq(self, other:Self) -> bool {
fn eq(self, other: Self) -> bool {
(self.x == other.x) & (self.y == other.y)
}
}
Expand All @@ -116,7 +115,7 @@ impl<Params> std::convert::From<(Field, Field)> for Curve<Params> where Params:
* Cost: 0 gates
**/
fn from((x, y): (Field, Field)) -> Self {
Curve{ x, y }
Curve { x, y }
}
}

Expand All @@ -132,7 +131,7 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
* Cost: 3 gates
**/
fn new(x: Field, y: Field) -> Self {
let result = Curve{ x, y };
let result = Curve { x, y };
result.assert_is_on_curve();
result
}
Expand All @@ -142,16 +141,18 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
*
* Cost: 0 gates
**/
fn zero() -> Self { Curve{ x: 0, y: 1 } }
fn zero() -> Self {
Curve { x: 0, y: 1 }
}

/**
* @brief return the Generator of the group
*
* Cost: 0 gates (assuming Params trait returns values known at compile time!)
**/
fn one() -> Self {
fn one() -> Self {
let (x, y) = Params::gen();
Curve{ x, y }
Curve { x, y }
}

/**
Expand Down Expand Up @@ -195,7 +196,7 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
let t2 = Params::a() * t0 + t1;
let t3 = 1 + Params::d() * t0 * t1;
(t2 == t3)
}
}

/**
* @brief compute `self + self`
Expand Down Expand Up @@ -277,8 +278,7 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
*
* TODO: use windowed non-adjacent form to remove 7 point additions per point when creating lookup table
**/
fn msm<let N: u32, let NScalarSlices: u32> (points: [Self; N], scalars: [ScalarField<NScalarSlices>; N]) -> Self
{
fn msm<let N: u32, let NScalarSlices: u32>(points: [Self; N], scalars: [ScalarField<NScalarSlices>; N]) -> Self {
let a = Params::a();
let d = Params::d();

Expand All @@ -289,10 +289,10 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
}

let idx = scalars[0].base4_slices[0];
let mut accumulator: Self = Curve{ x: point_tables[0].0[idx], y: point_tables[0].1[idx] };
let mut accumulator: Self = Curve { x: point_tables[0].0[idx], y: point_tables[0].1[idx] };
for j in 1..N {
let idx = scalars[j].base4_slices[0];
let P = Curve{ x: point_tables[j].0[idx], y: point_tables[j].1[idx] };
let P = Curve { x: point_tables[j].0[idx], y: point_tables[j].1[idx] };
accumulator = accumulator.add_internal(P, a, d);
}
for i in 1..NScalarSlices {
Expand All @@ -309,8 +309,7 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
}

for j in 0..N {
if (scalars[j].skew == true)
{
if (scalars[j].skew == true) {
accumulator = accumulator - points[j];
}
}
Expand All @@ -324,15 +323,6 @@ impl<Params> CurveTrait<Params> for Curve<Params> where Params: TECurveParameter
// ####################################################################################################################
// ####################################################################################################################
impl<Params> Curve<Params> {
/**
* @brief add points together, return output + lambda term
**/
unconstrained pub fn __add_unconstrained(x1: Field, x2: Field, y1: Field, y2: Field, a: Field, d: Field) -> (Field, Field, Field) {
let lambda = y1 * y2 * x1 * x2;
let y = (x1 * x2 * a - y1 * y2) / (lambda * d - 1);
let x = (x1 * y2 + y1 * x2) / (lambda * d + 1);
(x, y, lambda)
}

/**
* @brief add two points together
Expand All @@ -345,7 +335,7 @@ impl<Params> Curve<Params> {
let x2 = other.x;
let y1 = self.y;
let y2 = other.y;
let (x, y, lambda) = Curve::__add_unconstrained(x1, x2, y1, y2, a, d);
let (x, y, lambda) = __add_unconstrained(x1, x2, y1, y2, a, d);
let x1x2 = x1 * x2;
let x1y2 = x1 * y2;
std::as_witness(x1x2);
Expand All @@ -372,7 +362,7 @@ impl<Params> Curve<Params> {
fn dbl_internal(self, a: Field, d: Field) -> Self {
let x1 = self.x;
let y1 = self.y;
let (x3, y3, _) = Curve::__add_unconstrained(x1, x1, y1, y1, a, d);
let (x3, y3, _) = __add_unconstrained(x1, x1, y1, y1, a, d);
let x1x1a = x1 * x1 * a;
std::as_witness(x1x1a);
// t1 = a*x_1^2 + y_1^2
Expand Down Expand Up @@ -429,7 +419,7 @@ impl<Params> Curve<Params> {
table_y[8] = self.y;
let D = self.dbl_internal(a, d);
for i in 1..8 {
let Q = Curve { x: table_x[7 + i], y: table_y[7 + i] };
let Q = Self { x: table_x[7 + i], y: table_y[7 + i] };
let V = D.add_internal(Q, a, d);
table_x[8 + i] = V.x;
table_y[8 + i] = V.y;
Expand All @@ -441,3 +431,13 @@ impl<Params> Curve<Params> {
(table_x, table_y)
}
}

/**
* @brief add points together, return output + lambda term
**/
unconstrained fn __add_unconstrained(x1: Field, x2: Field, y1: Field, y2: Field, a: Field, d: Field) -> (Field, Field, Field) {
let lambda = y1 * y2 * x1 * x2;
let y = (x1 * x2 * a - y1 * y2) / (lambda * d - 1);
let x = (x1 * y2 + y1 * x2) / (lambda * d + 1);
(x, y, lambda)
}
17 changes: 5 additions & 12 deletions src/scalar_field.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ struct ScalarField<let N: u32> {
}

unconstrained fn get_wnaf_slices<let N: u32>(x: Field) -> ([u8; N], bool) {

let mut result: [u8; N] = [0; N];
let mut nibbles = x.to_le_radix(16, N);
let mut nibbles: [u8; N] = x.to_le_radix(16);

let skew: bool = nibbles[0] & 1 == 0;
nibbles[0] += skew as u8;
Expand Down Expand Up @@ -48,7 +47,6 @@ unconstrained fn from_wnaf_slices(x: [u8; 64], skew: bool) -> Field {

#[test]
fn test_wnaf() {

let result: Field = 0x123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0;
let (t0, t1) = get_wnaf_slices(result);
let expected = from_wnaf_slices(t0, t1);
Expand Down Expand Up @@ -78,8 +76,6 @@ unconstrained fn get_borrow_flag(lhs_lo: Field, rhs_lo: Field) -> bool {
}
impl<let N: u32> std::convert::From<Field> for ScalarField<N> {



/**
* @brief construct from a field element
* @details if N >= 64 we perform extra checks to ensure the slice decomposition represents the same integral value as the input
Expand All @@ -97,22 +93,19 @@ impl<let N: u32> std::convert::From<Field> for ScalarField<N> {
acc += (slices[i] as Field) * 2 - 15;
}
assert(acc - skew as Field == x);
}
else
{
} else {
// TODO: if num bits = 64, validate in sum of the bits is smaller than the Field modulus
let mut lo: Field = slices[(N/2)] as Field;
let mut lo: Field = slices[(N / 2)] as Field;
let mut hi: Field = slices[0] as Field;
let mut borrow_shift = 1;
for i in 1..(N/2) {
for i in 1..(N / 2) {
borrow_shift *= 16;
lo *= 16;
lo += (slices[(N/2) + i] as Field) * 2 - 15;
hi *= 16;
hi += (slices[i] as Field) * 2 - 15;
}
if ((N & 1) == 1)
{
if ((N & 1) == 1) {
borrow_shift *= 16;
lo *= 16;
lo += (slices[N-1] as Field) * 2 - 15;
Expand Down
3 changes: 0 additions & 3 deletions src/test.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dep::std;
use dep::std::ec;
use crate::scalar_field::ScalarField;
use crate::Curve;
use crate::TECurveParameterTrait;
use crate::bjj::BabyJubJubParams;
use std::ec::consts::te::baby_jubjub;
use std::ec::tecurve::affine::Point as TEPoint;
Expand Down
Loading