-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
69 lines (60 loc) · 2.06 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use alloy::{hex::ToHexExt, primitives::U256};
use crate::fields::{Field, FieldElement};
pub struct Channel {
pub state: String,
// proof contains all the messages that are exchanged between prover and verifier.
pub proof: Vec<Vec<u8>>,
// compressed proof contains only the messages that are sent from prover to verifier.
pub compressed_proof: Vec<Vec<u8>>,
}
impl Default for Channel {
fn default() -> Self {
Self::new()
}
}
impl Channel {
pub fn new() -> Channel {
Channel {
state: String::new(),
proof: Vec::new(),
compressed_proof: Vec::new(),
}
}
pub fn send(&mut self, s: Vec<u8>) {
log::debug!("sending to channel");
let data_for_digest = format!("{}:{:?}", self.state, s.clone());
self.state = sha256::digest(data_for_digest);
self.proof.push(s.clone());
self.compressed_proof.push(s);
}
pub fn receive_random_field_element(&mut self, field: Field) -> FieldElement {
let received_int = self.receive_random_int(0, (field.0 - 1) as u64, true);
FieldElement(received_int as u128, field)
}
pub fn receive_random_int(&mut self, min: u64, max: u64, show_in_proof: bool) -> u64 {
// convert state to hexadecimal number
let num = (U256::from(min) + U256::from_str_radix(&self.state, 16).unwrap())
% U256::from(max - min + 1);
let t_num = min + num.into_limbs()[0];
let state = self.state.clone() + &t_num.to_be_bytes().to_vec().encode_hex();
self.state = sha256::digest(state);
if show_in_proof {
self.proof.push(num.into_limbs()[0].to_be_bytes().to_vec());
}
min + num.into_limbs()[0]
}
pub fn proof_size(&self) -> usize {
let mut size = 0;
for proof in &self.proof {
size += proof.len();
}
size
}
pub fn compressed_proof_size(&self) -> usize {
let mut size = 0;
for proof in &self.compressed_proof {
size += proof.len();
}
size
}
}