def test_square(self): f1 = self.e.field f2 = GF2QuotientRing(0x211) for f in [f1,f2]: for k in xrange(3,250,7): assert f.mulraw(k,k) == f.squareraw(k) e = f.wrap(k) assert e*e == f.square(e)
def test2(self): for p, initstate in [(615, 0x92), (456, 0x37), (59857573, 0x123), (0x10000000000b7, 23)]: field = GF2QuotientRing(p) e = field.wrap(initstate) bits = [(e << k).coeffs >> (field.degree - 1) for k in xrange(2 * field.degree)] p, n = berlekamp_massey(bits) assert p == field.coeffs
def decimate(field,j): e = field.wrap(1) u = e n = field.degree m = e << j bits = [] for k in range(2*n): bits.append(u.coeffs >> (n-1)) u *= m return GF2QuotientRing(berlekamp_massey(bits)[0])
def test_inv(self): e = self.e e23 = GF2Element(23, self.FIELD) e23inv = e23.inv assert e23inv.coeffs == 0xa5 assert e23inv*e23 == e with pytest.raises(ValueError): GF2Element(0, self.FIELD).inv for k in xrange(1,1<<self.FIELD.degree): ek = GF2Element(k, self.FIELD) assert (ek.inv)*ek == e F8b = GF2QuotientRing(0x180) with pytest.raises(ValueError): GF2Element(2, F8b).inv
class Test_TracePattern(object): FIELD = GF2QuotientRing(0x187) def setup(self): self.e = GF2Element(1, self.FIELD) def test_0(self): for w in xrange(1,60): tr1 = GF2TracePattern.from_pattern(self.FIELD, w) tr2 = GF2TracePattern.from_mask(self.FIELD, tr1.mask) assert tr1.pattern == w assert tr2 == tr1 def test_1(self): w,winv,tr0 = self.FIELD.trace_constants tr1 = GF2TracePattern.from_mask(self.FIELD, 1 << (self.FIELD.degree - 1)) assert tr1.pattern == w u = 1 # now make that tr1 acts like an LFSR output for k in xrange(100): u = self.FIELD.lshiftraw1(u) assert tr1(u) == (u >> (self.FIELD.degree - 1)) def test_2(self): tr2 = GF2TracePattern.from_pattern(self.FIELD, 1) assert tr2 == self.FIELD.trace def test_3(self): for w in [1,2,3,4,7,81,self.FIELD.coeffs >> 1]: tr = GF2TracePattern.from_pattern(self.FIELD, w) for u in xrange(100): assert tr(u) == self.FIELD.mul(w,u).trace def test_delay(self): tr0 = GF2TracePattern.from_mask(self.FIELD, 0x80) bits0 = [] tr1 = tr0.delay(4) bits1 = [] tr2 = tr0.delay(9) bits2 = [] u = 1 for k in xrange(100): bits0.append(tr0(u)) bits1.append(tr1(u)) bits2.append(tr2(u)) u = self.FIELD.lshiftraw1(u) assert bits0[:-4] == bits1[4:] assert bits0[:-9] == bits2[9:] def test_mul(self): tr0 = GF2TracePattern.from_mask(self.FIELD, 0x80) assert (tr0 * 32) == tr0.delay(-5)
def test_polyval2(self): def plain_polyval(u,g,qr): y = 0 uk = 1 while g > 0: if g & 1: y ^= uk g >>= 1 uk = qr.mulraw(uk,u) return y r = random.Random() r.seed(123) qr = GF2QuotientRing((1<<48) + 0xb7) p = (1<<48)-1 assert checkPeriod(qr,p)==p for i in xrange(100): u = r.getrandbits(48) g = r.getrandbits(48) assert qr.polyvalraw(u,g) == plain_polyval(u,g,qr) e123 = qr.wrap(123) assert e123.polyval(0b100101) == e123**5 + e123**2 + 1
def prof(request): return PolyRingOverField(GF2QuotientRing(0x211))
def __init__(self, field): self.field = GF2QuotientRing.cast(field);
class Test_Element(object): FIELD = GF2QuotientRing(0x187) def setup(self): self.e = GF2Element(1, self.FIELD) def test_toolarge(self): with pytest.raises(ValueError): GF2Element(0x100, self.FIELD) with pytest.raises(ValueError): e1 = GF2Element(0x1, self.FIELD) e1 * 0x100 with pytest.raises(ValueError): e1 = GF2Element(0x1, self.FIELD) e1 + 0x1111 def test_lshift(self): e = self.e assert e.coeffs == 1 assert e.field == self.FIELD assert (e << 1).coeffs == 2 assert (e << 2).coeffs == 4 assert (e << 40).coeffs == 0x62 assert (e << 255).coeffs == 1 assert (e << 254).coeffs == 0xc3 assert (e << 253).coeffs == 0xa2 assert (e << -1).coeffs == 0xc3 assert (e << -2).coeffs == 0xa2 assert (e << -215).coeffs == 0x62 def test_rshift(self): e = self.e assert (e >> 1).coeffs == 0xc3 assert (e >> 2).coeffs == 0xa2 assert (e >> 215).coeffs == 0x62 assert (e >> 253).coeffs == 4 assert (e >> 254).coeffs == 2 assert (e >> 255).coeffs == 1 assert (e >> -1).coeffs == 2 assert (e >> -2).coeffs == 4 assert (e >> -40).coeffs == 0x62 def test_mul(self): e = self.e e2 = GF2Element(2, self.FIELD) e3 = GF2Element(3, self.FIELD) assert (e2*e3).coeffs == 6 assert (e2*3).coeffs == 6 assert (2*e3).coeffs == 6 def test_add(self): e = self.e e2 = GF2Element(2, self.FIELD) e3 = GF2Element(3, self.FIELD) assert (e2+e3).coeffs == 1 assert (e2+3).coeffs == 1 assert (2+e3).coeffs == 1 def test_pow(self): e3 = GF2Element(3, self.FIELD) assert (e3 ** 2).coeffs == 0x5 assert (e3 ** 4).coeffs == 0x11 assert (e3 ** 5).coeffs == 0x33 assert (e3 ** 100).coeffs == 0xa7 def test_pow2(self): f2 = GF2QuotientRing(0x211) e = f2.wrap(73) ey = e for k in xrange(1,200): assert (e ** k) == ey ey *= e def test_powvect(self): e = self.e assert self.FIELD.powvect(e,[2,4,5,100]) == [e**2, e**4, e**5, e**100] def test_square(self): f1 = self.e.field f2 = GF2QuotientRing(0x211) for f in [f1,f2]: for k in xrange(3,250,7): assert f.mulraw(k,k) == f.squareraw(k) e = f.wrap(k) assert e*e == f.square(e) def test_inv(self): e = self.e e23 = GF2Element(23, self.FIELD) e23inv = e23.inv assert e23inv.coeffs == 0xa5 assert e23inv*e23 == e with pytest.raises(ValueError): GF2Element(0, self.FIELD).inv for k in xrange(1,1<<self.FIELD.degree): ek = GF2Element(k, self.FIELD) assert (ek.inv)*ek == e F8b = GF2QuotientRing(0x180) with pytest.raises(ValueError): GF2Element(2, F8b).inv def test_div(self): e2 = GF2Element(2, self.FIELD) e3 = GF2Element(3, self.FIELD) eq = e2/e3 assert (eq).coeffs == 0x83 assert e2 == e3*eq assert e2/3 == eq assert 2/e3 == eq def test_log(self): e=self.e for k in xrange(1,100): u = e << k assert u.log == k with pytest.raises(ValueError): self.FIELD.wrap(0).log def test_mulx(self): x = 2 y = 1 for k in xrange(1,100): y2 = _gf2mulxmod(y,self.FIELD.coeffs) y = self.FIELD.mulraw(x,y) assert y == y2 def test_mulxinv(self): x = 2 xinv = self.FIELD.invraw(x) y = 1 for k in xrange(1,100): y2 = _gf2mulxinvmod(y,self.FIELD.coeffs) y = self.FIELD.mulraw(xinv,y) assert y == y2 def test_trace(self): w, winv, trace = self.FIELD.trace_constants assert w == 0x6 assert winv == 0x41 u = self.FIELD.wrap(1) u2 = u assert u.trace == self.FIELD.degree & 1 for k in xrange(100): u <<= 1 u2 <<= 2 assert u.trace == (winv*u).coeffs >> (self.FIELD.degree - 1) assert u.trace == u2.trace def test_polyval(self): e23 = GF2Element(23, self.FIELD) assert (e23**3 + e23**2 + 1) == self.FIELD.polyval(e23, 0b1101) assert (e23**9 + e23**4 + e23**3) == self.FIELD.polyval(e23, 0b1000011000) assert e23.polyval(0b11101) == e23**4 + e23**3 + e23**2 + 1 def test_polyval2(self): def plain_polyval(u,g,qr): y = 0 uk = 1 while g > 0: if g & 1: y ^= uk g >>= 1 uk = qr.mulraw(uk,u) return y r = random.Random() r.seed(123) qr = GF2QuotientRing((1<<48) + 0xb7) p = (1<<48)-1 assert checkPeriod(qr,p)==p for i in xrange(100): u = r.getrandbits(48) g = r.getrandbits(48) assert qr.polyvalraw(u,g) == plain_polyval(u,g,qr) e123 = qr.wrap(123) assert e123.polyval(0b100101) == e123**5 + e123**2 + 1
def setup(self): self.field = GF2QuotientRing(0x187)