This repository has been archived by the owner on Apr 9, 2024. 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: Add functions to convert between compressed witnesses and `JSWi…
…tnessMap` (noir-lang/acvm-simulator#42)
- Loading branch information
1 parent
d473fdd
commit 9300265
Showing
5 changed files
with
94 additions
and
0 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,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()) | ||
} |
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,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); | ||
}); |
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 @@ | ||
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); | ||
}); |
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,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"], | ||
]); |