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

fix: implement missing codegen for BlackBoxFunc::EcdsaSecp256r1 in brillig #3943

Merged
merged 3 commits into from
Jan 4, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,28 @@ pub(crate) fn convert_black_box_call(
)
}
}
BlackBoxFunc::EcdsaSecp256r1 => {
if let (
[BrilligVariable::BrilligArray(public_key_x), BrilligVariable::BrilligArray(public_key_y), BrilligVariable::BrilligArray(signature), message],
[BrilligVariable::Simple(result_register)],
) = (function_arguments, function_results)
{
let message_hash_vector =
convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::EcdsaSecp256r1 {
hashed_msg: message_hash_vector.to_heap_vector(),
public_key_x: public_key_x.to_heap_array(),
public_key_y: public_key_y.to_heap_array(),
signature: signature.to_heap_array(),
result: *result_register,
});
} else {
unreachable!(
"ICE: EcdsaSecp256r1 expects four array arguments and one register result"
)
}
}

BlackBoxFunc::PedersenCommitment => {
if let (
[message, BrilligVariable::Simple(domain_separator)],
Expand Down Expand Up @@ -160,7 +182,18 @@ pub(crate) fn convert_black_box_call(
)
}
}
_ => unimplemented!("ICE: Black box function {:?} is not implemented", bb_func),
BlackBoxFunc::AND => {
unreachable!("ICE: `BlackBoxFunc::AND` calls should be transformed into a `BinaryOp`")
}
BlackBoxFunc::XOR => {
unreachable!("ICE: `BlackBoxFunc::XOR` calls should be transformed into a `BinaryOp`")
}
BlackBoxFunc::RANGE => unreachable!(
"ICE: `BlackBoxFunc::RANGE` calls should be transformed into a `Instruction::Cast`"
),
BlackBoxFunc::RecursiveAggregation => unimplemented!(
"ICE: `BlackBoxFunc::RecursiveAggregation` is not implemented by the Brillig VM"
),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "brillig_ecdsa"
name = "brillig_ecdsa_secp256k1"
type = "bin"
authors = [""]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ use dep::std;
// Tests a very simple program.
//
// The features being tested is ecdsa in brillig
fn main(hashed_message: [u8;32], pub_key_x: [u8;32], pub_key_y: [u8;32], signature: [u8;64]) {
fn main(hashed_message: [u8; 32], pub_key_x: [u8; 32], pub_key_y: [u8; 32], signature: [u8; 64]) {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
}

unconstrained fn ecdsa(hashed_message: [u8;32], pub_key_x: [u8;32], pub_key_y: [u8;32], signature: [u8;64]) -> bool {
unconstrained fn ecdsa(
hashed_message: [u8; 32],
pub_key_x: [u8; 32],
pub_key_y: [u8; 32],
signature: [u8; 64]
) -> bool {
std::ecdsa_secp256k1::verify_signature(pub_key_x, pub_key_y, signature, hashed_message)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "brillig_ecdsa_secp256r1"
type = "bin"
authors = [""]

[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,16 @@
use dep::std;
// Tests a very simple program.
//
// The features being tested is ecdsa in brillig
fn main(hashed_message: [u8; 32], pub_key_x: [u8; 32], pub_key_y: [u8; 32], signature: [u8; 64]) {
assert(ecdsa(hashed_message, pub_key_x, pub_key_y, signature));
}

unconstrained fn ecdsa(
hashed_message: [u8; 32],
pub_key_x: [u8; 32],
pub_key_y: [u8; 32],
signature: [u8; 64]
) -> bool {
std::ecdsa_secp256r1::verify_signature(pub_key_x, pub_key_y, signature, hashed_message)
}
Loading