Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Add roots of unity to FQ class #156

Open
HarryR opened this issue Aug 19, 2019 · 0 comments
Open

Add roots of unity to FQ class #156

HarryR opened this issue Aug 19, 2019 · 0 comments
Labels
enhancement New feature or request

Comments

@HarryR
Copy link
Owner

HarryR commented Aug 19, 2019

So we can do something like FQ(...).primitive_root(5) which will return a primitive 5th root of unity in the field.

There are situations where there may not be a n-th root of unity, an exception should be thrown.

It may also be beneficial to do a deterministic search for the roots of unity, so that the results are consistent across runs.

Example code:

def find_root(p, n):
	x = randint(1, p-1)
	return pow(x, (p-1)//n, p)

def find_roots(p, n):
	# https://crypto.stackexchange.com/questions/63614/finding-the-n-th-root-of-unity-in-a-finite-field
	# In particular, if N is a power of 2 and ω^(N/2) = −1, then ω is a principal N-th root of unity.
	gs = set()
	while len(gs) != n:
		gs.add(find_root(p, n))
	return list(gs)

def is_primitive_root(g, p, n):
	return pow(g, n//2, p) != 1

def primitive_roots(p, n):
	return [_ for _ in find_roots(p, n) if is_primitive_root(_, p, n)]

def primitive_root(p, n):
	while True:
		g = find_root(p, n)
		if is_primitive_root(g, p, n):
			return g
@HarryR HarryR added the enhancement New feature or request label Aug 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant