Skip to content

Instantly share code, notes, and snippets.

@ralexstokes
Created December 1, 2021 17:21
Show Gist options
  • Save ralexstokes/f1d2b6557dd573ae496a752b7ab4e563 to your computer and use it in GitHub Desktop.
Save ralexstokes/f1d2b6557dd573ae496a752b7ab4e563 to your computer and use it in GitHub Desktop.
Compute the smallest primitive root of unity of BLS-12-381
# Python3 program to find primitive root
# of a given number n
from math import sqrt
from gmpy2 import is_prime
import primefac
# Function to find smallest primitive
# root of n
def findPrimitive(n):
if is_prime(n) == False:
return -1
phi = n - 1
# Find prime factors of phi and store in a set
s = set(primefac.primefac(phi))
print(s)
# Check for every number from 2 to phi
for r in range(2, phi + 1):
# Iterate through all prime factors of phi.
# and check if we found a power with value 1
flag = False
for it in s:
# Check if r^((phi)/primefactors)
# mod n is 1 or not
if pow(r, phi // it, n) == 1:
flag = True
break
# If there was no power with value 1.
if flag == False:
return r
# If no primitive root found
return -1
# Driver Code
n = 0x73EDA753299D7D483339D80809A1D80553BDA402FFFE5BFEFFFFFFFF00000001
print("Smallest primitive root of", n, "is", findPrimitive(n))
@ralexstokes
Copy link
Author

from @dankrad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment