Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add fast point-to-scalar multiplication via ecrecover #19

Merged
merged 3 commits into from
Jun 26, 2024

Conversation

StackOverflowExcept1on
Copy link
Contributor

@StackOverflowExcept1on StackOverflowExcept1on commented May 15, 2024

Resolves #18

Explanation of how it works:

  • ecrecover(e, v, r, s) works according to the formula $Q = r^{-1} ( sR - eG )$ from SEC-1 v2 section 4.1.6 "Public Key Recovery Operation"
  • the formula has some limitations: $v \in [ 27, 28 ], r \ne 0, s \ne 0$
  • to calculate $scalar \cdot Point$ need to substitute the following values:
    $e = 0, v = PointYParity + 27, r = PointX, s = r \cdot scalar$
  • so the formula will look like this after substitution:
    $Q = r^{-1} sR = r^{-1} \cdot r \cdot scalar \cdot R = \cancel{r^{-1}} \cdot \cancel{r} \cdot scalar \cdot R = scalar \cdot R$
  • point $R(x, y)$ has coordinates $(r, y)$, where $y$ is calculated from $x$ (coordinate compression) and $v$ (even/odd $y$ is specified)

@pmerkleplant
Copy link
Member

The code looks very good! Thanks for taking the time and following the projects coding style <3

I'll need some time to review the math (crysol is a learning project after all :P) and think about reasonability of the functionality.

@StackOverflowExcept1on
Copy link
Contributor Author

As for math, Vitalik approves xD! https://ethresear.ch/t/you-can-kinda-abuse-ecrecover-to-do-ecmul-in-secp256k1-today/2384
Also in thread there is dude k06a who is the co-founder of the 1inch network and this is basically a rewrite of his code.

Copy link
Member

@pmerkleplant pmerkleplant left a comment

Choose a reason for hiding this comment

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

Finally managed to jump back into this, sorry for taking so long.

Note that I renamed to function to mulToAddress() which, imho, carries more semantic meaning. Added a note to the function doc about the performance increase though.

Many thanks for the great PR!

@pmerkleplant pmerkleplant merged commit ac04a6b into verklegarden:main Jun 26, 2024
4 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add on-chain operation to derive address from private key using ecrecover
2 participants