arnaucube
arnaucube/shockwave-plusPublic Self-hosted repository
shockwave-plus/tensor_pcs/src/transcript.rs
tensor_pcs/src/transcript.rsRaw
 1use crate::FieldExt;
 2use merlin::Transcript as MerlinTranscript;
 3use std::marker::PhantomData;
 4
 5#[derive(Clone)]
 6pub struct Transcript<F: FieldExt> {
 7    transcript_inner: MerlinTranscript,
 8    _marker: PhantomData<F>,
 9}
10
11impl<F: FieldExt> Transcript<F> {
12    pub fn new(label: &'static [u8]) -> Self {
13        Self {
14            transcript_inner: MerlinTranscript::new(label),
15            _marker: PhantomData,
16        }
17    }
18
19    pub fn append_fe(&mut self, fe: &F) {
20        self.transcript_inner.append_message(b"", &fe.to_repr());
21    }
22
23    pub fn append_bytes(&mut self, bytes: &[u8]) {
24        self.transcript_inner.append_message(b"", bytes);
25    }
26
27    pub fn challenge_vec(&mut self, n: usize) -> Vec<F> {
28        (0..n)
29            .map(|_| {
30                let mut bytes = [0u8; 64];
31                self.transcript_inner.challenge_bytes(b"", &mut bytes);
32                F::from_uniform_bytes(&bytes)
33            })
34            .collect()
35    }
36
37    pub fn challenge_fe(&mut self) -> F {
38        let mut bytes = [0u8; 64];
39        self.transcript_inner.challenge_bytes(b"", &mut bytes);
40        F::from_uniform_bytes(&bytes)
41    }
42
43    pub fn challenge_bytes(&mut self, bytes: &mut [u8]) {
44        self.transcript_inner.challenge_bytes(b"", bytes);
45    }
46}
47
48pub trait AppendToTranscript<F: FieldExt> {
49    fn append_to_transcript(&self, transcript: &mut Transcript<F>);
50}
51
52impl<F: FieldExt> AppendToTranscript<F> for [u8; 32] {
53    fn append_to_transcript(&self, transcript: &mut Transcript<F>) {
54        transcript.append_bytes(self);
55    }
56}