-
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 2 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,214 @@ | ||
| //! Compression/decompression of points on the BN254 curve. | ||
|
|
||
| use super::{ALT_BN128_G1_SIZE, ALT_BN128_G2_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_SIZE: usize = ALT_BN128_G1_SIZE / 2; // 32 | ||
| pub const ALT_BN128_G2_COMPRESSED_SIZE: usize = ALT_BN128_G2_SIZE / 2; // 64 | ||
|
|
||
| // compression operations | ||
| const ALT_BN128_G1_COMPRESS: u64 = 0; | ||
| const ALT_BN128_G1_DECOMPRESS: u64 = 1; | ||
| const ALT_BN128_G2_COMPRESS: u64 = 2; | ||
| const ALT_BN128_G2_DECOMPRESS: 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. | ||
| /// | ||
| /// Note: This function does **not** check if the input has the correct length. | ||
| /// It will return an error if the length is invalid, incurring the cost of the syscall. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_compress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G1_COMPRESSED_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G1_COMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function checks if the input has the correct length, | ||
| /// returning an error without incurring the cost of the syscall. | ||
| pub fn checked_alt_bn128_g1_compress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G1_COMPRESSED_SIZE], ProgramError> { | ||
| if input.len() != ALT_BN128_G1_SIZE { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
| alt_bn128_compression(input, ALT_BN128_G1_COMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function does **not** check if the input has the correct length. | ||
| /// It will return an error if the length is invalid, incurring the cost of the syscall. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g1_decompress(input: &[u8]) -> Result<[u8; ALT_BN128_G1_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G1_DECOMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function checks if the input has the correct length, | ||
| /// returning an error without incurring the cost of the syscall. | ||
| pub fn checked_alt_bn128_g1_decompress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G1_SIZE], ProgramError> { | ||
| if input.len() != ALT_BN128_G1_COMPRESSED_SIZE { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
| alt_bn128_compression(input, ALT_BN128_G1_DECOMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function does **not** check if the input has the correct length. | ||
| /// It will return an error if the length is invalid, incurring the cost of the syscall. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g2_compress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G2_COMPRESSED_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G2_COMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function checks if the input has the correct length, | ||
| /// returning an error without incurring the cost of the syscall. | ||
| pub fn checked_alt_bn128_g2_compress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G2_COMPRESSED_SIZE], ProgramError> { | ||
| if input.len() != ALT_BN128_G2_SIZE { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
| alt_bn128_compression(input, ALT_BN128_G2_COMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function does **not** check if the input has the correct length. | ||
| /// It will return an error if the length is invalid, incurring the cost of the syscall. | ||
| #[inline(always)] | ||
| pub fn alt_bn128_g2_decompress(input: &[u8]) -> Result<[u8; ALT_BN128_G2_SIZE], ProgramError> { | ||
| alt_bn128_compression(input, ALT_BN128_G2_DECOMPRESS) | ||
| } | ||
|
|
||
| /// 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. | ||
| /// | ||
| /// Note: This function checks if the input has the correct length, | ||
| /// returning an error without incurring the cost of the syscall. | ||
| pub fn checked_alt_bn128_g2_decompress( | ||
| input: &[u8], | ||
| ) -> Result<[u8; ALT_BN128_G2_SIZE], ProgramError> { | ||
| if input.len() != ALT_BN128_G2_COMPRESSED_SIZE { | ||
| return Err(ProgramError::InvalidArgument); | ||
| } | ||
| alt_bn128_compression(input, ALT_BN128_G2_DECOMPRESS) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn alt_bn128_compression<const OUTPUT_DATA_LEN: usize>( | ||
| input: &[u8], | ||
| op: u64, | ||
| ) -> Result<[u8; OUTPUT_DATA_LEN], ProgramError> { | ||
| // Call via a system call to perform the calculation | ||
| #[cfg(target_os = "solana")] | ||
| { | ||
| let mut bytes = core::mem::MaybeUninit::<[u8; OUTPUT_DATA_LEN]>::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`") | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of taking in a &[u8], consider using a
&[[u8;ALT_BN128_G1_SIZE]]. This will avoid the need for the length check at runtime for properly defined constants.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will be the input type of
alt_bn128_compression?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, will it be convenient from the user's perspective if they have just &[u8] input?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, why
&[[u8;ALT_BN128_G1_SIZE]]if there is only one point in the input? Maybe&[u8;ALT_BN128_G1_SIZE]?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, just do the one, you are right. For multiple we want the array, for single, we want just the one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I can change input type to
&[u8;SIZE]for all compressions, thenchecked_variants will be unnecessary. But I'm still unsure about the convenience of usage for callers.