-
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
Open
LStan
wants to merge
11
commits into
anza-xyz:main
Choose a base branch
from
LStan:add-altbn128
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5bdb82c
add alt_bn128 syscalls
LStan 257db60
update solana.dic
LStan 5bffbcb
comments
LStan 6f1d04f
rename constants
LStan b119581
remove checked functions
LStan c536f9b
add _be suffix
LStan 6cef487
change suffix from LEN to SIZE
LStan 1521eb8
split group_ops
LStan f11441a
change input type for addition and multiplication
LStan b6b9ea6
fix comments
LStan 5f8281f
change pairing return type to bool
LStan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,3 +70,6 @@ mainnet | |
| getters | ||
| PRNG | ||
| middleware | ||
| BN254 | ||
| G1 | ||
| G2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| //! 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; | ||
|
|
||
| /// Checks whether the product of pairings of a sequence of G1 and G2 points (in big-endian EIP-197 encoding) | ||
| /// on the BN254 curve evaluates to the identity element (1). | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `input` - A sequence of pairs of G1 and G2 points in big-endian (EIP-197) encoding. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A `Result` containing: | ||
| /// - `Ok(true)` if the pairing product equals 1, | ||
| /// - `Ok(false)` otherwise, | ||
| /// - `Err(ProgramError)` 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_is_pairing_valid_be(input: &[u8]) -> Result<bool, ProgramError> { | ||
| alt_bn128_group_op::<32>(input, ALT_BN128_PAIRING_BE).map(|data| data[31] == 1) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| //! Functions for working with elliptic curves. | ||
|
|
||
| pub mod bn254; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.