Skip to content

Commit

Permalink
Merge branch 'master' into kw/change-to-lowercase-field
Browse files Browse the repository at this point in the history
* master:
  feat: add poseidon2 opcode implementation for acvm/brillig, and Noir (#4398)
  fix: remove panic when generic array length is not resolvable (#4408)
  chore(ci): enforce formatting of noir code in CI (#4422)
  • Loading branch information
TomAFrench committed Feb 26, 2024
2 parents 48d498b + 10e8292 commit 850565b
Show file tree
Hide file tree
Showing 68 changed files with 1,986 additions and 516 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,68 @@ jobs:

- name: Run `yarn lint`
run: yarn lint

build-nargo:
runs-on: ubuntu-22.04
timeout-minutes: 30

steps:
- name: Checkout Noir repo
uses: actions/checkout@v4

- name: Setup toolchain
uses: dtolnay/rust-toolchain@1.73.0

- uses: Swatinem/rust-cache@v2
with:
key: x86_64-unknown-linux-gnu
cache-on-failure: true
save-if: ${{ github.event_name != 'merge_group' }}

- name: Build Nargo
run: cargo build --package nargo_cli --release

- name: Package artifacts
run: |
mkdir dist
cp ./target/release/nargo ./dist/nargo
7z a -ttar -so -an ./dist/* | 7z a -si ./nargo-x86_64-unknown-linux-gnu.tar.gz
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: nargo
path: ./dist/*
retention-days: 3

nargo_fmt:
needs: [build-nargo]
name: Nargo fmt
runs-on: ubuntu-latest
timeout-minutes: 30

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Download nargo binary
uses: actions/download-artifact@v4
with:
name: nargo
path: ./nargo

- name: Set nargo on PATH
run: |
nargo_binary="${{ github.workspace }}/nargo/nargo"
chmod +x $nargo_binary
echo "$(dirname $nargo_binary)" >> $GITHUB_PATH
export PATH="$PATH:$(dirname $nargo_binary)"
nargo -V
- name: Format stdlib
working-directory: ./noir_stdlib
run: nargo fmt --check

- name: Format test suite
working-directory: ./test_programs
run: ./format.sh check
2 changes: 2 additions & 0 deletions Cargo.lock

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

36 changes: 35 additions & 1 deletion acvm-repo/acvm/src/pwg/blackbox/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acir::{
native_types::{Witness, WitnessMap},
BlackBoxFunc, FieldElement,
};
use acvm_blackbox_solver::{sha256compression, BlackBoxResolutionError};
use acvm_blackbox_solver::{sha256compression, BlackBoxFunctionSolver, BlackBoxResolutionError};

use crate::pwg::{insert_value, witness_to_value};
use crate::OpcodeResolutionError;
Expand Down Expand Up @@ -131,3 +131,37 @@ pub(crate) fn solve_sha_256_permutation_opcode(

Ok(())
}

pub(crate) fn solve_poseidon2_permutation_opcode(
backend: &impl BlackBoxFunctionSolver,
initial_witness: &mut WitnessMap,
inputs: &[FunctionInput],
outputs: &[Witness],
len: u32,
) -> Result<(), OpcodeResolutionError> {
if len as usize != inputs.len() {
return Err(OpcodeResolutionError::BlackBoxFunctionFailed(
acir::BlackBoxFunc::Poseidon2Permutation,
format!(
"the number of inputs does not match specified length. {} > {}",
inputs.len(),
len
),
));
}

// Read witness assignments
let mut state = Vec::new();
for input in inputs.iter() {
let witness_assignment = witness_to_value(initial_witness, input.witness)?;
state.push(*witness_assignment);
}

let state = backend.poseidon2_permutation(&state, len)?;

// Write witness assignments
for (output_witness, value) in outputs.iter().zip(state.into_iter()) {
insert_value(output_witness, value, initial_witness)?;
}
Ok(())
}
8 changes: 6 additions & 2 deletions acvm-repo/acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use acir::{
};
use acvm_blackbox_solver::{blake2s, blake3, keccak256, keccakf1600, sha256};

use self::{bigint::BigIntSolver, pedersen::pedersen_hash};
use self::{
bigint::BigIntSolver, hash::solve_poseidon2_permutation_opcode, pedersen::pedersen_hash,
};

use super::{insert_value, OpcodeNotSolvable, OpcodeResolutionError};
use crate::{pwg::witness_to_value, BlackBoxFunctionSolver};
Expand Down Expand Up @@ -204,7 +206,6 @@ pub(crate) fn solve(
BlackBoxFuncCall::BigIntToLeBytes { input, outputs } => {
bigint_solver.bigint_to_bytes(*input, outputs, initial_witness)
}
BlackBoxFuncCall::Poseidon2Permutation { .. } => todo!(),
BlackBoxFuncCall::Sha256Compression { inputs, hash_values, outputs } => {
solve_sha_256_permutation_opcode(
initial_witness,
Expand All @@ -214,5 +215,8 @@ pub(crate) fn solve(
bb_func.get_black_box_func(),
)
}
BlackBoxFuncCall::Poseidon2Permutation { inputs, outputs, len } => {
solve_poseidon2_permutation_opcode(backend, initial_witness, inputs, outputs, *len)
}
}
}
12 changes: 12 additions & 0 deletions acvm-repo/blackbox_solver/src/curve_specific_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub trait BlackBoxFunctionSolver {
input2_x: &FieldElement,
input2_y: &FieldElement,
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError>;
fn poseidon2_permutation(
&self,
_inputs: &[FieldElement],
_len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError>;
}

pub struct StubbedBlackBoxSolver;
Expand Down Expand Up @@ -89,4 +94,11 @@ impl BlackBoxFunctionSolver for StubbedBlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
Err(Self::fail(BlackBoxFunc::EmbeddedCurveAdd))
}
fn poseidon2_permutation(
&self,
_inputs: &[FieldElement],
_len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError> {
Err(Self::fail(BlackBoxFunc::Poseidon2Permutation))
}
}
1 change: 1 addition & 0 deletions acvm-repo/bn254_blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ repository.workspace = true
acir.workspace = true
acvm_blackbox_solver.workspace = true
thiserror.workspace = true
num-traits.workspace = true

rust-embed = { version = "6.6.0", features = [
"debug-embed",
Expand Down
11 changes: 11 additions & 0 deletions acvm-repo/bn254_blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use acir::{BlackBoxFunc, FieldElement};
use acvm_blackbox_solver::{BlackBoxFunctionSolver, BlackBoxResolutionError};

mod fixed_base_scalar_mul;
mod poseidon2;
mod wasm;

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

use self::wasm::{Pedersen, SchnorrSig};
Expand Down Expand Up @@ -97,4 +99,13 @@ impl BlackBoxFunctionSolver for Bn254BlackBoxSolver {
) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> {
embedded_curve_add(*input1_x, *input1_y, *input2_x, *input2_y)
}

fn poseidon2_permutation(
&self,
inputs: &[FieldElement],
len: u32,
) -> Result<Vec<FieldElement>, BlackBoxResolutionError> {
let poseidon = Poseidon2::new();
poseidon.permutation(inputs, len)
}
}
Loading

0 comments on commit 850565b

Please sign in to comment.