Skip to content

Commit 7c5ecc2

Browse files
authored
Merge pull request #1 from tusharv01/tusharv01-patch-1
Update modinv32.h
2 parents 4e68262 + cf657b8 commit 7c5ecc2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/modinv32.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,45 @@ typedef struct {
3030
*
3131
* On output, all of x's limbs will be in [0, 2^30).
3232
*/
33+
typedef struct {
34+
uint32_t m; // Modulus
35+
uint32_t minv8; // Precomputed inverse of m % 8
36+
uint32_t n; // Range for modular inverse
37+
} secp256k1_modinv32_modinfo;
38+
39+
int secp256k1_modinv32_modinfo_verify(const secp256k1_modinv32_modinfo* modinfo) {
40+
// Verify that m is prime
41+
if (!is_prime(modinfo->m)) {
42+
return 1; // Error code for non-prime modulus
43+
}
44+
45+
// Verify that minv8 is correct
46+
if ((modinfo->m % 8) != 0 && modinfo->minv8 != modinv32(modinfo->m % 8)) {
47+
return 2; // Error code for incorrect minv8
48+
}
49+
50+
// Verify that n is in the correct range
51+
if (modinfo->n < 2 || modinfo->n > modinfo->m) {
52+
return 3; // Error code for out-of-range n
53+
}
54+
55+
// No errors found
56+
return 0;
57+
}
58+
59+
int secp256k1_modinv32_do_something(const secp256k1_modinv32_modinfo* modinfo, ...) {
60+
// Call secp256k1_modinv32_modinfo_verify on entry
61+
int verify_result = secp256k1_modinv32_modinfo_verify(modinfo);
62+
if (verify_result != 0) {
63+
return verify_result; // Pass along the error code
64+
}
65+
66+
// Do something with modinfo
67+
...
68+
69+
return 0; // Success
70+
}
71+
3372
static void secp256k1_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo);
3473

3574
/* Same as secp256k1_modinv32_var, but constant time in x (not in the modulus). */

0 commit comments

Comments
 (0)