-
Notifications
You must be signed in to change notification settings - Fork 165
feat: add alt_bn128 syscalls #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
5bdb82c
257db60
5bffbcb
6f1d04f
b119581
c536f9b
6cef487
1521eb8
f11441a
b6b9ea6
5f8281f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,3 +70,6 @@ mainnet | |
| getters | ||
| PRNG | ||
| middleware | ||
| BN254 | ||
| G1 | ||
| G2 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //! Addition operations on the BN254 curve. | ||
|
|
||
| use super::{alt_bn128_group_op, ALT_BN128_G1_POINT_SIZE}; | ||
| use crate::program_error::ProgramError; | ||
|
|
||
| /// Input size for the add operation. | ||
| pub const ALT_BN128_ADDITION_INPUT_SIZE: usize = ALT_BN128_G1_POINT_SIZE * 2; // 128 | ||
|
|
||
| /// Output size for the add operation. | ||
| pub const ALT_BN128_ADDITION_OUTPUT_SIZE: usize = ALT_BN128_G1_POINT_SIZE; // 64 | ||
|
|
||
| const ALT_BN128_G1_ADD_BE: u64 = 0; | ||
| #[allow(dead_code)] | ||
| const ALT_BN128_G1_SUB_BE: u64 = 1; // not implemented in the syscall | ||
|
|
||
| /// Add two G1 points on the BN254 curve in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - Two consecutive G1 points in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the result of the addition in big-endian (EIP-197) encoding, | ||
| /// or an error if the input is invalid. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_addition_be( | ||
| input: &[u8; ALT_BN128_ADDITION_INPUT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_ADDITION_OUTPUT_SIZE], ProgramError> { | ||
| alt_bn128_group_op(input, ALT_BN128_G1_ADD_BE) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Compression/decompression of points on the BN254 curve. | ||
|
|
||
| use super::{ALT_BN128_G1_POINT_SIZE, ALT_BN128_G2_POINT_SIZE}; | ||
| use crate::program_error::ProgramError; | ||
|
|
||
| #[cfg(target_os = "solana")] | ||
| use crate::syscalls::sol_alt_bn128_compression; | ||
|
|
||
| // compression sizes | ||
| pub const ALT_BN128_G1_COMPRESSED_POINT_SIZE: usize = ALT_BN128_G1_POINT_SIZE / 2; // 32 | ||
| pub const ALT_BN128_G2_COMPRESSED_POINT_SIZE: usize = ALT_BN128_G2_POINT_SIZE / 2; // 64 | ||
|
|
||
| // compression operations | ||
| const ALT_BN128_G1_COMPRESS_BE: u64 = 0; | ||
| const ALT_BN128_G1_DECOMPRESS_BE: u64 = 1; | ||
| const ALT_BN128_G2_COMPRESS_BE: u64 = 2; | ||
| const ALT_BN128_G2_DECOMPRESS_BE: u64 = 3; | ||
|
|
||
| /// Compress a G1 point on the BN254 curve. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A G1 point in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the compressed G1 point in big-endian (EIP-197) encoding, | ||
| /// or an error if the input is not a valid G1 point. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_compress_be( | ||
| input: &[u8; ALT_BN128_G1_POINT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G1_COMPRESS_BE) | ||
| } | ||
|
|
||
| /// Decompress a G1 point on the BN254 curve. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A compressed G1 point in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the decompressed G1 point in big-endian (EIP-197) encoding, | ||
| /// or an error if the input is not a valid compressed G1 point. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_decompress_be( | ||
| input: &[u8; ALT_BN128_G1_COMPRESSED_POINT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_G1_POINT_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G1_DECOMPRESS_BE) | ||
| } | ||
|
|
||
| /// Compress a G2 point on the BN254 curve. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A G2 point in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the compressed G2 point in big-endian (EIP-197) encoding, | ||
| /// or an error if the input is not a valid G2 point. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g2_compress_be( | ||
| input: &[u8; ALT_BN128_G2_POINT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G2_COMPRESS_BE) | ||
| } | ||
|
|
||
| /// Decompress a G2 point on the BN254 curve. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A compressed G2 point in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the decompressed G2 point in big-endian (EIP-197) encoding, | ||
| /// or an error if the input is not a valid compressed G2 point. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g2_decompress_be( | ||
| input: &[u8; ALT_BN128_G2_COMPRESSED_POINT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_G2_POINT_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G2_DECOMPRESS_BE) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn alt_bn128_compression<const OUTPUT_DATA_SIZE: usize>( | ||
| input: &[u8], | ||
| op: u64, | ||
| ) -> Result<[u8; OUTPUT_DATA_SIZE], ProgramError> { | ||
| // Call via a system call to perform the calculation | ||
| #[cfg(target_os = "solana")] | ||
| { | ||
| let mut bytes = core::mem::MaybeUninit::<[u8; OUTPUT_DATA_SIZE]>::uninit(); | ||
|
|
||
| let result = unsafe { | ||
| sol_alt_bn128_compression( | ||
| op, | ||
| input as *const _ as *const u8, | ||
| input.len() as u64, | ||
| bytes.as_mut_ptr() as *mut u8, | ||
| ) | ||
| }; | ||
|
|
||
| match result { | ||
| // SAFETY: The syscall has initialized the bytes. | ||
| crate::SUCCESS => Ok(unsafe { bytes.assume_init() }), | ||
| _ => Err(ProgramError::InvalidArgument), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "solana"))] | ||
| { | ||
| core::hint::black_box((input, op)); | ||
| panic!("alt_bn128_compression is only available on target `solana`") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //! BN254 curve operations | ||
|
|
||
| pub mod addition; | ||
| pub mod compression; | ||
| pub mod multiplication; | ||
| pub mod pairing; | ||
|
|
||
| pub use addition::*; | ||
| pub use compression::*; | ||
| pub use multiplication::*; | ||
| pub use pairing::*; | ||
|
|
||
| use crate::program_error::ProgramError; | ||
|
|
||
| #[cfg(target_os = "solana")] | ||
| use crate::syscalls::sol_alt_bn128_group_op; | ||
|
|
||
| /// Size of the EC point field, in bytes. | ||
| pub const ALT_BN128_FIELD_SIZE: usize = 32; | ||
| /// A group element in G1 consists of two field elements `(x, y)`. | ||
| pub const ALT_BN128_G1_POINT_SIZE: usize = ALT_BN128_FIELD_SIZE * 2; | ||
| /// Elements in G2 is represented by 2 field-extension elements `(x, y)`. | ||
| pub const ALT_BN128_G2_POINT_SIZE: usize = ALT_BN128_FIELD_SIZE * 4; | ||
|
|
||
| #[inline] | ||
| fn alt_bn128_group_op<const OUTPUT_DATA_SIZE: usize>( | ||
| input: &[u8], | ||
| op: u64, | ||
| ) -> Result<[u8; OUTPUT_DATA_SIZE], ProgramError> { | ||
| // Call via a system call to perform the calculation | ||
| #[cfg(target_os = "solana")] | ||
| { | ||
| let mut bytes = core::mem::MaybeUninit::<[u8; OUTPUT_DATA_SIZE]>::uninit(); | ||
|
|
||
| let result = unsafe { | ||
| sol_alt_bn128_group_op( | ||
| op, | ||
| input as *const _ as *const u8, | ||
| input.len() as u64, | ||
| bytes.as_mut_ptr() as *mut u8, | ||
| ) | ||
| }; | ||
|
|
||
| match result { | ||
| // SAFETY: The syscall has initialized the bytes. | ||
| crate::SUCCESS => Ok(unsafe { bytes.assume_init() }), | ||
| _ => Err(ProgramError::InvalidArgument), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "solana"))] | ||
| { | ||
| core::hint::black_box((input, op)); | ||
| panic!("alt_bn128_group_op is only available on target `solana`") | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //! Multiplication operations on the BN254 curve. | ||
|
|
||
| use super::{alt_bn128_group_op, ALT_BN128_FIELD_SIZE, ALT_BN128_G1_POINT_SIZE}; | ||
| use crate::program_error::ProgramError; | ||
|
|
||
| /// Input size for the multiplication operation. | ||
| pub const ALT_BN128_MULTIPLICATION_INPUT_SIZE: usize = | ||
| ALT_BN128_G1_POINT_SIZE + ALT_BN128_FIELD_SIZE; // 96 | ||
|
|
||
| /// Output size for the multiplication operation. | ||
| pub const ALT_BN128_MULTIPLICATION_OUTPUT_SIZE: usize = ALT_BN128_G1_POINT_SIZE; // 64 | ||
|
|
||
| const ALT_BN128_G1_MUL_BE: u64 = 2; | ||
|
|
||
| /// Multiply a G1 point by a scalar on the BN254 curve in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A G1 point in big-endian (EIP-197) encoding, | ||
| /// followed by a scalar in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the result of the multiplication in big-endian (EIP-197) | ||
| /// encoding, or an error if the input is invalid. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_multiplication_be( | ||
| input: &[u8; ALT_BN128_MULTIPLICATION_INPUT_SIZE], | ||
| ) -> Result<[u8; ALT_BN128_MULTIPLICATION_OUTPUT_SIZE], ProgramError> { | ||
| alt_bn128_group_op(input, ALT_BN128_G1_MUL_BE) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //! Pairing operations on the BN254 curve. | ||
|
|
||
| use super::{alt_bn128_group_op, ALT_BN128_G1_POINT_SIZE, ALT_BN128_G2_POINT_SIZE}; | ||
| use crate::program_error::ProgramError; | ||
|
|
||
| /// Pair element size. | ||
| pub const ALT_BN128_PAIRING_ELEMENT_SIZE: usize = ALT_BN128_G1_POINT_SIZE + ALT_BN128_G2_POINT_SIZE; // 192 | ||
|
|
||
| const ALT_BN128_PAIRING_BE: u64 = 3; | ||
|
|
||
| /// Perform a pairing operation on the BN254 curve in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A sequence of pairs of G1 and G2 points in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing the result of the pairing operation, or an error if the input is invalid. | ||
| /// | ||
| /// Note: This function does **not** check if the input has the correct length. | ||
| /// Currently, if the length is invalid, it will not return an error; instead it will use only | ||
| /// multiples of [`ALT_BN128_PAIRING_ELEMENT_SIZE`] bytes and discard the rest. | ||
| /// After SIMD-0334 is implemented, it will return an error if the length is invalid, | ||
| /// incurring the cost of the syscall. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_pairing_be(input: &[u8]) -> Result<u8, ProgramError> { | ||
|
||
| alt_bn128_group_op::<32>(input, ALT_BN128_PAIRING_BE).map(|data| data[31]) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| //! Functions for working with elliptic curves. | ||
|
|
||
| pub mod bn254; |
Uh oh!
There was an error while loading. Please reload this page.