From 0645824e4d9db92ea03d2bbebd42730f0c8c8d0d Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Wed, 26 Feb 2020 19:23:03 +0000 Subject: [PATCH] eccref.py: handle order-2 points in Montgomery curves. If a point doubles to the identity, we should return the identity, rather than throwing a Python divide-by-zero exception. --- test/eccref.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/eccref.py b/test/eccref.py index 44743044..b1aee1b1 100644 --- a/test/eccref.py +++ b/test/eccref.py @@ -316,9 +316,13 @@ class MontgomeryCurve(CurveBase): xdiff = x2-x1 if xdiff != 0: slope = (y2-y1) / xdiff - else: + elif y1 != 0: assert y1 == y2 slope = (3*x1*x1 + 2*self.curve.a*x1 + 1) / (2*self.curve.b*y1) + else: + # If y1 was 0 as well, then we must have found an + # order-2 point that doubles to the identity. + return self.curve.point() xp = self.curve.b*slope*slope - self.curve.a - x1 - x2 yp = -(y1 + slope * (xp-x1)) return self.curve.point(xp, yp)