Skip to content

Commit 1fce346

Browse files
committed
chore: Update lean-ffi
1 parent eac2616 commit 1fce346

10 files changed

Lines changed: 50 additions & 50 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ anyhow = "1"
1111
blake3 = "1.8.2"
1212
itertools = "0.14.0"
1313
indexmap = { version = "2", features = ["rayon"] }
14-
lean-ffi = { git = "https://github.com/argumentcomputer/lean-ffi", rev = "a94c426f0ce0b13ffdf7940e3e6368560628f2c9" }
14+
lean-ffi = { git = "https://github.com/argumentcomputer/lean-ffi", rev = "b194f3f17d1959562ef850527aa70bde43358086" }
1515
multi-stark = { git = "https://github.com/argumentcomputer/multi-stark.git", rev = "bdb0d7d66c02b554e66c449da2dbf12dc0dc27af" }
1616
num-bigint = "0.4.6"
1717
rayon = "1"

src/ffi/aiur/protocol.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use multi_stark::{
44
types::{CommitmentParameters, FriParameters},
55
};
66
use rustc_hash::{FxBuildHasher, FxHashMap};
7-
use std::sync::OnceLock;
7+
use std::sync::LazyLock;
88

99
use lean_ffi::object::{
1010
ExternalClass, LeanArray, LeanBorrowed, LeanByteArray, LeanCtor, LeanExcept,
@@ -26,19 +26,13 @@ use crate::{
2626
};
2727

2828
// =============================================================================
29-
// External class registration (OnceLock pattern)
29+
// External class registration
3030
// =============================================================================
3131

32-
static AIUR_PROOF_CLASS: OnceLock<ExternalClass> = OnceLock::new();
33-
static AIUR_SYSTEM_CLASS: OnceLock<ExternalClass> = OnceLock::new();
34-
35-
fn proof_class() -> &'static ExternalClass {
36-
AIUR_PROOF_CLASS.get_or_init(ExternalClass::register_with_drop::<Proof>)
37-
}
38-
39-
fn system_class() -> &'static ExternalClass {
40-
AIUR_SYSTEM_CLASS.get_or_init(ExternalClass::register_with_drop::<AiurSystem>)
41-
}
32+
static AIUR_PROOF_CLASS: LazyLock<ExternalClass> =
33+
LazyLock::new(ExternalClass::register_with_drop::<Proof>);
34+
static AIUR_SYSTEM_CLASS: LazyLock<ExternalClass> =
35+
LazyLock::new(ExternalClass::register_with_drop::<AiurSystem>);
4236

4337
// =============================================================================
4438
// Lean FFI functions
@@ -60,7 +54,7 @@ extern "C" fn rs_aiur_proof_of_bytes(
6054
) -> LeanExternal<Proof, LeanOwned> {
6155
let proof =
6256
Proof::from_bytes(byte_array.as_bytes()).expect("Deserialization error");
63-
LeanExternal::alloc(proof_class(), proof)
57+
LeanExternal::alloc(&AIUR_PROOF_CLASS, proof)
6458
}
6559

6660
/// `AiurSystem.build : @&Bytecode.Toplevel → @&CommitmentParameters → AiurSystem`
@@ -73,7 +67,7 @@ extern "C" fn rs_aiur_system_build(
7367
decode_toplevel(&toplevel),
7468
decode_commitment_parameters(&commitment_parameters),
7569
);
76-
LeanExternal::alloc(system_class(), system)
70+
LeanExternal::alloc(&AIUR_SYSTEM_CLASS, system)
7771
}
7872

