Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
feat: Add functions to convert between compressed witnesses and `JSWi…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Jun 12, 2023
1 parent d473fdd commit 9300265
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
26 changes: 26 additions & 0 deletions acvm_js/src/compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use acvm::acir::native_types::WitnessMap;
use js_sys::JsString;
use wasm_bindgen::prelude::wasm_bindgen;

use crate::JsWitnessMap;

#[wasm_bindgen(js_name = compressWitness)]
pub fn compress_witness(witness_map: JsWitnessMap) -> Result<Vec<u8>, JsString> {
console_error_panic_hook::set_once();

let witness_map = WitnessMap::from(witness_map);
let compressed_witness_map: Vec<u8> =
Vec::<u8>::try_from(witness_map).map_err(|err| err.to_string())?;

Ok(compressed_witness_map)
}

#[wasm_bindgen(js_name = decompressWitness)]
pub fn decompress_witness(compressed_witness: Vec<u8>) -> Result<JsWitnessMap, JsString> {
console_error_panic_hook::set_once();

let witness_map =
WitnessMap::try_from(compressed_witness.as_slice()).map_err(|err| err.to_string())?;

Ok(witness_map.into())
}
2 changes: 2 additions & 0 deletions acvm_js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use wasm_bindgen::prelude::*;

mod abi;
mod barretenberg;
mod compression;
mod execute;
mod js_transforms;
mod public_witness;

pub use abi::{abi_decode, abi_encode};
pub use compression::{compress_witness, decompress_witness};
pub use execute::execute_circuit;
pub use public_witness::{get_public_parameters_witness, get_public_witness, get_return_witness};

Expand Down
25 changes: 25 additions & 0 deletions acvm_js/test/browser/witness_conversion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from "@esm-bundle/chai";
import initACVMSimulator, {
compressWitness,
decompressWitness,
} from "../../result/";
import {
expectedCompressedWitnessMap,
expectedWitnessMap,
} from "../shared/witness_compression";

it("successfully compresses the witness", async () => {
await initACVMSimulator();

const compressedWitnessMap = compressWitness(expectedWitnessMap);

expect(compressedWitnessMap).to.be.deep.eq(expectedCompressedWitnessMap);
});

it("successfully decompresses the witness", async () => {
await initACVMSimulator();

const witnessMap = decompressWitness(expectedCompressedWitnessMap);

expect(witnessMap).to.be.deep.eq(expectedWitnessMap);
});
18 changes: 18 additions & 0 deletions acvm_js/test/node/witness_conversion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect } from "chai";
import { compressWitness, decompressWitness } from "../../result/";
import {
expectedCompressedWitnessMap,
expectedWitnessMap,
} from "../shared/witness_compression";

it("successfully compresses the witness", () => {
const compressedWitnessMap = compressWitness(expectedWitnessMap);

expect(compressedWitnessMap).to.be.deep.eq(expectedCompressedWitnessMap);
});

it("successfully decompresses the witness", () => {
const witnessMap = decompressWitness(expectedCompressedWitnessMap);

expect(witnessMap).to.be.deep.eq(expectedWitnessMap);
});
23 changes: 23 additions & 0 deletions acvm_js/test/shared/witness_compression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Solved witness for noir program (x = 1, y = 2)
//
// fn main(x : Field, y : pub Field) -> pub Field {
// assert(x != y);
// x + y
// }

export const expectedCompressedWitnessMap = Uint8Array.from([
0xa5, 0xd0, 0x39, 0x11, 0xc0, 0x30, 0x0c, 0x00, 0xc1, 0xc9, 0xc7, 0x47, 0xaf,
0x2d, 0x75, 0xa6, 0x12, 0x4d, 0x64, 0x26, 0x01, 0x69, 0x36, 0x69, 0xc2, 0x40,
0x07, 0x60, 0x8b, 0x7b, 0xb7, 0x35, 0xa0, 0x16, 0xee, 0x65, 0x82, 0x8e, 0x35,
0x18, 0x9a, 0x48, 0x76, 0x4a, 0x64, 0xbc, 0x81, 0x3c, 0x4c, 0x41, 0x34, 0x9a,
0xa1, 0xa1, 0x9a, 0x3e, 0x64, 0xcc, 0x69, 0x62, 0xdd, 0xc3, 0x3b, 0x38, 0x0a,
0x27, 0x4e, 0x75, 0x9e, 0xbf, 0x71, 0xd6, 0x89, 0xab, 0xfe, 0xe2, 0x03,
]);

export const expectedWitnessMap = new Map([
[1, "0x0000000000000000000000000000000000000000000000000000000000000001"],
[2, "0x0000000000000000000000000000000000000000000000000000000000000002"],
[3, "0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000"],
[4, "0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000"],
[5, "0x0000000000000000000000000000000000000000000000000000000000000001"],
]);

0 comments on commit 9300265

Please sign in to comment.