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
204 lines (177 loc) · 6.05 KB
/
lib.rs
File metadata and controls
204 lines (177 loc) · 6.05 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
use alloy::sol_types::SolValue;
use log::error;
use merkle_verifier::verify_merkle_proof;
use mina_bridge_core::{
proof::account_proof::{MinaAccountProof, MinaAccountPubInputs},
sol::account::MinaAccountValidationExample,
};
use mina_tree::Account;
mod merkle_verifier;
#[no_mangle]
pub extern "C" fn verify_account_inclusion_ffi(
proof_bytes: *const u8,
proof_len: u32,
pub_input_bytes: *const u8,
pub_input_len: u32,
) -> i32 {
let result = std::panic::catch_unwind(|| {
inner_verify_account_inclusion_ffi(proof_bytes, proof_len, pub_input_bytes, pub_input_len)
});
match result {
Ok(v) => v as i32,
Err(_) => -1,
}
}
fn inner_verify_account_inclusion_ffi(
proof_bytes: *const u8,
proof_len: u32,
pub_input_bytes: *const u8,
pub_input_len: u32,
) -> bool {
if proof_bytes.is_null() || pub_input_bytes.is_null() {
error!("Input buffer null");
return false;
}
if proof_len == 0 || pub_input_len == 0 {
error!("Input buffer length zero size");
return false;
}
let proof_bytes = unsafe { std::slice::from_raw_parts(proof_bytes, proof_len as usize) };
let pub_input_bytes =
unsafe { std::slice::from_raw_parts(pub_input_bytes, pub_input_len as usize) };
verify_account_inclusion(proof_bytes, pub_input_bytes)
}
pub fn verify_account_inclusion(proof_bytes: &[u8], pub_input_bytes: &[u8]) -> bool {
let MinaAccountProof {
merkle_path,
account,
} = match bincode::deserialize(proof_bytes) {
Ok(proof) => proof,
Err(err) => {
error!("Failed to deserialize account proof: {}", err);
return false;
}
};
let MinaAccountPubInputs {
ledger_hash,
encoded_account,
} = match bincode::deserialize(pub_input_bytes) {
Ok(pub_inputs) => pub_inputs,
Err(err) => {
error!("Failed to deserialize account pub inputs: {}", err);
return false;
}
};
let expected_encoded_account =
match MinaAccountValidationExample::Account::try_from(&account) {
Ok(account) => account,
Err(err) => {
error!("Failed to convert Mina account to Solidity struct: {}", err);
return false;
}
}
.abi_encode();
if expected_encoded_account != encoded_account {
error!("ABI encoded account in public inputs doesn't match the account on the proof");
return false;
}
// the hash function for MinaBaseAccountBinableArgStableV2 produces a panic every
// time it's called. So we use Account's one.
let leaf_hash = Account::from(&account).hash();
// TODO(xqft): when the needed account GraphQL query is done, do:
// 1. send encoded account as part of the proof
// 2. check poseidon(account) == leaf_hash
// 3. check keccak(account) == account_hash
verify_merkle_proof(leaf_hash, merkle_path, ledger_hash)
}
#[cfg(test)]
mod test {
use super::*;
use core::ptr;
const PROOF_BYTES: &[u8] =
include_bytes!("../../../../scripts/test_files/mina_account/mina_account.proof");
const PUB_INPUT_BYTES: &[u8] =
include_bytes!("../../../../scripts/test_files/mina_account/mina_account.pub");
#[test]
fn valid_account_state_proof_verifies() {
let mut proof_buffer = [0u8; PROOF_BYTES.len()];
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; PUB_INPUT_BYTES.len()];
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_account_inclusion_ffi(
proof_buffer.as_ptr(),
proof_size as u32,
pub_input_buffer.as_ptr(),
pub_input_size as u32,
);
assert_eq!(result, 1);
}
#[test]
fn zeroized_account_state_proof_does_not_verify() {
const PROOF_SIZE: usize = PROOF_BYTES.len();
let proof_buffer = [0u8; PROOF_SIZE];
let result = verify_account_inclusion_ffi(
proof_buffer.as_ptr(),
PROOF_SIZE as u32,
PUB_INPUT_BYTES.as_ptr(),
PUB_INPUT_BYTES.len() as u32,
);
assert_eq!(result, 0);
}
#[test]
fn valid_account_state_proof_with_zeroized_pub_input_does_not_verify() {
const PUB_INPUT_SIZE: usize = PUB_INPUT_BYTES.len();
let pub_input_buffer = [0u8; PUB_INPUT_SIZE];
let result = verify_account_inclusion_ffi(
PROOF_BYTES.as_ptr(),
PROOF_BYTES.len() as u32,
pub_input_buffer.as_ptr(),
PUB_INPUT_SIZE as u32,
);
assert_eq!(result, 0);
}
#[test]
fn null_account_state_proof_does_not_verify() {
let result = verify_account_inclusion_ffi(
ptr::null(),
PROOF_BYTES.len() as u32,
PUB_INPUT_BYTES.as_ptr(),
PUB_INPUT_BYTES.len() as u32,
);
assert_eq!(result, 0);
}
#[test]
fn valid_account_state_proof_with_null_pub_input_does_not_verify() {
let result = verify_account_inclusion_ffi(
PROOF_BYTES.as_ptr(),
PROOF_BYTES.len() as u32,
ptr::null(),
PUB_INPUT_BYTES.len() as u32,
);
assert_eq!(result, 0);
}
#[test]
fn empty_account_state_proof_does_not_verify() {
let result = verify_account_inclusion_ffi(
PROOF_BYTES.as_ptr(),
0,
PUB_INPUT_BYTES.as_ptr(),
PUB_INPUT_BYTES.len() as u32,
);
assert_eq!(result, 0);
}
#[test]
fn valid_account_state_proof_with_empty_pub_input_does_not_verify() {
let result = verify_account_inclusion_ffi(
PROOF_BYTES.as_ptr(),
PROOF_BYTES.len() as u32,
PUB_INPUT_BYTES.as_ptr(),
0,
);
assert_eq!(result, 0);
}
}