Skip to content
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

Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant #3686

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

LStan
Copy link

@LStan LStan commented Nov 17, 2024

Problem

Input for alt_bn128_multiplication (Scalar multiplication in G1) consist of 1 point in G1 (64 bytes), 1 scalar (BigInteger256 - 32 bytes). So 96 bytes in total, not 128.

Summary of Changes

Changed ALT_BN128_MULTIPLICATION_INPUT_LEN to 96

@mergify mergify bot requested a review from a team November 17, 2024 12:53
@samkim-crypto samkim-crypto added the CI Pull Request is ready to enter CI label Nov 19, 2024
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Nov 19, 2024
Copy link

@samkim-crypto samkim-crypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay on this and thanks for the contribution! This is indeed something that I missed.

Unfortunately, this change will technically incur a breaking change to the behavior of the runtime, which can lead to break in consensus. These type of changes will generally need to be feature gated (example) Unfortunately, the functions in the bn254 module itself do not have access to the invoke context, which contains information about which feature gates that are activated. This makes the required changes slightly more involved:

  1. Define two versions of the alt_bn128_multiplication functions: one with ALT_BN128_MULTIPLICATION_INPUT_LEN = 128 and the other with ALT_BN128_MULTIPLICATION_INPUT_LEN = 96.
  2. Define a new feature gate in the feature-set crate.
  3. In this line, we should update the function to be either of the two multiplication functions defined above depending on whether the new feature gate is active or not.

Since the changes are somewhat involved, if you are okay with it, then I can commit the changes outlined above. If you are willing to take a shot, then that is fine as well, so please let me know!

@steviez
Copy link

steviez commented Nov 27, 2024

  1. Define a new feature gate in the feature-set crate.

More so than this, changes to runtime behavior need to go through a SIMD so other client teams (Firedancer, etc) can be aware of changes and implement corresponding changes as well

@LStan
Copy link
Author

LStan commented Nov 27, 2024

I want to try to do it, but I have some questions about naming:

  1. How to name the second function? alt_bn128_multiplication_fix or alt_bn128_multiplication_fix_input_len or something else?
  2. How to name the feature? enable_alt_bn128_multiplication_fix?
  3. Where to get id for the feature?

About SIMD. I don't know, how to do it. Firedancer also checks for 128: https://github.com/firedancer-io/firedancer/blob/513e3250c6154390c070920d0d76ef6876154820/src/ballet/bn254/fd_bn254.c#L192-L194

@samkim-crypto
Copy link

samkim-crypto commented Nov 28, 2024

  1. How to name the second function? alt_bn128_multiplication_fix or alt_bn128_multiplication_fix_input_len or something else?

For this, how about we create a non-public function alt_bn128_apply_multiplication(input: &[u8], expected_length: usize) -> Result<Vec<u8>, AltBn128Error> that is identical to the original alt_bn128_multiplication(input: &[u8]), but takes in the expected length as input.

Then, let's make alt_bn128_multiplication(input: &[u8]) invoke alt_bn128_apply_multiplication with the correct length, and define a new function alt_bn128_multiplication_128(input: &[u8]) that invokes alt_bn128_apply_multiplication on the original input of 128.

Technically, we are making a breaking change to alt_bn128_multiplication, which is a public function, but it is not used anywhere else in the run time, so it should be okay.

@samkim-crypto
Copy link

  1. How to name the feature? enable_alt_bn128_multiplication_fix?

Let's name it fix_alt_bn128_multiplication_input_length.

For the SIMD, I'll go ahead and create it.

  1. Where to get id for the feature?

Usually, one of the core contributors generate a keypair and use the corresponding public key. For this, let's use bn2puAyxUx6JUabAxYdKdJ5QHbNNmKw8dCGuGCyRrFN.

@LStan LStan requested a review from a team as a code owner November 28, 2024 10:06
Copy link

mergify bot commented Nov 28, 2024

The Firedancer team maintains a line-for-line reimplementation of the
native programs, and until native programs are moved to BPF, those
implementations must exactly match their Agave counterparts.
If this PR represents a change to a native program implementation (not
tests), please include a reviewer from the Firedancer team. And please
keep refactors to a minimum.

@LStan
Copy link
Author

LStan commented Nov 28, 2024

I've added the feature. Please check

@samkim-crypto samkim-crypto added the CI Pull Request is ready to enter CI label Dec 2, 2024
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Dec 2, 2024
Copy link

@samkim-crypto samkim-crypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just some minor comments below. Let me create a SIMD and let you know.

curves/bn254/src/lib.rs Outdated Show resolved Hide resolved
curves/bn254/src/lib.rs Outdated Show resolved Hide resolved
@@ -1110,6 +1114,7 @@ lazy_static! {
(accounts_lt_hash::id(), "enables lattice-based accounts hash #3333"),
(enable_secp256r1_precompile::id(), "Enable secp256r1 precompile SIMD-0075"),
(migrate_stake_program_to_core_bpf::id(), "Migrate Stake program to Core BPF SIMD-0196 #3655"),
(fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length #3686"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me work on the SIMD. Once the SIMD is merged, let's plug the SIMD number in here.

@samkim-crypto samkim-crypto added the CI Pull Request is ready to enter CI label Dec 3, 2024
@anza-team anza-team removed the CI Pull Request is ready to enter CI label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants