Skip to content

Commit 75c301c

Browse files
authored
[❄] Remove hard dep on sha2 (#232)
sha2 dependency has snuck in. We never depend directly on hash function implementations if we can avoid it.
1 parent af48ba1 commit 75c301c

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

schnorr_fun/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ secp256kfun = { workspace = true }
1818
bech32 = { version = "0.11", optional = true, default-features = false, features = ["alloc"] }
1919
bincode = { workspace = true, optional = true }
2020
vrf_fun = { workspace = true, optional = true }
21-
sha2 = { version = "0.10", optional = true, default-features = false }
2221

2322
[dev-dependencies]
2423
secp256kfun = { workspace = true, features = ["proptest", "bincode", "alloc"] }
@@ -51,7 +50,7 @@ libsecp_compat_0_29 = ["secp256kfun/libsecp_compat_0_29"]
5150
libsecp_compat_0_30 = ["secp256kfun/libsecp_compat_0_30"]
5251
proptest = ["secp256kfun/proptest"]
5352
share_backup = ["dep:bech32"]
54-
vrf_cert_keygen = ["dep:vrf_fun", "dep:sha2"]
53+
vrf_cert_keygen = ["dep:vrf_fun"]
5554

5655
[package.metadata.docs.rs]
5756
all-features = true

schnorr_fun/src/frost/chilldkg/certpedpop.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ impl<H: Hash32, NG: NonceGen> CertificationScheme for Schnorr<H, NG> {
7171
#[cfg(feature = "vrf_cert_keygen")]
7272
pub mod vrf_cert {
7373
use super::*;
74+
use secp256kfun::digest::core_api::BlockSizeUser;
7475
use vrf_fun::VrfProof;
7576

7677
/// VRF certification scheme using SSWU VRF
77-
#[derive(Clone, Debug, PartialEq)]
78-
pub struct VrfCertifier;
78+
#[derive(Clone, Copy, Debug, PartialEq, Default)]
79+
pub struct VrfCertifier<H> {
80+
hash: core::marker::PhantomData<H>,
81+
}
7982

8083
/// The output from VRF verification containing the gamma point
8184
#[derive(Clone, Debug, PartialEq)]
@@ -85,7 +88,7 @@ pub mod vrf_cert {
8588
}
8689

8790
/// Implement CertificationScheme for VrfCertifier
88-
impl CertificationScheme for VrfCertifier {
91+
impl<H: Hash32 + BlockSizeUser> CertificationScheme for VrfCertifier<H> {
8992
type Signature = VrfProof;
9093
type Output = VrfOutput;
9194

@@ -96,7 +99,7 @@ pub mod vrf_cert {
9699
) -> Self::Signature {
97100
// Use the certification bytes as the VRF input
98101
let cert_bytes = agg_input.cert_bytes();
99-
vrf_fun::rfc9381::sswu::prove::<sha2::Sha256>(keypair, &cert_bytes)
102+
vrf_fun::rfc9381::sswu::prove::<H>(keypair, &cert_bytes)
100103
}
101104

102105
fn verify_cert(
@@ -107,11 +110,11 @@ pub mod vrf_cert {
107110
) -> Option<Self::Output> {
108111
// Use the certification bytes as the VRF input
109112
let cert_bytes = agg_input.cert_bytes();
110-
vrf_fun::rfc9381::sswu::verify::<sha2::Sha256>(cert_key, &cert_bytes, signature).map(
111-
|output| VrfOutput {
113+
vrf_fun::rfc9381::sswu::verify::<H>(cert_key, &cert_bytes, signature).map(|output| {
114+
VrfOutput {
112115
gamma: output.gamma,
113-
},
114-
)
116+
}
117+
})
115118
}
116119
}
117120
}
@@ -277,7 +280,9 @@ impl<S: CertificationScheme> CertifiedKeygen<S> {
277280
}
278281

279282
#[cfg(feature = "vrf_cert_keygen")]
280-
impl CertifiedKeygen<vrf_cert::VrfCertifier> {
283+
impl<H: Hash32 + secp256kfun::digest::crypto_common::BlockSizeUser>
284+
CertifiedKeygen<vrf_cert::VrfCertifier<H>>
285+
{
281286
/// Compute a randomness beacon from the VRF outputs
282287
///
283288
/// This function hashes all the VRF gamma points together to produce
@@ -303,16 +308,14 @@ impl CertifiedKeygen<vrf_cert::VrfCertifier> {
303308
/// different views of the keygen outcome without detection, achieving similar
304309
/// security to comparing a full 32-byte hash but with better usability.
305310
pub fn compute_randomness_beacon(&self) -> [u8; 32] {
306-
use sha2::{Digest, Sha256};
307-
308-
let mut hasher = Sha256::new();
311+
let mut hasher = H::default();
309312

310313
// BTreeMap already maintains sorted order by key
311314
for output in self.outputs.values() {
312-
hasher.update(output.gamma.to_bytes());
315+
hasher.update(output.gamma.to_bytes().as_ref());
313316
}
314317

315-
hasher.finalize().into()
318+
hasher.finalize_fixed().into()
316319
}
317320
}
318321

@@ -590,7 +593,7 @@ mod test {
590593
use proptest::test_runner::{RngAlgorithm, TestRng};
591594

592595
let schnorr = crate::new_with_deterministic_nonces::<sha2::Sha256>();
593-
let vrf_certifier = vrf_cert::VrfCertifier;
596+
let vrf_certifier = vrf_cert::VrfCertifier::<sha2::Sha256>::default();
594597
let mut rng = TestRng::deterministic_rng(RngAlgorithm::ChaCha);
595598

596599
let threshold = 2;

secp256kfun/src/vendor/hash_to_curve.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ const YDEN: [FieldElement; 4] = [
147147

148148
/// Hash to curve implementation for secp256k1 following the IETF draft
149149
/// https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/
150-
pub fn hash_to_curve<H: Hash32 + digest::Update + BlockSizeUser>(
151-
msg: &[u8],
152-
dst: &[u8],
153-
) -> ProjectivePoint {
150+
pub fn hash_to_curve<H: Hash32 + BlockSizeUser>(msg: &[u8], dst: &[u8]) -> ProjectivePoint {
154151
let u = hash_to_field::<H>(msg, dst);
155152
let q0 = map_to_curve(u[0]);
156153
let q1 = map_to_curve(u[1]);
@@ -159,7 +156,7 @@ pub fn hash_to_curve<H: Hash32 + digest::Update + BlockSizeUser>(
159156

160157
/// Expand message using XMD (expand_message_xmd)
161158
/// Implements the algorithm from Section 5.4.1 of draft-irtf-cfrg-hash-to-curve
162-
fn expand_message_xmd<H: Hash32 + digest::Update + BlockSizeUser>(
159+
fn expand_message_xmd<H: Hash32 + BlockSizeUser>(
163160
msg: &[u8],
164161
dst: &[u8],
165162
len_in_bytes: usize,

vrf_fun/src/rfc9381.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub mod sswu {
199199
use crate::{Rfc9381SswuVrf, VerifiedRandomOutput, VrfProof};
200200
use secp256kfun::{
201201
KeyPair,
202+
digest::crypto_common::BlockSizeUser,
202203
hash::{Hash32, HashAdd},
203204
prelude::*,
204205
};
@@ -246,7 +247,7 @@ pub mod sswu {
246247
/// [RFC 9380]: https://datatracker.ietf.org/doc/html/rfc9380
247248
pub fn prove<H>(keypair: &KeyPair, alpha: &[u8]) -> VrfProof<U16>
248249
where
249-
H: Hash32 + secp256kfun::digest::crypto_common::BlockSizeUser,
250+
H: Hash32 + BlockSizeUser,
250251
{
251252
let vrf = Rfc9381SswuVrf::<H>::default();
252253
let dst = Dst.as_ref();
@@ -269,7 +270,7 @@ pub mod sswu {
269270
proof: &VrfProof<U16>,
270271
) -> Option<VerifiedRandomOutput>
271272
where
272-
H: Hash32 + secp256kfun::digest::crypto_common::BlockSizeUser,
273+
H: Hash32 + BlockSizeUser,
273274
{
274275
let vrf = Rfc9381SswuVrf::<H>::default();
275276
let h = Point::hash_to_curve_sswu::<H>(alpha, Dst.as_ref()).normalize();

0 commit comments

Comments
 (0)