catacomb/__init__.py: Generalize rationals to fields of fractions.
[catacomb-python] / catacomb / __init__.py
index 8dac0b4..b50c99a 100644 (file)
@@ -47,7 +47,7 @@ def _init():
             'DHInfo', 'BinDHInfo', 'RSAPriv', 'BBSPriv',
             'PrimeFilter', 'RabinMiller',
             'Group', 'GE',
-            'KeyData']:
+            'KeySZ', 'KeyData']:
     c = d[i]
     pre = '_' + i + '_'
     plen = len(pre)
@@ -99,6 +99,59 @@ bytes = ByteString.fromhex
 ###--------------------------------------------------------------------------
 ### Multiprecision integers and binary polynomials.
 
+def _split_rat(x):
+  if isinstance(x, BaseRat): return x._n, x._d
+  else: return x, 1
+class BaseRat (object):
+  """Base class implementing fields of fractions over Euclidean domains."""
+  def __new__(cls, a, b):
+    a, b = cls.RING(a), cls.RING(b)
+    q, r = divmod(a, b)
+    if r == 0: return q
+    g = b.gcd(r)
+    me = super(BaseRat, cls).__new__(cls)
+    me._n = a//g
+    me._d = b//g
+    return me
+  @property
+  def numer(me): return me._n
+  @property
+  def denom(me): return me._d
+  def __str__(me): return '%s/%s' % (me._n, me._d)
+  def __repr__(me): return '%s(%s, %s)' % (type(me).__name__, me._n, me._d)
+
+  def __add__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._n*d + n*me._d, d*me._d)
+  __radd__ = __add__
+  def __sub__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._n*d - n*me._d, d*me._d)
+  def __rsub__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(n*me._d - me._n*d, d*me._d)
+  def __mul__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._n*n, me._d*d)
+  def __div__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._n*d, me._d*n)
+  def __rdiv__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._d*n, me._n*d)
+  def __cmp__(me, you):
+    n, d = _split_rat(you)
+    return type(me)(me._n*d, n*me._d)
+  def __rcmp__(me, you):
+    n, d = _split_rat(you)
+    return cmp(n*me._d, me._n*d)
+
+class IntRat (BaseRat):
+  RING = MP
+
+class GFRat (BaseRat):
+  RING = GF
+
 class _tmp:
   def negp(x): return x < 0
   def posp(x): return x > 0
@@ -108,11 +161,8 @@ class _tmp:
   def mont(x): return MPMont(x)
   def barrett(x): return MPBarrett(x)
   def reduce(x): return MPReduce(x)
-  def factorial(x):
-    'factorial(X) -> X!'
-    if x < 0: raise ValueError, 'factorial argument must be > 0'
-    return MPMul.product(xrange(1, x + 1))
-  factorial = staticmethod(factorial)
+  def __div__(me, you): return IntRat(me, you)
+  def __rdiv__(me, you): return IntRat(you, me)
 _augment(MP, _tmp)
 
 class _tmp:
@@ -122,6 +172,8 @@ class _tmp:
   def halftrace(x, y): return x.reduce().halftrace(y)
   def modsqrt(x, y): return x.reduce().sqrt(y)
   def quadsolve(x, y): return x.reduce().quadsolve(y)
+  def __div__(me, you): return GFRat(me, you)
+  def __rdiv__(me, you): return GFRat(you, me)
 _augment(GF, _tmp)
 
 class _tmp:
@@ -141,6 +193,7 @@ _augment(Field, _tmp)
 
 class _tmp:
   def __repr__(me): return '%s(%sL)' % (type(me).__name__, me.p)
+  def __hash__(me): return 0x114401de ^ hash(me.p)
   def ec(me, a, b): return ECPrimeProjCurve(me, a, b)
 _augment(PrimeField, _tmp)
 
@@ -150,6 +203,18 @@ class _tmp:
 _augment(BinField, _tmp)
 
 class _tmp:
+  def __hash__(me): return 0x23e4701c ^ hash(me.p)
+_augment(BinPolyField, _tmp)
+
+class _tmp:
+  def __hash__(me):
+    h = 0x9a7d6240
+    h ^=   hash(me.p)
+    h ^= 2*hash(me.beta) & 0xffffffff
+    return h
+_augment(BinNormField, _tmp)
+
+class _tmp:
   def __str__(me): return str(me.value)
   def __repr__(me): return '%s(%s)' % (repr(me.field), repr(me.value))
 _augment(FE, _tmp)
@@ -169,6 +234,24 @@ class _tmp:
 _augment(ECCurve, _tmp)
 
 class _tmp:
+  def __hash__(me):
+    h = 0x6751d341
+    h ^=   hash(me.field)
+    h ^= 2*hash(me.a) ^ 0xffffffff
+    h ^= 5*hash(me.b) ^ 0xffffffff
+    return h
+_augment(ECPrimeCurve, _tmp)
+
+class _tmp:
+  def __hash__(me):
+    h = 0x2ac203c5
+    h ^=   hash(me.field)
+    h ^= 2*hash(me.a) ^ 0xffffffff
+    h ^= 5*hash(me.b) ^ 0xffffffff
+    return h
+_augment(ECBinCurve, _tmp)
+
+class _tmp:
   def __repr__(me):
     if not me: return 'ECPt()'
     return 'ECPt(%s, %s)' % (me.ix, me.iy)
@@ -181,6 +264,11 @@ class _tmp:
   def __repr__(me):
     return 'ECInfo(curve = %r, G = %r, r = %s, h = %s)' % \
            (me.curve, me.G, me.r, me.h)
+  def __hash__(me):
+    h = 0x9bedb8de
+    h ^=   hash(me.curve)
+    h ^= 2*hash(me.G) & 0xffffffff
+    return h
   def group(me):
     return ECGroup(me)
 _augment(ECInfo, _tmp)
@@ -248,6 +336,30 @@ class _tmp:
 _augment(Group, _tmp)
 
 class _tmp:
+  def __hash__(me):
+    info = me.info
+    h = 0xbce3cfe6
+    h ^=   hash(info.p)
+    h ^= 2*hash(info.r) & 0xffffffff
+    h ^= 5*hash(info.g) & 0xffffffff
+    return h
+_augment(PrimeGroup, _tmp)
+
+class _tmp:
+  def __hash__(me):
+    info = me.info
+    h = 0x80695949
+    h ^=   hash(info.p)
+    h ^= 2*hash(info.r) & 0xffffffff
+    h ^= 5*hash(info.g) & 0xffffffff
+    return h
+_augment(BinGroup, _tmp)
+
+class _tmp:
+  def __hash__(me): return 0x0ec23dab ^ hash(me.info)
+_augment(ECGroup, _tmp)
+
+class _tmp:
   def __repr__(me):
     return '%r(%r)' % (me.group, str(me))
 _augment(GE, _tmp)