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

[syscalls] Update mod exp compute cost #2807

Merged
merged 3 commits into from
Sep 2, 2024

Conversation

samkim-crypto
Copy link

@samkim-crypto samkim-crypto commented Aug 30, 2024

Problem

The big integer modular exponentiation (EIP-198) syscall was added in solana-labs#28503. However, the compute units were significantly under-assigned and there is currently a security advisory regarding this.

The syscalls are feature-gated (on the blocked list) and not activated on any of the networks. There are currently no projects that are actively waiting for these syscalls to land so these syscalls are not at the top priority especially given the current active development of firedancer. However, there are project that plans to use these syscalls in the future, so it would be best to address the compute-unit issue and close the security advisory even if the feature gate remain in the blocked list for some time.

Summary of Changes

I updated the compute cost.

The initial compute unit was assigned by benching the modular exponentiation and dividing the time by 33ns per CU. The initial bench not only failed to represent an average cluster performance in executing these modular exponentiation functions, but also failed to take into account certain edge cases in the modular exponentiation algorithm that performs significantly slower.

The modular exponentiation syscall implementation uses the rust num_bigint crate. In this crate, the modular exponentiation is implemented with the following logic:

  • If the modulus is odd, then use the montgomery arithmetic to do the exponentiation
  • If the modulus is even, then use regular modular arithmetic to do the exponentiation

The initial bench of the syscalls only took into account the case when the modulus is odd even though the performance is much slower when the modulus is even due to the unsuitability of the montgomery arithmetic.

I re-ran the benches in my devserver for the cases when the modulus is even (power-of-two) and re-calculated the compute units (33ns per CU). I interpolated the CUs in relation to the input size (N) and it seems that the function N^2/2 + 190 suitably approximates the CUs. This function does slightly over-assigns the CUs for small inputs (up to ~32 bytes), but given that most uses of these syscalls will be on much higher numbers, I think this is okay.

Note: There is actually a better algorithm for computing modular exponentiation that takes advantage of the Chinese remainder theorem (https://www.people.vcu.edu/~jwang3/CMSC691/j34monex.pdf). I tested the Aurora implementation of this algorithm (https://github.com/aurora-is-near/aurora-engine/tree/develop/engine-modexp), but it seems that this specific implementation actually performs slower than the num_bigint implementation.

Since this syscall is currently not at the top-priority list and will remain in the blocked list for some time, I think we can just bump-up the compute units to address and close the security advisory for now. In the future, we should revisit this to use the better algorithm to improve the syscall performance.

Fixes #

Copy link

mergify bot commented Aug 30, 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.

.unwrap_or(u64::MAX),
.checked_div(2)
.unwrap_or(u64::MAX)
.saturating_add(budget.big_modular_exponentiation_cost),
Copy link

Choose a reason for hiding this comment

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

Maybe this should be "base_cost"?

Copy link
Author

Choose a reason for hiding this comment

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

Yep, updated!

consume_compute_meter(
invoke_context,
budget.syscall_base_cost.saturating_add(
input_len
.saturating_mul(input_len)
.checked_div(budget.big_modular_exponentiation_cost)
.unwrap_or(u64::MAX),
.checked_div(2)
Copy link

Choose a reason for hiding this comment

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

Magic number should be parametrized.

Copy link
Author

Choose a reason for hiding this comment

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

Updated!

@@ -180,7 +182,8 @@ impl ComputeBudget {
alt_bn128_multiplication_cost: 3_840,
alt_bn128_pairing_one_pair_cost_first: 36_364,
alt_bn128_pairing_one_pair_cost_other: 12_121,
big_modular_exponentiation_cost: 33,
big_modular_exponentiation_base_cost: 190,
big_modular_exponentiation_cost_multiplicative_factor: 2,
Copy link

Choose a reason for hiding this comment

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

Isn't it a "divisor" or "denominator" instead of a multiplicative_factor?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah you are right. 1/2 is the multiplicative factor. I'll rename it to divisor.

@samkim-crypto samkim-crypto merged commit eb37b21 into anza-xyz:master Sep 2, 2024
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants