-
Couldn't load subscription status.
- Fork 140
[bn254] Refactor addition, multiplication, and pairing functions into validator and prelude modules
#297
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
[bn254] Refactor addition, multiplication, and pairing functions into validator and prelude modules
#297
Conversation
…or and prelude modules
bn254/src/multiplication.rs
Outdated
| _version: VersionedMultiplication, | ||
| input: &[u8], | ||
| endianness: Endianness, | ||
| expected_length: usize, |
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.
Wouldn't it be better to "move" expected_length to a version? If versioned_multiplication is public, this parameter can create confusion.
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.
Good point! I absorbed the length into the version as you suggested.
bn254/src/multiplication.rs
Outdated
| /// | ||
| /// Developers should be extremely careful when modifying this function, as a breaking change | ||
| /// can result in a fork in the Solana cluster. Any such change requires an | ||
| /// approved Solana SIMD. Subsequently, a new `VersionedAddition` variant must be added, |
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.
VersionedAddition -> VersionedMultiplication
bn254/src/pairing.rs
Outdated
| /// | ||
| /// Developers should be extremely careful when modifying this function, as a breaking change | ||
| /// can result in a fork in the Solana cluster. Any such change requires an | ||
| /// approved Solana SIMD. Subsequently, a new `VersionedAddition` variant must be added, |
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.
VersionedAddition -> VersionedPairing
bn254/src/multiplication.rs
Outdated
| #[inline(always)] | ||
| pub fn alt_bn128_multiplication_128(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> { | ||
| versioned_multiplication(VersionedMultiplication::V0, input, Endianness::BE) | ||
| // hard-code length; we will remove this function in the future |
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.
Why is this comment below the code?
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.
I ended up removing this comment actually since we don't hardcode the length as a function parameter any more.
fa33617 to
0283595
Compare
|
Couple of thoughts:
|
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.
Looks great! Just the one nit
bn254/src/lib.rs
Outdated
| /// This module contains the versioned syscall implementations and is intended for use | ||
| /// primarily by validator code. | ||
| #[cfg(not(target_os = "solana"))] | ||
| pub mod validator { |
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.
nit: how about just calling this versioned? That seems clearer than validator
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.
Yep, okay. Updated!
bn254/src/addition.rs
Outdated
| pub enum VersionedAddition { | ||
| V0, | ||
| } |
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.
I was reading through the SIMDs, but just so I'm sure I understand -- is the plan to add a V1 for the BE length check?
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.
Yes, the plan is to add V1 and then require that the input length is strictly 128 bytes (ALT_BN128_ADDITION_INPUT_LEN) for BE addition.
Yes, as you described, my plan was to update agave code so that the new I kept the
These are good ideas. When I was refactoring, I was hoping that the submodule would already scope the functions appropriately, but I ended up re-exporting all the functions as is. I'll add the prefix and also specify the group in the function names. |
But will |
Yeah I think this is fine. As soon as the changes are in v3.1, I can create a PR on the agave side to update the syscall to use the versioned functions. |
… `validator` and `prelude` modules (anza-xyz#297) * refactor addition, multiplication, and pairing functions into validator and prelude modules * remove `expected_length` parameter in `versioned_multiplication` * fix typos * remove comment on hard-coded length * rename `validator` to `versioned` * add `alt_bn128_` prefix to functions and identify group in function names
Problem
There are a number of proposed changes to the bn128 syscall (SIMD-0284, 0334, and also issues discussed in #267). Making these breaking changes are difficult because these changes have to be feature-gated in agave. Instead of making changes to the original function, we have to define a new function with the updated changes in order to properly feature gate the change (example).
Whenever there are new changes, we have to come up with alternate names for the new functions, which makes the functions in this crate inconsistent. This can also be confusing to developers who have to decipher which functions they have to use.
For the most part, a syscall implementation should rarely be updated. But since there are a number of changes lined up for the bn128 syscall, it would be nice to have a more modular way of defining the functions so that upgrading is easy.
Summary of Changes
I took a stab at modularizing the addition, multiplication, and pairing syscalls. For the addition, multiplication, and pairing functions, I created a enums
Versioned{Addition, Multiplication, Pairing}enums andversioned_{addition, multiplication, pairing}functions.These enums and functions are re-exported out as part of the
validatormodule. Whenever a breaking change is to be made to the syscalls, one should define a new version variant for the enum and then scope the change with this variant in theversioned_{addition, multiplication, pairing}functions.Then, in the validator implementation, we can feature gate the change with the versioning:
I just made the changes to the addition, multiplication, and pairing submodules for now. I don't think there are any immediate breaking changes pending for the compression and decompression syscalls, so we might not need such refactoring for the compression module, but I'll consider it again after this PR.