Skip to content

Commit 8d340bc

Browse files
committed
impl secp256r1_verify_signature
1 parent 38d287f commit 8d340bc

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

Cargo.lock

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guest_libs/crypto/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ceno_keccak = { path = "../keccak" }
1515
ceno_sha2 = { path = "../sha2" }
1616
ceno_syscall.workspace = true
1717
k256 = { git = "https://github.com/scroll-tech/elliptic-curves", branch = "ceno/k256-13.4", default-features = false, features = ["std", "ecdsa"] }
18+
p256 = { git = "https://github.com/scroll-tech/elliptic-curves", branch = "ceno/k256-13.4", default-features = false, features = ["std", "ecdsa"] }
1819
thiserror.workspace = true
1920

2021
[dev-dependencies]

guest_libs/crypto/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ macro_rules! ceno_crypto {
137137
sig: &[u8; 64],
138138
pk: &[u8; 64],
139139
) -> bool {
140-
$crate::secp256r1::secp256r1_verify_signature(msg, sig, pk)
140+
$crate::secp256r1::secp256r1_verify_signature(msg, sig, pk).is_some()
141141
}
142142
}
143143

guest_libs/crypto/src/secp256r1.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
1+
use p256::{
2+
EncodedPoint,
3+
ecdsa::{Signature, VerifyingKey, signature::hazmat::PrehashVerifier},
4+
};
5+
16
/// secp256r1 (P-256) signature verification.
27
#[inline]
3-
pub fn secp256r1_verify_signature(_msg: &[u8; 32], _sig: &[u8; 64], _pk: &[u8; 64]) -> bool {
4-
unimplemented!()
8+
pub fn secp256r1_verify_signature(msg: &[u8; 32], sig: &[u8; 64], pk: &[u8; 64]) -> Option<()> {
9+
#[cfg(feature = "profiling")]
10+
ceno_syscall::syscall_phantom_log_pc_cycle("secp256r1_verify_signature start");
11+
// Can fail only if the input is not exact length.
12+
let signature = Signature::from_slice(sig).ok()?;
13+
// Decode the public key bytes (x,y coordinates) using EncodedPoint
14+
let encoded_point = EncodedPoint::from_untagged_bytes(pk.into());
15+
// Create VerifyingKey from the encoded point
16+
let public_key = VerifyingKey::from_encoded_point(&encoded_point).ok()?;
17+
18+
#[cfg(feature = "profiling")]
19+
{
20+
ceno_syscall::syscall_phantom_log_pc_cycle("secp256r1_verify_signature end");
21+
public_key.verify_prehash(msg, &signature).ok()
22+
}
23+
#[cfg(not(feature = "profiling"))]
24+
{
25+
public_key.verify_prehash(msg, &signature).ok()
26+
}
527
}

0 commit comments

Comments
 (0)