def test_gf_irreducible(): assert gf_irreducible_p(gf_irreducible(1, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(2, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(3, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(4, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(5, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(6, 11, ZZ), 11, ZZ) == True assert gf_irreducible_p(gf_irreducible(7, 11, ZZ), 11, ZZ) == True
def gen(self): irr_poly = Poly(alpha ** self.m + alpha + 1, alpha).set_domain(GF(self.q)) if gf_irreducible_p([int(c) for c in irr_poly.all_coeffs()], self.q, ZZ): quotient_size = len(power_dict(self.n, irr_poly, self.q)) else: quotient_size = 0 log.info("irr(q_size: {}): {}".format(quotient_size, irr_poly)) while quotient_size < self.n: irr_poly = Poly([int(c.numerator) for c in gf_irreducible(self.m, self.q, ZZ)], alpha) quotient_size = len(power_dict(self.n, irr_poly, self.q)) log.info("irr(q_size: {}): {}".format(quotient_size, irr_poly)) g_poly = None for i in range(self.b, self.b + self.d - 1): if g_poly is None: g_poly = minimal_poly(i, self.n, self.q, irr_poly) else: g_poly = lcm(g_poly, minimal_poly(i, self.n, self.q, irr_poly)) g_poly = g_poly.trunc(self.q) log.info("g(x)={}".format(g_poly)) return irr_poly, g_poly
def irreducible_poly(m, p, var): return Poly([int(c.numerator) for c in gf_irreducible(m, p, ZZ)], var)
from participant import * from fieldelement import * import random from sympy.polys.galoistools import gf_irreducible from sympy.polys.domains import ZZ if __name__ == "__main__": """ Here we are trying to use a well known NIST curve. We get a generator P of the curve (with cofactor = 1) which we know the order. We must still generate a random Q linearly independent of P with the same order """ """ Generate the curve over a finite field""" p = 2**256 - 2**224 + 2**192 + 2**96 - 1 k = 3 q = p**k irreducible_poly = switchCoefs(gf_irreducible(k, p, ZZ)) a = FieldElement([ 115792089210356248762697446949407573530086143415290314195533631308867097853948, 0, 0 ], p, k, irreducible_poly) b = FieldElement([ 41058363725152142129326129780047268409114441015993725554835256314039467401291, 0, 0 ], p, k, irreducible_poly) ec = EllipticCurve(a, b) """ Get 2 random independent points P and Q with the same order, by extending the field to k=3 so that p^k = 3mod4 and we can easily obtain square roots of elements of the field """ # Just to make sure our assumption is correct if (q % 4) != 3: raise ValueError("q is not 3 mod 4")
def getIrreducible(p, n): return switchCoefs(gf_irreducible(n, p, ZZ))