Skip to content

Implement Montgomery multiplication assembly optimization for RV64GC - #28012

Closed
fengpengboa wants to merge 1 commit into
openssl:masterfrom
fengpengboa:rv64-mont-asm
Closed

fengpengboa wants to merge 1 commit into
openssl:masterfrom
fengpengboa:rv64-mont-asm

Conversation

@fengpengboa

@fengpengboa fengpengboa commented Jul 10, 2025 •

Copy link
Copy Markdown
Contributor

This PR introduces the RV64GC-optimized assembly implementation for Montgomery multiplication, and significantly accelerating BN modular exponentiation and RSA operations.

Performance Improvement

openssl speed rsa2048

benchmark results run on SG2042:

Test Item Baseline (C) ASM Optimized Improvement
​sign/s 101.6 209.0 105.71%
​verify/s ​4004.0 ​7270.1 81.57%
encr./s 3804.2 6682.1 ​75.65%
decr./s 100.7 205.3 ​103.87%

Test Verification

All relevant tests pass in the default build configuration:

make test

All tests successful.
Files=345, Tests=4491, 2186 wallclock secs (32.44 usr  3.42 sys + 1719.95 cusr 772.27 csys = 2528.08 CPU)
Result: PASS
make[2]: Leaving directory '/home/mnt/fpb/submit/openssl-master'
make[1]: Leaving directory '/home/mnt/fpb/submit/openssl-master'
Checklist
  • documentation is added or updated
  • tests are added or updated

@t8m t8m added branch: master Applies to master branch approval: review pending This pull request needs review by a committer triaged: feature The issue/pr requests/adds a feature tests: exempted The PR is exempt from requirements for testing triaged: performance The issue/pr reports/fixes a performance concern and removed triaged: feature The issue/pr requests/adds a feature labels Jul 10, 2025
@github-actions github-actions Bot added the severity: fips change The pull request changes FIPS provider sources label Jul 10, 2025
@fengpengboa

Copy link
Copy Markdown
Contributor Author

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!

@t8m

t8m commented Jul 25, 2025

Copy link
Copy Markdown
Member

@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.

@t8m

t8m commented Jul 25, 2025

Copy link
Copy Markdown
Member

@ZenithalHourlyRate @HeliC829 @JerryShih previously reviewed RISC-V assembly code.

@ZenithalHourlyRate ZenithalHourlyRate left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated

$code .= <<___;

mv s0, sp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
addi $ap, $ap, 16
slli $num, $num, 3
ld $n0, 0($n0)
andi $tp, $tp, -16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this be addi $tp, $tp, -16?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As previously mentioned, this instruction performs ​​memory address alignment​​, rather than an addioperation.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated

$code .= <<___;

.Lmul_mont:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might consider adding a branch here subsequently. If it's unnecessary now, I can delete it.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
.Lmul_mont:
ld $m0, 0($bp)
addi $bp, $bp, 8
slli $temp, $num, 3

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be merged with slli $num, $num, 3 below

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will modify this part

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
addi $np, $np, 16

mul $lo0, $hi0, $m0
addi $j, $num, -16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
mulhu $ahi, $aj, $m0

mul $m1, $lo0, $n0
mv sp, $tp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, $tp

into

slli $num, $num, 3
sub sp, sp, $num
sub sp, sp, 16
mv  $tp, sp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make the modifications as suggested.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
addi $tp, $tp, 8
bnez $j, .L1st

.L1st_skip:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name such section in a style like 1st_fix_tp_carry instead of an informationless skip; same for inner_skip below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make the modifications as suggested.

Comment thread crypto/bn/asm/riscv64-mont.pl Outdated
Comment on lines +405 to +411
.Lcond_copy:
addi $num,$num, -8
bnez $carry1,.set_aj1
mv $nj,$tj
j .end1
.set_aj1:
mv $nj,$aj

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These bnez/j are not constant-time. You can use snez to implement a constant time selection logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make the modifications as suggested

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@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.

Thank you for the recommendation. Should the reviewer’s availability be limited, we will proactively identify qualified RISC-V experts for an independent review.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

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.

​​The instruction andi $tp, $tp, -16 is used for memory address alignment, rather than an addi operation.In addition, I will supplement the relevant code comments, including the meaning of the variable names.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@t8m Hi, I've updated the code per review comments. Could you please approve the CI workflow? Thanks!

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@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 ZenithalHourlyRate left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM! Just some questions and nitpicking comments.

Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
Comment thread crypto/bn/asm/riscv64-mont.pl Resolved
@fengpengboa

Copy link
Copy Markdown
Contributor Author

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!

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@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.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@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 ZenithalHourlyRate left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my side I think it is ok.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

From my side I think it is ok.

Thank you for your review! I really appreciate the confirmation from your side.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@ZenithalHourlyRate Could you please add the formal approval checkmark when you have a moment?

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@t8m @nhorman Could you please help move this PR forward for merging? We have one approval and one verbal LGTM. Is there anything else required from our side?

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@HeliC829 Hi, I would be grateful if you could find the time to review this PR. Thank you!

@HeliC829 HeliC829 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@fengpengboa

Copy link
Copy Markdown
Contributor Author

LGTM.

Thank you for the review!

@fengpengboa

Copy link
Copy Markdown
Contributor Author

Hi @slontis , could you please review this PR when you have time? Thank you!

@fengpengboa

Copy link
Copy Markdown
Contributor Author

Hi @InfoHunter , could you please review this PR when you have time? Thank you!

@fengpengboa

Copy link
Copy Markdown
Contributor Author

@t8m @nhorman Hi ,This PR has been pending for some time. Could you please help to move it forward? A review and merge would be greatly appreciated.

@nhorman nhorman added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Sep 15, 2025
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels Sep 16, 2025
@openssl-machine

Copy link
Copy Markdown
Collaborator

This pull request is ready to merge

@paulidale

Copy link
Copy Markdown
Contributor

Merged after removing end of line whitespace. Thanks for the contribution.

@paulidale paulidale closed this Sep 17, 2025
openssl-machine pushed a commit that referenced this pull request Sep 17, 2025
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 18, 2025
…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.
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 18, 2025
… from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012).
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 19, 2025
…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.
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 19, 2025
…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.
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 22, 2025
…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.
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Dec 30, 2025
… from the ARMv8 __bn_sqr8x_mont algorithm, complementing the earlier multiplication optimization (openssl#28012).
openssl-machine pushed a commit that referenced this pull request Dec 30, 2025
…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)
fengpengboa added a commit to fengpengboa/openssl that referenced this pull request Feb 27, 2026
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 %        |
openssl-machine pushed a commit that referenced this pull request Mar 13, 2026
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)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Applies to master branch severity: fips change The pull request changes FIPS provider sources tests: exempted The PR is exempt from requirements for testing triaged: performance The issue/pr reports/fixes a performance concern

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants