Skip to content

Commit

Permalink
feat(crypto): add crypto module with signature verification functiona…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
yair-starkware committed Dec 25, 2023
1 parent 2efd76a commit 849af44
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/crypto.rs
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),
})
}
25 changes: 25 additions & 0 deletions src/crypto_test.rs
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);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub mod block;
pub mod core;
pub mod crypto;
pub mod data_availability;
pub mod deprecated_contract_class;
pub mod hash;
Expand Down

0 comments on commit 849af44

Please sign in to comment.