-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ElligatorSwift + integrated x-only DH #1129
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(just skimming)
Do you think there's a nice way to avoid code duplication between divsteps
and posdivsteps
?
@real-or-random Re posdivsteps see #979 which this PR is based on. |
Looks awesome! Wanted to clarify this bit, for implementors of EllSwift in other languages / for standardization purpose.
To which points exactly? It's not immediately obvious from the code. |
@paulmillr They are remapped as follows:
(for applicable odd-ordered curves with A=0, this covers all otherwise unmapped points, but this remapping doesn't work for every ellswift-compatible curve). |
34227ad
to
3f35f9e
Compare
3f35f9e
to
e46f81a
Compare
Added test vectors. |
0d17864
to
6dc30bb
Compare
I've made a number of improvements:
I think it's ready for more review. |
6dc30bb
to
1f82865
Compare
d27b68e
to
fad8b11
Compare
fad8b11
to
9a66978
Compare
Rebased on updated #979. |
9a66978
to
b7d5775
Compare
Added an explanation of the algorithm and its relation to the paper, in |
tACK 90e360a. Full testing backlog:
Test environments:Config script: Machine 1: Arch Linux on a AMD Ryze 5 (Linux 6.1.21-hardened1-1-hardened #1 SMP PREEMPT_DYNAMIC x86_64 GNU/Linux ) Machine 2: Raspbian on a Raspbery Pi 4b (Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT aarch64 GNU/Linux) Benchmarks (only for the new module)Machine 1, clang
Machine 1, gcc
Machine 2, clang
Machine 2, gcc
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 90e360a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 90e360a
This has been merged as it received adequate reviews and enough ACKs (as discussed in IRC). Post-merge ACKs are still welcome. If any issues still arise, they can be addressed in follow-up PRs. |
/** Given a private key, and ElligatorSwift public keys sent in both directions, | ||
* compute a shared secret using x-only Elliptic Curve Diffie-Hellman (ECDH). | ||
* | ||
* Returns: 1: shared secret was succesfully computed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Returns: 1: shared secret was succesfully computed | |
* Returns: 1: shared secret was successfully computed |
\begin{array}{lcl} | ||
X(u, t) & = & \left\\{\begin{array}{ll} | ||
\dfrac{g(u) - t^2}{2t} & a = 0 \\ | ||
\dfrac{g(u) + h(u)(Y_0(u) + X_0(u)t)^2}{X_0(u)(1 + h(u)t^2)} & a \neq 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
swiftEC paper shows this:
P_u^{-1}(X, Y) = \left\\{\begin{array}{ll} | ||
Yu\sqrt{-3} - X & a = 0 \\ | ||
\dfrac{Y-Y_0(u)}{X-X_0(u)} & a \neq 0 \land X \neq X_0(u) \\ | ||
\dfrac{-X_0(u)}{h(u)Y_0(u)} & a \neq 0 \land X = X_0(u) \land Y = Y_0(u) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried using WolframAlpha(my first time) for this condition - link.
EDIT: sorry for the spam! got it when i plugged in the right equation.
* If $s = 0$, return $\bot.$ | ||
* Let $v = (r/s - u)/2.$ | ||
* Let $w = \sqrt{s}$; return $\bot$ if not square. | ||
* If $a \neq 0$ and $w(u+2v) = 2X_0(u)$ and either $w \neq 2Y_0(u)$ or $h(u) = 0$, return $\bot.$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(from L332 actually) how does this happen? (when Y = 0 in x1, x2 computation or something else?)
EDIT: L332 needs to be updated to
* If $c = 3$ and $r = 0$, return $\bot.$ | ||
* Let $v = (r/s - u)/2.$ | ||
* Let $w = \sqrt{s}$; return $\bot$ if not square. | ||
* Let $w' = w$ if $sign(w/2) = sign(y)$; $-w$ otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
post-merge ACK 90e360a. |
Builds on top of #979, #1118. Replaces #982.
This implements encoding of curve points using the ElligatorSwift algorithm, using 4 new API calls:
secp256k1_ellswift_encode
, which converts a public key to a 64-byte pseudorandom encoding.secp256k1_ellswift_decode
, the reverse operation to convert back to normal public keys.secp256k1_ellswift_create
, which can be seen as a combination ofsecp256k1_ec_pubkey_create
+secp256k1_ellswift_encode
, but is somewhat safer.secp256k1_ellswift_xdh
, which implements x-only Diffie-Hellman directly on top of 64-byte encoded public keys, and more efficiently than decoding + invoking normal ECDH.The scheme matches that of the SwiftEC paper (https://eprint.iacr.org/2022/759), with two changes (remapping undefined inputs, and encoding the Y parity in the u/t values themselves rather than in a separate bit). To decode an ElligatorSwift 64-byte encoded public key:
This is significantly faster than the Elligator Squared code in #982.
Relevant benchmark (AMD Ryzen 5950X, GCC 12.2.0, default config options; frequency fixed at 2.80 GHz):