Skip to content
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

secp256k1: Optimize field inverse calc. #3421

Merged
merged 2 commits into from
Aug 12, 2024

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Aug 9, 2024

This optimizes the field multiplicative inverse calculation to use a more optimal addition chain which reduces the number of field squarings from 258 to 255 and the number field multiplications from 33 to 15.

This calculation is primarily involved when converting back to affine space which is done for various things such as:

  • Calculating public keys
  • ECDSA signing
  • Generating shared secrets via ECDHE
  • Public key recovery from a compact signature
  • Schnorr signing and signature verification
  • Calculating hierarchical deterministic extended keys

The following benchmarks show a before and after comparison of field inversion as well as how it that translates to public key calculation, Schnorr signature verification, and recovery from compact signatures:

name               old time/op   new time/op   delta
------------------------------------------------------------------------
FieldInverse       12.0µs ± 0%   10.9µs ± 1%   -8.96%  (p=0.008 n=10+10)
PrivateKeyPubKey   35.0µs ± 1%   33.9µs ± 2%   -3.18%  (p=0.008 n=10+10)
SchnorrSigVerify    122µs ± 1%    121µs ± 1%   -0.82%  (p=0.015 n=10+10)
RecoverCompact      137µs ± 1%    135µs ± 1%   -1.35%  (p=0.002 n=10+10)

@davecgh davecgh added this to the 2.1.0 milestone Aug 9, 2024
@davecgh davecgh force-pushed the secp256k1_optimize_field_inverse branch from 778b935 to 9f6426c Compare August 9, 2024 18:57
@davecgh davecgh force-pushed the secp256k1_optimize_field_inverse branch from 9f6426c to 848ba19 Compare August 12, 2024 16:37
f.Square().Square().Square() // f = a^(2^254 - 1073742072)
f.Mul(&a2) // f = a^(2^254 - 1073742069)
f.Square().Square() // f = a^(2^256 - 4294968276)
return f.Mul(&a) // f = a^(2^256 - 4294968275) = a^(p-2)
This optimizes the field multiplicative inverse calculation to use a
more optimal addition chain which reduces the number of field squarings
from 258 to 255 and the number field multiplications from 33 to 15.

This calculation is primarily involved when converting back to affine
space which is done for various things such as:

- Calculating public keys
- ECDSA signing
- Generating shared secrets via ECDHE
- Public key recovery from a compact signature
- Schnorr signing and signature verification
- Calculating hierarchical deterministic extended keys

The following benchmarks show a before and after comparison of field
inversion as well as how it that translates to public key calculation,
schnorr signature verification, and recovery from compact signatures:

name               old time/op   new time/op   delta
------------------------------------------------------------------------
FieldInverse       12.0µs ± 0%   10.9µs ± 1%   -8.96%  (p=0.008 n=10+10)
PrivateKeyPubKey   35.0µs ± 1%   33.9µs ± 2%   -3.18%  (p=0.008 n=10+10)
SchnorrSigVerify    122µs ± 1%    121µs ± 1%   -0.82%  (p=0.015 n=10+10)
RecoverCompact      137µs ± 1%    135µs ± 1%   -1.35%  (p=0.002 n=10+10)
@davecgh davecgh force-pushed the secp256k1_optimize_field_inverse branch from 848ba19 to 9aba0ce Compare August 12, 2024 18:13
@davecgh
Copy link
Member Author

davecgh commented Aug 12, 2024

Rebased to latest master.

@davecgh davecgh merged commit 9aba0ce into decred:master Aug 12, 2024
2 checks passed
@davecgh davecgh deleted the secp256k1_optimize_field_inverse branch August 12, 2024 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants