Skip to content

Commit 063d588

Browse files
committed
refactor: update lints and improve function signatures in blst implementation
1 parent a8779f4 commit 063d588

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

crates/charon-crypto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ hex.workspace = true
1616

1717
[lints.rust]
1818
# Allow unsafe code for blst C bindings (overrides workspace forbid)
19-
unsafe_code = "allow"
19+
unsafe_code = "deny"
2020
missing_docs = "deny"
2121

2222
[lints.clippy]

crates/charon-crypto/src/blst_impl.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! Implementation of threshold BLS signatures using the blst library.
44
//! This implementation is compatible with the Herumi BLS library used in the Go
55
//! implementation.
6+
#![allow(unsafe_code)]
67

78
use std::collections::HashMap;
89

@@ -107,7 +108,7 @@ impl Tbls for BlstImpl {
107108
self.threshold_split_insecure(secret_key, total, threshold, OsRng)
108109
}
109110

110-
fn recover_secret(&self, shares: HashMap<Index, PrivateKey>) -> Result<PrivateKey, Error> {
111+
fn recover_secret(&self, shares: &HashMap<Index, PrivateKey>) -> Result<PrivateKey, Error> {
111112
if shares.is_empty() {
112113
return Err(Error::InvalidThreshold {
113114
threshold: 0,
@@ -161,7 +162,7 @@ impl Tbls for BlstImpl {
161162

162163
fn threshold_aggregate(
163164
&self,
164-
partial_signatures_by_idx: HashMap<Index, Signature>,
165+
partial_signatures_by_idx: &HashMap<Index, Signature>,
165166
) -> Result<Signature, Error> {
166167
if partial_signatures_by_idx.is_empty() {
167168
return Err(Error::EmptySignatureArray);
@@ -598,7 +599,7 @@ mod tests {
598599
}
599600

600601
// Aggregate the threshold signatures
601-
let total_sig = blst.threshold_aggregate(signatures).unwrap();
602+
let total_sig = blst.threshold_aggregate(&signatures).unwrap();
602603

603604
// Expected signature from the Go implementation
604605
let expected_sig = hex::decode("b46736c3a1fb5d7977acc6abf3cb3a10fd1a5aed301437022f28cf616326186654d747fda7cd530c2bf18c640e4c024b01d7ba38d90e4abe0cc5356ef63b8e20f717ef0a1f68c3292bd62b4f891345ecafa89a8604f8f6c3ce193dc239215adf").unwrap();
@@ -658,7 +659,7 @@ mod tests {
658659
.map(|(k, v)| (*k, *v))
659660
.collect();
660661

661-
let recovered_sk = blst.recover_secret(subset).unwrap();
662+
let recovered_sk = blst.recover_secret(&subset).unwrap();
662663
assert_eq!(sk, recovered_sk);
663664
}
664665

@@ -675,7 +676,7 @@ mod tests {
675676
assert_eq!(shares.len(), total as usize);
676677

677678
// Recover using all shares
678-
let recovered = blst.recover_secret(shares).unwrap();
679+
let recovered = blst.recover_secret(&shares).unwrap();
679680
assert_eq!(
680681
secret, recovered,
681682
"Secret recovered from all shares should match original"
@@ -703,7 +704,7 @@ mod tests {
703704
}
704705

705706
// Aggregate threshold signatures
706-
let aggregated_sig = blst.threshold_aggregate(signatures).unwrap();
707+
let aggregated_sig = blst.threshold_aggregate(&signatures).unwrap();
707708

708709
// Both signatures should be identical
709710
assert_eq!(
@@ -891,7 +892,7 @@ mod tests {
891892
let subset: HashMap<Index, PrivateKey> =
892893
shares.iter().take(2).map(|(k, v)| (*k, *v)).collect();
893894

894-
let recovered = blst.recover_secret(subset).unwrap();
895+
let recovered = blst.recover_secret(&subset).unwrap();
895896
assert_eq!(sk, recovered);
896897
}
897898

crates/charon-crypto/src/tbls.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ use crate::types::{Error, Index, PrivateKey, PublicKey, Signature};
1010

1111
/// Tbls trait
1212
pub trait Tbls {
13-
/// GenerateSecretKey generates a secret key and returns its compressed
13+
/// Generates a secret key and returns its compressed
1414
/// serialized representation.
1515
fn generate_secret_key(&self, rng: impl RngCore + CryptoRng) -> Result<PrivateKey, Error>;
1616

17-
/// generateInsecureSecret generates a secret that is not cryptographically
17+
/// Generates a secret that is not cryptographically
1818
/// secure using the provided random number generator. This is useful
1919
/// for testing.
2020
fn generate_insecure_secret(&self, rng: impl RngCore + CryptoRng) -> Result<PrivateKey, Error>;
2121

22-
/// SecretToPublicKey extracts the public key associated with the secret
22+
/// Extracts the public key associated with the secret
2323
/// passed in input, and returns its compressed serialized
2424
/// representation.
2525
fn secret_to_public_key(&self, secret_key: &PrivateKey) -> Result<PublicKey, Error>;
2626

27-
/// thresholdSplitInsecure splits a compressed secret into total units of
27+
/// Splits a compressed secret into total units of
2828
/// secret keys, with the given threshold. It returns a map that
2929
/// associates each private, compressed private key to its ID.
3030
///
@@ -55,25 +55,25 @@ pub trait Tbls {
5555
threshold: Index,
5656
) -> Result<HashMap<Index, PrivateKey>, Error>;
5757

58-
/// RecoverSecret recovers a secret from a set of shares
58+
/// Recovers a secret from a set of shares
5959
///
6060
/// # Limitations
6161
///
6262
/// Share IDs must be < 255 due to underlying BLS library constraints.
63-
fn recover_secret(&self, shares: HashMap<Index, PrivateKey>) -> Result<PrivateKey, Error>;
63+
fn recover_secret(&self, shares: &HashMap<Index, PrivateKey>) -> Result<PrivateKey, Error>;
6464

65-
/// Aggregate aggregates a set of signatures into a single signature
65+
/// Aggregates a set of signatures into a single signature
6666
fn aggregate(&self, signatures: Vec<Signature>) -> Result<Signature, Error>;
6767

68-
/// ThresholdAggregate aggregates a set of partial signatures into a single
68+
/// Aggregates a set of partial signatures into a single
6969
/// signature
7070
///
7171
/// # Limitations
7272
///
7373
/// Share IDs must be < 255 due to underlying BLS library constraints.
7474
fn threshold_aggregate(
7575
&self,
76-
partial_signatures_by_idx: HashMap<Index, Signature>,
76+
partial_signatures_by_idx: &HashMap<Index, Signature>,
7777
) -> Result<Signature, Error>;
7878

7979
/// Verify verifies a signature
@@ -84,10 +84,10 @@ pub trait Tbls {
8484
raw_signature: &Signature,
8585
) -> Result<(), Error>;
8686

87-
/// Sign signs a message with a private key
87+
/// Signs a message with a private key
8888
fn sign(&self, private_key: &PrivateKey, data: &[u8]) -> Result<Signature, Error>;
8989

90-
/// ThresholdSign signs a message with a set of private keys
90+
/// Verifies an aggregate signature
9191
fn verify_aggregate(
9292
&self,
9393
public_keys: Vec<PublicKey>,

0 commit comments

Comments
 (0)