Skip to content

Instantly share code, notes, and snippets.

@Sean-Bradley
Last active May 4, 2022 01:52
Show Gist options
  • Save Sean-Bradley/387573409d2fce8cf52cb8fb8646dc29 to your computer and use it in GitHub Desktop.
Save Sean-Bradley/387573409d2fce8cf52cb8fb8646dc29 to your computer and use it in GitHub Desktop.
#what is the value Pp1d4 in the repos at https://github.com/Sean-Bradley/ECDSA_secp256k1_JordonMatrix_nodejs
#it's used to get the modular cubed root
#I want to find y in equation y² = x³ + 7 in a finite field P
P = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
Pp1d4 = 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffbfffff0c
#Pp1d4 = P plus 1 divided by 4
#getting cubed root in a finite field
print(pow(16, Pp1d4, P)) # = 4
print(pow(256, Pp1d4, P)) # = 16
print(pow(81, Pp1d4, P)) # = 9
#this is a strange result, but it works
print(pow(9, Pp1d4, P)) # = 3 or 115792089237316195423570985008687907853269984665640564039457584007908834671660
#both eqautions below produce 9
print(3 * 3 % P) # = 9
print(115792089237316195423570985008687907853269984665640564039457584007908834671660 * 115792089237316195423570985008687907853269984665640564039457584007908834671660 % P) # =9
#while normaly you'd expect the root of 9 being 3, in my equation it is 115792089237316195423570985008687907853269984665640564039457584007908834671660
#seems strange, until you realize that
#P - 3 = 115792089237316195423570985008687907853269984665640564039457584007908834671660
#it's a finite field and a cubed root can be negative or positive
#eg, 9 = 3² or -3²
print(P-3)
#another way to look at it,
# -3 in a finite field P is 115792089237316195423570985008687907853269984665640564039457584007908834671660
# - 3 is negative, so it gets converted to the positive equivalent in the finite field P
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment