2022-06-28 04:02:56 -04:00
|
|
|
use core::fmt::{Debug, Formatter};
|
2022-05-03 07:20:24 -04:00
|
|
|
|
2022-06-28 04:02:56 -04:00
|
|
|
use crate::Transcript;
|
2022-05-03 07:20:24 -04:00
|
|
|
|
2022-06-28 04:02:56 -04:00
|
|
|
#[derive(Clone)]
|
2022-05-06 07:33:08 -04:00
|
|
|
pub struct MerlinTranscript(pub merlin::Transcript);
|
2022-05-03 07:20:24 -04:00
|
|
|
// Merlin doesn't implement Debug so provide a stub which won't panic
|
|
|
|
|
impl Debug for MerlinTranscript {
|
2022-11-11 07:07:42 -05:00
|
|
|
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
|
|
|
|
|
fmt.debug_struct("MerlinTranscript").finish()
|
2022-07-15 01:26:07 -04:00
|
|
|
}
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Transcript for MerlinTranscript {
|
2022-06-28 04:02:56 -04:00
|
|
|
// Uses a challenge length of 64 bytes to support wide reduction on generated scalars
|
|
|
|
|
// From a security level standpoint, this should just be 32 bytes
|
|
|
|
|
// From a Merlin standpoint, this should be variable per call
|
|
|
|
|
// From a practical standpoint, this is a demo file not planned to be used and anything using
|
|
|
|
|
// this wrapper should be secure with this setting
|
|
|
|
|
type Challenge = [u8; 64];
|
|
|
|
|
|
2022-07-12 01:28:01 -04:00
|
|
|
fn new(name: &'static [u8]) -> Self {
|
|
|
|
|
MerlinTranscript(merlin::Transcript::new(name))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 01:37:12 -04:00
|
|
|
fn domain_separate(&mut self, label: &'static [u8]) {
|
2022-05-06 07:33:08 -04:00
|
|
|
self.append_message(b"dom-sep", label);
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-05 18:43:36 -04:00
|
|
|
fn append_message<M: AsRef<[u8]>>(&mut self, label: &'static [u8], message: M) {
|
|
|
|
|
self.0.append_message(label, message.as_ref());
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-28 04:02:56 -04:00
|
|
|
fn challenge(&mut self, label: &'static [u8]) -> Self::Challenge {
|
|
|
|
|
let mut challenge = [0; 64];
|
2022-05-03 07:20:24 -04:00
|
|
|
self.0.challenge_bytes(label, &mut challenge);
|
|
|
|
|
challenge
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-31 02:12:14 -04:00
|
|
|
fn rng_seed(&mut self, label: &'static [u8]) -> [u8; 32] {
|
2022-05-03 07:20:24 -04:00
|
|
|
let mut seed = [0; 32];
|
2022-06-28 04:02:56 -04:00
|
|
|
seed.copy_from_slice(&self.challenge(label)[.. 32]);
|
2022-05-06 07:33:08 -04:00
|
|
|
seed
|
2022-05-03 07:20:24 -04:00
|
|
|
}
|
|
|
|
|
}
|