Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added public_witness wasm utils #22

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use wasm_bindgen::prelude::*;
mod abi;
mod execute;
mod js_transforms;
mod public_witness;

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

#[derive(Serialize, Deserialize)]
pub struct BuildInfo {
Expand Down
65 changes: 65 additions & 0 deletions src/public_witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use acvm::acir::{
circuit::Circuit,
native_types::{Witness, WitnessMap},
};
use js_sys::JsString;
use wasm_bindgen::prelude::wasm_bindgen;

use crate::JsWitnessMap;

fn extract_indices(witness_map: &WitnessMap, indices: Vec<Witness>) -> Result<WitnessMap, String> {
let mut extracted_witness_map = WitnessMap::new();
for witness in indices {
let witness_value = witness_map.get(&witness).ok_or(format!(
"Failed to extract witness {} from witness map. Witness not found.",
witness.0
))?;
extracted_witness_map.insert(witness, *witness_value);
}
Ok(extracted_witness_map)
}

#[wasm_bindgen(js_name = getReturnWitness)]
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_return_witness(
circuit: Vec<u8>,
solved_witness: JsWitnessMap,
) -> Result<JsWitnessMap, JsString> {
console_error_panic_hook::set_once();
let circuit: Circuit = Circuit::read(&*circuit).expect("Failed to deserialize circuit");
let witness_map = WitnessMap::from(solved_witness);

let return_witness =
extract_indices(&witness_map, circuit.return_values.0.into_iter().collect())?;

Ok(JsWitnessMap::from(return_witness))
}

#[wasm_bindgen(js_name = getPublicParametersWitness)]
pub fn get_public_parameters_witness(
circuit: Vec<u8>,
solved_witness: JsWitnessMap,
) -> Result<JsWitnessMap, JsString> {
console_error_panic_hook::set_once();
let circuit: Circuit = Circuit::read(&*circuit).expect("Failed to deserialize circuit");
let witness_map = WitnessMap::from(solved_witness);

let public_params_witness =
extract_indices(&witness_map, circuit.public_parameters.0.into_iter().collect())?;

Ok(JsWitnessMap::from(public_params_witness))
}

#[wasm_bindgen(js_name = getPublicWitness)]
pub fn get_public_witness(
circuit: Vec<u8>,
solved_witness: JsWitnessMap,
) -> Result<JsWitnessMap, JsString> {
console_error_panic_hook::set_once();
let circuit: Circuit = Circuit::read(&*circuit).expect("Failed to deserialize circuit");
let witness_map = WitnessMap::from(solved_witness);

let public_witness =
extract_indices(&witness_map, circuit.public_inputs().0.into_iter().collect())?;

Ok(JsWitnessMap::from(public_witness))
}