-
Notifications
You must be signed in to change notification settings - Fork 293
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
secp256k1: Optimize field inverse calc. #3421
Conversation
778b935
to
9f6426c
Compare
9f6426c
to
848ba19
Compare
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) |
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.
Equation verification via Wolfram Alpha:
[a3] a^(2^3-1) = (a^(2^2-1))^2 * a
[a6] a^(2^6-1) = (a^(2^3-1))^(2^3) * a^(2^3-1)
[a9] a^(2^9-1) = (a^(2^6-1))^(2^3) * a^(2^3-1)
[a11] a^(2^11-1) = (a^(2^9-1))^(2^2) * a^(2^2-1)
[a22] a^(2^22-1) = (a^(2^11-1))^(2^11) * a^(2^11-1)
[a44] a^(2^44-1) = (a^(2^22-1))^(2^22) * a^(2^22-1)
[a88] a^(2^88-1) = (a^(2^44-1))^(2^44) * a^(2^44-1)
[a176] a^(2^176-1) = (a^(2^88-1))^(2^88) * a^(2^88-1)
[a220] a^(2^220-1) = (a^(2^176-1))^(2^44) * a^(2^44-1)
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)
848ba19
to
9aba0ce
Compare
Rebased to latest master. |
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:
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: