def test_Point_doubling(): """ Test whether the EC point doubling is correct. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() gx2, gy2 = (2 * g).get_affine() from Lab01Code import is_point_on_curve from Lab01Code import point_double x2, y2 = point_double(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, x2, y2) assert x2 == gx2 and y2 == gy2 x2, y2 = point_double(a, b, p, None, None) assert is_point_on_curve(a, b, p, x2, y2) assert x2 == None and y2 == None
def test_Point_scalar_mult_montgomerry_ladder(): """ Test the scalar multiplication using double and add. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() r = G.order().random() gx2, gy2 = (r * g).get_affine() from Lab01Code import is_point_on_curve from Lab01Code import point_scalar_multiplication_montgomerry_ladder x2, y2 = point_scalar_multiplication_montgomerry_ladder( a, b, p, gx0, gy0, r) assert is_point_on_curve(a, b, p, x2, y2) assert gx2 == x2 assert gy2 == y2
def time_scalar_mul(): # setup curve G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() order = G.order() r = order.random() gx2, gy2 = (r * g).get_affine() # First a value with low hamming weight scalar_1 = Bn.from_hex('11111111111111111111111111111111111111111') # The second scalar value with a much higher hamming weight scalar_2 = Bn.from_hex('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF') # Scalar values with higher hamming weight will take longer to # compute the multiplication of. t1 = time.clock() x2, y2 = point_scalar_multiplication_double_and_add( a, b, p, gx0, gy0, scalar_1) t2 = time.clock() runtime = t2 - t1 print("Runtime for scalar 1: " + str(runtime)) t1 = time.clock() x2, y2 = point_scalar_multiplication_double_and_add( a, b, p, gx0, gy0, scalar_2) t2 = time.clock() runtime = t2 - t1 print("Runtime for scalar 2: " + str(runtime))
def test_Point_doubling(): """ Test whether the EC point doubling is correct. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() gx2, gy2 = (2*g).get_affine() from Lab01Code import is_point_on_curve from Lab01Code import point_double x2, y2 = point_double(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, x2, y2) assert x2 == gx2 and y2 == gy2 x2, y2 = point_double(a, b, p, None, None) assert is_point_on_curve(a, b, p, x2, y2) assert x2 == None and y2 == None
def time_scalar_mul(): import time, pprint G = EcGroup() d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() x, y = g.get_affine() scalars = G.order().hex() results = [] for i in range(0, len(scalars), 3): r = Bn.from_hex(scalars[:i+1]) start = time.clock() point_scalar_multiplication_double_and_add(a, b, p, x, y, r) elapsed = (time.clock() - start) results.append((r, elapsed)) pp = pprint.PrettyPrinter(indent=2, width=160) pp.pprint(results) for i in range(0, len(scalars), 3): r = Bn.from_hex(scalars[:i+1]) start = time.clock() point_scalar_multiplication_montgomerry_ladder(a, b, p, x, y, r) elapsed = (time.clock() - start) results.append((r, elapsed)) pp = pprint.PrettyPrinter(indent=2, width=160) pp.pprint(results)
def time_scalar_mul(): G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() def average_time(func, scalar_name, samples=20): scalar = globals()[scalar_name] times = [] for i in range(samples): t1 = clock() func(a, b, p, gx0, gy0, scalar) t2 = clock() times.append(t2 - t1) mean = reduce((lambda x, y: x + y), times) / samples print('{}, {}, mean of {} samples: {}'.format(func.__name__, scalar_name, samples, mean)) average_time(point_scalar_multiplication_double_and_add, 'R1') average_time(point_scalar_multiplication_double_and_add, 'R2') average_time(point_scalar_multiplication_montgomerry_ladder, 'R1') average_time(point_scalar_multiplication_montgomerry_ladder, 'R2')
def test_Point_addition(): """ Test whether the EC point addition is correct. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() r = G.order().random() gx1, gy1 = (r * g).get_affine() assert is_point_on_curve(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, gx1, gy1) ## Test a simple addition h = (r + 1) * g hx1, hy1 = h.get_affine() x, y = point_add(a, b, p, gx0, gy0, gx1, gy1) assert is_point_on_curve(a, b, p, x, y) assert x == hx1 assert y == hy1 ## Ensure commutativity xp, yp = point_add(a, b, p, gx1, gy1, gx0, gy0) assert is_point_on_curve(a, b, p, xp, yp) assert x == xp assert y == yp ## Ensure addition with neutral returns the element xp, yp = point_add(a, b, p, gx1, gy1, None, None) assert is_point_on_curve(a, b, p, xp, yp) assert xp == gx1 assert yp == gy1 xp, yp = point_add(a, b, p, None, None, gx0, gy0) assert is_point_on_curve(a, b, p, xp, yp) assert gx0 == xp assert gy0 == yp ## An error is raised in case the points are equal with raises(Exception) as excinfo: point_add(a, b, p, gx0, gy0, gx0, gy0) assert 'EC Points must not be equal' in str(excinfo.value) with raises(Exception) as excinfo: point_add(a, b, p, None, None, None, None) assert 'EC Points must not be equal' in str(excinfo.value)
def time_scalar_mul(): # Get EC Parameters G = EcGroup(713) d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() x, y = g.get_affine() # Generate graph data double_add = list() fixed_double_add = list() montgomerry_ladder = list() n = 500 bits_set = range(1, n) for i in bits_set: scalar = Bn.from_decimal(("1" * i) + ("0" * (n - i - 1))) print i, scalar double_add.append( get_time_dif(point_scalar_multiplication_double_and_add, (a, b, p, x, y, scalar))) fixed_double_add.append( get_time_dif(fixed_point_scalar_multiplication_double_and_add, (a, b, p, x, y, scalar))) montgomerry_ladder.append( get_time_dif(point_scalar_multiplication_montgomerry_ladder, (a, b, p, x, y, scalar))) fig = plt.figure("Runtime graphs") gs = gridspec.GridSpec(4, 4) ax = plt.subplot(gs[0:2, 0:2]) ax.set_title("Double and Add") ax.plot(bits_set, double_add) ax.set_xlabel("Number of bits set") ax.set_ylabel("Time taken to multiply") ax = plt.subplot(gs[0:2, 2:]) ax.set_title("Montgomerry Ladder") ax.plot(bits_set, montgomerry_ladder) ax.set_xlabel("Number of bits set") ax.set_ylabel("Time taken to multiply") ax = plt.subplot(gs[2:4, 1:3]) ax.set_title("Fixed Double and Add") ax.plot(bits_set, fixed_double_add) ax.set_xlabel("Number of bits set") ax.set_ylabel("Time taken to multiply") gs.tight_layout(fig) plt.show()
def test_Point_addition(): """ Test whether the EC point addition is correct. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() r = G.order().random() gx1, gy1 = (r*g).get_affine() from Lab01Code import is_point_on_curve from Lab01Code import point_add assert is_point_on_curve(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, gx1, gy1) ## Test a simple addition h = (r + 1) * g hx1, hy1 = h.get_affine() x, y = point_add(a, b, p, gx0, gy0, gx1, gy1) assert is_point_on_curve(a, b, p, x, y) assert x == hx1 assert y == hy1 ## Ensure commutativity xp, yp = point_add(a, b, p, gx1, gy1, gx0, gy0) assert is_point_on_curve(a, b, p, xp, yp) assert x == xp assert y == yp ## Ensure addition with neutral returns the element xp, yp = point_add(a, b, p, gx1, gy1, None, None) assert is_point_on_curve(a, b, p, xp, yp) assert xp == gx1 assert yp == gy1 xp, yp = point_add(a, b, p, None, None, gx0, gy0) assert is_point_on_curve(a, b, p, xp, yp) assert gx0 == xp assert gy0 == yp ## An error is raised in case the points are equal with raises(Exception) as excinfo: point_add(a, b, p, gx0, gy0, gx0, gy0) assert 'EC Points must not be equal' in str(excinfo.value)
def test_on_curve(): """ Test the procedues that tests whether a point is on a curve. """ ## Example on how to define a curve from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx, gy = g.get_affine() from Lab01Code import is_point_on_curve assert is_point_on_curve(a, b, p, gx, gy) assert is_point_on_curve(a, b, p, None, None)
def test_Point_addition_check_inf_result(): """ Test whether the EC point addition is correct for pt - pt = inf """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() gx1, gy1 = gx0, p - gy0 assert is_point_on_curve(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, gx1, gy1) x, y = point_add(a, b, p, gx0, gy0, gx1, gy1) assert is_point_on_curve(a, b, p, x, y) assert (x, y) == (None, None)
def test_Point_addition_check_inf_result(): """ Test whether the EC point addition is correct for pt - pt = inf """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() gx1, gy1 = gx0, p - gy0 assert is_point_on_curve(a, b, p, gx0, gy0) assert is_point_on_curve(a, b, p, gx1, gy1) x, y = point_add(a, b, p, gx0, gy0, gx1, gy1) assert is_point_on_curve(a, b, p, x, y) assert (x,y) == (None, None)
def test_Point_scalar_mult_double_and_add(): """ Test the scalar multiplication using double and add. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() r = G.order().random() gx2, gy2 = (r*g).get_affine() x2, y2 = point_scalar_multiplication_double_and_add(a, b, p, gx0, gy0, r) assert is_point_on_curve(a, b, p, x2, y2) assert gx2 == x2 assert gy2 == y2
def test_Point_scalar_mult_double_and_add(): """ Test the scalar multiplication using double and add. """ from pytest import raises from petlib.ec import EcGroup, EcPt G = EcGroup(713) # NIST curve d = G.parameters() a, b, p = d["a"], d["b"], d["p"] g = G.generator() gx0, gy0 = g.get_affine() r = G.order().random() gx2, gy2 = (r * g).get_affine() x2, y2 = point_scalar_multiplication_double_and_add(a, b, p, gx0, gy0, r) assert is_point_on_curve(a, b, p, x2, y2) assert gx2 == x2 assert gy2 == y2 x3, y3 = point_scalar_multiplication_double_and_add(a, b, p, None, None, r) assert x3 is None assert y3 is None