-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprotocol.rs
More file actions
229 lines (207 loc) · 7.88 KB
/
protocol.rs
File metadata and controls
229 lines (207 loc) · 7.88 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
use multi_stark::{
p3_field::PrimeField64,
prover::Proof,
types::{CommitmentParameters, FriParameters},
};
use rustc_hash::{FxBuildHasher, FxHashMap};
use std::sync::LazyLock;
use lean_ffi::object::{
ExternalClass, LeanArray, LeanBorrowed, LeanByteArray, LeanCtor, LeanExcept,
LeanExternal, LeanNat, LeanOwned, LeanRef,
};
use crate::{
aiur::{
G,
execute::{IOBuffer, IOKeyInfo},
synthesis::AiurSystem,
},
ffi::aiur::{
lean_unbox_g, lean_unbox_nat_as_usize, toplevel::decode_toplevel,
},
lean::{
LeanAiurCommitmentParameters, LeanAiurFriParameters, LeanAiurToplevel,
},
};
// =============================================================================
// External class registration
// =============================================================================
static AIUR_PROOF_CLASS: LazyLock<ExternalClass> =
LazyLock::new(ExternalClass::register_with_drop::<Proof>);
static AIUR_SYSTEM_CLASS: LazyLock<ExternalClass> =
LazyLock::new(ExternalClass::register_with_drop::<AiurSystem>);
// =============================================================================
// Lean FFI functions
// =============================================================================
/// `Aiur.Proof.toBytes : @& Proof → ByteArray`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_proof_to_bytes(
proof_obj: LeanExternal<Proof, LeanBorrowed<'_>>,
) -> LeanByteArray<LeanOwned> {
let bytes = proof_obj.get().to_bytes().expect("Serialization error");
LeanByteArray::from_bytes(&bytes)
}
/// `Aiur.Proof.ofBytes : @& ByteArray → Proof`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_proof_of_bytes(
byte_array: LeanByteArray<LeanBorrowed<'_>>,
) -> LeanExternal<Proof, LeanOwned> {
let proof =
Proof::from_bytes(byte_array.as_bytes()).expect("Deserialization error");
LeanExternal::alloc(&AIUR_PROOF_CLASS, proof)
}
/// `AiurSystem.build : @&Bytecode.Toplevel → @&CommitmentParameters → AiurSystem`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_system_build(
toplevel: LeanAiurToplevel<LeanBorrowed<'_>>,
commitment_parameters: LeanAiurCommitmentParameters<LeanBorrowed<'_>>,
) -> LeanExternal<AiurSystem, LeanOwned> {
let system = AiurSystem::build(
decode_toplevel(&toplevel),
decode_commitment_parameters(&commitment_parameters),
);
LeanExternal::alloc(&AIUR_SYSTEM_CLASS, system)
}
/// `AiurSystem.verify : @& AiurSystem → @& FriParameters → @& Array G → @& Proof → Except String Unit`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_system_verify(
aiur_system_obj: LeanExternal<AiurSystem, LeanBorrowed<'_>>,
fri_parameters: LeanAiurFriParameters<LeanBorrowed<'_>>,
claim: LeanArray<LeanBorrowed<'_>>,
proof_obj: LeanExternal<Proof, LeanBorrowed<'_>>,
) -> LeanExcept<LeanOwned> {
let fri_parameters = decode_fri_parameters(&fri_parameters);
let claim = claim.map(|x| lean_unbox_g(&x));
match aiur_system_obj.get().verify(fri_parameters, &claim, proof_obj.get()) {
Ok(()) => LeanExcept::ok(LeanOwned::box_usize(0)),
Err(err) => LeanExcept::error_string(&format!("{err:?}")),
}
}
/// `Bytecode.Toplevel.execute`: runs execution only (no proof) and returns
/// `Array G × Array G × Array (Array G × IOKeyInfo)`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_toplevel_execute(
toplevel: LeanAiurToplevel<LeanBorrowed<'_>>,
fun_idx: LeanNat<LeanBorrowed<'_>>,
args: LeanArray<LeanBorrowed<'_>>,
io_data_arr: LeanArray<LeanBorrowed<'_>>,
io_map_arr: LeanArray<LeanBorrowed<'_>>,
) -> LeanOwned {
let toplevel = decode_toplevel(&toplevel);
let fun_idx = lean_unbox_nat_as_usize(fun_idx.inner());
let mut io_buffer = decode_io_buffer(&io_data_arr, &io_map_arr);
let (_query_record, output) =
toplevel.execute(fun_idx, args.map(|x| lean_unbox_g(&x)), &mut io_buffer);
let lean_io = build_lean_io_buffer(&io_buffer);
let result = LeanCtor::alloc(0, 2, 0);
result.set(0, build_g_array(&output));
result.set(1, lean_io);
result.into()
}
/// `AiurSystem.prove`: runs the prover and returns
/// `Array G × Proof × Array G × Array (Array G × IOKeyInfo)`
#[unsafe(no_mangle)]
extern "C" fn rs_aiur_system_prove(
aiur_system_obj: LeanExternal<AiurSystem, LeanBorrowed<'_>>,
fri_parameters: LeanAiurFriParameters<LeanBorrowed<'_>>,
fun_idx: LeanNat<LeanBorrowed<'_>>,
args: LeanArray<LeanBorrowed<'_>>,
io_data_arr: LeanArray<LeanBorrowed<'_>>,
io_map_arr: LeanArray<LeanBorrowed<'_>>,
) -> LeanOwned {
let fri_parameters = decode_fri_parameters(&fri_parameters);
let fun_idx = lean_unbox_nat_as_usize(fun_idx.inner());
let args = args.map(|x| lean_unbox_g(&x));
let mut io_buffer = decode_io_buffer(&io_data_arr, &io_map_arr);
let (claim, proof) =
aiur_system_obj.get().prove(fri_parameters, fun_idx, &args, &mut io_buffer);
let lean_proof: LeanOwned =
LeanExternal::alloc(&AIUR_PROOF_CLASS, proof).into();
let lean_io = build_lean_io_buffer(&io_buffer);
// Proof × Array G × Array (Array G × IOKeyInfo)
let proof_io_tuple = LeanCtor::alloc(0, 2, 0);
proof_io_tuple.set(0, lean_proof);
proof_io_tuple.set(1, lean_io);
// Array G × Proof × Array G × Array (Array G × IOKeyInfo)
let result = LeanCtor::alloc(0, 2, 0);
result.set(0, build_g_array(&claim));
result.set(1, proof_io_tuple);
result.into()
}
// =============================================================================
// Helpers
// =============================================================================
/// Build a Lean `Array G` from a slice of field elements.
fn build_g_array(values: &[G]) -> LeanArray<LeanOwned> {
let arr = LeanArray::alloc(values.len());
for (i, g) in values.iter().enumerate() {
arr.set(i, LeanOwned::box_u64(g.as_canonical_u64()));
}
arr
}
fn decode_io_buffer(
io_data_arr: &LeanArray<LeanBorrowed<'_>>,
io_map_arr: &LeanArray<LeanBorrowed<'_>>,
) -> IOBuffer {
let data = io_data_arr.map(|x| lean_unbox_g(&x));
let map = decode_io_buffer_map(io_map_arr);
IOBuffer { data, map }
}
/// Build a Lean `Array G × Array (Array G × IOKeyInfo)` from an `IOBuffer`.
fn build_lean_io_buffer(io_buffer: &IOBuffer) -> LeanOwned {
let lean_io_data = build_g_array(&io_buffer.data);
let lean_io_map = {
let arr = LeanArray::alloc(io_buffer.map.len());
for (i, (key, info)) in io_buffer.map.iter().enumerate() {
let key_arr = build_g_array(key);
let key_info = LeanCtor::alloc(0, 2, 0);
key_info.set(0, LeanOwned::box_usize(info.idx));
key_info.set(1, LeanOwned::box_usize(info.len));
let map_elt = LeanCtor::alloc(0, 2, 0);
map_elt.set(0, key_arr);
map_elt.set(1, key_info);
arr.set(i, map_elt);
}
arr
};
let io_tuple = LeanCtor::alloc(0, 2, 0);
io_tuple.set(0, lean_io_data);
io_tuple.set(1, lean_io_map);
io_tuple.into()
}
fn decode_commitment_parameters(
obj: &LeanAiurCommitmentParameters<impl LeanRef>,
) -> CommitmentParameters {
let ctor = obj.as_ctor();
CommitmentParameters {
log_blowup: lean_unbox_nat_as_usize(&ctor.get(0)),
cap_height: lean_unbox_nat_as_usize(&ctor.get(1)),
}
}
fn decode_fri_parameters(
obj: &LeanAiurFriParameters<impl LeanRef>,
) -> FriParameters {
let ctor = obj.as_ctor();
FriParameters {
log_final_poly_len: lean_unbox_nat_as_usize(&ctor.get(0)),
max_log_arity: lean_unbox_nat_as_usize(&ctor.get(1)),
num_queries: lean_unbox_nat_as_usize(&ctor.get(2)),
commit_proof_of_work_bits: lean_unbox_nat_as_usize(&ctor.get(3)),
query_proof_of_work_bits: lean_unbox_nat_as_usize(&ctor.get(4)),
}
}
fn decode_io_buffer_map(
arr: &LeanArray<LeanBorrowed<'_>>,
) -> FxHashMap<Vec<G>, IOKeyInfo> {
let mut map = FxHashMap::with_capacity_and_hasher(arr.len(), FxBuildHasher);
for elt in arr.iter() {
let pair = elt.as_ctor();
let key = pair.get(0).as_array().map(|x| lean_unbox_g(&x));
let info_ctor = pair.get(1).as_ctor();
let info = IOKeyInfo {
idx: lean_unbox_nat_as_usize(&info_ctor.get(0)),
len: lean_unbox_nat_as_usize(&info_ctor.get(1)),
};
map.insert(key, info);
}
map
}