Repository navigation
Implement Montgomery multiplication assembly optimization for RV64GC - #28012
fengpengboa wants to merge 1 commit into
Conversation
|
Hi @t8m @paulidale @kroeckx @nhorman ! The duplicate PR #[28067] was closed in favor of this one.When convenient, could the maintainer team please help move the review forward? I'm happy to address any needed changes or provide clarifications.Thank you for your time and consideration! |
|
@fengpengboa Could you please get some independent review from somebody from the RISC-V community? We do not have RISC-V assembly experts among committers. |
|
@ZenithalHourlyRate @HeliC829 @JerryShih previously reviewed RISC-V assembly code. |
ZenithalHourlyRate
left a comment
There was a problem hiding this comment.
I have not reviewed all the logic, only covering some control logics here; the arithmetic part needs further reviewing; but I think some questions should be answered as they affect the arithmetic logic (i.e. the num >= 2 assumption)
The andi -16 is confusing for me. If it is a typo for addi, how would make test pass?
Also, please add comments before each asm block (like when you separate them by an empty line so it is more readable; the variable names are a bit confusing, like lo0,hi0,alo,ahi,lo1,hi1,aj,nj,i,j, need some time to distinguish what 0/1 means and what i/j means.
|
|
||
| $code .= <<___; | ||
|
|
||
| mv s0, sp |
There was a problem hiding this comment.
If you want to temporarily store sp in s0 while keeping s0's content (or $fp, since it is callee-saved), you should use my ($sp0) = use_regs(8); and mv $sp0, sp to make the style more consistent, namely in perlasm use all use_regs allocated reg and names instead of directly using reg names.
There was a problem hiding this comment.
In the initial section, register mapping was already completed using my ($ra, $fp) = use_regs(1, 8). Here, I did not synchronize the variable names. I will modify this part.
| addi $ap, $ap, 16 | ||
| slli $num, $num, 3 | ||
| ld $n0, 0($n0) | ||
| andi $tp, $tp, -16 |
There was a problem hiding this comment.
Should not this be addi $tp, $tp, -16?
There was a problem hiding this comment.
As previously mentioned, this instruction performs memory address alignment, rather than an addioperation.
|
|
||
| $code .= <<___; | ||
|
|
||
| .Lmul_mont: |
There was a problem hiding this comment.
If we do not branch back here, please do not add a label here; I read such label as if we were going to branch back here later.
There was a problem hiding this comment.
I might consider adding a branch here subsequently. If it's unnecessary now, I can delete it.
| .Lmul_mont: | ||
| ld $m0, 0($bp) | ||
| addi $bp, $bp, 8 | ||
| slli $temp, $num, 3 |
There was a problem hiding this comment.
This line can be merged with slli $num, $num, 3 below
There was a problem hiding this comment.
I will modify this part
| addi $np, $np, 16 | ||
|
|
||
| mul $lo0, $hi0, $m0 | ||
| addi $j, $num, -16 |
There was a problem hiding this comment.
I understand that you want to process 2 ULONG at once, but are we guaranteed that num >= 2 (or 8 * num >= 16) ? what will happen when num = 1?
There was a problem hiding this comment.
Before executing this assembly code, conditional checks are performed to ensure num >= 2.
#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)
if (num > 1 && num <= BN_SOFT_LIMIT && a->top == num && b->top == num) {
if (bn_wexpand(r, num) == NULL)
return 0;
if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
r->neg = a->neg ^ b->neg;
r->top = num;
r->flags |= BN_FLG_FIXED_TOP;
return 1;
}
}
#endif| mulhu $ahi, $aj, $m0 | ||
|
|
||
| mul $m1, $lo0, $n0 | ||
| mv sp, $tp |
There was a problem hiding this comment.
if tp is (num + 2) * ULONG, I suggest allocaing tp at once instead of putting all these three lines apart
slli $temp, $num, 3
sub $tp, sp, $temp
andi(maybe addi) $tp, $tp, -16
mv sp, $tpinto
slli $num, $num, 3
sub sp, sp, $num
sub sp, sp, 16
mv $tp, spThere was a problem hiding this comment.
I will make the modifications as suggested.
| addi $tp, $tp, 8 | ||
| bnez $j, .L1st | ||
|
|
||
| .L1st_skip: |
There was a problem hiding this comment.
I would name such section in a style like 1st_fix_tp_carry instead of an informationless skip; same for inner_skip below.
There was a problem hiding this comment.
I will make the modifications as suggested.
| .Lcond_copy: | ||
| addi $num,$num, -8 | ||
| bnez $carry1,.set_aj1 | ||
| mv $nj,$tj | ||
| j .end1 | ||
| .set_aj1: | ||
| mv $nj,$aj |
There was a problem hiding this comment.
These bnez/j are not constant-time. You can use snez to implement a constant time selection logic.
There was a problem hiding this comment.
I will make the modifications as suggested
Thank you for the recommendation. Should the reviewer’s availability be limited, we will proactively identify qualified RISC-V experts for an independent review. |
The instruction |
584934f to
13312fe
Compare
|
@t8m Hi, I've updated the code per review comments. Could you please approve the CI workflow? Thanks! |
|
@ZenithalHourlyRate Hello! I have now addressed all your previous review comments. The latest changes have been pushed. Would you please take a look when you have a moment? If you have any further suggestions, feel free to let me know. Thank you for your guidance! |
ZenithalHourlyRate
left a comment
There was a problem hiding this comment.
Generally LGTM! Just some questions and nitpicking comments.
13312fe to
5fd89cd
Compare
|
Hi @t8m I've implemented all review suggestions in the latest code changes. Would you be kind enough to re-approve the CI workflow when you have a moment? Really appreciate your guidance on this! |
|
@ZenithalHourlyRate @paulidale I wanted to sincerely thank you all for taking the time to review the earlier version and provide your valuable insights. Following your guidance, I've now addressed all review comments in the latest code update. I'd be truly grateful if you could spare some time to examine the revised changes when your schedule permits. |
|
@t8m @ZenithalHourlyRate @paulidale Thank you for your thorough review and valuable suggestions! Could you please confirm if anything else needs adjustment? If not, would you be able to assist with the merge?Really appreciate your time and expertise! |
ZenithalHourlyRate
left a comment
There was a problem hiding this comment.
From my side I think it is ok.
Thank you for your review! I really appreciate the confirmation from your side. |
|
@ZenithalHourlyRate Could you please add the formal approval checkmark when you have a moment? |
|
@HeliC829 Hi, I would be grateful if you could find the time to review this PR. Thank you! |
Thank you for the review! |
|
Hi @slontis , could you please review this PR when you have time? Thank you! |
|
Hi @InfoHunter , could you please review this PR when you have time? Thank you! |
|
This pull request is ready to merge |
|
Merged after removing end of line whitespace. Thanks for the contribution. |
Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Paul Dale <[email protected]> (Merged from #28012)
…plement Montgomery multiplication assembly optimization for RV64GC" (openssl#28012). The key improvements include:code formatting unification、instruction scheduling optimization、register allocation strategy update、carry-chain subtraction implementation upgrade. These enhancements aim to improve both the code maintainability and its execution performance, particularly on out-of-order RISC-V cores.
… from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012).
…plement Montgomery multiplication assembly optimization for RV64GC" (openssl#28012). The key improvements include:code formatting unification、instruction scheduling optimization、register allocation strategy update、updates to addition/subtraction carry and borrow flag handling. These enhancements aim to improve both the code maintainability and its execution performance, particularly on out-of-order RISC-V cores.
…plement Montgomery multiplication assembly optimization for RV64GC" (openssl#28012). The key improvements include:code formatting unification、instruction scheduling optimization、register allocation strategy update、updates to addition/subtraction carry and borrow flag handling. These enhancements aim to improve both the code maintainability and its execution performance, particularly on out-of-order RISC-V cores.
…ged "Implement Montgomery multiplication assembly optimization for RV64GC" (openssl#28012). The key improvements include:code formatting unification、instruction scheduling optimization、register allocation strategy update、updates to addition/subtraction carry and borrow flag handling. These enhancements aim to improve both the code maintainability and its execution performance, particularly on out-of-order RISC-V cores.
… from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012).
…plement Montgomery multiplication assembly optimization for RV64GC" (#28012). The key improvements include:code formatting unification、instruction scheduling optimization、register allocation strategy update、updates to addition/subtraction carry and borrow flag handling. These enhancements aim to improve both the code maintainability and its execution performance, particularly on out-of-order RISC-V cores. Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Paul Dale <[email protected]> (Merged from #29438)
This PR adds an RV64GC-optimized Montgomery squaring assembly, ported from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012). Performance Improvement | Test Item | Baseline (C) | ASM Optimized | Improvement | | ---------- | ------------- | -------------- | ------------ | | sign/s | 208 | 220 | 5.77% | | verify/s | 7190 | 7956.6 | 10.66% | | encr./s | 6638 | 7156.0 | 7.8% | | decr./s | 203 | 216 | 6.4 % |
This PR adds an RV64GC-optimized Montgomery squaring assembly, ported from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (#28012). Performance Improvement | Test Item | Baseline (C) | ASM Optimized | Improvement | | ---------- | ------------- | -------------- | ------------ | | sign/s | 208 | 220 | 5.77% | | verify/s | 7190 | 7956.6 | 10.66% | | encr./s | 6638 | 7156.0 | 7.8% | | decr./s | 203 | 216 | 6.4 % | Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Paul Dale <[email protected]> MergeDate: Fri Mar 13 17:21:14 2026 (Merged from #29440)
This PR adds an RV64GC-optimized Montgomery squaring assembly, ported from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012). Performance Improvement | Test Item | Baseline (C) | ASM Optimized | Improvement | | ---------- | ------------- | -------------- | ------------ | | sign/s | 208 | 220 | 5.77% | | verify/s | 7190 | 7956.6 | 10.66% | | encr./s | 6638 | 7156.0 | 7.8% | | decr./s | 203 | 216 | 6.4 % | Reviewed-by: Neil Horman <[email protected]> Reviewed-by: Paul Dale <[email protected]> MergeDate: Fri Mar 13 17:21:14 2026 (Merged from openssl#29440)
This PR introduces the RV64GC-optimized assembly implementation for Montgomery multiplication, and significantly accelerating BN modular exponentiation and RSA operations.
Performance Improvement
benchmark results run on SG2042:
Test Verification
All relevant tests pass in the default build configuration:
Checklist