Have DigestTranscript require a 32-byte hash

Needed to generate RNG seeds without panicking. Mandates at least a 
128-bit security level.
This commit is contained in:
Luke Parker 2022-06-24 18:49:04 -04:00
parent eb94abb81a
commit 963d9eab10
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6

View file

@ -1,11 +1,11 @@
use core::{marker::PhantomData, fmt::Debug}; use core::fmt::Debug;
#[cfg(features = "merlin")] #[cfg(features = "merlin")]
mod merlin; mod merlin;
#[cfg(features = "merlin")] #[cfg(features = "merlin")]
pub use merlin::MerlinTranscript; pub use merlin::MerlinTranscript;
use digest::Digest; use digest::{typenum::type_operators::IsGreaterOrEqual, consts::U256, Digest};
pub trait Transcript { pub trait Transcript {
fn domain_separate(&mut self, label: &'static [u8]); fn domain_separate(&mut self, label: &'static [u8]);
@ -35,9 +35,9 @@ impl DigestTranscriptMember {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct DigestTranscript<D: Clone + Digest>(D, PhantomData<D>); pub struct DigestTranscript<D: Clone + Digest>(D) where D::OutputSize: IsGreaterOrEqual<U256>;
impl<D: Clone + Digest> DigestTranscript<D> { impl<D: Clone + Digest> DigestTranscript<D> where D::OutputSize: IsGreaterOrEqual<U256> {
fn append(&mut self, kind: DigestTranscriptMember, value: &[u8]) { fn append(&mut self, kind: DigestTranscriptMember, value: &[u8]) {
self.0.update(&[kind.as_u8()]); self.0.update(&[kind.as_u8()]);
// Assumes messages don't exceed 16 exabytes // Assumes messages don't exceed 16 exabytes
@ -46,13 +46,14 @@ impl<D: Clone + Digest> DigestTranscript<D> {
} }
pub fn new(name: &'static [u8]) -> Self { pub fn new(name: &'static [u8]) -> Self {
let mut res = DigestTranscript(D::new(), PhantomData); let mut res = DigestTranscript(D::new());
res.append(DigestTranscriptMember::Name, name); res.append(DigestTranscriptMember::Name, name);
res res
} }
} }
impl<D: Digest + Clone> Transcript for DigestTranscript<D> { impl<D: Digest + Clone> Transcript for DigestTranscript<D>
where D::OutputSize: IsGreaterOrEqual<U256> {
fn domain_separate(&mut self, label: &[u8]) { fn domain_separate(&mut self, label: &[u8]) {
self.append(DigestTranscriptMember::Domain, label); self.append(DigestTranscriptMember::Domain, label);
} }