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