From 453f454c17649e2c2aa768ddfa40af1b7c6ca224 Mon Sep 17 00:00:00 2001 From: tetramur <91258888+tetramur@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:21:35 +0600 Subject: [PATCH 1/4] Update ecc.py The file ecc.py was missing the important function of point negation. Without it, any practical application of this library in the part related to elliptic curves would be impossible. --- libnum/ecc.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/libnum/ecc.py b/libnum/ecc.py index 9c10bd1..478814c 100644 --- a/libnum/ecc.py +++ b/libnum/ecc.py @@ -131,19 +131,28 @@ def add(self, p1, p2): y = (l * (x1 - x) - y1) % self.module # yes, it's that new x return (x, y) - def power(self, p, n): - """ - n✕P or (P + P + ... + P) n times - """ - if n == 0 or self.is_null(p): + def negate(self, p): + """ + Negation of a point or a new point Q where P + Q = O + """ + return (p[0], -p[1] % self.module) + + def power(self, p, n): + """ + n✕P or (P + P + ... + P) n times + If n < 0, firstly negate P and then take (-n)✕(-P) + """ + if n < 0: + return self.power(self.negate(p), -n) + if n == 0 or self.is_null(p): return NULL_POINT - - res = NULL_POINT - while n: - if n & 1: - res = self.add(res, p) - p = self.add(p, p) - n >>= 1 + + res = NULL_POINT + while n: + if n & 1: + res = self.add(res, p) + p = self.add(p, p) + n >>= 1 return res def generate(self, n): From 1e955e587c3cbb3841cbae97bf06d181ae8a2550 Mon Sep 17 00:00:00 2001 From: tetramur <91258888+tetramur@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:47:40 +0600 Subject: [PATCH 2/4] Update ecc.py --- libnum/ecc.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libnum/ecc.py b/libnum/ecc.py index 478814c..90bb8db 100644 --- a/libnum/ecc.py +++ b/libnum/ecc.py @@ -137,22 +137,22 @@ def negate(self, p): """ return (p[0], -p[1] % self.module) - def power(self, p, n): - """ - n✕P or (P + P + ... + P) n times - If n < 0, firstly negate P and then take (-n)✕(-P) - """ - if n < 0: - return self.power(self.negate(p), -n) - if n == 0 or self.is_null(p): + def power(self, p, n): + """ + n✕P or (P + P + ... + P) n times + If n < 0, firstly negate P and then take (-n)✕(-P) + """ + if n < 0: + return self.power(self.negate(p), -n) + if n == 0 or self.is_null(p): return NULL_POINT - res = NULL_POINT - while n: - if n & 1: - res = self.add(res, p) - p = self.add(p, p) - n >>= 1 + res = NULL_POINT + while n: + if n & 1: + res = self.add(res, p) + p = self.add(p, p) + n >>= 1 return res def generate(self, n): From 12609c5cf335333f59a16698555f36ca31871dd9 Mon Sep 17 00:00:00 2001 From: tetramur <91258888+tetramur@users.noreply.github.com> Date: Mon, 11 Nov 2024 20:31:19 +0600 Subject: [PATCH 3/4] Update ecc.py and README.md Comments in ecc.py were slightly refined to better reflect their essence. The file README.md was updated in order to correspond to the contents of ecc.py. --- README.md | 1 + libnum/ecc.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dca6776..385c99e 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ warning: format of factorization is now dict like {p1: e1, p2: e2, ...} * .find\_points\_in\_range(start, end) - list of points in range of x coordinate * .find\_points\_rand(count) - list of count random points * .add(p1, p2) - p1 + p2 on elliptic curve +* .negate(p) - -p on elliptic curve * .power(p, n) - n✕P or (P + P + ... + P) n times * .generate(n) - n✕G * .get\_order(p, limit) - slow method, trying to determine order of p; limit is max order to try diff --git a/libnum/ecc.py b/libnum/ecc.py index 90bb8db..af60441 100644 --- a/libnum/ecc.py +++ b/libnum/ecc.py @@ -133,14 +133,14 @@ def add(self, p1, p2): def negate(self, p): """ - Negation of a point or a new point Q where P + Q = O + Negation of a point or a new point Q = -P where P + Q = O """ return (p[0], -p[1] % self.module) def power(self, p, n): """ n✕P or (P + P + ... + P) n times - If n < 0, firstly negate P and then take (-n)✕(-P) + If n < 0, firstly negate P to get -P and then take (-n)✕(-P) """ if n < 0: return self.power(self.negate(p), -n) From db6b89d797f2f86cce0aca342023289285e0e239 Mon Sep 17 00:00:00 2001 From: tetramur <91258888+tetramur@users.noreply.github.com> Date: Mon, 11 Nov 2024 20:39:57 +0600 Subject: [PATCH 4/4] Update ecc.py and README.md Added a small clarification related to the multiplying of a point P by the number n when n is negative. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 385c99e..593ed11 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ warning: format of factorization is now dict like {p1: e1, p2: e2, ...} * .find\_points\_rand(count) - list of count random points * .add(p1, p2) - p1 + p2 on elliptic curve * .negate(p) - -p on elliptic curve -* .power(p, n) - n✕P or (P + P + ... + P) n times +* .power(p, n) - n✕P or (P + P + ... + P) n times. If n < 0, firstly negate P to get (-P) and then take (-n)✕(-P) * .generate(n) - n✕G * .get\_order(p, limit) - slow method, trying to determine order of p; limit is max order to try