omnifarious

# GaloisField

• R/O
• SSH

## Commit

##### Tags

A small kernel of code for playing with Galois fields of arbitrary characteristic

### Commit MetaInfo

Revision 3f18b31e4b2653494ad2c1e8d4bd01f5b0d5d92a (tree) 2016-04-17 05:22:07 Eric Hopper Eric Hopper

### Log Message

Initial version of Galois Field code.

### Incremental Difference

diff -r 000000000000 -r 3f18b31e4b26 gf.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gf.py Sat Apr 16 16:22:07 2016 -0400
 @@ -0,0 +1,17 @@ 1 +def gfMeta(prime, basis): 2 + prime = int(prime) 3 + basis = tuple((int(p) for p in basis)) 4 + for p in basis: 5 + if (p < 0) or (p >= prime): 6 + raise ValueError("Each element of basis must be >= 0 " 7 + "and < prime.") 8 + if basis[0] != 1: 9 + raise ValueError("First element of basis must be 1") 10 + size = len(basis) 11 + 12 + class gf(type): 13 + def __init__(self, val): 14 + val = tuple((int(v) for v in val)) 15 + if len(val) != size: 16 + raise ValueError("val must be sequence of size size of ints") 17 + self.val_ = val_
diff -r 000000000000 -r 3f18b31e4b26 test_gf2.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test_gf2.py Sat Apr 16 16:22:07 2016 -0400
 @@ -0,0 +1,34 @@ 1 +import collections 2 + 3 +def generate(p, b=2): 4 + m = 1 5 + while m < p: 6 + m *= 2 7 + m = m // 2 8 + d = [] 9 + s = 1 10 +# print(s, m) 11 + generated = set() 12 + while s not in generated: 13 +# print(len(d), s) 14 + d.append(s) 15 + generated.add(s) 16 + s *= b 17 + while s >= m: 18 + tmp = p << (s.bit_length() - p.bit_length()) 19 +# print(s, tmp) 20 + s ^= tmp 21 + return d 22 + 23 +class testit(object): 24 + def __init__(self, *args, **kargs): 25 + print("__init__(args = %s, kargs = %s)" % (repr(args), repr(kargs))) 26 + def __getattr__(self, *args, **kargs): 27 + print("__getattr__(args = %s, kargs = %s)" % (repr(args), repr(kargs))) 28 + return None 29 + def __prepare__(self, *args, **kargs): 30 + print("__prepare__(args = %s, kargs = %s)" % (repr(args), repr(kargs))) 31 + return None 32 + def __call__(self, *args, **kargs): 33 + print("__call__(args = %s, kargs = %s)" % (repr(args), repr(kargs))) 34 + return object()