7973
/// `AiurSystem.verify : @& AiurSystem → @& FriParameters → @& Array G → @& Proof → Except String Unit`
@@ -135,7 +129,7 @@ extern "C" fn rs_aiur_system_prove(
135129
let (claim, proof) =
136130
aiur_system_obj.get().prove(fri_parameters, fun_idx, &args, &mut io_buffer);
137131

138-
let lean_proof: LeanOwned = LeanExternal::alloc(proof_class(), proof).into();
132+
let lean_proof: LeanOwned = LeanExternal::alloc(&AIUR_PROOF_CLASS, proof).into();
139133
let lean_io = build_lean_io_buffer(&io_buffer);
140134
// Proof × Array G × Array (Array G × IOKeyInfo)
141135
let proof_io_tuple = LeanCtor::alloc(0, 2, 0);

src/ffi/ix/constant.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl LeanIxConstantInfo<LeanOwned> {
165165
let cnst_obj = LeanIxConstantVal::build(cache, &v.cnst);
166166
let axiom_val = LeanCtor::alloc(0, 1, 1);
167167
axiom_val.set(0, cnst_obj);
168-
axiom_val.set_u8(1, 0, v.is_unsafe as u8);
168+
axiom_val.set_bool(1, 0, v.is_unsafe);
169169

170170
let obj = LeanCtor::alloc(0, 1, 0);
171171
obj.set(0, axiom_val);
@@ -223,7 +223,7 @@ impl LeanIxConstantInfo<LeanOwned> {
223223
opaque_val.set(0, cnst_obj);
224224
opaque_val.set(1, value_obj);
225225
opaque_val.set(2, all_obj);
226-
opaque_val.set_u8(3, 0, v.is_unsafe as u8);
226+
opaque_val.set_bool(3, 0, v.is_unsafe);
227227

228228
let obj = LeanCtor::alloc(3, 1, 0);
229229
obj.set(0, opaque_val);
@@ -267,9 +267,9 @@ impl LeanIxConstantInfo<LeanOwned> {
267267
induct_val.set(3, all_obj);
268268
induct_val.set(4, ctors_obj);
269269
induct_val.set(5, num_nested_obj);
270-
induct_val.set_u8(6, 0, v.is_rec as u8);
271-
induct_val.set_u8(6, 1, v.is_unsafe as u8);
272-
induct_val.set_u8(6, 2, v.is_reflexive as u8);
270+
induct_val.set_bool(6, 0, v.is_rec);
271+
induct_val.set_bool(6, 1, v.is_unsafe);
272+
induct_val.set_bool(6, 2, v.is_reflexive);
273273

274274
let obj = LeanCtor::alloc(5, 1, 0);
275275
obj.set(0, induct_val);
@@ -291,7 +291,7 @@ impl LeanIxConstantInfo<LeanOwned> {
291291
ctor_val.set(2, cidx_obj);
292292
ctor_val.set(3, num_params_obj);
293293
ctor_val.set(4, num_fields_obj);
294-
ctor_val.set_u8(5, 0, v.is_unsafe as u8);
294+
ctor_val.set_bool(5, 0, v.is_unsafe);
295295

296296
let obj = LeanCtor::alloc(6, 1, 0);
297297
obj.set(0, ctor_val);
@@ -317,8 +317,8 @@ impl LeanIxConstantInfo<LeanOwned> {
317317
rec_val.set(4, num_motives_obj);
318318
rec_val.set(5, num_minors_obj);
319319
rec_val.set(6, rules_obj);
320-
rec_val.set_u8(7, 0, v.k as u8);
321-
rec_val.set_u8(7, 1, v.is_unsafe as u8);
320+
rec_val.set_bool(7, 0, v.k);
321+
rec_val.set_bool(7, 1, v.is_unsafe);
322322

323323
let obj = LeanCtor::alloc(7, 1, 0);
324324
obj.set(0, rec_val);

src/ffi/ix/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl LeanIxSourceInfo<LeanOwned> {
8787
let obj = LeanCtor::alloc(1, 2, 1);
8888
obj.set(0, Nat::to_lean(pos));
8989
obj.set(1, Nat::to_lean(end_pos));
90-
obj.set_u8(2, 0, *canonical as u8);
90+
obj.set_bool(2, 0, *canonical);
9191
Self::new(obj.into())
9292
},
9393
// | none -- tag 2
@@ -299,7 +299,7 @@ impl LeanIxDataValue<LeanOwned> {
299299
DataValue::OfBool(b) => {
300300
// 0 object fields, 1 scalar byte
301301
let obj = LeanCtor::alloc(1, 0, 1);
302-
obj.set_u8(0, 0, *b as u8);
302+
obj.set_bool(0, 0, *b);
303303
Self::new(obj.into())
304304
},
305305
DataValue::OfName(n) => {

src/ffi/ix/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl LeanIxExpr<LeanOwned> {
120120
ctor.set(2, val_obj);
121121
ctor.set(3, body_obj);
122122
ctor.set(4, hash_obj);
123-
ctor.set_u8(5, 0, *non_dep as u8);
123+
ctor.set_bool(5, 0, *non_dep);
124124
Self::new(ctor.into())
125125
},
126126
ExprData::Lit(lit, h) => {

src/ffi/ixon/constant.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ impl LeanIxonRecursor<LeanOwned> {
136136
ctor.set_u64(2, 16, rec.indices);
137137
ctor.set_u64(2, 24, rec.motives);
138138
ctor.set_u64(2, 32, rec.minors);
139-
ctor.set_u8(2, 40, if rec.k { 1 } else { 0 });
140-
ctor.set_u8(2, 41, if rec.is_unsafe { 1 } else { 0 });
139+
ctor.set_bool(2, 40, rec.k);
140+
ctor.set_bool(2, 41, rec.is_unsafe);
141141
Self::new(ctor.into())
142142
}
143143
}
@@ -186,7 +186,7 @@ impl LeanIxonAxiom<LeanOwned> {
186186
ctor.set(0, typ_obj);
187187
// Scalar offsets from obj_cptr: 1*8=8 base
188188
ctor.set_u64(1, 0, ax.lvls);
189-
ctor.set_u8(1, 8, if ax.is_unsafe { 1 } else { 0 });
189+
ctor.set_bool(1, 8, ax.is_unsafe);
190190
Self::new(ctor.into())
191191
}
192192
}
@@ -265,7 +265,7 @@ impl LeanIxonConstructor<LeanOwned> {
265265
ctor.set_u64(1, 8, c.cidx);
266266
ctor.set_u64(1, 16, c.params);
267267
ctor.set_u64(1, 24, c.fields);
268-
ctor.set_u8(1, 32, if c.is_unsafe { 1 } else { 0 });
268+
ctor.set_bool(1, 32, c.is_unsafe);
269269
Self::new(ctor.into())
270270
}
271271
}
@@ -308,9 +308,9 @@ impl LeanIxonInductive<LeanOwned> {
308308
ctor.set_u64(2, 8, ind.params);
309309
ctor.set_u64(2, 16, ind.indices);
310310
ctor.set_u64(2, 24, ind.nested);
311-
ctor.set_u8(2, 32, if ind.recr { 1 } else { 0 });
312-
ctor.set_u8(2, 33, if ind.refl { 1 } else { 0 });
313-
ctor.set_u8(2, 34, if ind.is_unsafe { 1 } else { 0 });
311+
ctor.set_bool(2, 32, ind.recr);
312+
ctor.set_bool(2, 33, ind.refl);
313+
ctor.set_bool(2, 34, ind.is_unsafe);
314314
Self::new(ctor.into())
315315
}
316316
}

src/ffi/ixon/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl LeanIxonExpr<LeanOwned> {
109109
ctor.set(0, ty_obj);
110110
ctor.set(1, val_obj);
111111
ctor.set(2, body_obj);
112-
ctor.set_u8(3, 0, if *non_dep { 1 } else { 0 });
112+
ctor.set_bool(3, 0, *non_dep);
113113
ctor.into()
114114
},
115115
IxonExpr::Share(idx) => {

src/ffi/ixon/meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl LeanIxonDataValue<LeanOwned> {
101101
},
102102
IxonDataValue::OfBool(b) => {
103103
let ctor = LeanCtor::alloc(1, 0, 1);
104-
ctor.set_u8(0, 0, if *b { 1 } else { 0 });
104+
ctor.set_bool(0, 0, *b);
105105
ctor.into()
106106
},
107107
IxonDataValue::OfName(addr) => {

src/ffi/keccak.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
1-
use std::sync::OnceLock;
1+
use std::sync::LazyLock;
22

33
use tiny_keccak::{Hasher, Keccak};
44

55
use lean_ffi::object::{
66
ExternalClass, LeanBorrowed, LeanByteArray, LeanExternal, LeanOwned,
77
};
88

9-
static KECCAK_CLASS: OnceLock<ExternalClass> = OnceLock::new();
10-
11-
fn keccak_class() -> &'static ExternalClass {
12-
KECCAK_CLASS.get_or_init(ExternalClass::register_with_drop::<Keccak>)
13-
}
9+
static KECCAK_CLASS: LazyLock<ExternalClass> =
10+
LazyLock::new(ExternalClass::register_with_drop::<Keccak>);
1411

1512
/// `Keccak.Hasher.init : Unit → Hasher`
1613
#[unsafe(no_mangle)]
1714
extern "C" fn rs_keccak256_hasher_init(
1815
_unit: LeanOwned,
1916
) -> LeanExternal<Keccak, LeanOwned> {
20-
LeanExternal::alloc(keccak_class(), Keccak::v256())
17+
LeanExternal::alloc(&KECCAK_CLASS, Keccak::v256())
2118
}
2219

2320
/// `Keccak.Hasher.update : (hasher: Hasher) → (input: @& ByteArray) → Hasher`
2421
#[unsafe(no_mangle)]
2522
extern "C" fn rs_keccak256_hasher_update(
26-
hasher: LeanExternal<Keccak, LeanOwned>,
23+
mut hasher: LeanExternal<Keccak, LeanOwned>,
2724
input: LeanByteArray<LeanBorrowed<'_>>,
2825
) -> LeanExternal<Keccak, LeanOwned> {
29-
let mut new_hasher = hasher.get().clone();
30-
new_hasher.update(input.as_bytes());
31-
LeanExternal::alloc(keccak_class(), new_hasher)
26+
if let Some(h) = hasher.get_mut() {
27+
h.update(input.as_bytes());
28+
hasher
29+
} else {
30+
let mut new_hasher = hasher.get().clone();
31+
new_hasher.update(input.as_bytes());
32+
LeanExternal::alloc(&KECCAK_CLASS, new_hasher)
33+
}
3234
}
3335

3436
/// `Keccak.Hasher.finalize : (hasher: Hasher) → ByteArray`
3537
#[unsafe(no_mangle)]
3638
extern "C" fn rs_keccak256_hasher_finalize(
37-
hasher: LeanExternal<Keccak, LeanOwned>,
39+
mut hasher: LeanExternal<Keccak, LeanOwned>,
3840
) -> LeanByteArray<LeanOwned> {
3941
let mut data = [0u8; 32];
40-
hasher.get().clone().finalize(&mut data);
42+
if let Some(h) = hasher.get_mut() {
43+
std::mem::replace(h, Keccak::v256()).finalize(&mut data);
44+
} else {
45+
hasher.get().clone().finalize(&mut data);
46+
}
4147
LeanByteArray::from_bytes(&data)
4248
}

0 commit comments

Comments
 (0)