forked from yetanotherco/aligned_layer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
326 lines (287 loc) · 11.7 KB
/
lib.rs
File metadata and controls
326 lines (287 loc) · 11.7 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/// Consensus chain selection algorithms. The [`official specification`] was taken as a reference.
///
/// [`official specification`]: https://github.com/MinaProtocol/mina/blob/develop/docs/specs/consensus/README.md
mod consensus_state;
mod verifier_index;
use log::error;
use mina_bridge_core::proof::state_proof::{MinaStateProof, MinaStatePubInputs};
use ark_ec::short_weierstrass_jacobian::GroupAffine;
use consensus_state::{select_secure_chain, ChainResult};
use kimchi::mina_curves::pasta::{Fp, PallasParameters};
use kimchi::verifier_index::VerifierIndex;
use lazy_static::lazy_static;
use mina_curves::pasta::{Fq, Vesta};
use mina_p2p_messages::hash::MinaHash;
use mina_p2p_messages::v2::{MinaStateProtocolStateValueStableV2, StateHash};
use mina_tree::proofs::field::FieldWitness as _;
use mina_tree::proofs::verification::verify_block;
use poly_commitment::srs::SRS;
use verifier_index::{deserialize_blockchain_vk, MinaChain};
lazy_static! {
static ref DEVNET_VERIFIER_INDEX: VerifierIndex<GroupAffine<PallasParameters>> =
deserialize_blockchain_vk(MinaChain::Devnet).unwrap();
static ref MAINNET_VERIFIER_INDEX: VerifierIndex<GroupAffine<PallasParameters>> =
deserialize_blockchain_vk(MinaChain::Mainnet).unwrap();
static ref MINA_SRS: SRS<Vesta> = SRS::<Vesta>::create(Fq::SRS_DEPTH);
}
// TODO(xqft): check proof size
const MAX_PROOF_SIZE: usize = 48 * 1024;
const MAX_PUB_INPUT_SIZE: usize = 6 * 1024;
#[no_mangle]
pub extern "C" fn verify_mina_state_ffi(
proof_buffer: &[u8; MAX_PROOF_SIZE],
proof_len: usize,
pub_input_buffer: &[u8; MAX_PUB_INPUT_SIZE],
pub_input_len: usize,
) -> bool {
let Some(proof_buffer_slice) = proof_buffer.get(..proof_len) else {
error!("Proof length argument is greater than max proof size");
return false;
};
let Some(pub_input_buffer_slice) = pub_input_buffer.get(..pub_input_len) else {
error!("Public input length argument is greater than max public input size");
return false;
};
let proof: MinaStateProof = match bincode::deserialize(proof_buffer_slice) {
Ok(proof) => proof,
Err(err) => {
error!("Failed to deserialize state proof: {}", err);
return false;
}
};
let pub_inputs: MinaStatePubInputs = match bincode::deserialize(pub_input_buffer_slice) {
Ok(pub_inputs) => pub_inputs,
Err(err) => {
error!("Failed to deserialize state pub inputs: {}", err);
return false;
}
};
// Checks the integrity of the public inputs, also checks if the states form a chain.
let (candidate_tip_state, bridge_tip_state, candidate_tip_state_hash) =
match check_pub_inputs(&proof, &pub_inputs) {
Ok(validated_data) => validated_data,
Err(err) => {
error!("Failed to check pub inputs: {err}");
return false;
}
};
// Consensus checks
let secure_chain = match select_secure_chain(&candidate_tip_state, &bridge_tip_state) {
Ok(res) => res,
Err(err) => {
error!("Failed consensus checks for candidate tip: {err}");
return false;
}
};
if secure_chain == ChainResult::Bridge {
error!("Failed consensus checks for candidate tip: bridge's tip is more secure");
return false;
}
// Verify the tip block (and thanks to Pickles recursion all the previous states are verified
// as well)
if pub_inputs.is_state_proof_from_devnet {
verify_block(
&proof.candidate_tip_proof,
candidate_tip_state_hash,
&DEVNET_VERIFIER_INDEX,
&MINA_SRS,
)
} else {
verify_block(
&proof.candidate_tip_proof,
candidate_tip_state_hash,
&MAINNET_VERIFIER_INDEX,
&MINA_SRS,
)
}
}
/// Checks public inputs against the proof data, making sure the inputs correspond to the proofs
/// we're verifying. Returns validated data for executing the rest of the verification steps.
fn check_pub_inputs(
proof: &MinaStateProof,
pub_inputs: &MinaStatePubInputs,
) -> Result<
(
MinaStateProtocolStateValueStableV2,
MinaStateProtocolStateValueStableV2,
Fp,
),
String,
> {
let candidate_root_state_hash = proof
.candidate_chain_states
.first()
.map(|state| state.hash())
.ok_or("failed to retrieve root state hash".to_string())?;
// Reconstructs the state hashes if the states form a chain, and compares them to the public
// input state hashes. Does not compare the tip state hash.
let mut state_hash = candidate_root_state_hash;
for (body_hash, expected_prev_state_hash) in proof
.candidate_chain_states
.iter()
.skip(1)
.map(|state| state.body.hash())
.zip(pub_inputs.candidate_chain_state_hashes.iter())
{
let curr_state_hash = StateHash::from_hashes(&state_hash, &body_hash);
let prev_state_hash = std::mem::replace(&mut state_hash, curr_state_hash);
// Check if all hashes (but the last one) in the public input are correct
if &prev_state_hash != expected_prev_state_hash {
return Err("public input state hashes do not match the states to verify, or states don't form a chain".to_string());
}
}
// Check if the tip hash (the last one) is correct, so we also verify the Merkle list
if &state_hash
!= pub_inputs
.candidate_chain_state_hashes
.last()
.ok_or("failed to retrieve tip state hash".to_string())?
{
return Err("public input tip state hash is not correct".to_string());
}
// Validate the public input ledger hashes
let expected_candidate_chain_ledger_hashes = proof.candidate_chain_states.iter().map(|state| {
&state
.body
.blockchain_state
.ledger_proof_statement
.target
.first_pass_ledger
});
if pub_inputs
.candidate_chain_ledger_hashes
.iter()
.ne(expected_candidate_chain_ledger_hashes)
{
return Err(
"candidate chain ledger hashes on public inputs don't match the ones on the states to verify"
.to_string(),
);
}
// Validate the public input bridge's tip state hash
let bridge_tip_state_hash = pub_inputs
.bridge_tip_state_hash
.to_fp()
.map_err(|err| format!("Can't parse bridge tip state hash to fp: {err}"))?;
if MinaHash::hash(&proof.bridge_tip_state) != bridge_tip_state_hash {
return Err(
"the candidate's chain tip state doesn't match the hash provided as public input"
.to_string(),
);
}
let candidate_tip_state = proof
.candidate_chain_states
.last()
.ok_or("failed to get candidate tip state from proof".to_string())?
.clone();
let bridge_tip_state = proof.bridge_tip_state.clone();
let candidate_tip_state_hash = pub_inputs
.candidate_chain_state_hashes
.last()
.ok_or("failed to get candidate tip hash from public inputs".to_string())
.and_then(|hash| {
hash.to_fp()
.map_err(|err| format!("failed to convert tip state hash to field element: {err}"))
})?;
Ok((
candidate_tip_state,
bridge_tip_state,
candidate_tip_state_hash,
))
}
#[cfg(test)]
mod test {
use super::*;
const PROOF_BYTES: &[u8] =
include_bytes!("../../../../scripts/test_files/mina/mina_state.proof");
const PUB_INPUT_BYTES: &[u8] =
include_bytes!("../../../../scripts/test_files/mina/mina_state.pub");
const BAD_HASH_PUB_INPUT_BYTES: &[u8] =
include_bytes!("../../../../scripts/test_files/mina/mina_state_bad_hash.pub");
#[test]
fn valid_mina_state_proof_verifies() {
let mut proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let proof_size = PROOF_BYTES.len();
assert!(proof_size <= proof_buffer.len());
proof_buffer[..proof_size].clone_from_slice(PROOF_BYTES);
let mut pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let pub_input_size = PUB_INPUT_BYTES.len();
assert!(pub_input_size <= pub_input_buffer.len());
pub_input_buffer[..pub_input_size].clone_from_slice(PUB_INPUT_BYTES);
let result =
verify_mina_state_ffi(&proof_buffer, proof_size, &pub_input_buffer, pub_input_size);
assert!(result);
}
#[test]
fn mina_state_proof_with_bad_bridge_tip_hash_does_not_verify() {
let mut proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let proof_size = PROOF_BYTES.len();
assert!(proof_size <= proof_buffer.len());
proof_buffer[..proof_size].clone_from_slice(PROOF_BYTES);
let mut pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let pub_input_size = BAD_HASH_PUB_INPUT_BYTES.len();
assert!(pub_input_size <= pub_input_buffer.len());
pub_input_buffer[..pub_input_size].clone_from_slice(BAD_HASH_PUB_INPUT_BYTES);
let result =
verify_mina_state_ffi(&proof_buffer, proof_size, &pub_input_buffer, pub_input_size);
assert!(!result);
}
#[test]
fn empty_mina_state_proof_does_not_verify() {
let proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let proof_size = PROOF_BYTES.len();
let mut pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let pub_input_size = PUB_INPUT_BYTES.len();
assert!(pub_input_size <= pub_input_buffer.len());
pub_input_buffer[..pub_input_size].clone_from_slice(PUB_INPUT_BYTES);
let result =
verify_mina_state_ffi(&proof_buffer, proof_size, &pub_input_buffer, pub_input_size);
assert!(!result);
}
#[test]
fn valid_mina_state_proof_with_empty_pub_input_does_not_verify() {
let mut proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let proof_size = PROOF_BYTES.len();
assert!(proof_size <= proof_buffer.len());
proof_buffer[..proof_size].clone_from_slice(PROOF_BYTES);
let pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let pub_input_size = PUB_INPUT_BYTES.len();
let result =
verify_mina_state_ffi(&proof_buffer, proof_size, &pub_input_buffer, pub_input_size);
assert!(!result);
}
#[test]
fn valid_mina_state_proof_with_greater_proof_size_does_not_verify() {
let mut proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let wrong_proof_size = super::MAX_PROOF_SIZE + 1;
proof_buffer[..PROOF_BYTES.len()].clone_from_slice(PROOF_BYTES);
let mut pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let pub_input_size = PUB_INPUT_BYTES.len();
assert!(pub_input_size <= pub_input_buffer.len());
pub_input_buffer[..pub_input_size].clone_from_slice(PUB_INPUT_BYTES);
let result = verify_mina_state_ffi(
&proof_buffer,
wrong_proof_size,
&pub_input_buffer,
pub_input_size,
);
assert!(!result);
}
#[test]
fn valid_mina_state_proof_with_greater_pub_input_size_does_not_verify() {
let mut proof_buffer = [0u8; super::MAX_PROOF_SIZE];
let proof_size = PROOF_BYTES.len();
assert!(proof_size <= proof_buffer.len());
proof_buffer[..proof_size].clone_from_slice(PROOF_BYTES);
let mut pub_input_buffer = [0u8; super::MAX_PUB_INPUT_SIZE];
let wrong_pub_input_size = MAX_PUB_INPUT_SIZE + 1;
pub_input_buffer[..PUB_INPUT_BYTES.len()].clone_from_slice(PUB_INPUT_BYTES);
let result = verify_mina_state_ffi(
&proof_buffer,
proof_size,
&pub_input_buffer,
wrong_pub_input_size,
);
assert!(!result);
}
}