diff --git a/README.md b/README.md index dca6776..593ed11 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,8 @@ 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 -* .power(p, n) - n✕P or (P + P + ... + P) n times +* .negate(p) - -p on elliptic curve +* .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 diff --git a/libnum/ecc.py b/libnum/ecc.py index 9c10bd1..af60441 100644 --- a/libnum/ecc.py +++ b/libnum/ecc.py @@ -131,13 +131,22 @@ def add(self, p1, p2): y = (l * (x1 - x) - y1) % self.module # yes, it's that new x return (x, y) + def negate(self, p): + """ + 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 to get -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: