Skip to content

Commit

Permalink
feat(stdlib): Add secp256r1 builtin function (#1858)
Browse files Browse the repository at this point in the history
* feat: ecdsa secp256r1

* fix: remove verbose logging

* chore(cargo): update deps

* fix: remove commented code

* check

* chore(deps): update flake and acvm

* chore(bb-backend): update acvm bb

* chore(bb-backend): bump backend to 0.7.0

* update cargo.lock

* use exact wasm-bindgen version

---------

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
Co-authored-by: TomAFrench <tom@tomfren.ch>
  • Loading branch information
3 people authored Jul 10, 2023
1 parent 1747176 commit f3800c5
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 291 deletions.
548 changes: 258 additions & 290 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
tower.workspace = true
async-lsp = { version = "0.0.4", default-features = false, features = ["client-monitor", "stdio", "tracing"] }
async-lsp = { version = "0.0.4", default-features = false, features = [
"client-monitor",
"stdio",
"tracing",
] }
const_format = "0.2.30"
hex = "0.4.2"
termcolor = "1.1.2"
Expand Down
1 change: 1 addition & 0 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub(crate) fn run<B: Backend>(
}
} else {
let program = compile_circuit(backend, &config.program_dir, &args.compile_options)?;

common_reference_string =
update_common_reference_string(backend, &common_reference_string, &program.circuit)
.map_err(CliError::CommonReferenceStringError)?;
Expand Down
6 changes: 6 additions & 0 deletions crates/nargo_cli/tests/test_data/ecdsa_secp256r1/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ECDSA secp256r1 verification"
authors = [""]
compiler_version = "0.1"

[dependencies]
19 changes: 19 additions & 0 deletions crates/nargo_cli/tests/test_data/ecdsa_secp256r1/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
hashed_message = [
84, 112, 91, 163, 186, 175, 219, 223, 186, 140, 95, 154, 112, 247, 168, 155, 238, 152,
217, 6, 181, 62, 49, 7, 77, 167, 186, 236, 220, 13, 169, 173,
]
pub_key_x = [
85, 15, 71, 16, 3, 243, 223, 151, 195, 223, 80, 106, 199, 151, 246, 114, 31, 177, 161,
251, 123, 143, 111, 131, 210, 36, 73, 138, 101, 200, 142, 36,
]
pub_key_y = [
19, 96, 147, 215, 1, 46, 80, 154, 115, 113, 92, 189, 11, 0, 163, 204, 15, 244, 181,
192, 27, 63, 250, 25, 106, 177, 251, 50, 112, 54, 184, 230,
]
signature = [
44, 112, 168, 208, 132, 182, 43, 252, 92, 224, 54, 65, 202, 249, 247, 42,
212, 218, 140, 129, 191, 230, 236, 148, 135, 187, 94, 27, 239, 98, 161, 50,
24, 173, 158, 226, 158, 175, 53, 31, 220, 80, 241, 82, 12, 66, 94, 155,
144, 138, 7, 39, 139, 67, 176, 236, 123, 135, 39, 120, 193, 78, 7, 132
]

6 changes: 6 additions & 0 deletions crates/nargo_cli/tests/test_data/ecdsa_secp256r1/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std;

fn main(hashed_message : [u8;32], pub_key_x : [u8;32], pub_key_y : [u8;32], signature : [u8;64]) {
let valid_signature = std::ecdsa_secp256r1::verify_signature(pub_key_x, pub_key_y, signature, hashed_message);
assert(valid_signature);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "ECDSA secp256r1 verification"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
hashed_message = [
84, 112, 91, 163, 186, 175, 219, 223, 186, 140, 95, 154, 112, 247, 168, 155, 238, 152,
217, 6, 181, 62, 49, 7, 77, 167, 186, 236, 220, 13, 169, 173,
]
pub_key_x = [
85, 15, 71, 16, 3, 243, 223, 151, 195, 223, 80, 106, 199, 151, 246, 114, 31, 177, 161,
251, 123, 143, 111, 131, 210, 36, 73, 138, 101, 200, 142, 36,
]
pub_key_y = [
19, 96, 147, 215, 1, 46, 80, 154, 115, 113, 92, 189, 11, 0, 163, 204, 15, 244, 181,
192, 27, 63, 250, 25, 106, 177, 251, 50, 112, 54, 184, 230,
]
signature = [
44, 112, 168, 208, 132, 182, 43, 252, 92, 224, 54, 65, 202, 249, 247, 42,
212, 218, 140, 129, 191, 230, 236, 148, 135, 187, 94, 27, 239, 98, 161, 50,
24, 173, 158, 226, 158, 175, 53, 31, 220, 80, 241, 82, 12, 66, 94, 155,
144, 138, 7, 39, 139, 67, 176, 236, 123, 135, 39, 120, 193, 78, 7, 132
]


Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use dep::std;


fn main(hashed_message : [u8;32], pub_key_x : [u8;32], pub_key_y : [u8;32], signature : [u8;64]) {
let valid_signature = std::ecdsa_secp256r1::verify_signature(pub_key_x, pub_key_y, signature, hashed_message);
assert(valid_signature);
}
1 change: 1 addition & 0 deletions crates/noirc_evaluator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Evaluator {
) -> Result<(), RuntimeError> {
self.return_is_distinct =
program.return_distinctness == noirc_abi::AbiDistinctness::Distinct;

let mut ir_gen = IrGenerator::new(program);
self.parse_abi_alt(&mut ir_gen);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub(crate) fn evaluate(
}
BlackBoxFunc::SchnorrVerify
| BlackBoxFunc::EcdsaSecp256k1
| BlackBoxFunc::EcdsaSecp256r1
| BlackBoxFunc::HashToField128Security => {
prepare_outputs(&mut acir_gen.memory, instruction_id, 1, ctx, evaluator)
}
Expand Down Expand Up @@ -152,6 +153,13 @@ pub(crate) fn evaluate(
hashed_message: resolve_array(&args[3], acir_gen, ctx, evaluator),
output: outputs[0],
},
BlackBoxFunc::EcdsaSecp256r1 => BlackBoxFuncCall::EcdsaSecp256r1 {
public_key_x: resolve_array(&args[0], acir_gen, ctx, evaluator),
public_key_y: resolve_array(&args[1], acir_gen, ctx, evaluator),
signature: resolve_array(&args[2], acir_gen, ctx, evaluator),
hashed_message: resolve_array(&args[3], acir_gen, ctx, evaluator),
output: outputs[0],
},
BlackBoxFunc::HashToField128Security => BlackBoxFuncCall::HashToField128Security {
inputs: resolve_array(&args[0], acir_gen, ctx, evaluator),
output: outputs[0],
Expand Down
2 changes: 2 additions & 0 deletions noir_stdlib/src/ecdsa_secp256r1.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[foreign(ecdsa_secp256r1)]
fn verify_signature(_public_key_x : [u8; 32], _public_key_y : [u8; 32], _signature: [u8; 64], _message_hash: [u8]) -> bool {}
1 change: 1 addition & 0 deletions noir_stdlib/src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod slice;
mod merkle;
mod schnorr;
mod ecdsa_secp256k1;
mod ecdsa_secp256r1;
mod eddsa;
mod scalar_mul;
mod sha256;
Expand Down

0 comments on commit f3800c5

Please sign in to comment.