Fix roots of unity generation

This commit is contained in:
danielsanchezq 2024-06-11 18:08:02 +02:00
parent a3b501cbcb
commit 3620cdc8ae
2 changed files with 27 additions and 14 deletions

View File

@ -1,4 +1,4 @@
from typing import List
from typing import List, Tuple
import eth2spec.eip7594.mainnet
from py_ecc.bls.typing import G1Uncompressed, G2Uncompressed
@ -15,5 +15,7 @@ BLS_MODULUS = eth2spec.eip7594.mainnet.BLS_MODULUS
GLOBAL_PARAMETERS: List[G1]
GLOBAL_PARAMETERS_G2: List[G2]
# secret is fixed but this should come from a different synchronization protocol
GLOBAL_PARAMETERS, GLOBAL_PARAMETERS_G2 = map(list, generate_setup(1024, 8, 1987))
ROOTS_OF_UNITY: List[int] = compute_roots_of_unity(2, BLS_MODULUS, 4096)
GLOBAL_PARAMETERS, GLOBAL_PARAMETERS_G2 = map(list, generate_setup(4096, 8, 1987))
ROOTS_OF_UNITY: Tuple[int] = compute_roots_of_unity(
7, 4096, BLS_MODULUS
)

View File

@ -1,14 +1,25 @@
def compute_roots_of_unity(primitive_root, p, n):
"""
Compute the roots of unity modulo p.
from typing import Tuple
Parameters:
primitive_root (int): Primitive root modulo p.
p (int): Modulus.
n (int): Number of roots of unity to compute.
Returns:
list: List of roots of unity modulo p.
def compute_root_of_unity(primitive_root: int, order: int, modulus: int) -> int:
"""
roots_of_unity = [pow(primitive_root, i, p) for i in range(n)]
return roots_of_unity
Generate a w such that ``w**length = 1``.
"""
assert (modulus - 1) % order == 0
return pow(primitive_root, (modulus - 1) // order, modulus)
def compute_roots_of_unity(primitive_root: int, order: int, modulus: int) -> Tuple[int]:
"""
Compute a list of roots of unity for a given order.
The order must divide the BLS multiplicative group order, i.e. BLS_MODULUS - 1
"""
assert (modulus - 1) % order == 0
root_of_unity = compute_root_of_unity(primitive_root, order, modulus)
roots = []
current_root_of_unity = 1
for _ in range(order):
roots.append(current_root_of_unity)
current_root_of_unity = current_root_of_unity * root_of_unity % modulus
return tuple(roots)