2022-05-03 11:20:24 +00:00
|
|
|
use core::{marker::PhantomData, fmt::Debug};
|
|
|
|
|
|
|
|
#[cfg(features = "merlin")]
|
|
|
|
mod merlin;
|
|
|
|
#[cfg(features = "merlin")]
|
|
|
|
pub use merlin::MerlinTranscript;
|
|
|
|
|
|
|
|
use digest::Digest;
|
|
|
|
|
|
|
|
pub trait Transcript {
|
2022-06-03 05:37:12 +00:00
|
|
|
fn domain_separate(&mut self, label: &'static [u8]);
|
2022-05-03 11:20:24 +00:00
|
|
|
fn append_message(&mut self, label: &'static [u8], message: &[u8]);
|
2022-05-06 11:33:08 +00:00
|
|
|
fn challenge(&mut self, label: &'static [u8]) -> Vec<u8>;
|
2022-05-31 06:12:14 +00:00
|
|
|
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32];
|
2022-05-03 11:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 12:42:38 +00:00
|
|
|
enum DigestTranscriptMember {
|
|
|
|
Name,
|
|
|
|
Domain,
|
|
|
|
Label,
|
|
|
|
Value,
|
|
|
|
Challenge
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DigestTranscriptMember {
|
|
|
|
fn as_u8(&self) -> u8 {
|
|
|
|
match self {
|
|
|
|
DigestTranscriptMember::Name => 0,
|
|
|
|
DigestTranscriptMember::Domain => 1,
|
|
|
|
DigestTranscriptMember::Label => 2,
|
|
|
|
DigestTranscriptMember::Value => 3,
|
|
|
|
DigestTranscriptMember::Challenge => 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 11:20:24 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-06-24 22:43:32 +00:00
|
|
|
pub struct DigestTranscript<D: Clone + Digest>(D, PhantomData<D>);
|
2022-05-03 11:20:24 +00:00
|
|
|
|
2022-06-24 22:43:32 +00:00
|
|
|
impl<D: Clone + Digest> DigestTranscript<D> {
|
2022-06-24 12:42:38 +00:00
|
|
|
fn append(&mut self, kind: DigestTranscriptMember, value: &[u8]) {
|
2022-06-24 22:43:32 +00:00
|
|
|
self.0.update(&[kind.as_u8()]);
|
2022-06-24 12:42:38 +00:00
|
|
|
// Assumes messages don't exceed 16 exabytes
|
2022-06-24 22:43:32 +00:00
|
|
|
self.0.update(u64::try_from(value.len()).unwrap().to_le_bytes());
|
|
|
|
self.0.update(value);
|
2022-06-24 12:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(name: &'static [u8]) -> Self {
|
2022-06-24 22:43:32 +00:00
|
|
|
let mut res = DigestTranscript(D::new(), PhantomData);
|
2022-06-24 12:42:38 +00:00
|
|
|
res.append(DigestTranscriptMember::Name, name);
|
|
|
|
res
|
2022-05-06 11:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 22:43:32 +00:00
|
|
|
impl<D: Digest + Clone> Transcript for DigestTranscript<D> {
|
2022-05-06 11:33:08 +00:00
|
|
|
fn domain_separate(&mut self, label: &[u8]) {
|
2022-06-24 12:42:38 +00:00
|
|
|
self.append(DigestTranscriptMember::Domain, label);
|
2022-05-03 11:20:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn append_message(&mut self, label: &'static [u8], message: &[u8]) {
|
2022-06-24 12:42:38 +00:00
|
|
|
self.append(DigestTranscriptMember::Label, label);
|
|
|
|
self.append(DigestTranscriptMember::Value, message);
|
2022-05-03 11:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 11:33:08 +00:00
|
|
|
fn challenge(&mut self, label: &'static [u8]) -> Vec<u8> {
|
2022-06-24 12:42:38 +00:00
|
|
|
self.append(DigestTranscriptMember::Challenge, label);
|
2022-06-24 22:43:32 +00:00
|
|
|
self.0.clone().finalize().to_vec()
|
2022-05-03 11:20:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 06:12:14 +00:00
|
|
|
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32] {
|
2022-05-03 11:20:24 +00:00
|
|
|
let mut seed = [0; 32];
|
2022-05-06 11:33:08 +00:00
|
|
|
seed.copy_from_slice(&self.challenge(label)[0 .. 32]);
|
|
|
|
seed
|
2022-05-03 11:20:24 +00:00
|
|
|
}
|
|
|
|
}
|