| 1 |
#!/usr/bin/env sage |
| 2 |
|
| 3 |
# This file recomputes the values in src/fp.rs for each FFT-friendly finite |
| 4 |
# field. |
| 5 |
|
| 6 |
import pprint |
| 7 |
|
| 8 |
|
| 9 |
class Field: |
| 10 |
# The name of the field. |
| 11 |
name: str |
| 12 |
|
| 13 |
# The prime modulus that defines the field. |
| 14 |
modulus: Integer |
| 15 |
|
| 16 |
# A generator element that generates a large subgroup with an order that's |
| 17 |
# a power of two. This is _not_ in Montgomery representation. |
| 18 |
generator_element: Integer |
| 19 |
|
| 20 |
# The base 2 logarithm of the order of the FFT-friendly multiplicative |
| 21 |
# subgroup. The generator element will be a 2^num_roots-th root of unity. |
| 22 |
num_roots: Integer |
| 23 |
|
| 24 |
def __init__(self, name, modulus, generator_element): |
| 25 |
assert is_prime(modulus) |
| 26 |
self.name = name |
| 27 |
self.modulus = modulus |
| 28 |
self.generator_element = generator_element |
| 29 |
|
| 30 |
self.num_roots = None |
| 31 |
for (prime, power) in factor(modulus - 1): |
| 32 |
if prime == 2: |
| 33 |
self.num_roots = power |
| 34 |
break |
| 35 |
else: |
| 36 |
raise Exception( |
| 37 |
"Multiplicative subgroup order is not a multiple of two" |
| 38 |
) |
| 39 |
|
| 40 |
def mu(self): |
| 41 |
""" |
| 42 |
Computes mu, a constant used during multiplication. It is defined by |
| 43 |
mu = (-p)^-1 mod r, where r is the modulus implicitly used in wrapping |
| 44 |
machine word operations. |
| 45 |
""" |
| 46 |
r = 2 ^ 64 |
| 47 |
return (-self.modulus).inverse_mod(r) |
| 48 |
|
| 49 |
def r2(self): |
| 50 |
""" |
| 51 |
Computes R^2 mod p. This constant is used when converting into |
| 52 |
Montgomery representation. R is the machine word-friendly modulus |
| 53 |
used in the Montgomery representation. |
| 54 |
""" |
| 55 |
R = 2 ^ 128 |
| 56 |
return R ^ 2 % self.modulus |
| 57 |
|
| 58 |
def to_montgomery(self, element): |
| 59 |
""" |
| 60 |
Transforms an element into its Montgomery representation. |
| 61 |
""" |
| 62 |
R = 2 ^ 128 |
| 63 |
return element * R % self.modulus |
| 64 |
|
| 65 |
def bit_mask(self): |
| 66 |
""" |
| 67 |
An integer with the same bit length as the prime modulus, but with all |
| 68 |
bits set. |
| 69 |
""" |
| 70 |
return 2 ^ (self.modulus.nbits()) - 1 |
| 71 |
|
| 72 |
def roots(self): |
| 73 |
""" |
| 74 |
Returns a list of roots of unity, in Montgomery representation. The |
| 75 |
value at index i is a 2^i-th root of unity. Note that the first array |
| 76 |
element will thus be the Montgomery representation of one. |
| 77 |
""" |
| 78 |
return [ |
| 79 |
self.to_montgomery( |
| 80 |
pow( |
| 81 |
self.generator_element, |
| 82 |
2 ^ (self.num_roots - i), |
| 83 |
self.modulus, |
| 84 |
) |
| 85 |
) |
| 86 |
for i in range(min(self.num_roots, 20) + 1) |
| 87 |
] |
| 88 |
|
| 89 |
|
| 90 |
FIELDS = [ |
| 91 |
Field( |
| 92 |
"FieldPrio2", |
| 93 |
2 ^ 20 * 4095 + 1, |
| 94 |
3925978153, |
| 95 |
), |
| 96 |
Field( |
| 97 |
"Field64", |
| 98 |
2 ^ 32 * 4294967295 + 1, |
| 99 |
pow(7, 4294967295, 2 ^ 32 * 4294967295 + 1), |
| 100 |
), |
| 101 |
Field( |
| 102 |
"Field128", |
| 103 |
2 ^ 66 * 4611686018427387897 + 1, |
| 104 |
pow(7, 4611686018427387897, 2 ^ 66 * 4611686018427387897 + 1), |
| 105 |
), |
| 106 |
] |
| 107 |
for field in FIELDS: |
| 108 |
print(field.name) |
| 109 |
print(f"p: {field.modulus}") |
| 110 |
print(f"mu: {field.mu()}") |
| 111 |
print(f"r2: {field.r2()}") |
| 112 |
print(f"g: {field.to_montgomery(field.generator_element)}") |
| 113 |
print(f"num_roots: {field.num_roots}") |
| 114 |
print(f"bit_mask: {field.bit_mask()}") |
| 115 |
print("roots:") |
| 116 |
pprint.pprint(field.roots()) |
| 117 |
print() |