Add SM2 implementation in generic riscv64 asm by geliyaz · Pull Request #25918 · openssl/openssl · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Add SM2 implementation in generic riscv64 asm #25918

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

geliyaz
Copy link

@geliyaz geliyaz commented Nov 11, 2024

This PR adds the support of SM2 implementation for RISC-V 64 platform with base integer instruction set.

Testing
For testing, Built-in test suite was run using user-mode qemu. All tests pass.
First, configuring the build

$ CC="riscv64-unknown-linux-gnu-gcc" ./Configure linux64-riscv64 no-afalgeng
$ make -j <num threads>

Next, I ran the built-in test suite using make test. The summary is below:

All tests successful.
FFiles=327, Tests=3842, 8418 wallclock secs (99.68 usr 26.07 sys + 3654.55 cusr 3638.32 csys = 7418.62 CPU)
Result: PASS

benchmark results run on SG2042:

sign verify sign/s verify/s
Before 0.0024s 0.0019s 414.2 525.1
After 0.0002s 0.0005s 6367.7 1839.8
After(no-sm2-precomp) 0.0005s 0.0009s 1953.2 1112.8
Checklist
  • documentation is added or updated
  • tests are added or updated

Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

Could we get an independent review from another RISC-V developer?

@t8m t8m added branch: master Merge 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 labels Nov 11, 2024
@geliyaz geliyaz force-pushed the master branch 2 times, most recently from b724adb to 2af3a2a Compare November 12, 2024 03:16
@geliyaz
Copy link
Author

geliyaz commented Nov 12, 2024

Could we get an independent review from another RISC-V developer?

Sure!

@geliyaz geliyaz requested a review from t8m November 12, 2024 06:16
@openssl-machine
Copy link
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@geliyaz
Copy link
Author

geliyaz commented Dec 15, 2024

Could we get an independent review from another RISC-V developer?

how is the review going?

@t8m
Copy link
Member

t8m commented Dec 16, 2024

Please drop the merge commit - git reset --hard HEAD~ ; git push --force

@openssl-machine
Copy link
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@paulidale
Copy link
Contributor

We still need one of the RISC-V savvy developers to have a look at this.

add $b0, $t8, $b0

// Select based on carry
bltu $c0, $b0, ${mod}_mod_add

Choose a reason for hiding this comment

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

(Don't know much about either SM2 or RISC-V, but daring to ask) Is this branch a side-channel? Or is there no requirement for constant-time in this function?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing that out! We've made adjustments to ensure the function operates in constant-time, reducing the risk of side-channel attacks. Here's the updated code:

	slt $c0, $c0, $c3
	negw $c1, $c0
	addw $c0, $c0, -1

	and $t0, $t0, $c1
	and $t1, $t1, $c1
	and $t2, $t2, $c1
	and $t3, $t3, $c1
	and $t4, $t4, $c0
	and $t5, $t5, $c0
	and $t6, $t6, $c0
	and $t7, $t7, $c0
	or $t0, $t0, $t4
	or $t1, $t1, $t5
	or $t2, $t2, $t6
	or $t3, $t3, $t7

Let me know if you have any further concerns or suggestions!

@openssl-machine
Copy link
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@openssl-machine
Copy link
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@HeliC829
Copy link
Contributor

HeliC829 commented May 3, 2025

Can you provide benchmark results run on a real RISC-V board for comparison, like in #20754 ?

@geliyaz
Copy link
Author

geliyaz commented May 6, 2025

Can you provide benchmark results run on a real RISC-V board for comparison, like in #20754 ?

Yes, I've updated the benchmark results run on a real RISC-V board(SG2042)

@geliyaz geliyaz requested a review from vdukhovni May 19, 2025 10:35
@openssl-machine
Copy link
Collaborator

This PR is in a state where it requires action by @openssl/committers but the last update was 30 days ago

@geliyaz
Copy link
Author

geliyaz commented Jun 26, 2025

Pinging some RISC-V code developers for code review @ZenithalHourlyRate @xicilion @HeliC829 @JerryShih @cyyself

Copy link
Contributor

@ZenithalHourlyRate ZenithalHourlyRate left a comment

Choose a reason for hiding this comment

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

LGTM! I did not see the CI running, would be more confident when seeing the CI passing.

Copy link
Member

@slontis slontis left a comment

Choose a reason for hiding this comment

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

Just started looking at the instruction set..
Is it intended that instructions such as SLTI are constant time?

@slontis slontis closed this Jun 27, 2025
@slontis slontis reopened this Jun 27, 2025
@slontis
Copy link
Member

slontis commented Jun 27, 2025

Kicked off the tests..
This is the one we care about here...
https://github.com/openssl/openssl/actions/runs/15919802043/job/44904046728?pr=25918

# Function arguments
# Input block pointer, output block pointer, key pointer
my ($i0, $i1, $i2) = use_regs(10 .. 12);
my ($t0, $t1, $t2, $t3, $t4, $t5, $t6, $t7, $c0, $c1, $c2, $c3) = use_regs(5 .. 7, 13 ... 17, 28 .. 31);
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason for reassigning t and s variables here to different values than riscv.pm?

Copy link
Author

Choose a reason for hiding this comment

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

The reassignment of the t and s variables (primarily t here) is to manage register usage more clearly during assembly code generation, specifically to distinguish which registers are temporary (and do not need to be saved) from those that need to be saved (note that s registers are not used here, indicating that this function likely does not use any callee-saved registers).

t8m
t8m previously approved these changes Jun 30, 2025
@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label Jun 30, 2025
ld $t6, 16($i2)
ld $t7, 24($i2)

sltu $c2, $c0, $c3
Copy link
Member

Choose a reason for hiding this comment

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

Where does $c3 come from (it doesnt look like its set here)

Copy link
Author

Choose a reason for hiding this comment

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

The comparison instruction is unnecessary in this context, so it is redundant. I have modified it.

}

sub bn_mod_div_by_2() {
my $mod = shift;
Copy link
Member

@slontis slontis Jul 1, 2025

Choose a reason for hiding this comment

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

There is a long history in openssl of minimal comments in assembler files. This might be great for the author, but its probably not good for long term maintainability after it has been sitting in the code repository for many years.

Is there any chance that you could document this file a bit more to make it easier to follow and review?

e.g. for this function
// The template function bn_mod_div_by_2(a)
// returns (if a_is_odd ? a + mod : a) >> 1
// where mod is one of ...

I see the an EC implementation for a similar function that seems to do this with or's and shifts.
Maybe a few words explaining what is does would be useful.. Since it loks like you shift a first and then start adding to it..

Even the bn_sub function is hard to follow (at least for RISC-V).
The Additions of the carries hurt my brain...

Copy link
Author

Choose a reason for hiding this comment

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

I‘ve added more comments, kindly conduct a re-review.

@github-actions github-actions bot removed the severity: fips change The pull request changes FIPS provider sources label Jul 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: review pending This pull request needs review by a committer branch: master Merge to master branch tests: exempted The PR is exempt from requirements for testing triaged: feature The issue/pr requests/adds a feature triaged: performance The issue/pr reports/fixes a performance concern
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.