swiftness_transcript/
transcript.rs

1use alloc::vec;
2use alloc::vec::Vec;
3use starknet_crypto::{poseidon_hash, poseidon_hash_many, Felt};
4
5pub struct Transcript {
6    digest: Felt,
7    counter: Felt,
8}
9
10impl Transcript {
11    pub fn new(digest: Felt) -> Self {
12        Self { digest, counter: Felt::from(0) }
13    }
14
15    pub fn new_with_counter(digest: Felt, counter: Felt) -> Self {
16        Self { digest, counter }
17    }
18
19    pub fn digest(&self) -> &Felt {
20        &self.digest
21    }
22
23    pub fn counter(&self) -> &Felt {
24        &self.counter
25    }
26
27    pub fn random_felt_to_prover(&mut self) -> Felt {
28        let hash = poseidon_hash(self.digest, self.counter);
29        self.counter += Felt::ONE;
30        hash
31    }
32
33    pub fn random_felts_to_prover(&mut self, mut len: Felt) -> Vec<Felt> {
34        let mut res = Vec::new();
35        while len > Felt::ZERO {
36            res.push(self.random_felt_to_prover());
37            len -= Felt::ONE
38        }
39        res
40    }
41
42    pub fn read_felt_from_prover(&mut self, val: &Felt) {
43        let hash = poseidon_hash_many([&(self.digest + Felt::ONE), val]);
44        self.digest = hash;
45        self.counter = Felt::ZERO;
46    }
47
48    pub fn read_felt_vector_from_prover(&mut self, val: &[Felt]) {
49        let hash = poseidon_hash_many(vec![&(self.digest + Felt::ONE)].into_iter().chain(val));
50        self.digest = hash;
51        self.counter = Felt::ZERO;
52    }
53
54    pub fn read_uint64_from_prover(&mut self, val: u64) {
55        self.read_felt_from_prover(&Felt::from(val))
56    }
57}