-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): add crypto module with signature verification functiona…
…lity
- Loading branch information
1 parent
2efd76a
commit 849af44
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Cryptographic utilities. | ||
//! This module provides cryptographic utilities. | ||
#[cfg(test)] | ||
#[path = "crypto_test.rs"] | ||
mod crypto_test; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use starknet_crypto::FieldElement; | ||
|
||
use crate::hash::StarkFelt; | ||
|
||
/// An error that can occur during cryptographic operations. | ||
#[derive(thiserror::Error, Clone, Debug)] | ||
pub enum CryptoError { | ||
#[error("Invalid public key {0:?}.")] | ||
InvalidPublicKey(PublicKey), | ||
#[error("Invalid message hash {0:?}.")] | ||
InvalidMessageHash(StarkFelt), | ||
#[error("Invalid r {0:?}.")] | ||
InvalidR(StarkFelt), | ||
#[error("Invalid s {0:?}.")] | ||
InvalidS(StarkFelt), | ||
} | ||
|
||
/// A public key. | ||
#[derive( | ||
Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, | ||
)] | ||
pub struct PublicKey(pub StarkFelt); | ||
|
||
/// A signature. | ||
#[derive( | ||
Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, | ||
)] | ||
pub struct Signature { | ||
pub r: StarkFelt, | ||
pub s: StarkFelt, | ||
} | ||
|
||
/// Verifies the authenticity of a signed message hash given the public key of the signer. | ||
pub fn verify_message_hash_signature( | ||
message_hash: &StarkFelt, | ||
signature: &Signature, | ||
public_key: &PublicKey, | ||
) -> Result<bool, CryptoError> { | ||
starknet_crypto::verify( | ||
&public_key.0.into(), | ||
&FieldElement::from(*message_hash), | ||
&signature.r.into(), | ||
&signature.s.into(), | ||
) | ||
.map_err(|err| match err { | ||
starknet_crypto::VerifyError::InvalidPublicKey => { | ||
CryptoError::InvalidPublicKey(*public_key) | ||
} | ||
starknet_crypto::VerifyError::InvalidMessageHash => { | ||
CryptoError::InvalidMessageHash(*message_hash) | ||
} | ||
starknet_crypto::VerifyError::InvalidR => CryptoError::InvalidR(signature.r), | ||
starknet_crypto::VerifyError::InvalidS => CryptoError::InvalidS(signature.s), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Unittest for verify_message_signature | ||
|
||
use crate::crypto::{verify_message_hash_signature, PublicKey, Signature}; | ||
use crate::hash::{poseidon_hash_array, StarkFelt}; | ||
use crate::stark_felt; | ||
|
||
#[test] | ||
fn signature_verification() { | ||
// The signed message of block 4256. | ||
let message_hash = poseidon_hash_array(&[ | ||
stark_felt!("0x7d5db04c5ca2aea828180dc441afb1580e3cee7547a3567ced3aa5bb8b273c0"), | ||
stark_felt!("0x64689c12248e1110af4b3af0e2b43cd51ad13e8855f10e37669e2a4baf919c6"), | ||
]); | ||
// The signature of the message. | ||
let signature = Signature { | ||
r: stark_felt!("0x1b382bbfd693011c9b7692bc932b23ed9c288deb27c8e75772e172abbe5950c"), | ||
s: stark_felt!("0xbe4438085057e1a7c704a0da3b30f7b8340fe3d24c86772abfd24aa597e42"), | ||
}; | ||
// The public key of the sequencer. | ||
let public_key = | ||
PublicKey(stark_felt!("0x48253ff2c3bed7af18bde0b611b083b39445959102d4947c51c4db6aa4f4e58")); | ||
|
||
let result = verify_message_hash_signature(&message_hash.0, &signature, &public_key).unwrap(); | ||
assert!(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters