8000 Feat(syscall): add altbn128 g1 & g2 compression by ananas-block · Pull Request #32870 · solana-labs/solana · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Feat(syscall): add altbn128 g1 & g2 compression #32870

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions program-runtime/src/compute_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ pub struct ComputeBudget {
pub poseidon_cost_coefficient_c: u64,
/// Number of compute units consumed for accessing the remaining compute units.
pub get_remaining_compute_units_cost: u64,
/// Number of compute units consumed to call alt_bn128_g1_compress.
pub alt_bn128_g1_compress: u64,
/// Number of compute units consumed to call alt_bn128_g1_decompress.
pub alt_bn128_g1_decompress: u64,
/// Number of compute units consumed to call alt_bn128_g2_compress.
pub alt_bn128_g2_compress: u64,
/// Number of compute units consumed to call alt_bn128_g2_decompress.
pub alt_bn128_g2_decompress: u64,
}

impl Default for ComputeBudget {
Expand Down Expand Up @@ -183,6 +191,10 @@ impl ComputeBudget {
poseidon_cost_coefficient_a: 61,
poseidon_cost_coefficient_c: 542,
get_remaining_compute_units_cost: 100,
alt_bn128_g1_compress: 30,
alt_bn128_g1_decompress: 398,
alt_bn128_g2_compress: 86,
alt_bn128_g2_decompress: 13610,
}
}

Expand Down
122 changes: 118 additions & 4 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ use {
feature_set::{
self, blake3_syscall_enabled, curve25519_syscall_enabled,
disable_cpi_setting_executable_and_rent_epoch, disable_deploy_of_alloc_free_syscall,
disable_fees_sysvar, enable_alt_bn128_syscall, enable_big_mod_exp_syscall,
enable_early_verification_of_account_modifications, enable_partitioned_epoch_reward,
enable_poseidon_syscall, error_on_syscall_bpf_function_hash_collisions,
last_restart_slot_sysvar, libsecp256k1_0_5_upgrade_enabled, reject_callx_r10,
disable_fees_sysvar, enable_alt_bn128_compression_syscall, enable_alt_bn128_syscall,
enable_big_mod_exp_syscall, enable_early_verification_of_account_modifications,
enable_partitioned_epoch_reward, enable_poseidon_syscall,
error_on_syscall_bpf_function_hash_collisions, last_restart_slot_sysvar,
libsecp256k1_0_5_upgrade_enabled, reject_callx_r10,
remaining_compute_units_syscall_enabled, stop_sibling_instruction_search_at_parent,
stop_truncating_strings_in_syscalls, switch_to_new_elf_parser,
},
Expand Down Expand Up @@ -154,6 +155,8 @@ pub fn create_program_runtime_environment_v1<'a>(
debugging_features: bool,
) -> Result<BuiltinProgram<InvokeContext<'a>>, Error> {
let enable_alt_bn128_syscall = feature_set.is_active(&enable_alt_bn128_syscall::id());
let enable_alt_bn128_compression_syscall =
feature_set.is_active(&enable_alt_bn128_compression_syscall::id());
let enable_big_mod_exp_syscall = feature_set.is_active(&enable_big_mod_exp_syscall::id());
let blake3_syscall_enabled = feature_set.is_active(&blake3_syscall_enabled::id());
let curve25519_syscall_enabled = feature_set.is_active(&curve25519_syscall_enabled::id());
Expand Down Expand Up @@ -345,6 +348,14 @@ pub fn create_program_runtime_environment_v1<'a>(
SyscallRemainingComputeUnits::call
)?;

// Alt_bn128_compression
register_feature_gated_function!(
result,
enable_alt_bn128_compression_syscall,
*b"sol_alt_bn128_compression",
SyscallAltBn128Compression::call,
)?;

// Log data
result.register_function_hashed(*b"sol_log_data", SyscallLogData::call)?;

Expand Down Expand Up @@ -1907,6 +1918,109 @@ declare_syscall!(
}
);

declare_syscall!(
/// alt_bn128 g1 and g2 compression and decompression
SyscallAltBn128Compression,
fn inner_call(
invoke_context: &mut InvokeContext,
op: u64,
input_addr: u64,
input_size: u64,
result_addr: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
use solana_sdk::alt_bn128::compression::prelude::{
alt_bn128_g1_compress, alt_bn128_g1_decompress, alt_bn128_g2_compress,
alt_bn128_g2_decompress, ALT_BN128_G1_COMPRESS, ALT_BN128_G1_DECOMPRESS,
ALT_BN128_G2_COMPRESS, ALT_BN128_G2_DECOMPRESS, G1, G1_COMPRESSED, G2, G2_COMPRESSED,
};
let budget = invoke_context.get_compute_budget();
let base_cost = budget.syscall_base_cost;
let (cost, output): (u64, usize) = match op {
ALT_BN128_G1_COMPRESS => (
base_cost.saturating_add(budget.alt_bn128_g1_compress),
G1_COMPRESSED,
),
ALT_BN128_G1_DECOMPRESS => {
(base_cost.saturating_add(budget.alt_bn128_g1_decompress), G1)
}
ALT_BN128_G2_COMPRESS => (
base_cost.saturating_add(budget.alt_bn128_g2_compress),
G2_COMPRESSED,
),
ALT_BN128_G2_DECOMPRESS => {
(base_cost.saturating_add(budget.alt_bn128_g2_decompress), G2)
}
_ => {
return Err(SyscallError::InvalidAttribute.into());
}
};

consume_compute_meter(invoke_context, cost)?;

let input = translate_slice::<u8>(
memory_mapping,
input_addr,
input_size,
invoke_context.get_check_aligned(),
invoke_context.get_check_size(),
)?;

let call_result = translate_slice_mut::<u8>(
memory_mapping,
result_addr,
output as u64,
invoke_context.get_check_aligned(),
invoke_context.get_check_size(),
)?;

match op {
ALT_BN128_G1_COMPRESS => {
let result_point = match alt_bn128_g1_compress(input) {
Ok(result_point) => result_point,
Err(e) => {
return Ok(e.into());
}
};
call_result.copy_from_slice(&result_point);
Ok(SUCCESS)
}
ALT_BN128_G1_DECOMPRESS => {
let result_point = match alt_bn128_g1_decompress(input) {
Ok(result_point) => result_point,
Err(e) => {
return Ok(e.into());
}
};
call_result.copy_from_slice(&result_point);
Ok(SUCCESS)
}
ALT_BN128_G2_COMPRESS => {
let result_point = match alt_bn128_g2_compress(input) {
Ok(result_point) => result_point,
Err(e) => {
return Ok(e.into());
}
};
call_result.copy_from_slice(&result_point);
Ok(SUCCESS)
}
ALT_BN128_G2_DECOMPRESS => {
let result_point = match alt_bn128_g2_decompress(input) {
Ok(result_point) => result_point,
Err(e) => {
return Ok(e.into());
}
};
call_result.copy_from_slice(&result_point);
Ok(SUCCESS)
}
_ => Err(SyscallError::InvalidAttribute.into()),
}
}
);

#[cfg(test)]
#[allow(clippy::arithmetic_side_effects)]
#[allow(clippy::indexing_slicing)]
Expand Down
8 changes: 8 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions programs/sbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ members = [
"rust/128bit_dep",
"rust/alloc",
"rust/alt_bn128",
"rust/alt_bn128_compression",
"rust/big_mod_exp",
"rust/call_depth",
"rust/caller_access",
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn main() {
"128bit",
"alloc",
"alt_bn128",
"alt_bn128_compression",
"big_mod_exp",
"call_depth",
"caller_access",
Expand Down
50 changes: 50 additions & 0 deletions programs/sbf/c/src/alt_bn128_compression/alt_bn128.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @brief alt_bn128 syscall test
*/
#include <sol/alt_bn128_compression.h>
#include <sol/assert.h>
#include <sol/string.h>

extern uint64_t entrypoint(const uint8_t *input) {
// compress and decompress g1
{
uint8_t result_compressed[ALT_BN128_COMPRESSION_G1_COMPRESS_OUTPUT_LEN];
uint8_t result_decompressed[ALT_BN128_COMPRESSION_G1_DECOMPRESS_OUTPUT_LEN];
uint8_t input[] = {
45, 206, 255, 166, 152, 55, 128, 138, 79, 217, 145, 164, 25, 74, 120, 234, 234, 217,
68, 149, 162, 44, 133, 120, 184, 205, 12, 44, 175, 98, 168, 172, 20, 24, 216, 15, 209,
175, 106, 75, 147, 236, 90, 101, 123, 219, 245, 151, 209, 202, 218, 104, 148, 8, 32,
254, 243, 191, 218, 122, 42, 81, 193, 84,
};

sol_alt_bn128_compression(ALT_BN128_G1_COMPRESS, input, SOL_ARRAY_SIZE(input), result_compressed);
sol_alt_bn128_compression(ALT_BN128_G1_DECOMPRESS, result_compressed, SOL_ARRAY_SIZE(result_compressed), result_decompressed);

sol_assert(0 ==
sol_memcmp(result_decompressed, input, ALT_BN128_COMPRESSION_G1_DECOMPRESS_OUTPUT_LEN));
}

// compress and decompress g2
{

uint8_t result_compressed[ALT_BN128_COMPRESSION_G2_COMPRESS_OUTPUT_LEN];
uint8_t result_decompressed[ALT_BN128_COMPRESSION_G2_DECOMPRESS_OUTPUT_LEN];
uint8_t input[] = {
40, 57, 233, 205, 180, 46, 35, 111, 215, 5, 23, 93, 12, 71, 118, 225, 7, 46, 247, 147,
47, 130, 106, 189, 184, 80, 146, 103, 141, 52, 242, 25, 0, 203, 124, 176, 110, 34, 151,
212, 66, 180, 238, 151, 236, 189, 133, 209, 17, 137, 205, 183, 168, 196, 92, 159, 75,
174, 81, 168, 18, 86, 176, 56, 16, 26, 210, 20, 18, 81, 122, 142, 104, 62, 251, 169,
98, 141, 21, 253, 50, 130, 182, 15, 33, 109, 228, 31, 79, 183, 88, 147, 174, 108, 4,
22, 14, 129, 168, 6, 80, 246, 254, 100, 218, 131, 94, 49, 247, 211, 3, 245, 22, 200,
177, 91, 60, 144, 147, 174, 90, 17, 19, 189, 62, 147, 152, 18,
};

sol_alt_bn128_compression(ALT_BN128_G2_COMPRESS, input, SOL_ARRAY_SIZE(input), result_compressed);
sol_alt_bn128_compression(ALT_BN128_G2_DECOMPRESS, result_compressed, SOL_ARRAY_SIZE(result_compressed), result_decompressed);

sol_assert(
0 == sol_memcmp(result_decompressed, input, ALT_BN128_COMPRESSION_G2_DECOMPRESS_OUTPUT_LEN));
}

return SUCCESS;
}
19 changes: 19 additions & 0 deletions programs/sbf/rust/alt_bn128_compression/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "solana-sbf-rust-alt-bn128-compression"
description = "Solana BPF test program written in Rust"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
array-bytes = { workspace = true }
solana-program = { workspace = true }

[lib]
crate-type = ["cdylib"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
73 changes: 73 additions & 0 deletions programs/sbf/rust/alt_bn128_compression/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Alt_bn128 compression Syscalls tests

extern crate solana_program;
use solana_program::{
alt_bn128::compression::prelude::{
alt_bn128_g1_compress, alt_bn128_g1_decompress, alt_bn128_g2_compress,
alt_bn128_g2_decompress,
},
custom_heap_default, custom_panic_default, msg,
};

fn alt_bn128_compression_g1() {
let points_g1: [[u8; 64]; 3] = [
[
45, 206, 255, 166, 152, 55, 128, 138, 79, 217, 145, 164, 25, 74, 120, 234, 234, 217,
68, 149, 162, 44, 133, 120, 184, 205, 12, 44, 175, 98, 168, 172, 20, 24, 216, 15, 209,
175, 106, 75, 147, 236, 90, 101, 123, 219, 245, 151, 209, 202, 218, 104, 148, 8, 32,
254, 243, 191, 218, 122, 42, 81, 193, 84,
],
[
45, 206, 255, 166, 152, 55, 128, 138, 79, 217, 145, 164, 25, 74, 120, 234, 234, 217,
68, 149, 162, 44, 133, 120, 184, 205, 12, 44, 175, 98, 168, 172, 28, 75, 118, 99, 15,
130, 53, 222, 36, 99, 235, 81, 5, 165, 98, 197, 197, 182, 144, 40, 212, 105, 169, 142,
72, 96, 177, 156, 174, 43, 59, 243,
],
[0u8; 64],
];
points_g1.iter().for_each(|point| {
let g1_compressed = alt_bn128_g1_compress(point).unwrap();
let g1_decompressed = alt_bn128_g1_decompress(&g1_compressed).unwrap();
assert_eq!(*point, g1_decompressed);
});
}

fn alt_bn128_compression_g2() {
let points_g2: [[u8; 128]; 3] = [
[
40, 57, 233, 205, 180, 46, 35, 111, 215, 5, 23, 93, 12, 71, 118, 225, 7, 46, 247, 147,
47, 130, 106, 189, 184, 80, 146, 103, 141, 52, 242, 25, 0, 203, 124, 176, 110, 34, 151,
212, 66, 180, 238, 151, 236, 189, 133, 209, 17, 137, 205, 183, 168, 196, 92, 159, 75,
174, 81, 168, 18, 86, 176, 56, 16, 26, 210, 20, 18, 81, 122, 142, 104, 62, 251, 169,
98, 141, 21, 253, 50, 130, 182, 15, 33, 109, 228, 31, 79, 183, 88, 147, 174, 108, 4,
22, 14, 129, 168, 6, 80, 246, 254, 100, 218, 131, 94, 49, 247, 211, 3, 245, 22, 200,
177, 91, 60, 144, 147, 174, 90, 17, 19, 189, 62, 147, 152, 18,
],
[
40, 57, 233, 205, 180, 46, 35, 111, 215, 5, 23, 93, 12, 71, 118, 225, 7, 46, 247, 147,
47, 130, 106, 189, 184, 80, 146, 103, 141, 52, 242, 25, 0, 203, 124, 176, 110, 34, 151,
212, 66, 180, 238, 151, 236, 189, 133, 209, 17, 137, 205, 183, 168, 196, 92, 159, 75,
174, 81, 168, 18, 86, 176, 56, 32, 73, 124, 94, 206, 224, 37, 155, 80, 17, 74, 13, 30,
244, 66, 96, 100, 254, 180, 130, 71, 3, 230, 109, 236, 105, 51, 131, 42, 16, 249, 49,
33, 226, 166, 108, 144, 58, 161, 196, 221, 204, 231, 132, 137, 174, 84, 104, 128, 184,
185, 54, 43, 225, 54, 222, 226, 15, 120, 89, 153, 233, 101, 53,
],
[0u8; 128],
];
points_g2.iter().for_each(|point| {
let g2_compressed = alt_bn128_g2_compress(point).unwrap();
let g2_decompressed = alt_bn128_g2_decompress(&g2_compressed).unwrap();
assert_eq!(*point, g2_decompressed);
});
}
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
msg!("alt_bn128_compression");

alt_bn128_compression_g1();
alt_bn128_compression_g2();
0
}

custom_heap_default!();
custom_panic_default!();
2 changes: 2 additions & 0 deletions programs/sbf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fn test_program_sbf_sanity() {
programs.extend_from_slice(&[
("alloc", true),
("alt_bn128", true),
("alt_bn128_compression", true),
("sbf_to_sbf", true),
("float", true),
("multiple_static", true),
Expand All @@ -303,6 +304,7 @@ fn test_program_sbf_sanity() {
("solana_sbf_rust_128bit", true),
("solana_sbf_rust_alloc", true),
("solana_sbf_rust_alt_bn128", true),
("solana_sbf_rust_alt_bn128_compression", true),
("solana_sbf_rust_curve25519", true),
("solana_sbf_rust_custom_heap", true),
("solana_sbf_rust_dep_crate", true),
Expand Down
1 change: 1 addition & 0 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8129,6 +8129,7 @@ impl Bank {
feature_set::switch_to_new_elf_parser::id(),
feature_set::bpf_account_data_direct_mapping::id(),
feature_set::enable_alt_bn128_syscall::id(),
feature_set::enable_alt_bn128_compression_syscall::id(),
feature_set::enable_big_mod_exp_syscall::id(),
feature_set::blake3_syscall_enabled::id(),
feature_set::curve25519_syscall_enabled::id(),
Expand Down
Loading
0