Repository navigation
Add optimized Montgomery squaring for RV64GC (RISC-V) - #29440
fengpengboa wants to merge 1 commit into
Conversation
|
Hi @t8m @paulidale @nhorman @ZenithalHourlyRate @HeliC829 @JerryShih !! My submitted PR is currently in Open status. 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! |
ZenithalHourlyRate
left a comment
There was a problem hiding this comment.
Please add RISC-V in the title so the more RISC-V CI can cover them. The detailed criteria is
openssl/.github/workflows/riscv-more-cross-compiles.yml
Lines 22 to 29 in da8f098
It will take me some time to review 1k5 lines of assembly.
I've updated the PR title. |
ZenithalHourlyRate
left a comment
There was a problem hiding this comment.
I've roughly gone through it and it looks convincing. Though definitely needs careful checking...
I am busy in the next one or two months and may only get some Sunday night for you to do the review...sorry for the long process.
| .balign 32 | ||
| .globl bn_mul_mont | ||
| .type bn_mul_mont,\@function | ||
| bn_mul_mont: |
There was a problem hiding this comment.
# Take the bn_sqr8x_mont branch when num is a multiply of 8 and num <= 32
The branch to Lscalar_impl here might be unnecessary. The armv8-mont code contains the branch because it has another neon branch but we do not have them.
There was a problem hiding this comment.
# Take the bn_sqr8x_mont branch when num is a multiply of 8 and num <= 32The branch to Lscalar_impl here might be unnecessary. The armv8-mont code contains the branch because it has another neon branch but we do not have them.
Thank you for pointing this out. You are right — since our current implementation does not include the vector branch, this conditional branch is indeed unnecessary. I will remove it.
Thank you for taking the time to review this. I really appreciate your initial feedback. |
aec4cfe to
7d72f7d
Compare
|
Hi @t8m @paulidale @nhorman @ZenithalHourlyRate @HeliC829 @JerryShih !! My submitted PR is currently in Open status. 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! |
|
Hi @t8m @paulidale @nhorman @ZenithalHourlyRate @HeliC829 @JerryShih, this PR has been open since Dec 18, 2025When you have a moment, could someone please take a look? I'm happy to make any changes or provide clarifications. Thanks! |
|
Sorry for the late response. I've been busy with work in recent months and is expected to be busy throughout the Spring. Reviewing it line-by-line is out of my bandwidth now and the current state might be the best I can do. My opinion is that given it is a line-by-line transcription of existing code, and the tests also pass, I am OK with the code. (The previous review comments have been addressed in the new commit.) |
nhorman
left a comment
There was a problem hiding this comment.
I don't know much about riscv assembly, but the code passes on my riscv vm, so I think thats about the best I can do here.
| sd x8,88(sp) | ||
| sd x9,80(sp) | ||
| sd x18,72(sp) | ||
| sd x19,64(sp) | ||
| sd x20,56(sp) | ||
| sd x21,48(sp) | ||
| sd x22,40(sp) | ||
| sd x23,32(sp) | ||
| sd x24,24(sp) | ||
| sd x25,16(sp) | ||
| sd x27,8(sp) | ||
| sd x26,0(sp) |
There was a problem hiding this comment.
Can you use ABI name here instead of register?
There was a problem hiding this comment.
Can you use ABI name here instead of register?
Sure, I will make the modification accordingly.
| ld x8,88(sp) | ||
| ld x9,80(sp) | ||
| ld x18,72(sp) | ||
| ld x19,64(sp) | ||
| ld x20,56(sp) | ||
| ld x21,48(sp) | ||
| ld x22,40(sp) | ||
| ld x23,32(sp) | ||
| ld x24,24(sp) | ||
| ld x25,16(sp) | ||
| ld x27,8(sp) | ||
| ld x26,0(sp) |
There was a problem hiding this comment.
Same as above.
I will make the modification accordingly.
| # Return address and Frame pointer | ||
| # RISC-V ABI | ||
| # $ra x1 ra | ||
| # $fp x8 s0 | ||
| my ($ra,$fp) = use_regs(1,8); | ||
|
|
||
| # Temporary variable allocation | ||
| # RISC-V ABI | ||
| # $a0 x5 t0 | ||
| # $a1 x6 t1 | ||
| # $a2 x7 t2 | ||
| # $a3 x28 t3 | ||
| # $a4 x9 s1 | ||
| # $a5 x18 s2 | ||
| # $a6 x19 s3 | ||
| # $a7 x20 s4 | ||
| my ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7) = use_regs(5,6,7,28,9,18,19,20); |
There was a problem hiding this comment.
ABI name has been define in riscv.pm line 51, so we don't need to redefine here again. Assembly can use ABI name directly without '$'
There was a problem hiding this comment.
ABI name has been define in riscv.pm line 51, so we don't need to redefine here again. Assembly can use ABI name directly without '$'
Here, I am merely mapping these variables to the corresponding register numbers, allowing subsequent code to refer to registers by variable names. This enhances code readability and maintainability and does not constitute a redefinition of the ABI. I have tried removing the '$', and the code fails to compile at this point.
Thank you for your response and recognition! I completely understand that you've been busy with work recently, and I truly appreciate you taking the time to review this. |
Thank you for your testing and confirmation. |
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 % |
7d72f7d to
cbd20d6
Compare
|
@HeliC829 , I have made the modifications as per your suggestion and pushed the updated code. Could you please review it again? Thanks! |
|
@paulidale Could you please review this pull request? Thank you! |
|
This pull request is ready to merge |
|
Merged to the master branch with trimmed whitespace at EOL. Thank you for your contribution. |
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 adds an RV64GC-optimized Montgomery squaring assembly, ported from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (#28012).
Performance Improvement