-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrusted_setup.rs
89 lines (77 loc) · 2.48 KB
/
trusted_setup.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use ark_ec::{pairing::Pairing, Group};
use ark_ff::{field_hashers::HashToField, PrimeField};
use polynomials::multilinear_polynomial::eval_form::MLE;
////////////////////////////////////////////
/// Multilinear PCS Trusted Setup
/// ////////////////////////////////////////
#[derive(Debug, Clone)]
pub struct TrustedSetup<P: Pairing, H: HashToField<P::ScalarField>> {
hasher: H,
number_of_vars: usize,
pub powers_of_tau: Vec<P::G1>,
// Taus in G2 should be indexed by variable
pub tau_in_g2: Vec<P::G2>,
}
impl<P, H> TrustedSetup<P, H>
where
P: Pairing,
H: HashToField<P::ScalarField>,
{
pub fn new(hasher_domain: &[u8], taus: Vec<Vec<u8>>) -> Self {
let hasher: H = HashToField::new(hasher_domain);
let taus: Vec<P::ScalarField> = taus
.iter()
.map(|tau| {
*hasher
.hash_to_field(tau, 1)
.get(0)
.expect("Error encountered when hashing to field")
})
.collect();
let eq_for_tau_in_g1 = MLE::eq(&taus)
.val
.iter()
.map(|val| P::G1::generator().mul_bigint(val.into_bigint()))
.collect();
let taus_in_g2 = taus
.iter()
.map(|val| P::G2::generator().mul_bigint(val.into_bigint()))
.collect();
Self {
hasher,
number_of_vars: taus.len(),
powers_of_tau: eq_for_tau_in_g1,
tau_in_g2: taus_in_g2,
}
}
pub fn contribute(&mut self, randomness: &Vec<Vec<u8>>) {
assert_eq!(
self.number_of_vars,
randomness.len(),
"Not enough taus for commitment"
);
let taus: Vec<P::ScalarField> = randomness
.iter()
.map(|val| {
*self
.hasher
.hash_to_field(val, 1)
.get(0)
.expect("Error encountered when hashing to field")
})
.collect();
let eq_for_taus = MLE::eq(&taus);
self.powers_of_tau = self
.powers_of_tau
.iter()
.zip(&eq_for_taus.val)
.map(|(tau, val)| tau.mul_bigint(val.into_bigint()))
.collect::<Vec<P::G1>>();
self.tau_in_g2 = self
.tau_in_g2
.iter()
.zip(&taus)
.map(|(tau, val)| tau.mul_bigint(val.into_bigint()))
.collect::<Vec<P::G2>>();
}
}