def log(p, q): assert curve.is_on_curve(p) assert curve.is_on_curve(q) sqrt_n = int(math.sqrt(curve.n)) + 1 # Compute the baby steps and store them in the 'precomputed' hash table. r = None precomputed = {None: 0} for a in range(1, sqrt_n): r = curve.add(r, p) precomputed[r] = a # Now compute the giant steps and check the hash table for any # matching point. r = q s = curve.mult(sqrt_n, curve.neg(p)) for b in range(sqrt_n): try: a = precomputed[r] except KeyError: pass else: steps = sqrt_n + b logarithm = a + sqrt_n * b return logarithm, steps r = curve.add(r, s) raise AssertionError('logarithm not found')
def __init__(self, point1, point2): self.point1 = point1 self.point2 = point2 self.add_a1 = random.randrange(1, curve.n) self.add_b1 = random.randrange(1, curve.n) self.add_x1 = curve.add( curve.mult(self.add_a1, point1), curve.mult(self.add_b1, point2), ) self.add_a2 = random.randrange(1, curve.n) self.add_b2 = random.randrange(1, curve.n) self.add_x2 = curve.add( curve.mult(self.add_a2, point1), curve.mult(self.add_b2, point2), )
def __iter__(self): partition_size = curve.p // 3 + 1 x = None a = 0 b = 0 while True: if x is None: i = 0 else: i = x[0] // partition_size if i == 0: # x is either the point at infinity (None), or is in the first # third of the plane (x[0] <= curve.p / 3). a += self.add_a1 b += self.add_b1 x = curve.add(x, self.add_x1) elif i == 1: # x is in the second third of the plane # (curve.p / 3 < x[0] <= curve.p * 2 / 3). a *= 2 b *= 2 x = curve.double(x) elif i == 2: # x is in the last third of the plane (x[0] > curve.p * 2 / 3). a += self.add_a2 b += self.add_b2 x = curve.add(x, self.add_x2) else: raise AssertionError(i) a = a % curve.n b = b % curve.n yield x, a, b
def log(p, q): assert curve.is_on_curve(p) assert curve.is_on_curve(q) start = random.randrange(curve.n) r = curve.mult(start, p) for x in range(curve.n): if q == r: logarithm = (start + x) % curve.n steps = x + 1 return logarithm, steps r = curve.add(r, p) raise AssertionError('logarithm not found')