use codec::Encode;
use merlin::Transcript;
use schnorrkel::vrf::{VRFOutput, VRFProof};
#[derive(Clone, Encode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum VRFTranscriptValue {
Bytes(Vec<u8>),
U64(u64),
}
#[derive(Clone, Encode)]
pub struct VRFTranscriptData {
pub label: &'static [u8],
pub items: Vec<(&'static str, VRFTranscriptValue)>,
}
pub struct VRFSignature {
pub output: VRFOutput,
pub proof: VRFProof,
}
pub fn make_transcript(data: VRFTranscriptData) -> Transcript {
let mut transcript = Transcript::new(data.label);
for (label, value) in data.items.into_iter() {
match value {
VRFTranscriptValue::Bytes(bytes) => {
transcript.append_message(label.as_bytes(), &bytes);
},
VRFTranscriptValue::U64(val) => {
transcript.append_u64(label.as_bytes(), val);
},
}
}
transcript
}
#[cfg(test)]
mod tests {
use super::*;
use rand::RngCore;
use rand_chacha::{rand_core::SeedableRng, ChaChaRng};
#[test]
fn transcript_creation_matches() {
let mut orig_transcript = Transcript::new(b"My label");
orig_transcript.append_u64(b"one", 1);
orig_transcript.append_message(b"two", "test".as_bytes());
let new_transcript = make_transcript(VRFTranscriptData {
label: b"My label",
items: vec![
("one", VRFTranscriptValue::U64(1)),
("two", VRFTranscriptValue::Bytes("test".as_bytes().to_vec())),
],
});
let test = |t: Transcript| -> [u8; 16] {
let mut b = [0u8; 16];
t.build_rng().finalize(&mut ChaChaRng::from_seed([0u8; 32])).fill_bytes(&mut b);
b
};
debug_assert!(test(orig_transcript) == test(new_transcript));
}
}