-
Notifications
You must be signed in to change notification settings - Fork 330
Added __eq__ method to classes to improve comparison abilities #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
179d528
907a2df
8e9636d
b8d4122
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,9 @@ t/ | |
| .project | ||
| .pydevproject | ||
|
|
||
| #vscode | ||
| .vscode | ||
|
|
||
| # Backup files | ||
| *.swp | ||
| *~ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,28 +30,11 @@ | |
| Gx = 0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012 | ||
| Gy = 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811 | ||
|
|
||
|
|
||
| c192 = CurveFp(p, -3, b) | ||
| p192 = Point(c192, Gx, Gy, r) | ||
|
|
||
|
|
||
| def test_p192(): | ||
| # Checking against some sample computations presented | ||
| # in X9.62: | ||
| d = 651056770906015076056810763456358567190100156695615665659 | ||
| Q = d * p192 | ||
| assert Q.x() == 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5 | ||
|
|
||
| k = 6140507067065001063065065565667405560006161556565665656654 | ||
| R = k * p192 | ||
| assert R.x() == 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD \ | ||
| and R.y() == 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835 | ||
|
|
||
| u1 = 2563697409189434185194736134579731015366492496392189760599 | ||
| u2 = 6266643813348617967186477710235785849136406323338782220568 | ||
| temp = u1 * p192 + u2 * Q | ||
| assert temp.x() == 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD \ | ||
| and temp.y() == 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835 | ||
| c_23 = CurveFp(23, 1, 1) | ||
| g_23 = Point(c_23, 13, 7, 7) | ||
|
|
||
|
|
||
| @settings(**HYP_SETTINGS) | ||
|
|
@@ -61,63 +44,16 @@ def test_p192_mult_tests(multiple): | |
|
|
||
| p1 = p192 * multiple | ||
| assert p1 * inv_m == p192 | ||
|
|
||
|
|
||
| def add_n_times(point, n): | ||
| ret = INFINITY | ||
| i = 0 | ||
| while i <= n: | ||
| yield ret | ||
| ret = ret + point | ||
| i += 1 | ||
|
|
||
|
|
||
| c_23 = CurveFp(23, 1, 1) | ||
|
|
||
|
|
||
| g_23 = Point(c_23, 13, 7, 7) | ||
|
|
||
|
|
||
| # Trivial tests from X9.62 B.3: | ||
| @pytest.mark.parametrize( | ||
| "c,x1,y1,x2,y2,x3,y3", | ||
| [(c_23, 3, 10, 9, 7, 17, 20), | ||
| (c_23, 3, 10, 3, 10, 7, 12)], | ||
| ids=["real add", "double"]) | ||
| def test_add(c, x1, y1, x2, y2, x3, y3): | ||
| """We expect that on curve c, (x1,y1) + (x2, y2 ) = (x3, y3).""" | ||
| p1 = Point(c, x1, y1) | ||
| p2 = Point(c, x2, y2) | ||
| p3 = p1 + p2 | ||
| assert p3.x() == x3 and p3.y() == y3 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "c, x1, y1, x3, y3", | ||
| [(c_23, 3, 10, 7, 12)], | ||
| ids=["real add"]) | ||
| def test_double(c, x1, y1, x3, y3): | ||
| p1 = Point(c, x1, y1) | ||
| p3 = p1.double() | ||
| assert p3.x() == x3 and p3.y() == y3 | ||
|
|
||
|
|
||
| def test_double_infinity(): | ||
| p1 = INFINITY | ||
| p3 = p1.double() | ||
| assert p1 == p3 | ||
| assert p3.x() == p1.x() and p3.y() == p3.y() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "c, x1, y1, m, x3, y3", | ||
| [(c_23, 3, 10, 2, 7, 12)], | ||
| ids=["multiply by 2"]) | ||
| def test_multiply(c, x1, y1, m, x3, y3): | ||
| p1 = Point(c, x1, y1) | ||
| p3 = p1 * m | ||
| assert p3.x() == x3 and p3.y() == y3 | ||
|
|
||
|
|
||
|
|
||
| # From X9.62 I.1 (p. 96): | ||
| @pytest.mark.parametrize( | ||
|
|
@@ -126,3 +62,109 @@ def test_multiply(c, x1, y1, m, x3, y3): | |
| ids=["g_23 test with mult {0}".format(i) for i in range(9)]) | ||
| def test_add_and_mult_equivalence(p, m, check): | ||
| assert p * m == check | ||
|
|
||
|
|
||
| class TestCurve(unittest.TestCase): | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.c_23 = CurveFp(23, 1, 1) | ||
|
|
||
| def test_equality_curves(self): | ||
| self.assertEqual(self.c_23, CurveFp(23, 1, 1)) | ||
|
|
||
| def test_inequality_curves(self): | ||
| c192 = CurveFp(p, -3, b) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the other test cases use global variables because they use the functional style and the pytest framework, this one uses the object-based approach from unittest, so I'd say that the use of global variables is less clean here...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to classes. Not sure if it is possible to convert test_p192_mult_tests preserving functionality.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, I'm fine with either approach, what I'm not a fan of is mixing them |
||
| self.assertNotEqual(self.c_23, c192) | ||
|
|
||
|
|
||
| class TestPoint(unittest.TestCase): | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.c_23 = CurveFp(23, 1, 1) | ||
| cls.g_23 = Point(cls.c_23, 13, 7, 7) | ||
|
|
||
| p = 6277101735386680763835789423207666416083908700390324961279 | ||
| r = 6277101735386680763835789423176059013767194773182842284081 | ||
| # s = 0x3045ae6fc8422f64ed579528d38120eae12196d5 | ||
| # c = 0x3099d2bbbfcb2538542dcd5fb078b6ef5f3d6fe2c745de65 | ||
| b = 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1 | ||
| Gx = 0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012 | ||
| Gy = 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811 | ||
|
|
||
| cls.c192 = CurveFp(p, -3, b) | ||
| cls.p192 = Point(cls.c192, Gx, Gy, r) | ||
|
|
||
| def test_p192(self): | ||
| # Checking against some sample computations presented | ||
| # in X9.62: | ||
| d = 651056770906015076056810763456358567190100156695615665659 | ||
| Q = d * self.p192 | ||
| self.assertEqual(Q.x(), 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5) | ||
|
|
||
| k = 6140507067065001063065065565667405560006161556565665656654 | ||
| R = k * self.p192 | ||
| self.assertEqual(R.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD) | ||
| self.assertEqual(R.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835) | ||
|
|
||
| u1 = 2563697409189434185194736134579731015366492496392189760599 | ||
| u2 = 6266643813348617967186477710235785849136406323338782220568 | ||
| temp = u1 * self.p192 + u2 * Q | ||
| self.assertEqual(temp.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD) | ||
| self.assertEqual(temp.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835) | ||
|
|
||
| def test_double_infinity(self): | ||
| p1 = INFINITY | ||
| p3 = p1.double() | ||
| self.assertEqual(p1, p3) | ||
| self.assertEqual(p3.x(), p1.x()) | ||
| self.assertEqual(p3.y(), p3.y()) | ||
|
|
||
| def test_double(self): | ||
| x1, y1, x3, y3 = (3, 10, 7, 12) | ||
|
|
||
| p1 = Point(self.c_23, x1, y1) | ||
| p3 = p1.double() | ||
| self.assertEqual(p3.x(), x3) | ||
| self.assertEqual(p3.y(), y3) | ||
|
|
||
| def test_multiply(self): | ||
| x1, y1, m, x3, y3 = (3, 10, 2, 7, 12) | ||
| p1 = Point(self.c_23, x1, y1) | ||
| p3 = p1 * m | ||
| self.assertEqual(p3.x(), x3) | ||
| self.assertEqual(p3.y(), y3) | ||
|
|
||
| # Trivial tests from X9.62 B.3: | ||
| def test_add(self): | ||
| """We expect that on curve c, (x1,y1) + (x2, y2 ) = (x3, y3).""" | ||
|
|
||
| x1, y1, x2, y2, x3, y3 = (3, 10, 9, 7, 17, 20) | ||
| p1 = Point(self.c_23, x1, y1) | ||
| p2 = Point(self.c_23, x2, y2) | ||
| p3 = p1 + p2 | ||
| self.assertEqual(p3.x(), x3) | ||
| self.assertEqual(p3.y(), y3) | ||
|
|
||
| def test_add_as_double(self): | ||
| """We expect that on curve c, (x1,y1) + (x2, y2 ) = (x3, y3).""" | ||
|
|
||
| x1, y1, x2, y2, x3, y3 = (3, 10, 3, 10, 7, 12) | ||
| p1 = Point(self.c_23, x1, y1) | ||
| p2 = Point(self.c_23, x2, y2) | ||
| p3 = p1 + p2 | ||
| self.assertEqual(p3.x(), x3) | ||
| self.assertEqual(p3.y(), y3) | ||
|
|
||
| def test_equality_points(self): | ||
| self.assertEqual(self.g_23, Point(self.c_23, 13, 7, 7)) | ||
|
|
||
| def test_inequality_points(self): | ||
| c = CurveFp(100, -3, 100) | ||
| p = Point(c, 100, 100, 100) | ||
| self.assertNotEqual(self.g_23, p) | ||
tomato42 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_inaquality_points_diff_types(self): | ||
| c = CurveFp(100, -3, 100) | ||
| self.assertNotEqual(self.g_23, c) | ||
Uh oh!
There was an error while loading. Please reload this page.