-
Notifications
You must be signed in to change notification settings - Fork 200
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
Conversation
The Firedancer team maintains a line-for-line reimplementation of the |
b8d7d68
to
6f267cb
Compare
.unwrap_or(u64::MAX), | ||
.checked_div(2) | ||
.unwrap_or(u64::MAX) | ||
.saturating_add(budget.big_modular_exponentiation_cost), |
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.
Maybe this should be "base_cost"?
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, 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) |
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.
Magic number should be parametrized.
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.
Updated!
compute-budget/src/compute_budget.rs
Outdated
@@ -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, |
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.
Isn't it a "divisor" or "denominator" instead of a multiplicative_factor
?
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.
Yeah you are right. 1/2 is the multiplicative factor. I'll rename it to divisor.
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: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 functionN^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 #