Skip to main content

bench_ecrecover/
lib.rs

1//! secp256k1 ecrecover benchmark — runs a single ECDSA public key recovery.
2
3#![cfg_attr(target_os = "none", no_std)]
4
5use subsoil as _;
6
7// ---------------------------------------------------------------------------
8// Bump allocator — k256 needs alloc for internal operations.
9// Single ecrecover uses bounded memory; no deallocation needed.
10// ---------------------------------------------------------------------------
11
12#[cfg(target_os = "none")]
13extern crate alloc;
14
15#[cfg(target_os = "none")]
16mod bump_alloc {
17    use core::alloc::{GlobalAlloc, Layout};
18    use core::cell::UnsafeCell;
19
20    const HEAP_SIZE: usize = 64 * 1024; // 64 KB
21
22    pub struct BumpAlloc {
23        heap: UnsafeCell<[u8; HEAP_SIZE]>,
24        pos: UnsafeCell<usize>,
25    }
26
27    unsafe impl Sync for BumpAlloc {}
28
29    unsafe impl GlobalAlloc for BumpAlloc {
30        unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
31            let pos = unsafe { &mut *self.pos.get() };
32            let aligned = (*pos + layout.align() - 1) & !(layout.align() - 1);
33            let next = aligned + layout.size();
34            if next > HEAP_SIZE {
35                return core::ptr::null_mut();
36            }
37            *pos = next;
38            unsafe { (*self.heap.get()).as_mut_ptr().add(aligned) }
39        }
40
41        unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
42    }
43
44    #[global_allocator]
45    pub static ALLOC: BumpAlloc = BumpAlloc {
46        heap: UnsafeCell::new([0; HEAP_SIZE]),
47        pos: UnsafeCell::new(0),
48    };
49}
50
51// ---------------------------------------------------------------------------
52// Test vector (generated from a known private key, verified on host)
53// ---------------------------------------------------------------------------
54
55const MSG_HASH: [u8; 32] = [
56    0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
57    0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
58];
59
60const SIGNATURE: [u8; 64] = [
61    0xff, 0x65, 0x1c, 0x65, 0xee, 0xde, 0xd4, 0x63, 0x83, 0xa4, 0xbd, 0xcd, 0x91, 0x70, 0xff, 0x65,
62    0x9a, 0x4f, 0x61, 0x7b, 0xb6, 0x58, 0xa4, 0x6d, 0xd4, 0x56, 0xc5, 0x1e, 0xc8, 0xcc, 0x21, 0x1a,
63    0x7d, 0xc4, 0xde, 0x91, 0xd0, 0xc8, 0x47, 0xbf, 0x5d, 0xef, 0x99, 0x5b, 0xd0, 0x43, 0x65, 0x81,
64    0x36, 0xfe, 0x21, 0x35, 0xaf, 0xe6, 0x92, 0x82, 0xf7, 0xde, 0x87, 0x39, 0x90, 0xda, 0xcb, 0x77,
65];
66
67const RECOVERY_ID: u8 = 1;
68
69const EXPECTED_PUBKEY: [u8; 33] = [
70    0x02, 0x84, 0xbf, 0x75, 0x62, 0x26, 0x2b, 0xbd, 0x69, 0x40, 0x08, 0x57, 0x48, 0xf3, 0xbe, 0x6a,
71    0xfa, 0x52, 0xae, 0x31, 0x71, 0x55, 0x18, 0x1e, 0xce, 0x31, 0xb6, 0x63, 0x51, 0xcc, 0xff, 0xa4,
72    0xb0,
73];
74
75/// Perform ecrecover: recover the public key from a signature + message hash.
76/// Returns 1 if the recovered key matches the expected public key, 0 otherwise.
77pub fn ecrecover_bench() -> u32 {
78    let sig = match k256::ecdsa::Signature::from_slice(&SIGNATURE) {
79        Ok(s) => s,
80        Err(_) => return 0,
81    };
82    let recid = k256::ecdsa::RecoveryId::new(RECOVERY_ID & 1 != 0, RECOVERY_ID & 2 != 0);
83
84    match k256::ecdsa::VerifyingKey::recover_from_prehash(&MSG_HASH, &sig, recid) {
85        Ok(key) => {
86            let pubkey = key.to_encoded_point(true);
87            let pubkey_bytes = pubkey.as_bytes();
88            if pubkey_bytes.len() != EXPECTED_PUBKEY.len() {
89                return 0;
90            }
91            let mut i = 0;
92            while i < EXPECTED_PUBKEY.len() {
93                if pubkey_bytes[i] != EXPECTED_PUBKEY[i] {
94                    return 0;
95                }
96                i += 1;
97            }
98            1
99        }
100        Err(_) => 0,
101    }
102}