This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update
aztec_backend_wasm
to use new serialization (#94)
* 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
1 parent
2617835
commit 28014d8
Showing
9 changed files
with
104 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters