field.c, mp.c: Implement the `nb_index' conversion.
[catacomb-python] / catacomb / __init__.py
index 9e4d47f..5483cca 100644 (file)
@@ -84,6 +84,9 @@ def _bin(s): return s
 def _iteritems(dict): return dict.iteritems()
 def _itervalues(dict): return dict.itervalues()
 
+## The built-in bignum type.
+_long = long
+
 ## How to fix a name back into the right identifier.  Alas, the rules are not
 ## consistent.
 def _fixname(name):
@@ -180,10 +183,10 @@ class _tmp:
   def fromhex(x):
     return ByteString(_unhexify(x))
   fromhex = staticmethod(fromhex)
-  def __hex__(me):
-    return _hexify(me)
+  def hex(me): return _hexify(me)
+  __hex__ = hex
   def __repr__(me):
-    return 'bytes(%r)' % hex(me)
+    return 'bytes(%r)' % me.hex()
 _augment(ByteString, _tmp)
 ByteString.__hash__ = str.__hash__
 bytes = ByteString.fromhex
@@ -192,7 +195,7 @@ bytes = ByteString.fromhex
 ### Symmetric encryption.
 
 class _tmp:
-  def encrypt(me, n, m, tsz = None, h = ByteString('')):
+  def encrypt(me, n, m, tsz = None, h = ByteString.zero(0)):
     if tsz is None: tsz = me.__class__.tagsz.default
     e = me.enc(n, len(h), len(m), tsz)
     if not len(h): a = None
@@ -200,7 +203,7 @@ class _tmp:
     c0 = e.encrypt(m)
     c1, t = e.done(aad = a)
     return c0 + c1, t
-  def decrypt(me, n, c, t, h = ByteString('')):
+  def decrypt(me, n, c, t, h = ByteString.zero(0)):
     d = me.dec(n, len(h), len(c), len(t))
     if not len(h): a = None
     else: a = d.aad().hash(h)
@@ -387,15 +390,22 @@ class BaseRat (object):
     return type(me)(me._d*n, me._n*d)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
-  def __cmp__(me, you):
-    n, d = _split_rat(you)
-    return cmp(me._n*d, n*me._d)
-  def __rcmp__(me, you):
+  def _order(me, you, op):
     n, d = _split_rat(you)
-    return cmp(n*me._d, me._n*d)
+    return op(me._n*d, n*me._d)
+  def __eq__(me, you): return me._order(you, lambda x, y: x == y)
+  def __ne__(me, you): return me._order(you, lambda x, y: x != y)
+  def __le__(me, you): return me._order(you, lambda x, y: x <= y)
+  def __lt__(me, you): return me._order(you, lambda x, y: x <  y)
+  def __gt__(me, you): return me._order(you, lambda x, y: x >  y)
+  def __ge__(me, you): return me._order(you, lambda x, y: x >= y)
 
 class IntRat (BaseRat):
   RING = MP
+  def __new__(cls, a, b):
+    if isinstance(a, float) or isinstance(b, float): return a/b
+    return super(IntRat, cls).__new__(cls, a, b)
+  def __float__(me): return float(me._n)/float(me._d)
 
 class GFRat (BaseRat):
   RING = GF
@@ -409,8 +419,12 @@ class _tmp:
   def mont(x): return MPMont(x)
   def barrett(x): return MPBarrett(x)
   def reduce(x): return MPReduce(x)
-  def __truediv__(me, you): return IntRat(me, you)
-  def __rtruediv__(me, you): return IntRat(you, me)
+  def __truediv__(me, you):
+    if isinstance(you, float): return _long(me)/you
+    else: return IntRat(me, you)
+  def __rtruediv__(me, you):
+    if isinstance(you, float): return you/_long(me)
+    else: return IntRat(you, me)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
   _repr_pretty_ = _pp_str
@@ -446,7 +460,7 @@ class _tmp:
 _augment(Field, _tmp)
 
 class _tmp:
-  def __repr__(me): return '%s(%sL)' % (_clsname(me), me.p)
+  def __repr__(me): return '%s(%s)' % (_clsname(me), me.p)
   def __hash__(me): return 0x114401de ^ hash(me.p)
   def _repr_pretty_(me, pp, cyclep):
     ind = _pp_bgroup_tyname(pp, me)
@@ -457,7 +471,7 @@ class _tmp:
 _augment(PrimeField, _tmp)
 
 class _tmp:
-  def __repr__(me): return '%s(%#xL)' % (_clsname(me), me.p)
+  def __repr__(me): return '%s(%#x)' % (_clsname(me), me.p)
   def ec(me, a, b): return ECBinProjCurve(me, a, b)
   def _repr_pretty_(me, pp, cyclep):
     ind = _pp_bgroup_tyname(pp, me)
@@ -652,10 +666,23 @@ _augment(KeySZSet, _tmp)
 ### Key data objects.
 
 class _tmp:
+  def merge(me, file, report = None):
+    """KF.merge(FILE, [report = <built-in-reporter>])"""
+    name = file.name
+    lno = 1
+    for line in file:
+      me.mergeline(name, lno, line, report)
+      lno += 1
+    return me
   def __repr__(me): return '%s(%r)' % (_clsname(me), me.name)
 _augment(KeyFile, _tmp)
 
 class _tmp:
+  def extract(me, file, filter = ''):
+    """KEY.extract(FILE, [filter = <any>])"""
+    line = me.extractline(filter)
+    file.write(line)
+    return me
   def __repr__(me): return '%s(%r)' % (_clsname(me), me.fulltag)
 _augment(Key, _tmp)
 
@@ -685,27 +712,41 @@ class _tmp:
       pp.text(','); pp.breakable()
       pp.pretty(me.writeflags(me.flags))
     pp.end_group(ind, ')')
+  def __hash__(me): return me._HASHBASE ^ hash(me._guts())
+  def __eq__(me, kd):
+    return type(me) == type(kd) and \
+      me._guts() == kd._guts() and \
+      me.flags == kd.flags
+  def __ne__(me, kd):
+    return not me == kd
 _augment(KeyData, _tmp)
 
 class _tmp:
   def _guts(me): return me.bin
+  def __eq__(me, kd):
+    return isinstance(kd, KeyDataBinary) and me.bin == kd.bin
 _augment(KeyDataBinary, _tmp)
+KeyDataBinary._HASHBASE = 0x961755c3
 
 class _tmp:
   def _guts(me): return me.ct
 _augment(KeyDataEncrypted, _tmp)
+KeyDataEncrypted._HASHBASE = 0xffe000d4
 
 class _tmp:
   def _guts(me): return me.mp
 _augment(KeyDataMP, _tmp)
+KeyDataMP._HASHBASE = 0x1cb64d69
 
 class _tmp:
   def _guts(me): return me.str
 _augment(KeyDataString, _tmp)
+KeyDataString._HASHBASE = 0x349c33ea
 
 class _tmp:
   def _guts(me): return me.ecpt
 _augment(KeyDataECPt, _tmp)
+KeyDataECPt._HASHBASE = 0x2509718b
 
 class _tmp:
   def __repr__(me):
@@ -716,7 +757,21 @@ class _tmp:
     if cyclep: pp.text('...')
     else: _pp_dict(pp, _iteritems(me))
     pp.end_group(ind, ' })')
+  def __hash__(me):
+    h = me._HASHBASE
+    for k, v in _iteritems(me):
+      h = ((h << 1) ^ 3*hash(k) ^ 5*hash(v))&0xffffffff
+    return h
+  def __eq__(me, kd):
+    if type(me) != type(kd) or me.flags != kd.flags or len(me) != len(kd):
+      return False
+    for k, v in _iteritems(me):
+      try: vv = kd[k]
+      except KeyError: return False
+      if v != vv: return False
+    return True
 _augment(KeyDataStructured, _tmp)
+KeyDataStructured._HASHBASE = 0x85851b21
 
 ###--------------------------------------------------------------------------
 ### Abstract groups.