Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jayz22 committed Apr 12, 2024
1 parent 6a3e40c commit 1cd21bf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions soroban-sdk/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use crate::{
/// by a secure cryptographic function is required.
pub struct Digest(BytesN<32>);

impl Digest {
/// Constructs a new `Digest` from a fixed-length bytes array.
pub fn from_bytes(bytes: BytesN<32>) -> Self {
Self(bytes)
}
}

impl IntoVal<Env, Val> for Digest {
fn into_val(&self, e: &Env) -> Val {
self.0.into_val(e)
Expand Down
1 change: 1 addition & 0 deletions soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod contractimport_with_error;
mod crypto_ed25519;
mod crypto_keccak256;
mod crypto_secp256k1;
mod crypto_secp256r1;
mod crypto_sha256;
mod env;
mod max_ttl;
Expand Down
8 changes: 4 additions & 4 deletions soroban-sdk/src/tests/crypto_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::{bytesn, Env};
use crate::{bytesn, crypto::Digest, Env};

#[test]
fn test_recover_key_ecdsa_secp256k1() {
let env = Env::default();

// From: https://github.com/ethereum/go-ethereum/blob/90d5bd85bcf2919ac2735a47fde675213348a0a6/crypto/secp256k1/secp256_test.go#L204-L217
let message_digest = bytesn!(
let message_digest = Digest::from_bytes(bytesn!(
&env,
0xce0677bb30baa8cf067c88db9811f4333d131bf8bcf12fe7065d211dce971008
);
));
let signature = bytesn!(
&env,
0x90f27b8b488db00b00606796d2987f6a5f59ae62ea05effe84fef5b8b0e549984a691139ad57a3f0b906637673aa2f63d1f55cb1a69199d4009eea23ceaddc93
Expand All @@ -19,7 +19,7 @@ fn test_recover_key_ecdsa_secp256k1() {
0x04e32df42865e97135acfb65f3bae71bdc86f4d49150ad6a440b6f15878109880a0a2b2667f7e725ceea70c673093bf67663e0312623c8e091b13cf2c0f11ef652
);
assert_eq!(
env.crypto_hazmat()
env.crypto()
.secp256k1_recover(&message_digest, &signature, recovery_id),
expected_public_key
);
Expand Down
25 changes: 25 additions & 0 deletions soroban-sdk/src/tests/crypto_secp256r1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{bytesn, crypto::Digest, Env};

#[test]
fn test_recover_key_ecdsa_secp256r1() {
let env = Env::default();

// Test vector copied and adapted from
// https://csrc.nist.gov/groups/STM/cavp/documents/dss/186-3ecdsatestvectors.zip
// `SigVer.rsp` section [P-256,SHA-256]
let message_digest = Digest::from_bytes(bytesn!(
&env,
0xd1b8ef21eb4182ee270638061063a3f3c16c114e33937f69fb232cc833965a94
));
let signature = bytesn!(
&env,
0xbf96b99aa49c705c910be33142017c642ff540c76349b9dab72f981fd9347f4f17c55095819089c2e03b9cd415abdf12444e323075d98f31920b9e0f57ec871c
);
let public_key = bytesn!(
&env,
0x04e424dc61d4bb3cb7ef4344a7f8957a0c5134e16f7a67c074f82e6e12f49abf3c970eed7aa2bc48651545949de1dddaf0127e5965ac85d1243d6f60e7dfaee927
);

env.crypto()
.secp256r1_verify(&public_key, &message_digest, &signature)
}

0 comments on commit 1cd21bf

Please sign in to comment.