Skip to content

Commit

Permalink
Add API docs for secp256k1_instruction and secp256k1_recover (solana-…
Browse files Browse the repository at this point in the history
…labs#26065)

* Add API docs for secp256k1_instruction and secp256k1_recover

* typo

* Remove unused variable from secp256k1 program test

* Bump solana_bpf_rust_secp256k1_recover ix count

Co-authored-by: Tyera Eulberg <[email protected]>
  • Loading branch information
2 people authored and xiangzhu70 committed Aug 17, 2022
1 parent 5ced4f6 commit ba42fdb
Show file tree
Hide file tree
Showing 10 changed files with 1,347 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 56 additions & 10 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/bpf/rust/secp256k1_recover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ documentation = "https://docs.rs/solana-bpf-rust-secp256k1-recover"
edition = "2021"

[dependencies]
libsecp256k1 = { version = "0.7.0", default-features = false }
solana-program = { path = "../../../../sdk/program", version = "=1.12.0" }

[lib]
Expand Down
51 changes: 48 additions & 3 deletions programs/bpf/rust/secp256k1_recover/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Secp256k1Recover Syscall test
extern crate solana_program;
use solana_program::{custom_heap_default, custom_panic_default, msg};
use solana_program::{
custom_heap_default, custom_panic_default, msg, secp256k1_recover::secp256k1_recover,
};

fn test_secp256k1_recover() {
use solana_program::secp256k1_recover::secp256k1_recover;

let expected: [u8; 64] = [
0x42, 0xcd, 0x27, 0xe4, 0x0f, 0xdf, 0x7c, 0x97, 0x0a, 0xa2, 0xca, 0x0b, 0x88, 0x5b, 0x96,
0x0f, 0x8b, 0x62, 0x8a, 0x41, 0xa1, 0x81, 0xe7, 0xe6, 0x8e, 0x03, 0xea, 0x0b, 0x84, 0x20,
Expand All @@ -32,11 +32,56 @@ fn test_secp256k1_recover() {
assert_eq!(public_key.to_bytes(), expected);
}

/// secp256k1_recover allows malleable signatures
fn test_secp256k1_recover_malleability() {
let message = b"hello world";
let message_hash = {
let mut hasher = solana_program::keccak::Hasher::default();
hasher.hash(message);
hasher.result()
};

let pubkey_bytes: [u8; 64] = [
0x9B, 0xEE, 0x7C, 0x18, 0x34, 0xE0, 0x18, 0x21, 0x7B, 0x40, 0x14, 0x9B, 0x84, 0x2E, 0xFA,
0x80, 0x96, 0x00, 0x1A, 0x9B, 0x17, 0x88, 0x01, 0x80, 0xA8, 0x46, 0x99, 0x09, 0xE9, 0xC4,
0x73, 0x6E, 0x39, 0x0B, 0x94, 0x00, 0x97, 0x68, 0xC2, 0x28, 0xB5, 0x55, 0xD3, 0x0C, 0x0C,
0x42, 0x43, 0xC1, 0xEE, 0xA5, 0x0D, 0xC0, 0x48, 0x62, 0xD3, 0xAE, 0xB0, 0x3D, 0xA2, 0x20,
0xAC, 0x11, 0x85, 0xEE,
];
let signature_bytes: [u8; 64] = [
0x93, 0x92, 0xC4, 0x6C, 0x42, 0xF6, 0x31, 0x73, 0x81, 0xD4, 0xB2, 0x44, 0xE9, 0x2F, 0xFC,
0xE3, 0xF4, 0x57, 0xDD, 0x50, 0xB3, 0xA5, 0x20, 0x26, 0x3B, 0xE7, 0xEF, 0x8A, 0xB0, 0x69,
0xBB, 0xDE, 0x2F, 0x90, 0x12, 0x93, 0xD7, 0x3F, 0xA0, 0x29, 0x0C, 0x46, 0x4B, 0x97, 0xC5,
0x00, 0xAD, 0xEA, 0x6A, 0x64, 0x4D, 0xC3, 0x8D, 0x25, 0x24, 0xEF, 0x97, 0x6D, 0xC6, 0xD7,
0x1D, 0x9F, 0x5A, 0x26,
];
let recovery_id: u8 = 0;

let signature = libsecp256k1::Signature::parse_standard_slice(&signature_bytes).unwrap();

// Flip the S value in the signature to make a different but valid signature.
let mut alt_signature = signature.clone();
alt_signature.s = -alt_signature.s;
let alt_recovery_id = libsecp256k1::RecoveryId::parse(recovery_id ^ 1).unwrap();

let alt_signature_bytes = alt_signature.serialize();
let alt_recovery_id = alt_recovery_id.serialize();

let recovered_pubkey =
secp256k1_recover(&message_hash.0, recovery_id, &signature_bytes[..]).unwrap();
assert_eq!(recovered_pubkey.to_bytes(), pubkey_bytes);

let alt_recovered_pubkey =
secp256k1_recover(&message_hash.0, alt_recovery_id, &alt_signature_bytes[..]).unwrap();
assert_eq!(alt_recovered_pubkey.to_bytes(), pubkey_bytes);
}

#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
msg!("secp256k1_recover");

test_secp256k1_recover();
test_secp256k1_recover_malleability();

0
}
Expand Down
2 changes: 1 addition & 1 deletion programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,7 @@ fn assert_instruction_count() {
("solana_bpf_rust_param_passing", 146),
("solana_bpf_rust_rand", 469),
("solana_bpf_rust_sanity", 52054),
("solana_bpf_rust_secp256k1_recover", 25707),
("solana_bpf_rust_secp256k1_recover", 91195),
("solana_bpf_rust_sha", 24081),
]);
}
Expand Down
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ js-sys = "0.3.58"
[dev-dependencies]
anyhow = "1.0.58"
curve25519-dalek = "3.2.1"
hex = "0.4.3"
static_assertions = "1.1.0"
tiny-bip39 = "0.8.2"

Expand Down
3 changes: 2 additions & 1 deletion sdk/program/src/example_mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub mod solana_client {
/// programs.
pub mod solana_sdk {
pub use crate::{
address_lookup_table_account, hash, instruction, message, nonce,
address_lookup_table_account, hash, instruction, keccak, message, nonce,
pubkey::{self, Pubkey},
system_instruction, system_program,
};
Expand Down Expand Up @@ -178,6 +178,7 @@ pub mod solana_sdk {

pub trait Signers {}

impl<T: Signer> Signers for [&T] {}
impl<T: Signer> Signers for [&T; 1] {}
impl<T: Signer> Signers for [&T; 2] {}
}
Expand Down
5 changes: 5 additions & 0 deletions sdk/program/src/secp256k1_program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! The [secp256k1 native program][np].
//!
//! [np]: https://docs.solana.com/developing/runtime-facilities/programs#secp256k1-program
//!
//! Constructors for secp256k1 program instructions, and documentation on the
//! program's usage can be found in [`solana_sdk::secp256k1_instruction`].
//!
//! [`solana_sdk::secp256k1_instruction`]: https://docs.rs/solana-sdk/latest/solana_sdk/secp256k1_instruction/index.html
crate::declare_id!("KeccakSecp256k11111111111111111111111111111");
Loading

0 comments on commit ba42fdb

Please sign in to comment.