Skip to content

Commit

Permalink
Merge branch 'master' into tf/optimize-constant-div
Browse files Browse the repository at this point in the history
* master:
  feat: implement euclidean division and signed division in terms of `AcirVar`s (#3230)
  feat: replace boolean range constraints with arithmetic opcodes (#3234)
  feat!: Switch to new pedersen implementation (#3151)
  feat: noir-wasm takes dependency graph (#3213)
  chore(docs): Rearrange NoirJS section (#3260)
  chore(docs): Document `nargo fmt` (#3262)
  feat(debugger): Print limited source code context (#3217)
  fix: recompile artefacts from a different noir version (#3248)
  chore: Update ACIR artifacts (#3263)
  fix: Show println output before an error occurs in `nargo execute` (#3211)
  chore(docs): document info codelens (#3252)
  fix: Impl methods are no longer placed in contracts (#3255)
  feat(stdlib): optimize constraint counts in sha256/sha512 (#3253)
  chore: enhance code formatting for If expressions (#3246)
  feat: Cache debug artifacts  (#3133)
  • Loading branch information
TomAFrench committed Oct 24, 2023
2 parents 5a61783 + b8b7782 commit 3a8fe3f
Show file tree
Hide file tree
Showing 126 changed files with 1,438 additions and 595 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test-noir_wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ jobs:
name: nargo
path: ./nargo

- name: Compile test program with Nargo CLI
working-directory: ./compiler/wasm/noir-script
- name: Compile fixtures with Nargo CLI
working-directory: ./compiler/wasm/fixtures
run: |
nargo_binary=${{ github.workspace }}/nargo/nargo
chmod +x $nargo_binary
$nargo_binary compile
for dir in $(ls -d */); do
pushd $dir/noir-script
$nargo_binary compile
popd
done
- name: Install Yarn dependencies
uses: ./.github/actions/setup
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ dirs = "4"
lsp-types = "0.94"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0"
smol_str = "0.1.17"
smol_str = { version = "0.1.17", features = ["serde"] }
thiserror = "1.0.21"
toml = "0.7.2"
tower = "0.4"
Expand Down
24 changes: 21 additions & 3 deletions acvm-repo/acvm/src/compiler/optimizers/redundant_range.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use acir::{
circuit::{opcodes::BlackBoxFuncCall, Circuit, Opcode},
native_types::Witness,
circuit::{
opcodes::{BlackBoxFuncCall, FunctionInput},
Circuit, Opcode,
},
native_types::{Expression, Witness},
FieldElement,
};
use std::collections::{BTreeMap, HashSet};

Expand Down Expand Up @@ -101,7 +105,7 @@ impl RangeOptimizer {
if is_lowest_bit_size {
already_seen_witness.insert(witness);
new_order_list.push(order_list[idx]);
optimized_opcodes.push(opcode);
optimized_opcodes.push(optimized_range_opcode(witness, num_bits));
}
}

Expand All @@ -126,6 +130,20 @@ fn extract_range_opcode(opcode: &Opcode) -> Option<(Witness, u32)> {
}
}

fn optimized_range_opcode(witness: Witness, num_bits: u32) -> Opcode {
if num_bits == 1 {
Opcode::Arithmetic(Expression {
mul_terms: vec![(FieldElement::one(), witness, witness)],
linear_combinations: vec![(-FieldElement::one(), witness)],
q_c: FieldElement::zero(),
})
} else {
Opcode::BlackBoxFuncCall(BlackBoxFuncCall::RANGE {
input: FunctionInput { witness, num_bits },
})
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
Expand Down
4 changes: 2 additions & 2 deletions acvm-repo/acvm_js/test/shared/pedersen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const initialWitnessMap = new Map([[1, '0x0000000000000000000000000000000

export const expectedWitnessMap = new Map([
[1, '0x0000000000000000000000000000000000000000000000000000000000000001'],
[2, '0x09489945604c9686e698cb69d7bd6fc0cdb02e9faae3e1a433f1c342c1a5ecc4'],
[3, '0x24f50d25508b4dfb1e8a834e39565f646e217b24cb3a475c2e4991d1bb07a9d8'],
[2, '0x083e7911d835097629f0067531fc15cafd79a89beecb39903f69572c636f4a5a'],
[3, '0x1a7f5efaad7f315c25a918f30cc8d7333fccab7ad7c90f14de81bcc528f9935d'],
]);
132 changes: 66 additions & 66 deletions acvm-repo/acvm_js/test/shared/schnorr_verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,72 @@ export const bytecode = Uint8Array.from([
]);

export const initialWitnessMap = new Map([
[1, '0x17cbd3ed3151ccfd170efe1d54280a6a4822640bf5c369908ad74ea21518a9c5'],
[2, '0x0e0456e3795c1a31f20035b741cd6158929eeccd320d299cfcac962865a6bc74'],
[3, '0x0000000000000000000000000000000000000000000000000000000000000005'],
[4, '0x00000000000000000000000000000000000000000000000000000000000000ca'],
[5, '0x000000000000000000000000000000000000000000000000000000000000001f'],
[6, '0x0000000000000000000000000000000000000000000000000000000000000092'],
[7, '0x0000000000000000000000000000000000000000000000000000000000000051'],
[8, '0x00000000000000000000000000000000000000000000000000000000000000f2'],
[9, '0x00000000000000000000000000000000000000000000000000000000000000f6'],
[10, '0x0000000000000000000000000000000000000000000000000000000000000045'],
[11, '0x000000000000000000000000000000000000000000000000000000000000002b'],
[12, '0x000000000000000000000000000000000000000000000000000000000000006b'],
[13, '0x00000000000000000000000000000000000000000000000000000000000000f9'],
[14, '0x0000000000000000000000000000000000000000000000000000000000000099'],
[15, '0x00000000000000000000000000000000000000000000000000000000000000c6'],
[16, '0x000000000000000000000000000000000000000000000000000000000000002c'],
[17, '0x000000000000000000000000000000000000000000000000000000000000000e'],
[18, '0x000000000000000000000000000000000000000000000000000000000000006f'],
[19, '0x00000000000000000000000000000000000000000000000000000000000000bf'],
[20, '0x0000000000000000000000000000000000000000000000000000000000000079'],
[21, '0x0000000000000000000000000000000000000000000000000000000000000089'],
[22, '0x00000000000000000000000000000000000000000000000000000000000000a6'],
[23, '0x00000000000000000000000000000000000000000000000000000000000000a0'],
[24, '0x0000000000000000000000000000000000000000000000000000000000000067'],
[25, '0x0000000000000000000000000000000000000000000000000000000000000012'],
[26, '0x00000000000000000000000000000000000000000000000000000000000000b5'],
[27, '0x00000000000000000000000000000000000000000000000000000000000000f3'],
[28, '0x00000000000000000000000000000000000000000000000000000000000000e9'],
[29, '0x00000000000000000000000000000000000000000000000000000000000000e2'],
[30, '0x000000000000000000000000000000000000000000000000000000000000005f'],
[31, '0x0000000000000000000000000000000000000000000000000000000000000043'],
[32, '0x0000000000000000000000000000000000000000000000000000000000000010'],
[33, '0x0000000000000000000000000000000000000000000000000000000000000025'],
[34, '0x0000000000000000000000000000000000000000000000000000000000000080'],
[35, '0x0000000000000000000000000000000000000000000000000000000000000055'],
[36, '0x000000000000000000000000000000000000000000000000000000000000004c'],
[37, '0x0000000000000000000000000000000000000000000000000000000000000013'],
[38, '0x00000000000000000000000000000000000000000000000000000000000000fd'],
[39, '0x000000000000000000000000000000000000000000000000000000000000001e'],
[40, '0x000000000000000000000000000000000000000000000000000000000000004d'],
[41, '0x00000000000000000000000000000000000000000000000000000000000000c0'],
[42, '0x0000000000000000000000000000000000000000000000000000000000000035'],
[43, '0x000000000000000000000000000000000000000000000000000000000000008a'],
[44, '0x00000000000000000000000000000000000000000000000000000000000000cd'],
[45, '0x0000000000000000000000000000000000000000000000000000000000000045'],
[46, '0x0000000000000000000000000000000000000000000000000000000000000021'],
[47, '0x00000000000000000000000000000000000000000000000000000000000000ec'],
[48, '0x00000000000000000000000000000000000000000000000000000000000000a3'],
[49, '0x0000000000000000000000000000000000000000000000000000000000000053'],
[50, '0x00000000000000000000000000000000000000000000000000000000000000c2'],
[51, '0x0000000000000000000000000000000000000000000000000000000000000054'],
[52, '0x0000000000000000000000000000000000000000000000000000000000000089'],
[53, '0x00000000000000000000000000000000000000000000000000000000000000b8'],
[54, '0x00000000000000000000000000000000000000000000000000000000000000dd'],
[55, '0x00000000000000000000000000000000000000000000000000000000000000b0'],
[56, '0x0000000000000000000000000000000000000000000000000000000000000079'],
[57, '0x00000000000000000000000000000000000000000000000000000000000000b3'],
[58, '0x000000000000000000000000000000000000000000000000000000000000001b'],
[59, '0x000000000000000000000000000000000000000000000000000000000000003f'],
[60, '0x0000000000000000000000000000000000000000000000000000000000000046'],
[61, '0x0000000000000000000000000000000000000000000000000000000000000036'],
[62, '0x0000000000000000000000000000000000000000000000000000000000000010'],
[63, '0x00000000000000000000000000000000000000000000000000000000000000b0'],
[64, '0x00000000000000000000000000000000000000000000000000000000000000fa'],
[65, '0x0000000000000000000000000000000000000000000000000000000000000027'],
[66, '0x00000000000000000000000000000000000000000000000000000000000000ef'],
[1, '0x04b260954662e97f00cab9adb773a259097f7a274b83b113532bce27fa3fb96a'],
[2, '0x2fd51571db6c08666b0edfbfbc57d432068bccd0110a39b166ab243da0037197'],
[3, '0x000000000000000000000000000000000000000000000000000000000000002e'],
[4, '0x00000000000000000000000000000000000000000000000000000000000000ce'],
[5, '0x0000000000000000000000000000000000000000000000000000000000000052'],
[6, '0x00000000000000000000000000000000000000000000000000000000000000aa'],
[7, '0x0000000000000000000000000000000000000000000000000000000000000087'],
[8, '0x000000000000000000000000000000000000000000000000000000000000002a'],
[9, '0x0000000000000000000000000000000000000000000000000000000000000049'],
[10, '0x000000000000000000000000000000000000000000000000000000000000009d'],
[11, '0x0000000000000000000000000000000000000000000000000000000000000050'],
[12, '0x000000000000000000000000000000000000000000000000000000000000007c'],
[13, '0x000000000000000000000000000000000000000000000000000000000000009a'],
[14, '0x00000000000000000000000000000000000000000000000000000000000000aa'],
[15, '0x00000000000000000000000000000000000000000000000000000000000000df'],
[16, '0x0000000000000000000000000000000000000000000000000000000000000023'],
[17, '0x0000000000000000000000000000000000000000000000000000000000000034'],
[18, '0x0000000000000000000000000000000000000000000000000000000000000010'],
[19, '0x000000000000000000000000000000000000000000000000000000000000008a'],
[20, '0x0000000000000000000000000000000000000000000000000000000000000047'],
[21, '0x0000000000000000000000000000000000000000000000000000000000000063'],
[22, '0x00000000000000000000000000000000000000000000000000000000000000e8'],
[23, '0x0000000000000000000000000000000000000000000000000000000000000037'],
[24, '0x0000000000000000000000000000000000000000000000000000000000000054'],
[25, '0x0000000000000000000000000000000000000000000000000000000000000096'],
[26, '0x000000000000000000000000000000000000000000000000000000000000003e'],
[27, '0x00000000000000000000000000000000000000000000000000000000000000d5'],
[28, '0x00000000000000000000000000000000000000000000000000000000000000ae'],
[29, '0x0000000000000000000000000000000000000000000000000000000000000024'],
[30, '0x000000000000000000000000000000000000000000000000000000000000002d'],
[31, '0x0000000000000000000000000000000000000000000000000000000000000020'],
[32, '0x0000000000000000000000000000000000000000000000000000000000000080'],
[33, '0x000000000000000000000000000000000000000000000000000000000000004d'],
[34, '0x0000000000000000000000000000000000000000000000000000000000000047'],
[35, '0x00000000000000000000000000000000000000000000000000000000000000a5'],
[36, '0x00000000000000000000000000000000000000000000000000000000000000bb'],
[37, '0x00000000000000000000000000000000000000000000000000000000000000f6'],
[38, '0x00000000000000000000000000000000000000000000000000000000000000c3'],
[39, '0x000000000000000000000000000000000000000000000000000000000000000b'],
[40, '0x000000000000000000000000000000000000000000000000000000000000003b'],
[41, '0x0000000000000000000000000000000000000000000000000000000000000065'],
[42, '0x00000000000000000000000000000000000000000000000000000000000000c9'],
[43, '0x0000000000000000000000000000000000000000000000000000000000000001'],
[44, '0x0000000000000000000000000000000000000000000000000000000000000085'],
[45, '0x0000000000000000000000000000000000000000000000000000000000000006'],
[46, '0x000000000000000000000000000000000000000000000000000000000000009e'],
[47, '0x000000000000000000000000000000000000000000000000000000000000002f'],
[48, '0x0000000000000000000000000000000000000000000000000000000000000010'],
[49, '0x00000000000000000000000000000000000000000000000000000000000000e6'],
[50, '0x0000000000000000000000000000000000000000000000000000000000000030'],
[51, '0x000000000000000000000000000000000000000000000000000000000000004a'],
[52, '0x0000000000000000000000000000000000000000000000000000000000000018'],
[53, '0x000000000000000000000000000000000000000000000000000000000000007c'],
[54, '0x00000000000000000000000000000000000000000000000000000000000000d0'],
[55, '0x00000000000000000000000000000000000000000000000000000000000000ab'],
[56, '0x0000000000000000000000000000000000000000000000000000000000000031'],
[57, '0x00000000000000000000000000000000000000000000000000000000000000d5'],
[58, '0x0000000000000000000000000000000000000000000000000000000000000063'],
[59, '0x0000000000000000000000000000000000000000000000000000000000000084'],
[60, '0x00000000000000000000000000000000000000000000000000000000000000a3'],
[61, '0x00000000000000000000000000000000000000000000000000000000000000a6'],
[62, '0x00000000000000000000000000000000000000000000000000000000000000d5'],
[63, '0x0000000000000000000000000000000000000000000000000000000000000091'],
[64, '0x000000000000000000000000000000000000000000000000000000000000000d'],
[65, '0x000000000000000000000000000000000000000000000000000000000000009c'],
[66, '0x00000000000000000000000000000000000000000000000000000000000000f9'],
[67, '0x0000000000000000000000000000000000000000000000000000000000000000'],
[68, '0x0000000000000000000000000000000000000000000000000000000000000001'],
[69, '0x0000000000000000000000000000000000000000000000000000000000000002'],
Expand Down
57 changes: 5 additions & 52 deletions acvm-repo/barretenberg_blackbox_solver/build.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,14 @@
use std::{
fs::File,
io::{Cursor, Read},
path::{Path, PathBuf},
};
use std::path::PathBuf;

const BARRETENBERG_ARCHIVE: &str = "BARRETENBERG_ARCHIVE";
const BARRETENBERG_BIN_DIR: &str = "BARRETENBERG_BIN_DIR";

const BARRETENBERG_ARCHIVE_FALLBACK: &str = "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.5.0/acvm_backend.wasm.tar.gz";
// const ARCHIVE_SHA256: &str = "1xpycikqlvsjcryi3hkbc4mwmmdz7zshw6f76vyf1qssq53asyfx";

fn unpack_wasm(archive_path: &Path, target_dir: &Path) -> Result<(), String> {
if archive_path.exists() && archive_path.is_file() {
let archive = File::open(archive_path).map_err(|_| "Could not read archive")?;
unpack_archive(archive, target_dir);

Ok(())
} else {
Err(format!("Unable to locate {BARRETENBERG_ARCHIVE} - Please set the BARRETENBERG_BIN_DIR env var to the directory where it exists, or ensure it's located at {}", archive_path.display()))
}
}

fn unpack_archive<T: Read>(archive: T, target_dir: &Path) {
use flate2::read::GzDecoder;
use tar::Archive;

let gz_decoder = GzDecoder::new(archive);
let mut archive = Archive::new(gz_decoder);

archive.unpack(target_dir).unwrap();
}

/// Try to download the specified URL into a buffer which is returned.
fn download_binary_from_url(url: &str) -> Result<Cursor<Vec<u8>>, String> {
let response = reqwest::blocking::get(url).map_err(|error| error.to_string())?;

let bytes = response.bytes().unwrap();
Ok(Cursor::new(bytes.to_vec()))
}

fn main() -> Result<(), String> {
let out_dir = std::env::var("OUT_DIR").unwrap();

match std::env::var(BARRETENBERG_ARCHIVE) {
Ok(archive_path) => {
unpack_wasm(&PathBuf::from(archive_path), &PathBuf::from(&out_dir))?;
println!("cargo:rustc-env={BARRETENBERG_BIN_DIR}={out_dir}");
Ok(())
}
Err(_) => {
let wasm_bytes = download_binary_from_url(BARRETENBERG_ARCHIVE_FALLBACK)
.expect("download should succeed");
let dest_path = PathBuf::from(out_dir.clone()).join("acvm_backend.wasm");

unpack_archive(wasm_bytes, &PathBuf::from(&out_dir));
println!("cargo:rustc-env={BARRETENBERG_BIN_DIR}={out_dir}");
println!("cargo:rustc-env={BARRETENBERG_BIN_DIR}={out_dir}");
std::fs::copy("./src/acvm_backend.wasm", dest_path).unwrap();

Ok(())
}
}
Ok(())
}
Binary file not shown.
7 changes: 4 additions & 3 deletions acvm-repo/barretenberg_blackbox_solver/src/wasm/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ impl Pedersen for Barretenberg {
#[test]
fn pedersen_hash_to_point() -> Result<(), Error> {
let barretenberg = Barretenberg::new();
let (x, y) = barretenberg.encrypt(vec![FieldElement::zero(), FieldElement::one()], 0)?;
let (x, y) = barretenberg
.encrypt(vec![FieldElement::from(1 as u128), FieldElement::from(1 as u128)], 1)?;
let expected_x = FieldElement::from_hex(
"0x0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af",
"0x12afb43195f5c621d1d2cabb5f629707095c5307fd4185a663d4e80bb083e878",
)
.unwrap();
let expected_y = FieldElement::from_hex(
"0x230294a041e26fe80b827c2ef5cb8784642bbaa83842da2714d62b1f3c4f9752",
"0x25793f5b5e62beb92fd18a66050293a9fd554a2ff13bceba0339cae1a038d7c1",
)
.unwrap();

Expand Down
24 changes: 18 additions & 6 deletions acvm-repo/brillig_vm/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ pub(crate) fn evaluate_binary_bigint_op(
a: BigUint,
b: BigUint,
bit_size: u32,
) -> BigUint {
) -> Result<BigUint, String> {
let bit_modulo = &(BigUint::one() << bit_size);
match op {
let result = match op {
// Perform addition, subtraction, and multiplication, applying a modulo operation to keep the result within the bit size.
BinaryIntOp::Add => (a + b) % bit_modulo,
BinaryIntOp::Sub => (bit_modulo + a - b) % bit_modulo,
BinaryIntOp::Mul => (a * b) % bit_modulo,
// Perform unsigned division using the modulo operation on a and b.
BinaryIntOp::UnsignedDiv => (a % bit_modulo) / (b % bit_modulo),
BinaryIntOp::UnsignedDiv => {
let b_mod = b % bit_modulo;
if b_mod.is_zero() {
return Err("Division by zero".to_owned());
}
(a % bit_modulo) / b_mod
}
// Perform signed division by first converting a and b to signed integers and then back to unsigned after the operation.
BinaryIntOp::SignedDiv => {
let signed_div = to_big_signed(a, bit_size) / to_big_signed(b, bit_size);
let b_signed = to_big_signed(b, bit_size);
if b_signed.is_zero() {
return Err("Division by zero".to_owned());
}
let signed_div = to_big_signed(a, bit_size) / b_signed;
to_big_unsigned(signed_div, bit_size)
}
// Perform a == operation, returning 0 or 1
Expand Down Expand Up @@ -77,7 +87,9 @@ pub(crate) fn evaluate_binary_bigint_op(
let b = b.to_u128().unwrap();
(a >> b) % bit_modulo
}
}
};

Ok(result)
}

fn to_big_signed(a: BigUint, bit_size: u32) -> BigInt {
Expand Down Expand Up @@ -111,7 +123,7 @@ mod tests {
// Convert to big integers
let lhs_big = BigUint::from(a);
let rhs_big = BigUint::from(b);
let result_value = evaluate_binary_bigint_op(op, lhs_big, rhs_big, bit_size);
let result_value = evaluate_binary_bigint_op(op, lhs_big, rhs_big, bit_size).unwrap();
// Convert back to u128
result_value.to_u128().unwrap()
}
Expand Down
Loading

0 comments on commit 3a8fe3f

Please sign in to comment.