Skip to content

Commit

Permalink
[solana-program] Move bn254 logic from solana-program into a separa…
Browse files Browse the repository at this point in the history
…te crate (#1495)

* move `alt_bn128` module logic into a separate crate

* clean up `solana-bn254` crate

* remove unnecessary dependencies from `solana-program`

* update dependencies in the rest of the repo

* cargo sort

* cargo fmt

* update previously missed dependency in bn254 crate

* remove `ark-bn254` from `solana-program`

* cargo lock

* cargo sort

* fix bytemuck dependency

* cargo lock

* re-add `solana-compute-budget` dependency to `bpf_loader`

* re-export `solana-bn254` inside `solana-program`

* cargo lock

* cargo sort
  • Loading branch information
samkim-crypto authored Jul 18, 2024
1 parent d8791a6 commit c54d840
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 58 deletions.
23 changes: 19 additions & 4 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ solana-banks-interface = { path = "banks-interface", version = "=2.1.0" }
solana-banks-server = { path = "banks-server", version = "=2.1.0" }
solana-bench-tps = { path = "bench-tps", version = "=2.1.0" }
solana-bloom = { path = "bloom", version = "=2.1.0" }
solana-bn254 = { path = "curves/bn254", version = "=2.1.0" }
solana-bpf-loader-program = { path = "programs/bpf_loader", version = "=2.1.0" }
solana-bucket-map = { path = "bucket_map", version = "=2.1.0" }
agave-cargo-registry = { path = "cargo-registry", version = "=2.1.0" }
Expand Down
27 changes: 27 additions & 0 deletions curves/bn254/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "solana-bn254"
description = "Solana BN254"
documentation = "https://docs.rs/solana-bn254"
version = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
homepage = { workspace = true }
license = { workspace = true }
edition = { workspace = true }

[dependencies]
bytemuck = { workspace = true, features = ["derive"] }
solana-program = { workspace = true }
thiserror = { workspace = true }

[target.'cfg(not(target_os = "solana"))'.dependencies]
ark-bn254 = { workspace = true }
ark-ec = { workspace = true }
ark-ff = { workspace = true }
ark-serialize = { workspace = true }

[dev-dependencies]
array-bytes = { workspace = true }
serde = { workspace = true }
serde_derive = { workspace = true }
serde_json = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod prelude {
pub use crate::alt_bn128::compression::{
pub use crate::compression::{
alt_bn128_compression_size::*, consts::*, target_arch::*, AltBn128CompressionError,
};
}
Expand Down Expand Up @@ -70,7 +70,7 @@ mod target_arch {

use {
super::*,
crate::alt_bn128::compression::alt_bn128_compression_size,
crate::compression::alt_bn128_compression_size,
ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate},
};

Expand Down Expand Up @@ -194,14 +194,15 @@ mod target_arch {
super::*,
alt_bn128_compression_size::{G1, G1_COMPRESSED, G2, G2_COMPRESSED},
prelude::*,
solana_program::syscalls,
};

pub fn alt_bn128_g1_compress(
input: &[u8],
) -> Result<[u8; G1_COMPRESSED], AltBn128CompressionError> {
let mut result_buffer = [0; G1_COMPRESSED];
let result = unsafe {
crate::syscalls::sol_alt_bn128_compression(
syscalls::sol_alt_bn128_compression(
ALT_BN128_G1_COMPRESS,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -218,7 +219,7 @@ mod target_arch {
pub fn alt_bn128_g1_decompress(input: &[u8]) -> Result<[u8; G1], AltBn128CompressionError> {
let mut result_buffer = [0; G1];
let result = unsafe {
crate::syscalls::sol_alt_bn128_compression(
syscalls::sol_alt_bn128_compression(
ALT_BN128_G1_DECOMPRESS,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -237,7 +238,7 @@ mod target_arch {
) -> Result<[u8; G2_COMPRESSED], AltBn128CompressionError> {
let mut result_buffer = [0; G2_COMPRESSED];
let result = unsafe {
crate::syscalls::sol_alt_bn128_compression(
syscalls::sol_alt_bn128_compression(
ALT_BN128_G2_COMPRESS,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -256,7 +257,7 @@ mod target_arch {
) -> Result<[u8; G2], AltBn128CompressionError> {
let mut result_buffer = [0; G2];
let result = unsafe {
crate::syscalls::sol_alt_bn128_compression(
syscalls::sol_alt_bn128_compression(
ALT_BN128_G2_DECOMPRESS,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -275,7 +276,7 @@ mod target_arch {
mod tests {
use {
super::*,
crate::alt_bn128::compression::target_arch::convert_endianness,
crate::compression::target_arch::convert_endianness,
ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate},
std::ops::Neg,
target_arch::{
Expand Down
15 changes: 8 additions & 7 deletions sdk/program/src/alt_bn128/mod.rs → curves/bn254/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
pub mod compression;
pub mod prelude {
pub use crate::alt_bn128::{consts::*, target_arch::*, AltBn128Error};
pub use crate::{consts::*, target_arch::*, AltBn128Error};
}

use {
bytemuck_derive::{Pod, Zeroable},
bytemuck::{Pod, Zeroable},
consts::*,
thiserror::Error,
};
Expand Down Expand Up @@ -305,14 +305,15 @@ mod target_arch {

#[cfg(target_os = "solana")]
mod target_arch {
use super::*;
use {super::*, solana_program::syscalls};

pub fn alt_bn128_addition(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> {
if input.len() > ALT_BN128_ADDITION_INPUT_LEN {
return Err(AltBn128Error::InvalidInputData);
}
let mut result_buffer = [0; ALT_BN128_ADDITION_OUTPUT_LEN];
let result = unsafe {
crate::syscalls::sol_alt_bn128_group_op(
syscalls::sol_alt_bn128_group_op(
ALT_BN128_ADD,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -332,7 +333,7 @@ mod target_arch {
}
let mut result_buffer = [0u8; ALT_BN128_POINT_SIZE];
let result = unsafe {
crate::syscalls::sol_alt_bn128_group_op(
syscalls::sol_alt_bn128_group_op(
ALT_BN128_MUL,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -356,7 +357,7 @@ mod target_arch {
}
let mut result_buffer = [0u8; 32];
let result = unsafe {
crate::syscalls::sol_alt_bn128_group_op(
syscalls::sol_alt_bn128_group_op(
ALT_BN128_PAIRING,
input as *const _ as *const u8,
input.len() as u64,
Expand All @@ -374,7 +375,7 @@ mod target_arch {
#[cfg(test)]
mod tests {
use {
crate::alt_bn128::{prelude::*, PodG1},
crate::{prelude::*, PodG1},
ark_bn254::g1::G1Affine,
ark_ec::AffineRepr,
ark_serialize::{CanonicalSerialize, Compress},
Expand Down
6 changes: 5 additions & 1 deletion programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ byteorder = { workspace = true }
libsecp256k1 = { workspace = true }
log = { workspace = true }
scopeguard = { workspace = true }
solana-bn254 = { workspace = true }
solana-compute-budget = { workspace = true }
solana-curve25519 = { workspace = true }
solana-log-collector = { workspace = true }
Expand Down Expand Up @@ -43,4 +44,7 @@ name = "solana_bpf_loader_program"
targets = ["x86_64-unknown-linux-gnu"]

[features]
shuttle-test = ["solana-type-overrides/shuttle-test", "solana-program-runtime/shuttle-test"]
shuttle-test = [
"solana-type-overrides/shuttle-test",
"solana-program-runtime/shuttle-test",
]
14 changes: 7 additions & 7 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub use self::{
};
#[allow(deprecated)]
use {
solana_bn254::prelude::{
alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing, AltBn128Error,
ALT_BN128_ADDITION_OUTPUT_LEN, ALT_BN128_MULTIPLICATION_OUTPUT_LEN,
ALT_BN128_PAIRING_ELEMENT_LEN, ALT_BN128_PAIRING_OUTPUT_LEN,
},
solana_compute_budget::compute_budget::ComputeBudget,
solana_log_collector::{ic_logger_msg, ic_msg},
solana_poseidon as poseidon,
Expand All @@ -24,11 +29,6 @@ use {
},
solana_sdk::{
account_info::AccountInfo,
alt_bn128::prelude::{
alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing, AltBn128Error,
ALT_BN128_ADDITION_OUTPUT_LEN, ALT_BN128_MULTIPLICATION_OUTPUT_LEN,
ALT_BN128_PAIRING_ELEMENT_LEN, ALT_BN128_PAIRING_OUTPUT_LEN,
},
big_mod_exp::{big_mod_exp, BigModExpParams},
blake3, bpf_loader, bpf_loader_deprecated, bpf_loader_upgradeable,
entrypoint::{BPF_ALIGN_OF_U128, MAX_PERMITTED_DATA_INCREASE, SUCCESS},
Expand Down Expand Up @@ -1572,7 +1572,7 @@ declare_builtin_function!(
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
use solana_sdk::alt_bn128::prelude::{ALT_BN128_ADD, ALT_BN128_MUL, ALT_BN128_PAIRING};
use solana_bn254::prelude::{ALT_BN128_ADD, ALT_BN128_MUL, ALT_BN128_PAIRING};
let budget = invoke_context.get_compute_budget();
let (cost, output): (u64, usize) = match group_op {
ALT_BN128_ADD => (
Expand Down Expand Up @@ -1839,7 +1839,7 @@ declare_builtin_function!(
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
use solana_sdk::alt_bn128::compression::prelude::{
use solana_bn254::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,
Expand Down
21 changes: 17 additions & 4 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 @@ -27,6 +27,7 @@ serde_derive = "1.0.112" # must match the serde version, see https://github.com/
serde_json = "1.0.56"
solana-account-decoder = { path = "../../account-decoder", version = "=2.1.0" }
solana-accounts-db = { path = "../../accounts-db", version = "=2.1.0" }
solana-bn254 = { path = "../../curves/bn254", version = "=2.1.0" }
solana-bpf-loader-program = { path = "../bpf_loader", version = "=2.1.0" }
solana-cli-output = { path = "../../cli-output", version = "=2.1.0" }
solana-compute-budget = { path = "../../compute-budget", version = "=2.1.0" }
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/rust/alt_bn128/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = { workspace = true }

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

[lib]
Expand Down
6 changes: 3 additions & 3 deletions programs/sbf/rust/alt_bn128/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Alt_bn128 Syscalls tests
extern crate solana_program;
use solana_program::{
alt_bn128::prelude::{alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing},
custom_heap_default, custom_panic_default, msg,
use {
solana_bn254::prelude::{alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing},
solana_program::{custom_heap_default, custom_panic_default, msg},
};

fn alt_bn128_addition_test() {
Expand Down
1 change: 1 addition & 0 deletions programs/sbf/rust/alt_bn128_compression/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = { workspace = true }

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

[lib]
Expand Down
6 changes: 3 additions & 3 deletions programs/sbf/rust/alt_bn128_compression/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Alt_bn128 compression Syscalls tests
extern crate solana_program;
use solana_program::{
alt_bn128::compression::prelude::{
use {
solana_bn254::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,
solana_program::{custom_heap_default, custom_panic_default, msg},
};

fn alt_bn128_compression_g1() {
Expand Down
Loading

0 comments on commit c54d840

Please sign in to comment.