Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat: update aztec_backend_wasm to use new serialization (#94)
Browse files Browse the repository at this point in the history
* feat: update `aztec_backend_wasm` to use new serialization

* chore(ci): add build script and ci for `aztec_backend_wasm`

* chore: add feature flag for std-only dependencies

* fix: correct working directory in CI
  • Loading branch information
TomAFrench authored Mar 31, 2023
1 parent 2617835 commit 28014d8
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 115 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Wasm

on: [push, pull_request]

# This will cancel previous runs when a branch or PR is updated
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
name: Build Wasm
runs-on: ubuntu-latest

steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Setup rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.66.0

- name: Install wasm-pack
run: cargo install wasm-pack

- name: Build wasm crate
working-directory: ./aztec_backend_wasm
run: ./build-wasm
21 changes: 21 additions & 0 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]

members = ["common", "barretenberg_wasm", "barretenberg_static_lib"]

exclude = ["aztec_backend_wasm"]
members = ["common", "barretenberg_wasm", "barretenberg_static_lib", "aztec_backend_wasm"]

[workspace.package]

Expand Down
1 change: 1 addition & 0 deletions aztec_backend_wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg
8 changes: 2 additions & 6 deletions aztec_backend_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ edition = "2021"
crate-type = ["cdylib", "rlib"]

[dependencies]
barretenberg_wasm = { path = "../barretenberg_wasm", default-features = false, features = [
"js",
] }
common = { path = "../common", default-features = false }

console_error_panic_hook = { version = "*" }
wasm-bindgen = { version = "*", features = ["serde-serialize"] }
js-sys = { version = "0.3.55" }
getrandom = { version = "0.2.4", features = ["js"] }
wasm-bindgen = { version = "*", features = ["serde-serialize"] }
18 changes: 18 additions & 0 deletions aztec_backend_wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Aztec Backend Wasm

## Dependencies

In order to build the wasm package, the following must be installed:

