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 test_gf_irreducible_p(): assert gf_irred_p_ben_or([7], 11, ZZ) == True assert gf_irred_p_ben_or([7, 3], 11, ZZ) == True assert gf_irred_p_ben_or([7, 3, 1], 11, ZZ) == False assert gf_irred_p_rabin([7], 11, ZZ) == True assert gf_irred_p_rabin([7, 3], 11, ZZ) == True assert gf_irred_p_rabin([7, 3, 1], 11, ZZ) == False assert gf_irreducible_p([7], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7, 3], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7, 3, 1], 11, ZZ, method='ben-or') == False assert gf_irreducible_p([7], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7, 3], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7, 3, 1], 11, ZZ, method='rabin') == False raises(KeyError, "gf_irreducible_p([7], 11, ZZ, method='other')") f = [1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10] g = [1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9] h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) == True assert gf_irred_p_ben_or(g, 17, ZZ) == True assert gf_irred_p_ben_or(h, 17, ZZ) == False assert gf_irred_p_rabin(f, 17, ZZ) == True assert gf_irred_p_rabin(g, 17, ZZ) == True assert gf_irred_p_rabin(h, 17, ZZ) == False
def test_gf_irreducible_p(): assert gf_irred_p_ben_or([7], 11, ZZ) == True assert gf_irred_p_ben_or([7,3], 11, ZZ) == True assert gf_irred_p_ben_or([7,3,1], 11, ZZ) == False assert gf_irred_p_rabin([7], 11, ZZ) == True assert gf_irred_p_rabin([7,3], 11, ZZ) == True assert gf_irred_p_rabin([7,3,1], 11, ZZ) == False assert gf_irreducible_p([7], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7,3], 11, ZZ, method='ben-or') == True assert gf_irreducible_p([7,3,1], 11, ZZ, method='ben-or') == False assert gf_irreducible_p([7], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7,3], 11, ZZ, method='rabin') == True assert gf_irreducible_p([7,3,1], 11, ZZ, method='rabin') == False raises(KeyError, "gf_irreducible_p([7], 11, ZZ, method='other')") f = [1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10] g = [1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9] h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) == True assert gf_irred_p_ben_or(g, 17, ZZ) == True assert gf_irred_p_ben_or(h, 17, ZZ) == False assert gf_irred_p_rabin(f, 17, ZZ) == True assert gf_irred_p_rabin(g, 17, ZZ) == True assert gf_irred_p_rabin(h, 17, ZZ) == False
def __init__(self, p, n=1): p, n = int(p), int(n) if not isprime(p): raise ValueError("p must be a prime number, not %s" % p) if n <= 0: raise ValueError("n must be a positive integer, not %s" % n) self.p = p self.n = n if n == 1: self.reducing = [1, 0] else: for c in itertools.product(range(p), repeat=n): poly = (1, *c) if gf_irreducible_p(poly, p, ZZ): self.reducing = poly break
def __init__(self, p: int, n: int = 1) -> None: p, n = int(p), int(n) self.check_p(p) self.check_n(n) self.flush() self._p = p self._n = n if n == 1: self.reducing = [1, 0] else: for c in itertools.product(range(p), repeat=n): poly = (1, *c) if gf_irreducible_p(poly, p, ZZ): self.reducing = poly break
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 __init__(self, p, n=None): if n is None: L = primeFact(p) if len(set(L)) > 1: raise ValueError('order must be a prime power') p = L[0] n = len(L) p, n = int(p), int(n) if not isprime(p): raise ValueError("p must be a prime number, not %s" % p) if n <= 0: raise ValueError("n must be a positive integer, not %s" % n) self.p = p self.n = n if n == 1: self.reducing = [1, 0] else: for c in itertools.product(range(p), repeat=n): poly = (1, *c) if gf_irreducible_p(poly, p, ZZ): self.reducing = poly break self.iterpos = 0
def test_gf_irreducible_p(): assert gf_irred_p_ben_or([7], 11, ZZ) == True assert gf_irred_p_ben_or([7,3], 11, ZZ) == True assert gf_irred_p_ben_or([7,3,1], 11, ZZ) == False assert gf_irred_p_rabin([7], 11, ZZ) == True assert gf_irred_p_rabin([7,3], 11, ZZ) == True assert gf_irred_p_rabin([7,3,1], 11, ZZ) == False config.setup('GF_IRRED_METHOD', 'ben-or') assert gf_irreducible_p([7], 11, ZZ) == True assert gf_irreducible_p([7,3], 11, ZZ) == True assert gf_irreducible_p([7,3,1], 11, ZZ) == False config.setup('GF_IRRED_METHOD', 'rabin') assert gf_irreducible_p([7], 11, ZZ) == True assert gf_irreducible_p([7,3], 11, ZZ) == True assert gf_irreducible_p([7,3,1], 11, ZZ) == False config.setup('GF_IRRED_METHOD', 'other') raises(KeyError, lambda: gf_irreducible_p([7], 11, ZZ)) config.setup('GF_IRRED_METHOD') f = [1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10] g = [1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9] h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) == True assert gf_irred_p_ben_or(g, 17, ZZ) == True assert gf_irred_p_ben_or(h, 17, ZZ) == False assert gf_irred_p_rabin(f, 17, ZZ) == True assert gf_irred_p_rabin(g, 17, ZZ) == True assert gf_irred_p_rabin(h, 17, ZZ) == False
def test_gf_irreducible_p(): assert gf_irred_p_ben_or(ZZ.map([7]), 11, ZZ) is True assert gf_irred_p_ben_or(ZZ.map([7, 3]), 11, ZZ) is True assert gf_irred_p_ben_or(ZZ.map([7, 3, 1]), 11, ZZ) is False assert gf_irred_p_rabin(ZZ.map([7]), 11, ZZ) is True assert gf_irred_p_rabin(ZZ.map([7, 3]), 11, ZZ) is True assert gf_irred_p_rabin(ZZ.map([7, 3, 1]), 11, ZZ) is False config.setup("GF_IRRED_METHOD", "ben-or") assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False config.setup("GF_IRRED_METHOD", "rabin") assert gf_irreducible_p(ZZ.map([7]), 11, ZZ) is True assert gf_irreducible_p(ZZ.map([7, 3]), 11, ZZ) is True assert gf_irreducible_p(ZZ.map([7, 3, 1]), 11, ZZ) is False config.setup("GF_IRRED_METHOD", "other") raises(KeyError, lambda: gf_irreducible_p([7], 11, ZZ)) config.setup("GF_IRRED_METHOD") f = ZZ.map([1, 9, 9, 13, 16, 15, 6, 7, 7, 7, 10]) g = ZZ.map([1, 7, 16, 7, 15, 13, 13, 11, 16, 10, 9]) h = gf_mul(f, g, 17, ZZ) assert gf_irred_p_ben_or(f, 17, ZZ) is True assert gf_irred_p_ben_or(g, 17, ZZ) is True assert gf_irred_p_ben_or(h, 17, ZZ) is False assert gf_irred_p_rabin(f, 17, ZZ) is True assert gf_irred_p_rabin(g, 17, ZZ) is True assert gf_irred_p_rabin(h, 17, ZZ) is False
def is_irreducible(f, p): return gf_irreducible_p(f, p, ZZ)
def is_irreducible_poly(poly, p): return gf_irreducible_p([int(c) for c in poly.all_coeffs()], p, ZZ)