Skip to content

Commit

Permalink
feat: Sync from noir (AztecProtocol/aztec-packages#5794)
Browse files Browse the repository at this point in the history
Automated pull of development from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
chore: fix alerts on rust msrv
(#4817)
chore(ci): fix alerts on msrv issues
(#4816)
chore: run clippy (#4810)
chore: optimize poseidon2 implementation
(#4807)
fix: catch panics from EC point creation (e.g. the point is at infinity)
(#4790)
feat: Sync from aztec-packages
(#4792)
feat: lalrpop lexer prototype
(#4656)
feat(nargo): Handle call stacks for multiple Acir calls
(#4711)
fix: proper field inversion for bigints
(#4802)
feat: add `NARGO_FOREIGN_CALL_TIMEOUT` environment variable
(#4780)
chore(debugger): Docs (#4145)
feat: narrow ABI encoding errors down to target problem argument/field
(#4798)
chore: Rename 'global' to 'function' in the monomorphization pass
(#4774)
chore: Add Hir -> Ast conversion
(#4788)
fix: Fix panic when returning a zeroed unit value
(#4797)
END_COMMIT_OVERRIDE

---------

Co-authored-by: vezenovm <mvezenov@gmail.com>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 17, 2024
2 parents b25ca49 + 3d39823 commit 536a067
Show file tree
Hide file tree
Showing 13 changed files with 610 additions and 990 deletions.
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
274f7d935230ce21d062644f6ec5f7cd0f58ae62
84c930a912ca9ed0d9c0ce2436309a4e9a840bcb
6 changes: 5 additions & 1 deletion .github/workflows/test-rust-workspace-msrv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ jobs:
# We treat any cancelled, skipped or failing jobs as a failure for the workflow as a whole.
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}

- name: Checkout
if: ${{ failure() }}
uses: actions/checkout@v4

# Raise an issue if the tests failed
- name: Alert on failed publish
uses: JasonEtco/create-an-issue@v2
Expand All @@ -122,4 +126,4 @@ jobs:
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
update_existing: true
filename: .github/JS_PUBLISH_FAILED.md
filename: .github/ACVM_NOT_PUBLISHABLE.md
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ examples/**/target/
examples/9
node_modules
pkg/
.idea

# Yarn
.pnp.*
Expand Down
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion acvm-repo/bn254_blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ repository.workspace = true
acir.workspace = true
acvm_blackbox_solver.workspace = true
thiserror.workspace = true
num-traits.workspace = true
cfg-if = "1.0.0"
hex.workspace = true
lazy_static = "1.4"

# BN254 fixed base scalar multiplication solver
grumpkin = { version = "0.1.0", package = "noir_grumpkin", features = ["std"] }
Expand All @@ -38,6 +39,18 @@ js-sys.workspace = true
getrandom.workspace = true
wasmer = "4.2.6"

[dev-dependencies]
criterion = "0.5.0"
pprof = { version = "0.12", features = [
"flamegraph",
"frame-pointer",
"criterion",
] }

[[bench]]
name = "criterion"
harness = false

[features]
default = ["bn254"]
bn254 = ["acir/bn254"]
21 changes: 21 additions & 0 deletions acvm-repo/bn254_blackbox_solver/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use criterion::{criterion_group, criterion_main, Criterion};
use std::{hint::black_box, time::Duration};

use acir::FieldElement;
use bn254_blackbox_solver::poseidon2_permutation;

use pprof::criterion::{Output, PProfProfiler};

fn bench_poseidon2(c: &mut Criterion) {
let inputs = [FieldElement::zero(); 4];

c.bench_function("poseidon2", |b| b.iter(|| poseidon2_permutation(black_box(&inputs), 4)));
}

criterion_group!(
name = benches;
config = Criterion::default().sample_size(40).measurement_time(Duration::from_secs(20)).with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench_poseidon2
);

criterion_main!(benches);
46 changes: 38 additions & 8 deletions acvm-repo/bn254_blackbox_solver/src/fixed_base_scalar_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,29 @@ pub fn fixed_base_scalar_mul(
}
}

fn create_point(x: FieldElement, y: FieldElement) -> Result<grumpkin::SWAffine, String> {
let point = grumpkin::SWAffine::new_unchecked(x.into_repr(), y.into_repr());
if !point.is_on_curve() {
return Err(format!("Point ({}, {}) is not on curve", x.to_hex(), y.to_hex()));
};
if !point.is_in_correct_subgroup_assuming_on_curve() {
return Err(format!("Point ({}, {}) is not in correct subgroup", x.to_hex(), y.to_hex()));
};
Ok(point)
}

pub fn embedded_curve_add(
input1_x: FieldElement,
input1_y: FieldElement,
input2_x: FieldElement,
input2_y: FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
let mut point1 = grumpkin::SWAffine::new(input1_x.into_repr(), input1_y.into_repr());
let point2 = grumpkin::SWAffine::new(input2_x.into_repr(), input2_y.into_repr());
let res = point1 + point2;
point1 = res.into();
if let Some((res_x, res_y)) = point1.xy() {
let point1 = create_point(input1_x, input1_y)
.map_err(|e| BlackBoxResolutionError::Failed(BlackBoxFunc::EmbeddedCurveAdd, e))?;
let point2 = create_point(input2_x, input2_y)
.map_err(|e| BlackBoxResolutionError::Failed(BlackBoxFunc::EmbeddedCurveAdd, e))?;
let res = grumpkin::SWAffine::from(point1 + point2);
if let Some((res_x, res_y)) = res.xy() {
Ok((FieldElement::from_repr(*res_x), FieldElement::from_repr(*res_y)))
} else {
Err(BlackBoxResolutionError::Failed(
Expand All @@ -72,6 +84,7 @@ mod grumpkin_fixed_base_scalar_mul {
use ark_ff::BigInteger;

use super::*;

#[test]
fn smoke_test() -> Result<(), BlackBoxResolutionError> {
let input = FieldElement::one();
Expand All @@ -84,6 +97,7 @@ mod grumpkin_fixed_base_scalar_mul {
assert_eq!(y, res.1.to_hex());
Ok(())
}

#[test]
fn low_high_smoke_test() -> Result<(), BlackBoxResolutionError> {
let low = FieldElement::one();
Expand All @@ -103,9 +117,9 @@ mod grumpkin_fixed_base_scalar_mul {
let max_limb = FieldElement::from(u128::MAX);
let invalid_limb = max_limb + FieldElement::one();

let expected_error = Err(BlackBoxResolutionError::Failed(
let expected_error = Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::FixedBaseScalarMul,
"Limb 0000000000000000000000000000000100000000000000000000000000000000 is not less than 2^128".into()
"Limb 0000000000000000000000000000000100000000000000000000000000000000 is not less than 2^128".into(),
));

let res = fixed_base_scalar_mul(&invalid_limb, &FieldElement::zero());
Expand All @@ -128,7 +142,23 @@ mod grumpkin_fixed_base_scalar_mul {
res,
Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::FixedBaseScalarMul,
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 is not a valid grumpkin scalar".into()
"30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 is not a valid grumpkin scalar".into(),
))
);
}

#[test]
fn rejects_addition_of_points_not_in_curve() {
let x = FieldElement::from(1u128);
let y = FieldElement::from(2u128);

let res = embedded_curve_add(x, y, x, y);

assert_eq!(
res,
Err(BlackBoxResolutionError::Failed(
BlackBoxFunc::EmbeddedCurveAdd,
"Point (0000000000000000000000000000000000000000000000000000000000000001, 0000000000000000000000000000000000000000000000000000000000000002) is not on curve".into(),
))
);
}
Expand Down
5 changes: 2 additions & 3 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod poseidon2;
mod wasm;

pub use fixed_base_scalar_mul::{embedded_curve_add, fixed_base_scalar_mul};
use poseidon2::Poseidon2;
pub use poseidon2::poseidon2_permutation;
use wasm::Barretenberg;

use self::wasm::{Pedersen, SchnorrSig};
Expand Down Expand Up @@ -112,7 +112,6 @@ impl BlackBoxFunctionSolver for Bn254BlackBoxSolver {
inputs: &[FieldElement],
len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError> {
let poseidon = Poseidon2::new();
poseidon.permutation(inputs, len)
poseidon2_permutation(inputs, len)
}
}
Loading

0 comments on commit 536a067

Please sign in to comment.