Repository navigation
[WIP][RFC] Implement Montgomery Modular Multiplication in RISC-V Vector Extension ASM - #18479
Closed
ZenithalHourlyRate wants to merge 2 commits into
Closed
ZenithalHourlyRate wants to merge 2 commits into
ZenithalHourlyRate wants to merge 2 commits into
Conversation
2 tasks done
Contributor
|
This has been idle for a while. Should we convert this to an issue to allow another developer to take on the work? |
Contributor
Author
|
This attempt turned out to be inefficient as there were some typos in the reference paper. It was opened as PR in hope it could serve other people. Closing now. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR tries to demo that OpenSSL could use RISC-V Vector Extension to do modular multiplication, potentially accelerating (most notably) RSA4096 and other crypto operations. The author expect that with higher VLEN (e.g. 2048) the speedup would be more significant.
This is a draft and a baseline, and the author hereby request for comments on this implementation to seek for an eventually SOTA implementation.
More info on this implementation could be found at https://github.com/ZenithalHourlyRate/rvv-mmm
Technical details
This is a modified version of Montgomery Multiplication on the Cell. Most notably, instead of representing one big number (BN) consecutively in one reg, this paper uses a column representation. This reduces the cost of carry propagation and division in each round.
However, by using this representation, this algorithm needs to use a different montgomery radix
Rthan theROpenSSL used. For example, whenM = 2 ** 255 - 19, theRfor OpenSSL would be 256, but theRfor this algorithm would be 272.Since the algorithm uses a different
R, all computation should then be carried out bybn_mont_muland we should avoidwordvariant likebn_from_montgomery_word. By forcing all computation being carried out bybn_mont_mul, we need to break an invariant called minimal representation (details at #6640), thus we can not useBN_value_oneas we need to expand the width. This also leads to some quirks on some lib functions, likeBN_MONT_CTX_setandBN_mod_exp_mont.Also, the algorithm needs to allocate space as it uses redundent representation and we need to convert BN to/from it; in the meantime the
uint32_t AB[260] = {0};allowsvlsseg8.For demostration purpose, there are only two implementations generated (256 bits and 4096 bits mmm), and the
Rfor them are constants. You can altervl/bnin these two perlasm (corresponding tommm_loop.plandmmm_mem.plin my repo) for differentBNwith different minimumVLENCompilation
You need to enable
vextention when compiling, I used the lastest toolchain from riscv-collabCorrectness
You can try the following commands between your native openssl and the RVV-backed openssl
Note that thanks to the feature of MMM, you can use RSA2048 even with 4096bit MMM.
Benchmark
AFAIK, there is no existing Vector 1.0 real machine available generally, let me know if there is one that I can play with.
Discussion
R choice
Currently all
bn_mul_montasm use theRprovided by OpenSSL. But this algorithm uses a differentR. Also, there is some hard-encoded behavior that relies on the choice ofR, e.g. Shay Gueron's suggestion inbn_exp.cShould we support different
R, or just implement RVV MMM just like RSAZ and stick to the OpenSSLR.subtraction less
From the paper above we know that for subtraction-less MMM we can represent the number in
Z/2MZinstead ofZ/MZ. This is especially beneficial for modular exponentiation. However, some comments showed that in some asm implementation it is assumed the input is less thanM.VLEN
As the function name
bn_mul_mont_rv64imv_zvl128b_sew32_bn4096_nelement260suggests, we would have a Cartesian product of all possible configurations when compiling. This increases the code size if we generate all of them, but if only some of them are provided, the speedup is a concern.I know this is the packed SIMD way, but it seems not easy to implement in the vector way. Namely register assignment should be determined at codegen (by perlasm) time instead of runtime. I'll try to explore the vector way.
Checklist