From f3a3246bc9da027e0527b89204e1b95ed52af5c6 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 19 Oct 2020 12:56:31 +0300 Subject: [PATCH] GOST3410Curve.contains() --- news.texi | 7 +++++++ pygost/__init__.py | 2 +- pygost/gost3410.py | 14 +++++++++++--- pygost/stubs/pygost/gost3410.pyi | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/news.texi b/news.texi index c07e23f..b0c6933 100644 --- a/news.texi +++ b/news.texi @@ -3,6 +3,13 @@ @table @strong +@anchor{Release 5.2} +@item 5.2 + @itemize + @item @code{GOST3410Curve} has @code{.contains(point)} method for + checking if point is on the curve. + @end itemize + @anchor{Release 5.1} @item 5.1 Small typing stubs fixes. diff --git a/pygost/__init__.py b/pygost/__init__.py index 1ea51ea..1c9a1ed 100644 --- a/pygost/__init__.py +++ b/pygost/__init__.py @@ -3,4 +3,4 @@ PyGOST is free software: see the file COPYING for copying conditions. """ -__version__ = "5.1" +__version__ = "5.2" diff --git a/pygost/gost3410.py b/pygost/gost3410.py index 433c818..a2cc7ce 100644 --- a/pygost/gost3410.py +++ b/pygost/gost3410.py @@ -64,9 +64,7 @@ class GOST3410Curve(object): self.cofactor = cofactor self.e = e self.d = d - r1 = self.y * self.y % self.p - r2 = ((self.x * self.x + self.a) * self.x + self.b) % self.p - if r1 != self.pos(r2): + if not self.contains((x, y)): raise ValueError("Invalid parameters") self._st = None @@ -81,6 +79,16 @@ class GOST3410Curve(object): return v + self.p return v + def contains(self, point): + """Is point on the curve? + + :type point: (long, long) + """ + x, y = point + r1 = y * y % self.p + r2 = ((x * x + self.a) * x + self.b) % self.p + return r1 == self.pos(r2) + def _add(self, p1x, p1y, p2x, p2y): if p1x == p2x and p1y == p2y: # double diff --git a/pygost/stubs/pygost/gost3410.pyi b/pygost/stubs/pygost/gost3410.pyi index 7f55fa9..ee0e9b6 100644 --- a/pygost/stubs/pygost/gost3410.pyi +++ b/pygost/stubs/pygost/gost3410.pyi @@ -40,6 +40,8 @@ class GOST3410Curve(object): @property def point_size(self) -> int: ... + def contains(self, point: Tuple[int, int]) -> bool: ... + def public_key(curve: GOST3410Curve, prv: int) -> PublicKey: ... -- 2.44.0