Inverse modulo doesn't give the same result as in Java (with BigInteger) #1182
-
Hello, I'm trying to compute the inverse modulo with your function but I don't get the "correct" result. There is something I don't understand. To illustrate, I made a simple example in java to compare: Java (with BigInteger.modInverse) : In each case, I test: a * inva = 1 I tried to multiply in java the inverse obtained in cpp by the number: I don't understand... How to get the same result as Java, please? I don't see the trick. Here is my code with your lib:
Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
The prime you've put is the field's order, not the scalar's order. >>> hex(pow(7, -1, 115792089237316195423570985008687907852837564279074904382605163141518161494337))
0x49249249249249249249249249249248c79facd43214c011123c1b03a93412a5 And if you use the static const secp256k1_fe secp256k1_fe_seven = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 7);
unsigned char sever_inv_bytes[32];
secp256k1_fe inv_7;
secp256k1_fe_inv(&inv_7, &secp256k1_fe_seven);
secp256k1_fe_get_b32(sever_inv_bytes, &inv_7);
printf("inv_7 = 0x");
for (int i = 0; i < 32; i++)
printf("%02x", sever_inv_bytes[i]);
printf("\n");
return 0; Which prints the same as this python: >>> hex(pow(7, -1, 115792089237316195423570985008687907853269984665640564039457584007908834671663))
'0xdb6db6db6db6db6db6db6db6db6db6db6db6db6db6db6db6db6db6da9249214d' |
Beta Was this translation helpful? Give feedback.
-
Ok, thank you. I'm not sure I understand completely.
I would like to code an elliptic addition only with add, mul, inverse, etc. But I don't get the correct results. Maybe you could help me? Here is my code:
I don't get the correct answer for a and m (may be the invmod?). |
Beta Was this translation helpful? Give feedback.
-
The X and Y coordinates on the elliptic curves are numbers modulo p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f. They are referred to as field elements in the libsecp256k1 source code, with type Points on the curve are referred to as group elements ( Scalars ( You can't use scalars for field operations; the modulus will be wrong. |
Beta Was this translation helpful? Give feedback.
The X and Y coordinates on the elliptic curves are numbers modulo p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f. They are referred to as field elements in the libsecp256k1 source code, with type
secp256k1_fe
.Points on the curve are referred to as group elements (
secp256k1_ge
andsecp256k1_gej
).Scalars (
secp256k1_scalar
) are integers modulo the number of points on the curve, n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141. They're used for private keys, nonces, ...You can't use scalars for field operations; the modulus will be wrong.