-
Notifications
You must be signed in to change notification settings - Fork 214
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(acvm_js): export black box solver functions #2812
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
035e106
feat(acvm_js): export black box solver functions
TomAFrench 35699e3
chore: fix linter errors
TomAFrench 4a5f2db
Merge branch 'master' into tf/export-ts-solvers
TomAFrench 3beecb8
Merge branch 'master' into tf/export-ts-solvers
TomAFrench 1100301
chore: fix imports in tests
TomAFrench 5aec722
chore: linter fix
TomAFrench fcea3c4
Merge branch 'master' into tf/export-ts-solvers
TomAFrench 22ced72
chore: fix tests
TomAFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,100 @@ | ||
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; | ||
|
||
/// Performs a bitwise AND operation between `lhs` and `rhs` | ||
#[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) | ||
} | ||
|
||
/// Performs a bitwise XOR operation between `lhs` and `rhs` | ||
#[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) | ||
} | ||
|
||
/// Calculates the SHA256 hash of the input bytes | ||
#[wasm_bindgen] | ||
pub fn sha256(inputs: &[u8]) -> Vec<u8> { | ||
acvm::blackbox_solver::sha256(inputs).unwrap().into() | ||
} | ||
|
||
/// Calculates the Blake2s256 hash of the input bytes | ||
#[wasm_bindgen] | ||
pub fn blake2s256(inputs: &[u8]) -> Vec<u8> { | ||
acvm::blackbox_solver::blake2s(inputs).unwrap().into() | ||
} | ||
|
||
/// Calculates the Keccak256 hash of the input bytes | ||
#[wasm_bindgen] | ||
pub fn keccak256(inputs: &[u8]) -> Vec<u8> { | ||
acvm::blackbox_solver::keccak256(inputs).unwrap().into() | ||
} | ||
|
||
/// Calculates the Blake2s256 hash of the input bytes and represents these as a single field element. | ||
// #[wasm_bindgen] | ||
// pub fn hash_to_field_128_security(inputs: Vec<JsString>) -> JsString { | ||
// let input_bytes: Vec<u8> = inputs | ||
// .into_iter() | ||
// .flat_map(|field_string| { | ||
// let field_element = js_value_to_field_element(field_string.into()).unwrap(); | ||
// witness_assignment.fetch_nearest_bytes(FieldElement::max_num_bits()); | ||
// }) | ||
// .collect(); | ||
// field_element_to_js_string( | ||
// &acvm::blackbox_solver::hash_to_field_128_security(&input_bytes).unwrap(), | ||
// ) | ||
// } | ||
|
||
/// Verifies a ECDSA signature over the secp256k1 curve. | ||
#[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() | ||
} | ||
|
||
/// Verifies a ECDSA signature over the secp256r1 curve. | ||
#[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() | ||
} |
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
131 changes: 131 additions & 0 deletions
131
acvm-repo/acvm_js/test/browser/black_box_solvers.test.ts
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,131 @@ | ||
import { expect } from "@esm-bundle/chai"; | ||
import initACVM, { | ||
and, | ||
blake2s256, | ||
ecdsa_secp256k1_verify, | ||
ecdsa_secp256r1_verify, | ||
initLogLevel, | ||
keccak256, | ||
sha256, | ||
xor, | ||
} from "@noir-lang/acvm_js"; | ||
|
||
beforeEach(async () => { | ||
await initACVM(); | ||
|
||
initLogLevel("INFO"); | ||
}); | ||
|
||
it("successfully calculates the bitwise AND of two fields", async () => { | ||
const { and_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of and_test_cases) { | ||
const [[lhs, rhs], expectedResult] = testCase; | ||
expect(and(lhs, rhs)).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully calculates the bitwise XOR of two fields", async () => { | ||
const { xor_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of xor_test_cases) { | ||
const [[lhs, rhs], expectedResult] = testCase; | ||
expect(xor(lhs, rhs)).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully calculates the sha256 hash", async () => { | ||
const { sha256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of sha256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = sha256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
it("successfully calculates the blake2s256 hash", async () => { | ||
const { blake2s256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of blake2s256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = blake2s256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
it("successfully calculates the keccak256 hash", async () => { | ||
const { keccak256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of keccak256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = keccak256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
// it("successfully calculates the hash_to_field_128_security field", async () => { | ||
// const { hash_to_field_128_security_test_cases } = await import( | ||
// "../shared/black_box_solvers" | ||
// ); | ||
|
||
// for (const testCase of hash_to_field_128_security_test_cases) { | ||
// const [preimage, expectedResult] = testCase; | ||
// const hashField = hash_to_field_128_security(preimage); | ||
// expect(hashField).to.be.eq(expectedResult); | ||
// } | ||
// }); | ||
|
||
it("successfully verifies secp256k1 ECDSA signatures", async () => { | ||
const { ecdsa_secp256k1_test_cases } = await import( | ||
"../shared/black_box_solvers" | ||
); | ||
|
||
for (const testCase of ecdsa_secp256k1_test_cases) { | ||
const [[hashed_msg, pubkey_x, pubkey_y, signature], expectedResult] = | ||
testCase; | ||
|
||
expect(hashed_msg.length).to.be.eq(32); | ||
expect(pubkey_x.length).to.be.eq(32); | ||
expect(pubkey_y.length).to.be.eq(32); | ||
expect(signature.length).to.be.eq(64); | ||
|
||
const result = ecdsa_secp256k1_verify( | ||
hashed_msg, | ||
pubkey_x, | ||
pubkey_y, | ||
signature, | ||
); | ||
expect(result).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully verifies secp256r1 ECDSA signatures", async () => { | ||
const { ecdsa_secp256r1_test_cases } = await import( | ||
"../shared/black_box_solvers" | ||
); | ||
|
||
for (const testCase of ecdsa_secp256r1_test_cases) { | ||
const [[hashed_msg, pubkey_x, pubkey_y, signature], expectedResult] = | ||
testCase; | ||
|
||
expect(hashed_msg.length).to.be.eq(32); | ||
expect(pubkey_x.length).to.be.eq(32); | ||
expect(pubkey_y.length).to.be.eq(32); | ||
expect(signature.length).to.be.eq(64); | ||
|
||
const result = ecdsa_secp256r1_verify( | ||
hashed_msg, | ||
pubkey_x, | ||
pubkey_y, | ||
signature, | ||
); | ||
expect(result).to.be.eq(expectedResult); | ||
} | ||
}); |
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,124 @@ | ||
import { expect } from "chai"; | ||
import { | ||
and, | ||
blake2s256, | ||
ecdsa_secp256k1_verify, | ||
ecdsa_secp256r1_verify, | ||
keccak256, | ||
sha256, | ||
xor, | ||
} from "@noir-lang/acvm_js"; | ||
|
||
it("successfully calculates the bitwise AND of two fields", async () => { | ||
const { and_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of and_test_cases) { | ||
const [[lhs, rhs], expectedResult] = testCase; | ||
expect(and(lhs, rhs)).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully calculates the bitwise XOR of two fields", async () => { | ||
const { xor_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of xor_test_cases) { | ||
const [[lhs, rhs], expectedResult] = testCase; | ||
expect(xor(lhs, rhs)).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully calculates the sha256 hash", async () => { | ||
const { sha256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of sha256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = sha256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
it("successfully calculates the blake2s256 hash", async () => { | ||
const { blake2s256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of blake2s256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = blake2s256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
it("successfully calculates the keccak256 hash", async () => { | ||
const { keccak256_test_cases } = await import("../shared/black_box_solvers"); | ||
|
||
for (const testCase of keccak256_test_cases) { | ||
const [preimage, expectedResult] = testCase; | ||
const hash = keccak256(preimage); | ||
hash.forEach((value, index) => | ||
expect(value).to.be.eq(expectedResult.at(index)), | ||
); | ||
} | ||
}); | ||
|
||
// it("successfully calculates the hash_to_field_128_security field", async () => { | ||
// const { hash_to_field_128_security_test_cases } = await import( | ||
// "../shared/black_box_solvers" | ||
// ); | ||
|
||
// for (const testCase of hash_to_field_128_security_test_cases) { | ||
// const [preimage, expectedResult] = testCase; | ||
// const hashField = hash_to_field_128_security(preimage); | ||
// expect(hashField).to.be.eq(expectedResult); | ||
// } | ||
// }); | ||
|
||
it("successfully verifies secp256k1 ECDSA signatures", async () => { | ||
const { ecdsa_secp256k1_test_cases } = await import( | ||
"../shared/black_box_solvers" | ||
); | ||
|
||
for (const testCase of ecdsa_secp256k1_test_cases) { | ||
const [[hashed_msg, pubkey_x, pubkey_y, signature], expectedResult] = | ||
testCase; | ||
|
||
expect(hashed_msg.length).to.be.eq(32); | ||
expect(pubkey_x.length).to.be.eq(32); | ||
expect(pubkey_y.length).to.be.eq(32); | ||
expect(signature.length).to.be.eq(64); | ||
|
||
const result = ecdsa_secp256k1_verify( | ||
hashed_msg, | ||
pubkey_x, | ||
pubkey_y, | ||
signature, | ||
); | ||
expect(result).to.be.eq(expectedResult); | ||
} | ||
}); | ||
|
||
it("successfully verifies secp256r1 ECDSA signatures", async () => { | ||
const { ecdsa_secp256r1_test_cases } = await import( | ||
"../shared/black_box_solvers" | ||
); | ||
|
||
for (const testCase of ecdsa_secp256r1_test_cases) { | ||
const [[hashed_msg, pubkey_x, pubkey_y, signature], expectedResult] = | ||
testCase; | ||
|
||
expect(hashed_msg.length).to.be.eq(32); | ||
expect(pubkey_x.length).to.be.eq(32); | ||
expect(pubkey_y.length).to.be.eq(32); | ||
expect(signature.length).to.be.eq(64); | ||
|
||
const result = ecdsa_secp256r1_verify( | ||
hashed_msg, | ||
pubkey_x, | ||
pubkey_y, | ||
signature, | ||
); | ||
expect(result).to.be.eq(expectedResult); | ||
} | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should open up an issue for this or note it on the original issue