testudo/src/transcript.rs
src/transcript.rsRaw
1use super::group::CompressedGroup;
2use super::scalar::Scalar;
3use merlin::Transcript;
4
5pub trait ProofTranscript {
6 fn append_protocol_name(&mut self, protocol_name: &'static [u8]);
7 fn append_scalar(&mut self, label: &'static [u8], scalar: &Scalar);
8 fn append_point(&mut self, label: &'static [u8], point: &CompressedGroup);
9 fn challenge_scalar(&mut self, label: &'static [u8]) -> Scalar;
10 fn challenge_vector(&mut self, label: &'static [u8], len: usize) -> Vec<Scalar>;
11}
12
13impl ProofTranscript for Transcript {
14 fn append_protocol_name(&mut self, protocol_name: &'static [u8]) {
15 self.append_message(b"protocol-name", protocol_name);
16 }
17
18 fn append_scalar(&mut self, label: &'static [u8], scalar: &Scalar) {
19 self.append_message(label, &scalar.to_bytes());
20 }
21
22 fn append_point(&mut self, label: &'static [u8], point: &CompressedGroup) {
23 self.append_message(label, point.as_bytes());
24 }
25
26 fn challenge_scalar(&mut self, label: &'static [u8]) -> Scalar {
27 let mut buf = [0u8; 64];
28 self.challenge_bytes(label, &mut buf);
29 Scalar::from_bytes_wide(&buf)
30 }
31
32 fn challenge_vector(&mut self, label: &'static [u8], len: usize) -> Vec<Scalar> {
33 (0..len)
34 .map(|_i| self.challenge_scalar(label))
35 .collect::<Vec<Scalar>>()
36 }
37}
38
39pub trait AppendToTranscript {
40 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript);
41}
42
43impl AppendToTranscript for Scalar {
44 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
45 transcript.append_scalar(label, self);
46 }
47}
48
49impl AppendToTranscript for [Scalar] {
50 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
51 transcript.append_message(label, b"begin_append_vector");
52 for item in self {
53 transcript.append_scalar(label, item);
54 }
55 transcript.append_message(label, b"end_append_vector");
56 }
57}
58
59impl AppendToTranscript for CompressedGroup {
60 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
61 transcript.append_point(label, self);
62 }
63}