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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::marker::PhantomData;
use bellperson::{gadgets::num::AllocatedNum, Circuit, ConstraintSystem, SynthesisError};
use blstrs::Scalar as Fr;
use ff::Field;
use filecoin_hashers::{poseidon::PoseidonFunction, HashFunction, Hasher, PoseidonMDArity};
use generic_array::typenum::Unsigned;
use storage_proofs_core::{
compound_proof::CircuitComponent,
gadgets::{constraint, por::PoRCircuit, variables::Root},
merkle::MerkleTreeTrait,
};
pub struct ElectionPoStCircuit<Tree: MerkleTreeTrait> {
pub comm_r: Option<Fr>,
pub comm_c: Option<Fr>,
pub comm_r_last: Option<Fr>,
pub leafs: Vec<Option<Fr>>,
#[allow(clippy::type_complexity)]
pub paths: Vec<Vec<(Vec<Option<Fr>>, Option<usize>)>>,
pub partial_ticket: Option<Fr>,
pub randomness: Option<Fr>,
pub prover_id: Option<Fr>,
pub sector_id: Option<Fr>,
pub _t: PhantomData<Tree>,
}
#[derive(Clone, Default)]
pub struct ComponentPrivateInputs {}
impl<'a, Tree: MerkleTreeTrait> CircuitComponent for ElectionPoStCircuit<Tree> {
type ComponentPrivateInputs = ComponentPrivateInputs;
}
impl<'a, Tree: 'static + MerkleTreeTrait> Circuit<Fr> for ElectionPoStCircuit<Tree> {
fn synthesize<CS: ConstraintSystem<Fr>>(self, cs: &mut CS) -> Result<(), SynthesisError> {
let comm_r = self.comm_r;
let comm_c = self.comm_c;
let comm_r_last = self.comm_r_last;
let leafs = self.leafs;
let paths = self.paths;
let partial_ticket = self.partial_ticket;
let randomness = self.randomness;
let prover_id = self.prover_id;
let sector_id = self.sector_id;
assert_eq!(paths.len(), leafs.len());
let comm_r_last_num = AllocatedNum::alloc(cs.namespace(|| "comm_r_last"), || {
comm_r_last
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
let comm_c_num = AllocatedNum::alloc(cs.namespace(|| "comm_c"), || {
comm_c
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
let comm_r_num = AllocatedNum::alloc(cs.namespace(|| "comm_r"), || {
comm_r
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
comm_r_num.inputize(cs.namespace(|| "comm_r_input"))?;
{
let hash_num = <Tree::Hasher as Hasher>::Function::hash2_circuit(
cs.namespace(|| "H_comm_c_comm_r_last"),
&comm_c_num,
&comm_r_last_num,
)?;
constraint::equal(
cs,
|| "enforce_comm_c_comm_r_last_hash_comm_r",
&comm_r_num,
&hash_num,
);
}
for (i, (leaf, path)) in leafs.iter().zip(paths.iter()).enumerate() {
PoRCircuit::<Tree>::synthesize(
cs.namespace(|| format!("challenge_inclusion{}", i)),
Root::Val(*leaf),
path.clone().into(),
Root::from_allocated::<CS>(comm_r_last_num.clone()),
true,
)?;
}
let randomness_num = AllocatedNum::alloc(cs.namespace(|| "randomness"), || {
randomness
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
let prover_id_num = AllocatedNum::alloc(cs.namespace(|| "prover_id"), || {
prover_id
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
let sector_id_num = AllocatedNum::alloc(cs.namespace(|| "sector_id"), || {
sector_id
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
let mut partial_ticket_nums = vec![randomness_num, prover_id_num, sector_id_num];
for (i, leaf) in leafs.iter().enumerate() {
let leaf_num = AllocatedNum::alloc(cs.namespace(|| format!("leaf_{}", i)), || {
leaf.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
partial_ticket_nums.push(leaf_num);
}
let arity = PoseidonMDArity::to_usize();
while partial_ticket_nums.len() % arity != 0 {
partial_ticket_nums.push(AllocatedNum::alloc(
cs.namespace(|| format!("padding_{}", partial_ticket_nums.len())),
|| Ok(Fr::zero()),
)?);
}
let partial_ticket_num = PoseidonFunction::hash_md_circuit::<_>(
&mut cs.namespace(|| "partial_ticket_hash"),
&partial_ticket_nums,
)?;
let expected_partial_ticket_num =
AllocatedNum::alloc(cs.namespace(|| "partial_ticket"), || {
partial_ticket
.map(Into::into)
.ok_or(SynthesisError::AssignmentMissing)
})?;
expected_partial_ticket_num.inputize(cs.namespace(|| "partial_ticket_input"))?;
constraint::equal(
cs,
|| "enforce partial_ticket is correct",
&partial_ticket_num,
&expected_partial_ticket_num,
);
Ok(())
}
}