Skip to content

Commit

Permalink
Merge branch 'master' into tf/acir-var-euclid
Browse files Browse the repository at this point in the history
* master:
  feat: replace boolean range constraints with arithmetic opcodes (#3234)
  feat!: Switch to new pedersen implementation (#3151)
  feat: noir-wasm takes dependency graph (#3213)
  • Loading branch information
TomAFrench committed Oct 24, 2023
2 parents af4fc55 + 949222c commit cc0e70d
Show file tree
Hide file tree
Showing 52 changed files with 691 additions and 282 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
1 change: 1 addition & 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
1 change: 1 addition & 0 deletions compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ chumsky.workspace = true
thiserror.workspace = true
smol_str.workspace = true
serde_json.workspace = true
serde.workspace = true
rustc-hash = "1.1.0"
small-ord-set = "0.1.3"
regex = "1.9.1"
Expand Down
8 changes: 7 additions & 1 deletion compiler/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{fmt::Display, str::FromStr};

use fm::FileId;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand All @@ -32,7 +33,7 @@ impl CrateId {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub struct CrateName(SmolStr);

impl CrateName {
Expand Down Expand Up @@ -90,6 +91,11 @@ mod crate_name {
assert!(!CrateName::is_valid_name(&bad_char_string));
}
}

#[test]
fn it_rejects_bad_crate_names_when_deserializing() {
assert!(serde_json::from_str::<CrateName>("bad-name").is_err());
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
Expand Down
8 changes: 8 additions & 0 deletions compiler/wasm/fixtures/deps/lib-a/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name="lib_a"
type="lib"
authors = [""]
compiler_version = "0.1"

[dependencies]
lib_b = { path = "../lib-b" }
7 changes: 7 additions & 0 deletions compiler/wasm/fixtures/deps/lib-a/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

use dep::lib_b::assert_non_zero;

pub fn divide(a: u64, b: u64) -> u64 {
assert_non_zero(b);
a / b
}
7 changes: 7 additions & 0 deletions compiler/wasm/fixtures/deps/lib-b/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name="lib_b"
type="lib"
authors = [""]
compiler_version = "0.1"

[dependencies]
4 changes: 4 additions & 0 deletions compiler/wasm/fixtures/deps/lib-b/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub fn assert_non_zero(x: u64) {
assert(x != 0);
}
8 changes: 8 additions & 0 deletions compiler/wasm/fixtures/deps/noir-script/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name="noir_wasm_testing"
type="bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
lib_a = { path="../lib-a" }
4 changes: 4 additions & 0 deletions compiler/wasm/fixtures/deps/noir-script/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use dep::lib_a::divide;
fn main(x : u64, y : pub u64) {
divide(x, y);
}
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit cc0e70d

Please sign in to comment.