- [wasm-pack](https://github.com/rustwasm/wasm-pack)
- [jq](https://github.com/stedolan/jq)

## Build

The wasm package can be built using the command below:

```bash
./build-wasm
```

Using `wasm-pack` directly isn't recommended as it doesn't generate a complete `package.json` file, resulting in files being omitted from the published package.
25 changes: 25 additions & 0 deletions aztec_backend_wasm/build-wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# Clear out the existing build artifacts as these aren't automatically removed by wasm-pack.
if [ -d ./pkg/ ]; then
rm -rf ./pkg/
fi

# Build the new wasm package
wasm-pack build --scope noir-lang --target nodejs --out-dir pkg/nodejs

wasm-pack build --scope noir-lang --target web --out-dir pkg/web

COMMIT_SHORT=$(git rev-parse --short HEAD)
VERSION_APPENDIX=""
if [ -n "$COMMIT_SHORT" ]; then
VERSION_APPENDIX="-$COMMIT_SHORT"
else
VERSION_APPENDIX="-NOGIT"
fi

jq -s '.[0] * .[1]' pkg/nodejs/package.json pkg/web/package.json | jq '.files = ["nodejs", "web", "package.json"]' | jq ".version += \"$VERSION_APPENDIX\"" | jq '.main = "./nodejs/" + .main | .module = "./web/" + .module | .types = "./web/" + .types' | tee ./pkg/package.json

rm pkg/nodejs/package.json pkg/nodejs/README.md pkg/nodejs/.gitignore

rm pkg/web/package.json pkg/web/README.md pkg/web/.gitignore
107 changes: 4 additions & 103 deletions aztec_backend_wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,14 @@
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]

pub use barretenberg_wasm::Barretenberg;

use common::acvm::acir::circuit::Circuit;
use common::serializer::serialize_circuit;
use wasm_bindgen::prelude::*;

use common::acvm::{
acir::circuit::Circuit, acir::native_types::Witness, FieldElement, PartialWitnessGenerator,
};

use std::collections::BTreeMap;

// Flattened
pub type ComputedWitness = Vec<u8>;

#[wasm_bindgen]
pub fn compute_witnesses(
circuit: JsValue,
initial_js_witness: Vec<js_sys::JsString>,
) -> ComputedWitness {
console_error_panic_hook::set_once();

let circuit: Circuit = circuit.into_serde().unwrap();

let mut initial_witness = Vec::new();
for js_val in initial_js_witness {
initial_witness.push(String::from(js_val))
}

// Convert initial witness vector to a BTreeMap and add the zero witness as the first one
let mut witness_map: BTreeMap<Witness, FieldElement> = BTreeMap::new();
let num_wits = circuit.current_witness_index;
for (index, element) in initial_witness.into_iter().enumerate() {
witness_map.insert(
Witness((index + 1) as u32),
FieldElement::from_hex(&element).expect("expected hex strings"),
);
}
debug_assert_eq!((num_wits + 1) as usize, witness_map.len());

// Now use the partial witness generator to fill in the rest of the witnesses
// which are possible

let plonk = Plonk;
match plonk.solve(&mut witness_map, circuit.opcodes) {
Ok(_) => {}
Err(opcode) => panic!("solver came across an error with opcode {}", opcode),
};

// Serialize the witness in a way that the C++ codebase can deserialize
let assignments = proof::flatten_witness_map(&circuit, witness_values);

assignments.to_bytes()
}

#[wasm_bindgen]
pub fn serialise_acir_to_barrtenberg_circuit(acir: JsValue) -> Vec<u8> {
pub fn serialize_acir_to_barretenberg_circuit(acir_bytes: Vec<u8>) -> Vec<u8> {
console_error_panic_hook::set_once();

let circuit: Circuit = acir.into_serde().unwrap();
let circuit = Circuit::read(&*acir_bytes).unwrap();
serialize_circuit(&circuit).to_bytes()
}

#[wasm_bindgen]
pub fn packed_witness_to_witness(acir: JsValue, witness_arr: Vec<u8>) -> Vec<u8> {
console_error_panic_hook::set_once();

use common::barretenberg_structures::Assignments;
let circuit: Circuit = acir.into_serde().unwrap();
let witness_values = Witness::from_bytes(&witness_arr);
let mut sorted_witness = Assignments::new();
let num_witnesses = circuit.num_vars();
for i in 1..num_witnesses {
// Get the value if it exists. If i does not, then we fill it with the zero value
let value = match witness_values.get(&Witness(i)) {
Some(value) => *value,
None => FieldElement::zero(),
};

sorted_witness.push(value);
}
sorted_witness.to_bytes()
}

#[wasm_bindgen]
pub fn eth_contract_from_cs(vk_method: String) -> String {
crate::contract::turbo_verifier::create(&vk_method)
}

#[wasm_bindgen]
pub fn serialise_public_inputs(pub_inputs_js_string: Vec<js_sys::JsString>) -> Vec<u8> {
console_error_panic_hook::set_once();

use common::acvm::FieldElement;

let mut pub_inputs_string = Vec::new();
for val in pub_inputs_js_string {
pub_inputs_string.push(String::from(val))
}

let mut pub_inputs = Vec::new();
for string in pub_inputs_string {
let field = FieldElement::from_hex(&string).expect("unexpected hex string");
pub_inputs.push(field)
}

pub_inputs
.into_iter()
.map(|field| field.to_bytes())
.flatten()
.collect()
}
6 changes: 3 additions & 3 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ acvm = { version = "0.8.0", features = ["bn254"] }

sled = "0.34.6"
blake2 = "0.9.1"
dirs = "3.0"
dirs = { version = "3.0", optional = true }
downloader = { version = "0.2.7", optional = true, default-features = false, features = ["rustls-tls"] }
indicatif = "0.15.0"
indicatif = { version = "0.15.0", optional = true }
regex = "1.4.0"

[features]
# This feature flag is here because we cannot compile
# the downloader dependency with the wasm32 target
default = ["std"]
std = ["downloader"]
std = ["dep:downloader", "dep:dirs", "dep:indicatif"]

0 comments on commit 28014d8

Please sign in to comment.