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

feat(acvm_js): expose black box solver functions #481

Closed
wants to merge 2 commits into from
Closed
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
82 changes: 82 additions & 0 deletions acvm_js/src/black_box_solvers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use js_sys::JsString;
use wasm_bindgen::prelude::*;

use crate::js_witness_map::{field_element_to_js_string, js_value_to_field_element};
use acvm::FieldElement;

#[wasm_bindgen]
pub fn and(lhs: JsString, rhs: JsString) -> JsString {
let lhs = js_value_to_field_element(lhs.into()).unwrap();
let rhs = js_value_to_field_element(rhs.into()).unwrap();
let result = lhs.and(&rhs, FieldElement::max_num_bits());
field_element_to_js_string(&result)
}

#[wasm_bindgen]
pub fn xor(lhs: JsString, rhs: JsString) -> JsString {
let lhs = js_value_to_field_element(lhs.into()).unwrap();
let rhs = js_value_to_field_element(rhs.into()).unwrap();
let result = lhs.xor(&rhs, FieldElement::max_num_bits());
field_element_to_js_string(&result)
}
#[wasm_bindgen]
pub fn sha256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::sha256(inputs).unwrap().into()
}

#[wasm_bindgen]
pub fn blake2s256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::blake2s(inputs).unwrap().into()
}

#[wasm_bindgen]
pub fn keccak256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::keccak256(inputs).unwrap().into()
}

#[wasm_bindgen]
pub fn hash_to_field_128_security(inputs: &[u8]) -> JsString {
field_element_to_js_string(&acvm::blackbox_solver::hash_to_field_128_security(inputs).unwrap())
}

#[wasm_bindgen]
pub fn ecdsa_secp256k1_verify(
hashed_msg: &[u8],
public_key_x_bytes: &[u8],
public_key_y_bytes: &[u8],
signature: &[u8],
) -> bool {
let public_key_x_bytes: &[u8; 32] = public_key_x_bytes.try_into().unwrap();
let public_key_y_bytes: &[u8; 32] = public_key_y_bytes.try_into().unwrap();
let signature: &[u8; 64] = signature.try_into().unwrap();

acvm::blackbox_solver::ecdsa_secp256k1_verify(
hashed_msg,
public_key_x_bytes,
public_key_y_bytes,
signature,
)
.unwrap()
.into()
}

#[wasm_bindgen]
pub fn ecdsa_secp256r1_verify(
hashed_msg: &[u8],
public_key_x_bytes: &[u8],
public_key_y_bytes: &[u8],
signature: &[u8],
) -> bool {
let public_key_x_bytes: &[u8; 32] = public_key_x_bytes.try_into().unwrap();
let public_key_y_bytes: &[u8; 32] = public_key_y_bytes.try_into().unwrap();
let signature: &[u8; 64] = signature.try_into().unwrap();

acvm::blackbox_solver::ecdsa_secp256r1_verify(
hashed_msg,
public_key_x_bytes,
public_key_y_bytes,
signature,
)
.unwrap()
.into()
}
2 changes: 2 additions & 0 deletions acvm_js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
mod black_box_solvers;
mod build_info;
mod compression;
mod execute;
Expand All @@ -17,6 +18,7 @@ cfg_if::cfg_if! {
mod js_execution_error;

pub use build_info::build_info;
pub use black_box_solvers::{and, xor, blake2s256, sha256, keccak256, hash_to_field_128_security, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify};
pub use compression::{compress_witness, decompress_witness};
pub use execute::{execute_circuit, execute_circuit_with_black_box_solver, create_black_box_solver};
pub use js_witness_map::JsWitnessMap;
Expand Down