Skip to content

Commit

Permalink
Review part1
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx committed Feb 19, 2025
1 parent d77d251 commit 60e38eb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ linked-hash-map = "0.5.6"
num-bigint = "0.4.3"
num-traits = "0.2.18"
octocrab = "0.42.1"
once_cell = "1.20"
oneshot = "0.1.6"
port-selector = "0.1.6"
prettyplease = "0.2.16"
Expand Down
1 change: 0 additions & 1 deletion linera-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ getrandom = { workspace = true, optional = true }
hex.workspace = true
is-terminal.workspace = true
linera-witty = { workspace = true, features = ["macros"] }
once_cell.workspace = true
prometheus = { workspace = true, optional = true }
proptest = { workspace = true, optional = true, features = ["alloc"] }
rand.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions linera-base/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum CryptoError {
IncorrectPublicKeySize(usize),
#[error("Could not parse integer: {0}")]
ParseIntError(#[from] ParseIntError),
#[error("secp256k1 error: {0}")]
Secp256k1Error(::secp256k1::Error),
}

#[cfg(with_getrandom)]
Expand Down
36 changes: 9 additions & 27 deletions linera-base/src/crypto/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

//! Defines secp256k1 signature primitives used by the Linera protocol.
use std::{fmt, str::FromStr};
use std::{fmt, str::FromStr, sync::LazyLock};

use once_cell::sync::Lazy;
use secp256k1::{self, All, Message, Secp256k1};
use serde::{Deserialize, Serialize};

use super::{BcsSignable, CryptoError, CryptoHash, HasTypeName};
use crate::doc_scalar;

/// Static Secp256k1 context for reuse.
pub static SECP256K1: Lazy<Secp256k1<All>> = Lazy::new(secp256k1::Secp256k1::new);
pub static SECP256K1: LazyLock<Secp256k1<All>> = LazyLock::new(secp256k1::Secp256k1::new);

/// A secp256k1 secret key.
#[derive(Eq, PartialEq)]
pub struct Secp256k1SecretKey(pub secp256k1::SecretKey);

/// A secp256k1 public key.
Expand All @@ -36,20 +36,12 @@ pub struct Secp256k1KeyPair {
#[derive(Eq, PartialEq, Copy, Clone)]
pub struct Secp256k1Signature(pub secp256k1::ecdsa::Signature);

impl PartialEq for Secp256k1SecretKey {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl fmt::Debug for Secp256k1SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<redacted for Secp256k1 secret key>")
}
}

impl Eq for Secp256k1SecretKey {}

impl Serialize for Secp256k1PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -88,14 +80,13 @@ impl FromStr for Secp256k1PublicKey {
type Err = CryptoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let pk = secp256k1::PublicKey::from_str(s)
.map_err(|_| CryptoError::IncorrectPublicKeySize(0))?;
let pk = secp256k1::PublicKey::from_str(s).map_err(CryptoError::Secp256k1Error)?;
Ok(Secp256k1PublicKey(pk))
}
}

impl From<[u8; 33]> for Secp256k1PublicKey {
fn from(value: [u8; 33]) -> Self {
impl From<[u8; secp256k1::constants::PUBLIC_KEY_SIZE]> for Secp256k1PublicKey {
fn from(value: [u8; secp256k1::constants::PUBLIC_KEY_SIZE]) -> Self {
let pk = secp256k1::PublicKey::from_slice(&value).expect("Invalid public key");
Secp256k1PublicKey(pk)
}
Expand All @@ -105,8 +96,7 @@ impl TryFrom<&[u8]> for Secp256k1PublicKey {
type Error = CryptoError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let pk = secp256k1::PublicKey::from_slice(value)
.map_err(|_| CryptoError::IncorrectPublicKeySize(value.len()))?;
let pk = secp256k1::PublicKey::from_slice(value).map_err(CryptoError::Secp256k1Error)?;
Ok(Secp256k1PublicKey(pk))
}
}
Expand All @@ -125,15 +115,15 @@ impl fmt::Debug for Secp256k1PublicKey {
}

impl Secp256k1KeyPair {
#[cfg(all(with_getrandom, with_testing))]
/// Generates a new key-pair.
#[cfg(all(with_getrandom, with_testing))]
pub fn generate() -> Self {
let mut rng = rand::rngs::OsRng;
Self::generate_from(&mut rng)
}

#[cfg(with_getrandom)]
/// Generates a new key-pair from the given RNG. Use with care.
#[cfg(with_getrandom)]
pub fn generate_from<R: super::CryptoRng>(rng: &mut R) -> Self {
let (sk, pk) = SECP256K1.generate_keypair(rng);
Secp256k1KeyPair {
Expand All @@ -150,14 +140,6 @@ impl Secp256k1SecretKey {
}
}

impl Secp256k1PublicKey {
/// Returns a public key for the given secret key.
#[allow(dead_code)]
fn from_secret_key(secret: &Secp256k1SecretKey) -> Self {
secret.to_public()
}
}

impl Secp256k1Signature {
/// Computes a secp256k1 signature for `value` using the given `secret`.
/// It first serializes the `T` type and then creates the `CryptoHash` from the serialized bytes.
Expand Down

0 comments on commit 60e38eb

Please sign in to comment.