def testSmallOrder(self): O = (0, 1, 1, 0) # identity point Q = ed.point_mul(8, O) self.assertTrue(ed.point_equal(O, Q)) P = (38214883241950591754978413199355411911188925816896391856984770930832735035197, 0, 1, 0) Q = ed.point_mul(8, P) self.assertTrue(ed.point_equal(O, Q)) P = ed.G Q = ed.point_mul(8, P) self.assertFalse(ed.point_equal(O, Q))
#!/usr/bin/env python3 import base64 from itertools import cycle from rfc8032 import point_add, point_compress, point_decompress, point_equal, point_mul, G KEYLEN = 12 def catch(f, g, ty=Exception): try: return f() except ty as e: return g(e) topoint = lambda n: point_mul(n, G) frompoint = lambda p: catch(lambda: filter(lambda x: point_equal(p,topoint(x)), range(256)).__next__(), lambda _: None, StopIteration) assert frompoint(topoint(42)) == 42 negate = lambda p: (p[0], -p[1], -p[2], p[3]) assert point_equal(point_add(negate(G), G), (0, 1, 1, 0)) def encrypt(key, ptxt): print(repr((key, ptxt))) points = [point_add(topoint(x), topoint(ord(y))) for (x,y) in zip(cycle(key), ptxt)] return b';'.join(base64.b64encode(point_compress(p)) for p in points) def decrypt(key, ctxt): points = [point_decompress(base64.b64decode(p)) for p in ctxt.split(b';')] ptxt = ''.join([chr(frompoint(point_add(negate(topoint(x)), y))) for (x,y) in zip(cycle(key), points)]) return ptxt
def testIsOnCurve(self): self.assertTrue(ed.is_on_curve((0,1,1,0))) self.assertFalse(ed.is_on_curve((1,0,1,0))) self.assertTrue(ed.is_on_curve(ed.G)) self.assertTrue(ed.is_on_curve(ed.point_mul(5, ed.G)))