-
Notifications
You must be signed in to change notification settings - Fork 0
/
trusted_setup.rs
91 lines (74 loc) · 2.58 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
90
91
//////////////////////////////////////////
/// Trusted Setup Ceremony
//////////////////////////////////////////
use ark_ec::{pairing::Pairing, Group};
use ark_ff::{field_hashers::HashToField, Field, PrimeField};
#[derive(Debug)]
pub struct PublicParameters<C: Pairing> {
// powers of tau for G1
pub g1_powers_of_tau: Vec<C::G1>,
// tau in G2
pub g2_power_of_tau: Vec<C::G2>,
}
impl<C: Pairing> PublicParameters<C> {
pub fn new(g1_powers_of_tau: Vec<C::G1>, g2_power_of_tau: Vec<C::G2>) -> Self {
Self {
g1_powers_of_tau,
g2_power_of_tau,
}
}
}
pub struct TrustedSetUpCeremony<C: Pairing, H: HashToField<C::ScalarField>> {
hasher: H,
max_degree: u64,
pub public_parameters: PublicParameters<C>,
// Not considered for now
// pub contributors_proof: Vec<F>
}
impl<C: Pairing, H: HashToField<C::ScalarField>> TrustedSetUpCeremony<C, H> {
pub fn instantiate(hasher_domain: &[u8], degree: u64, randomness: &[u8]) -> Self {
let hasher = HashToField::new(hasher_domain);
let mut instance = Self {
hasher,
max_degree: degree,
public_parameters: PublicParameters::new(vec![], vec![]),
};
let tau = instance
.hasher
.hash_to_field(randomness, 1)
.get(0)
.unwrap()
.to_owned();
let mut g1_powers_of_tau: Vec<C::G1> = vec![];
let g = C::G1::generator();
g1_powers_of_tau.push(g);
for i in 0..degree {
g1_powers_of_tau.push(g1_powers_of_tau[i as usize].mul_bigint(tau.into_bigint()));
}
let g2_power_of_tau = vec![C::G2::generator().mul_bigint(tau.pow([1]).into_bigint())];
let public_parameters = PublicParameters::new(g1_powers_of_tau, g2_power_of_tau);
instance.public_parameters = public_parameters;
instance
}
pub fn contribute(&mut self, randomness: &[u8]) {
let tau: C::ScalarField = self
.hasher
.hash_to_field(randomness, 1)
.get(0)
.unwrap()
.to_owned();
self.public_parameters.g1_powers_of_tau = self
.public_parameters
.g1_powers_of_tau
.iter()
.enumerate()
.map(|(index, val)| val.mul_bigint(tau.pow([index as u64]).into_bigint()))
.collect();
self.public_parameters.g2_power_of_tau = self
.public_parameters
.g2_power_of_tau
.iter()
.map(|val| val.mul_bigint(tau.into_bigint()))
.collect();
}
}