diff --git a/Cargo.lock b/Cargo.lock index 492100629..10286d367 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1051,6 +1051,7 @@ dependencies = [ "ceno_sha2", "ceno_syscall", "k256 0.13.4 (git+https://github.com/scroll-tech/elliptic-curves?branch=ceno%2Fk256-13.4)", + "p256 0.13.2 (git+https://github.com/scroll-tech/elliptic-curves?branch=ceno%2Fk256-13.4)", "revm-precompile", "substrate-bn 0.6.0 (git+https://github.com/scroll-tech/bn?branch=ceno)", "thiserror 2.0.12", @@ -4599,7 +4600,18 @@ checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" dependencies = [ "ecdsa 0.16.9 (registry+https://github.com/rust-lang/crates.io-index)", "elliptic-curve", - "primeorder", + "primeorder 0.13.6 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2", +] + +[[package]] +name = "p256" +version = "0.13.2" +source = "git+https://github.com/scroll-tech/elliptic-curves?branch=ceno%2Fk256-13.4#05a8a81ef6300ff8bbf1d810b018bbeb71ab947c" +dependencies = [ + "ecdsa 0.16.9 (registry+https://github.com/rust-lang/crates.io-index)", + "elliptic-curve", + "primeorder 0.13.6 (git+https://github.com/scroll-tech/elliptic-curves?branch=ceno%2Fk256-13.4)", "sha2", ] @@ -5286,6 +5298,14 @@ dependencies = [ "elliptic-curve", ] +[[package]] +name = "primeorder" +version = "0.13.6" +source = "git+https://github.com/scroll-tech/elliptic-curves?branch=ceno%2Fk256-13.4#05a8a81ef6300ff8bbf1d810b018bbeb71ab947c" +dependencies = [ + "elliptic-curve", +] + [[package]] name = "primitive-types" version = "0.12.2" @@ -5671,7 +5691,7 @@ dependencies = [ "aurora-engine-modexp", "cfg-if", "k256 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)", - "p256", + "p256 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", "revm-primitives", "ripemd", "sha2", @@ -6242,7 +6262,7 @@ dependencies = [ "k256 0.13.4 (registry+https://github.com/rust-lang/crates.io-index)", "multilinear_extensions 0.1.0 (git+https://github.com/scroll-tech/gkr-backend.git?tag=v1.0.0-alpha.18)", "num", - "p256", + "p256 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", "p3-field", "rug", "serde", diff --git a/guest_libs/crypto/Cargo.toml b/guest_libs/crypto/Cargo.toml index f8fc04ec4..8c94a5351 100644 --- a/guest_libs/crypto/Cargo.toml +++ b/guest_libs/crypto/Cargo.toml @@ -15,6 +15,7 @@ ceno_keccak = { path = "../keccak" } ceno_sha2 = { path = "../sha2" } ceno_syscall.workspace = true k256 = { git = "https://github.com/scroll-tech/elliptic-curves", branch = "ceno/k256-13.4", default-features = false, features = ["std", "ecdsa"] } +p256 = { git = "https://github.com/scroll-tech/elliptic-curves", branch = "ceno/k256-13.4", default-features = false, features = ["std", "ecdsa"] } thiserror.workspace = true [dev-dependencies] diff --git a/guest_libs/crypto/src/macros.rs b/guest_libs/crypto/src/macros.rs index cd9afb81b..47d4b4e70 100644 --- a/guest_libs/crypto/src/macros.rs +++ b/guest_libs/crypto/src/macros.rs @@ -137,7 +137,7 @@ macro_rules! ceno_crypto { sig: &[u8; 64], pk: &[u8; 64], ) -> bool { - $crate::secp256r1::secp256r1_verify_signature(msg, sig, pk) + $crate::secp256r1::secp256r1_verify_signature(msg, sig, pk).is_some() } } diff --git a/guest_libs/crypto/src/secp256r1.rs b/guest_libs/crypto/src/secp256r1.rs index 2e73c277c..9b4af90e7 100644 --- a/guest_libs/crypto/src/secp256r1.rs +++ b/guest_libs/crypto/src/secp256r1.rs @@ -1,5 +1,28 @@ +use p256::{ + EncodedPoint, + ecdsa::{Signature, VerifyingKey, signature::hazmat::PrehashVerifier}, +}; + /// secp256r1 (P-256) signature verification. #[inline] -pub fn secp256r1_verify_signature(_msg: &[u8; 32], _sig: &[u8; 64], _pk: &[u8; 64]) -> bool { - unimplemented!() +pub fn secp256r1_verify_signature(msg: &[u8; 32], sig: &[u8; 64], pk: &[u8; 64]) -> Option<()> { + #[cfg(feature = "profiling")] + ceno_syscall::syscall_phantom_log_pc_cycle("secp256r1_verify_signature start"); + // Can fail only if the input is not exact length. + let signature = Signature::from_slice(sig).ok()?; + // Decode the public key bytes (x,y coordinates) using EncodedPoint + let encoded_point = EncodedPoint::from_untagged_bytes(pk.into()); + // Create VerifyingKey from the encoded point + let public_key = VerifyingKey::from_encoded_point(&encoded_point).ok()?; + + #[cfg(feature = "profiling")] + { + let res = public_key.verify_prehash(msg, &signature).ok(); + ceno_syscall::syscall_phantom_log_pc_cycle("secp256r1_verify_signature end"); + res + } + #[cfg(not(feature = "profiling"))] + { + public_key.verify_prehash(msg, &signature).ok() + } }