Skip to content

Commit 7aa8cbf

Browse files
Add lazy setup for edwards curves
1 parent 68909ce commit 7aa8cbf

File tree

1 file changed

+24
-53
lines changed
  • extensions/ecc/te-macros/src

1 file changed

+24
-53
lines changed

extensions/ecc/te-macros/src/lib.rs

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,14 @@ pub fn te_declare(input: TokenStream) -> TokenStream {
7979
};
8080
}
8181
create_extern_func!(te_add_extern_func);
82-
create_extern_func!(te_hint_decompress_extern_func);
83-
create_extern_func!(hint_non_qr_extern_func);
82+
create_extern_func!(te_setup_extern_func);
8483

8584
let group_ops_mod_name = format_ident!("{}_ops", struct_name.to_string().to_lowercase());
8685

8786
let result = TokenStream::from(quote::quote_spanned! { span.into() =>
8887
extern "C" {
8988
fn #te_add_extern_func(rd: usize, rs1: usize, rs2: usize);
90-
fn #te_hint_decompress_extern_func(rs1: usize, rs2: usize);
91-
fn #hint_non_qr_extern_func();
89+
fn #te_setup_extern_func();
9290
}
9391

9492
#[derive(Eq, PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)]
@@ -126,6 +124,7 @@ pub fn te_declare(input: TokenStream) -> TokenStream {
126124
}
127125
#[cfg(target_os = "zkvm")]
128126
{
127+
Self::set_up_once();
129128
let mut uninit: core::mem::MaybeUninit<#struct_name> = core::mem::MaybeUninit::uninit();
130129
unsafe {
131130
#te_add_extern_func(
@@ -137,6 +136,22 @@ pub fn te_declare(input: TokenStream) -> TokenStream {
137136
unsafe { uninit.assume_init() }
138137
}
139138
}
139+
140+
// Helper function to call the setup instruction on first use
141+
#[cfg(target_os = "zkvm")]
142+
fn set_up_once() {
143+
static is_setup: ::openvm_ecc_guest::once_cell::race::OnceBool = ::openvm_ecc_guest::once_cell::race::OnceBool::new();
144+
is_setup.get_or_init(|| {
145+
unsafe { #te_setup_extern_func(); }
146+
<#intmod_type as openvm_algebra_guest::IntMod>::set_up_once();
147+
true
148+
});
149+
}
150+
151+
#[cfg(not(target_os = "zkvm"))]
152+
fn set_up_once() {
153+
// No-op for non-ZKVM targets
154+
}
140155
}
141156

142157
impl ::openvm_ecc_guest::edwards::TwistedEdwardsPoint for #struct_name {
@@ -263,8 +278,6 @@ pub fn te_init(input: TokenStream) -> TokenStream {
263278
let TeDefine { items } = parse_macro_input!(input as TeDefine);
264279

265280
let mut externs = Vec::new();
266-
let mut setups = Vec::new();
267-
let mut setup_all_te_curves = Vec::new();
268281

269282
let span = proc_macro::Span::call_site();
270283

@@ -277,14 +290,8 @@ pub fn te_init(input: TokenStream) -> TokenStream {
277290
.join("_");
278291
let add_extern_func =
279292
syn::Ident::new(&format!("te_add_extern_func_{}", str_path), span.into());
280-
let te_hint_decompress_extern_func = syn::Ident::new(
281-
&format!("te_hint_decompress_extern_func_{}", str_path),
282-
span.into(),
283-
);
284-
let hint_non_qr_extern_func = syn::Ident::new(
285-
&format!("hint_non_qr_extern_func_{}", str_path),
286-
span.into(),
287-
);
293+
let setup_extern_func =
294+
syn::Ident::new(&format!("te_setup_extern_func_{}", str_path), span.into());
288295
externs.push(quote::quote_spanned! { span.into() =>
289296
#[no_mangle]
290297
extern "C" fn #add_extern_func(rd: usize, rs1: usize, rs2: usize) {
@@ -300,39 +307,10 @@ pub fn te_init(input: TokenStream) -> TokenStream {
300307
}
301308

302309
#[no_mangle]
303-
extern "C" fn #te_hint_decompress_extern_func(rs1: usize, rs2: usize) {
304-
openvm::platform::custom_insn_r!(
305-
opcode = TE_OPCODE,
306-
funct3 = TE_FUNCT3 as usize,
307-
funct7 = TeBaseFunct7::TeHintDecompress as usize + #ec_idx
308-
* (TeBaseFunct7::TWISTED_EDWARDS_MAX_KINDS as usize),
309-
rd = Const "x0",
310-
rs1 = In rs1,
311-
rs2 = In rs2
312-
);
313-
}
314-
315-
#[no_mangle]
316-
extern "C" fn #hint_non_qr_extern_func() {
317-
openvm::platform::custom_insn_r!(
318-
opcode = TE_OPCODE,
319-
funct3 = TE_FUNCT3 as usize,
320-
funct7 = TeBaseFunct7::TeHintNonQr as usize + #ec_idx
321-
* (TeBaseFunct7::TWISTED_EDWARDS_MAX_KINDS as usize),
322-
rd = Const "x0",
323-
rs1 = Const "x0",
324-
rs2 = Const "x0"
325-
);
326-
}
327-
});
328-
329-
let setup_function = syn::Ident::new(&format!("setup_te_{}", str_path), span.into());
330-
setups.push(quote::quote_spanned! { span.into() =>
331-
332-
#[allow(non_snake_case)]
333-
pub fn #setup_function() {
310+
extern "C" fn #setup_extern_func() {
334311
#[cfg(target_os = "zkvm")]
335312
{
313+
use super::#item;
336314
let modulus_bytes = <<#item as openvm_ecc_guest::edwards::TwistedEdwardsPoint>::Coordinate as openvm_algebra_guest::IntMod>::MODULUS;
337315
let mut zero = [0u8; <<#item as openvm_ecc_guest::edwards::TwistedEdwardsPoint>::Coordinate as openvm_algebra_guest::IntMod>::NUM_LIMBS];
338316
let curve_a_bytes = openvm_algebra_guest::IntMod::as_le_bytes(&<#item as openvm_ecc_guest::edwards::TwistedEdwardsPoint>::CURVE_A);
@@ -353,22 +331,15 @@ pub fn te_init(input: TokenStream) -> TokenStream {
353331
}
354332
}
355333
});
356-
357-
setup_all_te_curves.push(quote::quote_spanned! { span.into() =>
358-
#setup_function();
359-
});
360334
}
361335

362336
TokenStream::from(quote::quote_spanned! { span.into() =>
337+
#[allow(non_snake_case)]
363338
#[cfg(target_os = "zkvm")]
364339
mod openvm_intrinsics_ffi_2_te {
365340
use ::openvm_ecc_guest::{TE_OPCODE, TE_FUNCT3, TeBaseFunct7};
366341

367342
#(#externs)*
368343
}
369-
#(#setups)*
370-
pub fn setup_all_te_curves() {
371-
#(#setup_all_te_curves)*
372-
}
373344
})
374345
}

0 commit comments

Comments
 (0)