From ee4aba08e6f736b37196664a2caac958d7c36de0 Mon Sep 17 00:00:00 2001 From: Laurentiu Ciobanu Date: Tue, 12 Aug 2025 12:20:52 +0300 Subject: [PATCH 1/2] unsafe mode hooks --- c-api/libvmexeccapi.h | 5 ++++ c-api/src/capi_vm_hook_pointers.rs | 5 ++++ c-api/src/capi_vm_hooks.rs | 20 +++++++++++++ vm-executor-experimental/src/we_imports.rs | 30 +++++++++++++++++++ vm-executor-wasmer/src/wasmer_imports.rs | 30 +++++++++++++++++++ .../src/new_traits/vm_hooks_legacy_adapter.rs | 20 +++++++++++++ vm-executor/src/new_traits/vm_hooks_new.rs | 30 +++++++++++++++++++ vm-executor/src/vm_hooks.rs | 26 ++++++++++++++++ 8 files changed, 166 insertions(+) diff --git a/c-api/libvmexeccapi.h b/c-api/libvmexeccapi.h index 14ab427..bd83b54 100644 --- a/c-api/libvmexeccapi.h +++ b/c-api/libvmexeccapi.h @@ -315,6 +315,11 @@ typedef struct { int32_t (*managed_verify_secp256r1_func_ptr)(void *context, int32_t key_handle, int32_t message_handle, int32_t sig_handle); int32_t (*managed_verify_blssignature_share_func_ptr)(void *context, int32_t key_handle, int32_t message_handle, int32_t sig_handle); int32_t (*managed_verify_blsaggregated_signature_func_ptr)(void *context, int32_t key_handle, int32_t message_handle, int32_t sig_handle); + void (*activate_unsafe_mode_func_ptr)(void *context); + void (*deactivate_unsafe_mode_func_ptr)(void *context); + int32_t (*managed_get_num_errors_func_ptr)(void *context); + void (*managed_get_error_with_index_func_ptr)(void *context, int32_t index, int32_t error_handle); + void (*managed_get_last_error_func_ptr)(void *context, int32_t error_handle); } vm_exec_vm_hook_c_func_pointers; typedef struct { diff --git a/c-api/src/capi_vm_hook_pointers.rs b/c-api/src/capi_vm_hook_pointers.rs index 3029000..1200dae 100644 --- a/c-api/src/capi_vm_hook_pointers.rs +++ b/c-api/src/capi_vm_hook_pointers.rs @@ -288,6 +288,11 @@ pub struct vm_exec_vm_hook_c_func_pointers { pub managed_verify_secp256r1_func_ptr: extern "C" fn(context: *mut c_void, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32, pub managed_verify_blssignature_share_func_ptr: extern "C" fn(context: *mut c_void, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32, pub managed_verify_blsaggregated_signature_func_ptr: extern "C" fn(context: *mut c_void, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32, + pub activate_unsafe_mode_func_ptr: extern "C" fn(context: *mut c_void), + pub deactivate_unsafe_mode_func_ptr: extern "C" fn(context: *mut c_void), + pub managed_get_num_errors_func_ptr: extern "C" fn(context: *mut c_void) -> i32, + pub managed_get_error_with_index_func_ptr: extern "C" fn(context: *mut c_void, index: i32, error_handle: i32), + pub managed_get_last_error_func_ptr: extern "C" fn(context: *mut c_void, error_handle: i32), } impl std::fmt::Debug for vm_exec_vm_hook_c_func_pointers { diff --git a/c-api/src/capi_vm_hooks.rs b/c-api/src/capi_vm_hooks.rs index f1a4295..5936bd3 100644 --- a/c-api/src/capi_vm_hooks.rs +++ b/c-api/src/capi_vm_hooks.rs @@ -1146,4 +1146,24 @@ impl multiversx_chain_vm_executor::VMHooksLegacy for CapiVMHooks { fn managed_verify_blsaggregated_signature(&self, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32 { (self.c_func_pointers_ptr.managed_verify_blsaggregated_signature_func_ptr)(self.vm_hooks_ptr, key_handle, message_handle, sig_handle) } + + fn activate_unsafe_mode(&self) { + (self.c_func_pointers_ptr.activate_unsafe_mode_func_ptr)(self.vm_hooks_ptr) + } + + fn deactivate_unsafe_mode(&self) { + (self.c_func_pointers_ptr.deactivate_unsafe_mode_func_ptr)(self.vm_hooks_ptr) + } + + fn managed_get_num_errors(&self) -> i32 { + (self.c_func_pointers_ptr.managed_get_num_errors_func_ptr)(self.vm_hooks_ptr) + } + + fn managed_get_error_with_index(&self, index: i32, error_handle: i32) { + (self.c_func_pointers_ptr.managed_get_error_with_index_func_ptr)(self.vm_hooks_ptr, index, error_handle) + } + + fn managed_get_last_error(&self, error_handle: i32) { + (self.c_func_pointers_ptr.managed_get_last_error_func_ptr)(self.vm_hooks_ptr, error_handle) + } } diff --git a/vm-executor-experimental/src/we_imports.rs b/vm-executor-experimental/src/we_imports.rs index 39d3f28..78a843e 100644 --- a/vm-executor-experimental/src/we_imports.rs +++ b/vm-executor-experimental/src/we_imports.rs @@ -1396,6 +1396,31 @@ fn wasmer_import_managed_verify_blsaggregated_signature(env: FunctionEnvMut) -> Result<(), VMHooksEarlyExit> { + with_vm_hooks(env, |vh| vh.activate_unsafe_mode()) +} + +#[rustfmt::skip] +fn wasmer_import_deactivate_unsafe_mode(env: FunctionEnvMut) -> Result<(), VMHooksEarlyExit> { + with_vm_hooks(env, |vh| vh.deactivate_unsafe_mode()) +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_num_errors(env: FunctionEnvMut) -> Result { + with_vm_hooks(env, |vh| vh.managed_get_num_errors()) +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_error_with_index(env: FunctionEnvMut, index: i32, error_handle: i32) -> Result<(), VMHooksEarlyExit> { + with_vm_hooks(env, |vh| vh.managed_get_error_with_index(index, error_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_last_error(env: FunctionEnvMut, error_handle: i32) -> Result<(), VMHooksEarlyExit> { + with_vm_hooks(env, |vh| vh.managed_get_last_error(error_handle)) +} + pub fn generate_import_object(store: &mut Store, vh_wrapper: VMHooksWrapper) -> Imports { let function_env = FunctionEnv::new(store, vh_wrapper); @@ -1678,6 +1703,11 @@ pub fn generate_import_object(store: &mut Store, vh_wrapper: VMHooksWrapper) -> "managedVerifySecp256r1" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_verify_secp256r1), "managedVerifyBLSSignatureShare" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_verify_blssignature_share), "managedVerifyBLSAggregatedSignature" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_verify_blsaggregated_signature), + "activateUnsafeMode" => Function::new_typed_with_env(store, &function_env, wasmer_import_activate_unsafe_mode), + "deactivateUnsafeMode" => Function::new_typed_with_env(store, &function_env, wasmer_import_deactivate_unsafe_mode), + "managedGetNumErrors" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_num_errors), + "managedGetErrorWithIndex" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_error_with_index), + "managedGetLastError" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_last_error), } } diff --git a/vm-executor-wasmer/src/wasmer_imports.rs b/vm-executor-wasmer/src/wasmer_imports.rs index 7e41d64..0a7a6ab 100644 --- a/vm-executor-wasmer/src/wasmer_imports.rs +++ b/vm-executor-wasmer/src/wasmer_imports.rs @@ -1395,6 +1395,31 @@ fn wasmer_import_managed_verify_blsaggregated_signature(env: &VMHooksWrapper, ke env.vm_hooks.managed_verify_blsaggregated_signature(key_handle, message_handle, sig_handle) } +#[rustfmt::skip] +fn wasmer_import_activate_unsafe_mode(env: &VMHooksWrapper) { + env.vm_hooks.activate_unsafe_mode() +} + +#[rustfmt::skip] +fn wasmer_import_deactivate_unsafe_mode(env: &VMHooksWrapper) { + env.vm_hooks.deactivate_unsafe_mode() +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_num_errors(env: &VMHooksWrapper) -> i32 { + env.vm_hooks.managed_get_num_errors() +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_error_with_index(env: &VMHooksWrapper, index: i32, error_handle: i32) { + env.vm_hooks.managed_get_error_with_index(index, error_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_get_last_error(env: &VMHooksWrapper, error_handle: i32) { + env.vm_hooks.managed_get_last_error(error_handle) +} + pub fn generate_import_object(store: &Store, env: &VMHooksWrapper) -> ImportObject { imports! { "env" => { @@ -1675,6 +1700,11 @@ pub fn generate_import_object(store: &Store, env: &VMHooksWrapper) -> ImportObje "managedVerifySecp256r1" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_verify_secp256r1), "managedVerifyBLSSignatureShare" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_verify_blssignature_share), "managedVerifyBLSAggregatedSignature" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_verify_blsaggregated_signature), + "activateUnsafeMode" => Function::new_native_with_env(store, env.clone(), wasmer_import_activate_unsafe_mode), + "deactivateUnsafeMode" => Function::new_native_with_env(store, env.clone(), wasmer_import_deactivate_unsafe_mode), + "managedGetNumErrors" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_num_errors), + "managedGetErrorWithIndex" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_error_with_index), + "managedGetLastError" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_last_error), } } diff --git a/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs b/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs index 1637554..c79d9b6 100644 --- a/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs +++ b/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs @@ -1154,4 +1154,24 @@ impl VMHooksLegacy for VMHooksLegacyAdapter { fn managed_verify_blsaggregated_signature(&self, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32 { self.adapt_vm_hooks(|inner| VMHooks::managed_verify_blsaggregated_signature(inner, key_handle, message_handle, sig_handle)) } + + fn activate_unsafe_mode(&self) { + self.adapt_vm_hooks(|inner| VMHooks::activate_unsafe_mode(inner)) + } + + fn deactivate_unsafe_mode(&self) { + self.adapt_vm_hooks(|inner| VMHooks::deactivate_unsafe_mode(inner)) + } + + fn managed_get_num_errors(&self) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_get_num_errors(inner)) + } + + fn managed_get_error_with_index(&self, index: i32, error_handle: i32) { + self.adapt_vm_hooks(|inner| VMHooks::managed_get_error_with_index(inner, index, error_handle)) + } + + fn managed_get_last_error(&self, error_handle: i32) { + self.adapt_vm_hooks(|inner| VMHooks::managed_get_last_error(inner, error_handle)) + } } diff --git a/vm-executor/src/new_traits/vm_hooks_new.rs b/vm-executor/src/new_traits/vm_hooks_new.rs index 0bd5410..26a6aa5 100644 --- a/vm-executor/src/new_traits/vm_hooks_new.rs +++ b/vm-executor/src/new_traits/vm_hooks_new.rs @@ -286,6 +286,11 @@ pub trait VMHooks: core::fmt::Debug { fn managed_verify_secp256r1(&mut self, key_handle: i32, message_handle: i32, sig_handle: i32) -> Result; fn managed_verify_blssignature_share(&mut self, key_handle: i32, message_handle: i32, sig_handle: i32) -> Result; fn managed_verify_blsaggregated_signature(&mut self, key_handle: i32, message_handle: i32, sig_handle: i32) -> Result; + fn activate_unsafe_mode(&mut self) -> Result<(), VMHooksEarlyExit>; + fn deactivate_unsafe_mode(&mut self) -> Result<(), VMHooksEarlyExit>; + fn managed_get_num_errors(&mut self) -> Result; + fn managed_get_error_with_index(&mut self, index: i32, error_handle: i32) -> Result<(), VMHooksEarlyExit>; + fn managed_get_last_error(&mut self, error_handle: i32) -> Result<(), VMHooksEarlyExit>; } /// Dummy implementation for VMHooks. Can be used as placeholder, or in tests. @@ -1679,4 +1684,29 @@ impl VMHooks for VMHooksDefault { println!("Called: managed_verify_blsaggregated_signature"); Ok(0) } + + fn activate_unsafe_mode(&mut self) -> Result<(), VMHooksEarlyExit> { + println!("Called: activate_unsafe_mode"); + Ok(()) + } + + fn deactivate_unsafe_mode(&mut self) -> Result<(), VMHooksEarlyExit> { + println!("Called: deactivate_unsafe_mode"); + Ok(()) + } + + fn managed_get_num_errors(&mut self) -> Result { + println!("Called: managed_get_num_errors"); + Ok(0) + } + + fn managed_get_error_with_index(&mut self, index: i32, error_handle: i32) -> Result<(), VMHooksEarlyExit> { + println!("Called: managed_get_error_with_index"); + Ok(()) + } + + fn managed_get_last_error(&mut self, error_handle: i32) -> Result<(), VMHooksEarlyExit> { + println!("Called: managed_get_last_error"); + Ok(()) + } } diff --git a/vm-executor/src/vm_hooks.rs b/vm-executor/src/vm_hooks.rs index 6cce8fb..07f0dd7 100644 --- a/vm-executor/src/vm_hooks.rs +++ b/vm-executor/src/vm_hooks.rs @@ -290,6 +290,11 @@ pub trait VMHooksLegacy: core::fmt::Debug { fn managed_verify_secp256r1(&self, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32; fn managed_verify_blssignature_share(&self, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32; fn managed_verify_blsaggregated_signature(&self, key_handle: i32, message_handle: i32, sig_handle: i32) -> i32; + fn activate_unsafe_mode(&self); + fn deactivate_unsafe_mode(&self); + fn managed_get_num_errors(&self) -> i32; + fn managed_get_error_with_index(&self, index: i32, error_handle: i32); + fn managed_get_last_error(&self, error_handle: i32); } /// Dummy implementation for VMHooks. Can be used as placeholder, or in tests. @@ -1585,4 +1590,25 @@ impl VMHooksLegacy for VMHooksLegacyDefault { println!("Called: managed_verify_blsaggregated_signature"); 0 } + + fn activate_unsafe_mode(&self) { + println!("Called: activate_unsafe_mode"); + } + + fn deactivate_unsafe_mode(&self) { + println!("Called: deactivate_unsafe_mode"); + } + + fn managed_get_num_errors(&self) -> i32 { + println!("Called: managed_get_num_errors"); + 0 + } + + fn managed_get_error_with_index(&self, index: i32, error_handle: i32) { + println!("Called: managed_get_error_with_index"); + } + + fn managed_get_last_error(&self, error_handle: i32) { + println!("Called: managed_get_last_error"); + } } From 5292b717fdf2886179d11711f3d910b458dc8f21 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Thu, 21 Aug 2025 13:41:38 +0300 Subject: [PATCH 2/2] ZK crypto hooks --- c-api/src/capi_vm_hook_pointers.rs | 7 ++++ c-api/src/capi_vm_hooks.rs | 28 +++++++++++++ vm-executor-experimental/src/we_imports.rs | 42 +++++++++++++++++++ vm-executor-wasmer/src/wasmer_imports.rs | 42 +++++++++++++++++++ .../src/new_traits/vm_hooks_legacy_adapter.rs | 28 +++++++++++++ vm-executor/src/new_traits/vm_hooks_new.rs | 42 +++++++++++++++++++ vm-executor/src/vm_hooks.rs | 42 +++++++++++++++++++ 7 files changed, 231 insertions(+) diff --git a/c-api/src/capi_vm_hook_pointers.rs b/c-api/src/capi_vm_hook_pointers.rs index 1200dae..0f423c3 100644 --- a/c-api/src/capi_vm_hook_pointers.rs +++ b/c-api/src/capi_vm_hook_pointers.rs @@ -293,6 +293,13 @@ pub struct vm_exec_vm_hook_c_func_pointers { pub managed_get_num_errors_func_ptr: extern "C" fn(context: *mut c_void) -> i32, pub managed_get_error_with_index_func_ptr: extern "C" fn(context: *mut c_void, index: i32, error_handle: i32), pub managed_get_last_error_func_ptr: extern "C" fn(context: *mut c_void, error_handle: i32), + pub managed_verify_groth16_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32, + pub managed_verify_plonk_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32, + pub managed_add_ec_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32, + pub managed_mul_ec_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32, + pub managed_multi_exp_ec_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32, + pub managed_map_to_curve_ec_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32, + pub managed_pairing_checks_ec_func_ptr: extern "C" fn(context: *mut c_void, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32, } impl std::fmt::Debug for vm_exec_vm_hook_c_func_pointers { diff --git a/c-api/src/capi_vm_hooks.rs b/c-api/src/capi_vm_hooks.rs index 5936bd3..6339af6 100644 --- a/c-api/src/capi_vm_hooks.rs +++ b/c-api/src/capi_vm_hooks.rs @@ -1166,4 +1166,32 @@ impl multiversx_chain_vm_executor::VMHooksLegacy for CapiVMHooks { fn managed_get_last_error(&self, error_handle: i32) { (self.c_func_pointers_ptr.managed_get_last_error_func_ptr)(self.vm_hooks_ptr, error_handle) } + + fn managed_verify_groth16(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_verify_groth16_func_ptr)(self.vm_hooks_ptr, curve_id, proof_handle, vk_handle, pub_witness_handle) + } + + fn managed_verify_plonk(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_verify_plonk_func_ptr)(self.vm_hooks_ptr, curve_id, proof_handle, vk_handle, pub_witness_handle) + } + + fn managed_add_ec(&self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_add_ec_func_ptr)(self.vm_hooks_ptr, curve_id, group_id, point1_handle, point2_handle, result_handle) + } + + fn managed_mul_ec(&self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_mul_ec_func_ptr)(self.vm_hooks_ptr, curve_id, group_id, point_handle, scalar_handle, result_handle) + } + + fn managed_multi_exp_ec(&self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_multi_exp_ec_func_ptr)(self.vm_hooks_ptr, curve_id, group_id, points_handle, scalars_handle, result_handle) + } + + fn managed_map_to_curve_ec(&self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_map_to_curve_ec_func_ptr)(self.vm_hooks_ptr, curve_id, group_id, element_handle, result_handle) + } + + fn managed_pairing_checks_ec(&self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32 { + (self.c_func_pointers_ptr.managed_pairing_checks_ec_func_ptr)(self.vm_hooks_ptr, curve_id, points_g1_handle, points_g2_handle) + } } diff --git a/vm-executor-experimental/src/we_imports.rs b/vm-executor-experimental/src/we_imports.rs index 78a843e..f43f4dd 100644 --- a/vm-executor-experimental/src/we_imports.rs +++ b/vm-executor-experimental/src/we_imports.rs @@ -1421,6 +1421,41 @@ fn wasmer_import_managed_get_last_error(env: FunctionEnvMut, err with_vm_hooks(env, |vh| vh.managed_get_last_error(error_handle)) } +#[rustfmt::skip] +fn wasmer_import_managed_verify_groth16(env: FunctionEnvMut, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_verify_groth16(curve_id, proof_handle, vk_handle, pub_witness_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_verify_plonk(env: FunctionEnvMut, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_verify_plonk(curve_id, proof_handle, vk_handle, pub_witness_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_add_ec(env: FunctionEnvMut, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_add_ec(curve_id, group_id, point1_handle, point2_handle, result_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_mul_ec(env: FunctionEnvMut, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_mul_ec(curve_id, group_id, point_handle, scalar_handle, result_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_multi_exp_ec(env: FunctionEnvMut, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_multi_exp_ec(curve_id, group_id, points_handle, scalars_handle, result_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_map_to_curve_ec(env: FunctionEnvMut, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_map_to_curve_ec(curve_id, group_id, element_handle, result_handle)) +} + +#[rustfmt::skip] +fn wasmer_import_managed_pairing_checks_ec(env: FunctionEnvMut, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> Result { + with_vm_hooks(env, |vh| vh.managed_pairing_checks_ec(curve_id, points_g1_handle, points_g2_handle)) +} + pub fn generate_import_object(store: &mut Store, vh_wrapper: VMHooksWrapper) -> Imports { let function_env = FunctionEnv::new(store, vh_wrapper); @@ -1708,6 +1743,13 @@ pub fn generate_import_object(store: &mut Store, vh_wrapper: VMHooksWrapper) -> "managedGetNumErrors" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_num_errors), "managedGetErrorWithIndex" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_error_with_index), "managedGetLastError" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_get_last_error), + "managedVerifyGroth16" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_verify_groth16), + "managedVerifyPlonk" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_verify_plonk), + "managedAddEC" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_add_ec), + "managedMulEC" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_mul_ec), + "managedMultiExpEC" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_multi_exp_ec), + "managedMapToCurveEC" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_map_to_curve_ec), + "managedPairingChecksEC" => Function::new_typed_with_env(store, &function_env, wasmer_import_managed_pairing_checks_ec), } } diff --git a/vm-executor-wasmer/src/wasmer_imports.rs b/vm-executor-wasmer/src/wasmer_imports.rs index 0a7a6ab..3c03add 100644 --- a/vm-executor-wasmer/src/wasmer_imports.rs +++ b/vm-executor-wasmer/src/wasmer_imports.rs @@ -1420,6 +1420,41 @@ fn wasmer_import_managed_get_last_error(env: &VMHooksWrapper, error_handle: i32) env.vm_hooks.managed_get_last_error(error_handle) } +#[rustfmt::skip] +fn wasmer_import_managed_verify_groth16(env: &VMHooksWrapper, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + env.vm_hooks.managed_verify_groth16(curve_id, proof_handle, vk_handle, pub_witness_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_verify_plonk(env: &VMHooksWrapper, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + env.vm_hooks.managed_verify_plonk(curve_id, proof_handle, vk_handle, pub_witness_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_add_ec(env: &VMHooksWrapper, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32 { + env.vm_hooks.managed_add_ec(curve_id, group_id, point1_handle, point2_handle, result_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_mul_ec(env: &VMHooksWrapper, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32 { + env.vm_hooks.managed_mul_ec(curve_id, group_id, point_handle, scalar_handle, result_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_multi_exp_ec(env: &VMHooksWrapper, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32 { + env.vm_hooks.managed_multi_exp_ec(curve_id, group_id, points_handle, scalars_handle, result_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_map_to_curve_ec(env: &VMHooksWrapper, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32 { + env.vm_hooks.managed_map_to_curve_ec(curve_id, group_id, element_handle, result_handle) +} + +#[rustfmt::skip] +fn wasmer_import_managed_pairing_checks_ec(env: &VMHooksWrapper, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32 { + env.vm_hooks.managed_pairing_checks_ec(curve_id, points_g1_handle, points_g2_handle) +} + pub fn generate_import_object(store: &Store, env: &VMHooksWrapper) -> ImportObject { imports! { "env" => { @@ -1705,6 +1740,13 @@ pub fn generate_import_object(store: &Store, env: &VMHooksWrapper) -> ImportObje "managedGetNumErrors" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_num_errors), "managedGetErrorWithIndex" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_error_with_index), "managedGetLastError" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_get_last_error), + "managedVerifyGroth16" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_verify_groth16), + "managedVerifyPlonk" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_verify_plonk), + "managedAddEC" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_add_ec), + "managedMulEC" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_mul_ec), + "managedMultiExpEC" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_multi_exp_ec), + "managedMapToCurveEC" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_map_to_curve_ec), + "managedPairingChecksEC" => Function::new_native_with_env(store, env.clone(), wasmer_import_managed_pairing_checks_ec), } } diff --git a/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs b/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs index c79d9b6..e907046 100644 --- a/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs +++ b/vm-executor/src/new_traits/vm_hooks_legacy_adapter.rs @@ -1174,4 +1174,32 @@ impl VMHooksLegacy for VMHooksLegacyAdapter { fn managed_get_last_error(&self, error_handle: i32) { self.adapt_vm_hooks(|inner| VMHooks::managed_get_last_error(inner, error_handle)) } + + fn managed_verify_groth16(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_verify_groth16(inner, curve_id, proof_handle, vk_handle, pub_witness_handle)) + } + + fn managed_verify_plonk(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_verify_plonk(inner, curve_id, proof_handle, vk_handle, pub_witness_handle)) + } + + fn managed_add_ec(&self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_add_ec(inner, curve_id, group_id, point1_handle, point2_handle, result_handle)) + } + + fn managed_mul_ec(&self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_mul_ec(inner, curve_id, group_id, point_handle, scalar_handle, result_handle)) + } + + fn managed_multi_exp_ec(&self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_multi_exp_ec(inner, curve_id, group_id, points_handle, scalars_handle, result_handle)) + } + + fn managed_map_to_curve_ec(&self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_map_to_curve_ec(inner, curve_id, group_id, element_handle, result_handle)) + } + + fn managed_pairing_checks_ec(&self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32 { + self.adapt_vm_hooks(|inner| VMHooks::managed_pairing_checks_ec(inner, curve_id, points_g1_handle, points_g2_handle)) + } } diff --git a/vm-executor/src/new_traits/vm_hooks_new.rs b/vm-executor/src/new_traits/vm_hooks_new.rs index 26a6aa5..fef9070 100644 --- a/vm-executor/src/new_traits/vm_hooks_new.rs +++ b/vm-executor/src/new_traits/vm_hooks_new.rs @@ -291,6 +291,13 @@ pub trait VMHooks: core::fmt::Debug { fn managed_get_num_errors(&mut self) -> Result; fn managed_get_error_with_index(&mut self, index: i32, error_handle: i32) -> Result<(), VMHooksEarlyExit>; fn managed_get_last_error(&mut self, error_handle: i32) -> Result<(), VMHooksEarlyExit>; + fn managed_verify_groth16(&mut self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result; + fn managed_verify_plonk(&mut self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result; + fn managed_add_ec(&mut self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> Result; + fn managed_mul_ec(&mut self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> Result; + fn managed_multi_exp_ec(&mut self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> Result; + fn managed_map_to_curve_ec(&mut self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> Result; + fn managed_pairing_checks_ec(&mut self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> Result; } /// Dummy implementation for VMHooks. Can be used as placeholder, or in tests. @@ -1709,4 +1716,39 @@ impl VMHooks for VMHooksDefault { println!("Called: managed_get_last_error"); Ok(()) } + + fn managed_verify_groth16(&mut self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result { + println!("Called: managed_verify_groth16"); + Ok(0) + } + + fn managed_verify_plonk(&mut self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> Result { + println!("Called: managed_verify_plonk"); + Ok(0) + } + + fn managed_add_ec(&mut self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> Result { + println!("Called: managed_add_ec"); + Ok(0) + } + + fn managed_mul_ec(&mut self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> Result { + println!("Called: managed_mul_ec"); + Ok(0) + } + + fn managed_multi_exp_ec(&mut self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> Result { + println!("Called: managed_multi_exp_ec"); + Ok(0) + } + + fn managed_map_to_curve_ec(&mut self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> Result { + println!("Called: managed_map_to_curve_ec"); + Ok(0) + } + + fn managed_pairing_checks_ec(&mut self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> Result { + println!("Called: managed_pairing_checks_ec"); + Ok(0) + } } diff --git a/vm-executor/src/vm_hooks.rs b/vm-executor/src/vm_hooks.rs index 07f0dd7..5c742f1 100644 --- a/vm-executor/src/vm_hooks.rs +++ b/vm-executor/src/vm_hooks.rs @@ -295,6 +295,13 @@ pub trait VMHooksLegacy: core::fmt::Debug { fn managed_get_num_errors(&self) -> i32; fn managed_get_error_with_index(&self, index: i32, error_handle: i32); fn managed_get_last_error(&self, error_handle: i32); + fn managed_verify_groth16(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32; + fn managed_verify_plonk(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32; + fn managed_add_ec(&self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32; + fn managed_mul_ec(&self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32; + fn managed_multi_exp_ec(&self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32; + fn managed_map_to_curve_ec(&self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32; + fn managed_pairing_checks_ec(&self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32; } /// Dummy implementation for VMHooks. Can be used as placeholder, or in tests. @@ -1611,4 +1618,39 @@ impl VMHooksLegacy for VMHooksLegacyDefault { fn managed_get_last_error(&self, error_handle: i32) { println!("Called: managed_get_last_error"); } + + fn managed_verify_groth16(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + println!("Called: managed_verify_groth16"); + 0 + } + + fn managed_verify_plonk(&self, curve_id: i32, proof_handle: i32, vk_handle: i32, pub_witness_handle: i32) -> i32 { + println!("Called: managed_verify_plonk"); + 0 + } + + fn managed_add_ec(&self, curve_id: i32, group_id: i32, point1_handle: i32, point2_handle: i32, result_handle: i32) -> i32 { + println!("Called: managed_add_ec"); + 0 + } + + fn managed_mul_ec(&self, curve_id: i32, group_id: i32, point_handle: i32, scalar_handle: i32, result_handle: i32) -> i32 { + println!("Called: managed_mul_ec"); + 0 + } + + fn managed_multi_exp_ec(&self, curve_id: i32, group_id: i32, points_handle: i32, scalars_handle: i32, result_handle: i32) -> i32 { + println!("Called: managed_multi_exp_ec"); + 0 + } + + fn managed_map_to_curve_ec(&self, curve_id: i32, group_id: i32, element_handle: i32, result_handle: i32) -> i32 { + println!("Called: managed_map_to_curve_ec"); + 0 + } + + fn managed_pairing_checks_ec(&self, curve_id: i32, points_g1_handle: i32, points_g2_handle: i32) -> i32 { + println!("Called: managed_pairing_checks_ec"); + 0 + } }