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

engine-modexp bug fixes and performance improvements #809

Merged
merged 8 commits into from
Aug 1, 2023

Conversation

guidovranken
Copy link
Contributor

Description

This fixes the following issues with engine-modexp:

  • addition overflow in big_sq that would lead to incorrect output (and a crash in debug mode)
  • assert failure in sub_to_same_size that would lead to incorrect output
  • slow modular exponentiation with even modulus. The bottleneck was the routine were the inverse of A mod M was computed, where M is a power of two. This inversion function has been replaced with the algorithm found in this paper, which is much faster.

Performance / NEAR gas cost considerations

The performance of modular exponentiation with even modulus is improved. Performance remains the same for odd modulus.

Testing

Extensive fuzz testing, was was also the method used to detect the issues that this PR addresses.

How should this be reviewed

Additional information

The main issue was that q_hat could exceed Word::MAX, so it needs
to be DoubleWord, not Word.

Additionally, the q_hat/r_hat adjustment loop has been replaced with
the canonical version from Knuth's algorithm, and some minor
optimizations around Word/DoubleWord casting are implemented.
@joshuajbouw
Copy link
Contributor

I believe the cargo fmt is failing due to the use of nightly cargo fmt check in the CI.

@aleksuss
Copy link
Member

@joshuajbouw no. We have pretty old nightly in our CI so it should be fixed, I guess.

@birchmd birchmd added this pull request to the merge queue Aug 1, 2023
Merged via the queue into aurora-is-near:develop with commit 4ecee7d Aug 1, 2023
18 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Aug 3, 2023
## Description

With the improvements from #809 merged, we can start using the Aurora
implementation of modexp.

## Performance / NEAR gas cost considerations

There is a gas cost increase in one of the repro tests. This must mean
the `ibig` implementation of modexp is faster for the case used in that
transaction. But using the Aurora implementation of modexp is still an
improvement because the worst-case performance of the Aurora
implementation is better than `ibig`, as exemplified in the
`bench_modexp_standalone` test.

## Testing

Existing tests.
aleksuss pushed a commit that referenced this pull request Aug 10, 2023
## Description

This fixes the following issues with `engine-modexp`:

- addition overflow in big_sq that would lead to incorrect output (and a
crash in debug mode)
- assert failure in sub_to_same_size that would lead to incorrect output
- slow modular exponentiation with even modulus. The bottleneck was the
routine were the inverse of A mod M was computed, where M is a power of
two. This inversion function has been replaced with the algorithm found
in [this paper](https://eprint.iacr.org/2017/411.pdf), which is much
faster.

## Performance / NEAR gas cost considerations

The performance of modular exponentiation with even modulus is improved.
Performance remains the same for odd modulus.

## Testing

Extensive fuzz testing, was was also the method used to detect the
issues that this PR addresses.

## How should this be reviewed

## Additional information

---------

Co-authored-by: Michael Birch <michael.birch@aurora.dev>
aleksuss pushed a commit that referenced this pull request Aug 10, 2023
## Description

With the improvements from #809 merged, we can start using the Aurora
implementation of modexp.

## Performance / NEAR gas cost considerations

There is a gas cost increase in one of the repro tests. This must mean
the `ibig` implementation of modexp is faster for the case used in that
transaction. But using the Aurora implementation of modexp is still an
improvement because the worst-case performance of the Aurora
implementation is better than `ibig`, as exemplified in the
`bench_modexp_standalone` test.

## Testing

Existing tests.
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.

5 participants