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