use core::iter;
use merlin::Transcript;
use ark_bls12_381::Fr;
use ark_ff::Field;
use ark_serialize::CanonicalSerialize;
pub trait CurdleproofsTranscript {
fn append(&mut self, label: &'static [u8], item: &impl CanonicalSerialize);
fn append_list<T: CanonicalSerialize>(&mut self, label: &'static [u8], items: &[&T]);
fn get_and_append_challenge(&mut self, label: &'static [u8]) -> Fr;
fn get_and_append_challenges(&mut self, label: &'static [u8], n: usize) -> Vec<Fr>;
}
impl CurdleproofsTranscript for Transcript {
fn append(&mut self, label: &'static [u8], item: &impl CanonicalSerialize) {
let mut bytes = Vec::new();
item.serialize(&mut bytes).unwrap();
self.append_message(label, &bytes)
}
fn append_list<T: CanonicalSerialize>(&mut self, label: &'static [u8], items: &[&T]) {
for item in items {
self.append(label, *item);
}
}
fn get_and_append_challenge(&mut self, label: &'static [u8]) -> Fr {
loop {
let mut buf = [0; 64];
self.challenge_bytes(label, &mut buf);
if let Some(e) = Fr::from_random_bytes(&buf) {
self.append(label, &e);
return e;
}
}
}
fn get_and_append_challenges(&mut self, label: &'static [u8], n: usize) -> Vec<Fr> {
iter::repeat_with(|| self.get_and_append_challenge(label))
.take(n)
.collect()
}
}