# import everything and define a test runner function
from importlib import reload
from helper import run_test
import ecc
import helper
Remember the % operator does the actual modulo operation. Also remember that + and - need to be in () because in the order of operations % comes before + and -.
2+15=?
17+21=?
29-4=?
15-30=?
1.2. Make these tests pass:
ecc.py:FieldElementTest:test_add
ecc.py:FieldElementTest:test_sub
# Exercise 1.1
# remember that % is the modulo operator
prime = 31
# 2+15=?
# 17+21=?
# 29-4=?
# 15-30=?
# Exercise 1.2
reload(ecc)
run_test(ecc.FieldElementTest('test_add'))
run_test(ecc.FieldElementTest('test_sub'))
\(24\cdot19=?\)
\(17^3=?\)
\(5^5\cdot18=?\)
2.2. Write a program to calculate \(0k, 1k, 2k, 3k, ... 30k\) for some k in \(F_{31}\). Notice anything about these sets?
2.4. Make these tests pass:
ecc.py:FieldElementTest:test_mul
ecc.py:FieldElementTest:test_pow
# Exercise 2.1
# remember that ** is the exponentiation operator
prime = 31
# 24*19=?
# 17^3=?
# 5^5*18=?
# Exercise 2.2
from random import randint
prime = 31
k = randint(1,prime)
# use range(prime) to iterate over all numbers from 0 to 30 inclusive
# Exercise 2.3
prime = 31
# use range(1, prime) to iterate over all numbers from 1 to 30 inclusive
# Exercise 2.4
reload(ecc)
run_test(ecc.FieldElementTest('test_mul'))
run_test(ecc.FieldElementTest('test_pow'))
\(3/24 = ?\)
\(17^{-3} = ?\)
\(4^{-4}\cdot{11} = ?\)
3.2. Write a program to calculate \(k/1, k/2, k/3, ...k/30\) for some k in \(F_{31}\). Notice anything about these sets?
3.3. Make these tests pass:
ecc.py:FieldElementTest:test_div
# Exercise 3.1
# remember pow(x, p-2, p) is the same as 1/x in F_p
prime = 31
# 3/24 = ?
# 17^(-3) = ?
# 4^(-4)*11 = ?
# Exercise 3.2
from random import randint
prime = 31
k = randint(1, prime)
# use range(31) to iterate over all numbers from 0 to 30 inclusive
# Exercise 3.3
reload(ecc)
run_test(ecc.FieldElementTest('test_div'))
# Hint: the __pow__ method needs a positive number for the exponent.
# You can mod by p-1
\((-2,4), (3,7), (18,77)\)
4.2. Make this test pass
ecc.py:PointTest:test_on_curve
# Exercise 4.1
# (-2,4), (3,7), (18,77)
# equation in python is: y**2 == x**3 + 5*x + 7
points = ((-2,4), (3,7), (18,77))
for x, y in points:
# determine whether (x,y) is on the curve
# Exercise 4.2
reload(ecc)
run_test(ecc.PointTest('test_on_curve'))
5.1. Make this test pass
ecc.py:PointTest:test_add0
# Exercise 5.1
reload(ecc)
run_test(ecc.PointTest('test_add0'))
6.2. Make this test pass
ecc.py:PointTest:test_add1
# Exercise 6.1
x1, y1 = (2,5)
x2, y2 = (-1,-1)
# formula in python:
# s = (y2-y1)/(x2-x1)
# x3 = s**2 - x2 - x1
# y3 = s*(x1-x3)-y1
# Exercise 6.2
reload(ecc)
run_test(ecc.PointTest('test_add1'))
7.2. Make this test pass
ecc.py:PointTest:test_add2
# Exercise 7.1
a, b = 5, 7
x1, y1 = -1, -1
# formula in python
# s = (3*x1**2+a)/(2*y1)
# x3 = s**2 - 2*x1
# y3 = s*(x1-x3) - y1y3 = s*(x1-x3) - y1
# Exercise 7.2
reload(ecc)
run_test(ecc.PointTest('test_add2'))