seda_sdk_rs/
secp256k1.rs

1//! This module provides functions for verifying secp256k1 ECDSA signatures
2//! using the Seda VM's FFI interface.
3
4use super::raw;
5
6/// Verifies a secp256k1 ECDSA signature.
7///
8/// This function verifies that a signature was created by the holder of the private key
9/// corresponding to the provided public key, for the given message. If the signature is valid,
10/// it returns `true`; otherwise, it returns `false`.
11///
12/// # Panics
13///
14/// Panics if the result from the VM is not a valid boolean (0 or 1).
15/// This is unexpected and indicates a bug in the VM or the signature verification logic, so it should never be hit.
16///
17/// # Examples
18///
19/// ```no_run
20/// use seda_sdk_rs::secp256k1::secp256k1_verify;
21///
22/// let message = b"Hello, world!";
23/// let signature = vec![/* 64 bytes signature */];
24/// let public_key = vec![/* 33 or 65 bytes public key */];
25///
26/// let is_valid = secp256k1_verify(message, &signature, &public_key);
27/// if is_valid {
28///     println!("Signature is valid!");
29/// } else {
30///     println!("Signature is invalid!");
31/// }
32/// ```
33pub fn secp256k1_verify(message: &[u8], signature: &[u8], public_key: &[u8]) -> bool {
34    let message_len = message.len() as i64;
35    let signature_bytes = signature.to_vec();
36    let signature_length = signature_bytes.len() as i32;
37    let public_key_bytes = public_key.to_vec();
38    let public_key_length = public_key_bytes.len() as i32;
39
40    let result: u8 = unsafe {
41        raw::secp256k1_verify(
42            message.as_ptr(),
43            message_len,
44            signature_bytes.as_ptr(),
45            signature_length,
46            public_key_bytes.as_ptr(),
47            public_key_length,
48        )
49    };
50
51    match result {
52        0 => false,
53        1 => true,
54        _ => panic!("Secp256k1 verify returned invalid bool in u8: {result}"),
55    }
56}