|
| 1 | +""" |
| 2 | +Copyright (c) 2021 Codiesalert.com |
| 3 | +These scripts should be used for commercial purpose without Codies Alert Permission |
| 4 | +Any violations may lead to legal action |
| 5 | +""" |
| 6 | +from FieldElement import FieldElement |
| 7 | + |
| 8 | + |
| 9 | +class Point: |
| 10 | + def __init__(self, x, y, a, b): |
| 11 | + self.a = a |
| 12 | + self.b = b |
| 13 | + self.x = x |
| 14 | + self.y = y |
| 15 | + if self.x is None and self.y is None: |
| 16 | + return |
| 17 | + if self.y ** 2 != self.x ** 3 + a * x + b: |
| 18 | + raise ValueError("({}, {}) is not on the curve".format(x, y)) |
| 19 | + |
| 20 | + # end::source1[] |
| 21 | + |
| 22 | + def __eq__(self, other): |
| 23 | + return ( |
| 24 | + self.x == other.x |
| 25 | + and self.y == other.y |
| 26 | + and self.a == other.a |
| 27 | + and self.b == other.b |
| 28 | + ) |
| 29 | + |
| 30 | + def __ne__(self, other): |
| 31 | + # this should be the inverse of the == operator |
| 32 | + return not (self == other) |
| 33 | + |
| 34 | + def __repr__(self): |
| 35 | + if self.x is None: |
| 36 | + return "Point(infinity)" |
| 37 | + elif isinstance(self.x, FieldElement): |
| 38 | + return "Point({},{})_{}_{} FieldElement({})".format( |
| 39 | + self.x.num, self.y.num, self.a.num, self.b.num, self.x.prime |
| 40 | + ) |
| 41 | + else: |
| 42 | + return "Point({},{})_{}_{}".format(self.x, self.y, self.a, self.b) |
| 43 | + |
| 44 | + def __add__(self, other): |
| 45 | + if self.a != other.a or self.b != other.b: |
| 46 | + raise TypeError( |
| 47 | + "Points {}, {} are not on the same curve".format(self, other) |
| 48 | + ) |
| 49 | + # Case 0.0: self is the point at infinity, return other |
| 50 | + if self.x is None: |
| 51 | + return other |
| 52 | + # Case 0.1: other is the point at infinity, return self |
| 53 | + if other.x is None: |
| 54 | + return self |
| 55 | + |
| 56 | + # Case 1: self.x == other.x, self.y != other.y |
| 57 | + # Result is point at infinity |
| 58 | + if self.x == other.x and self.y != other.y: |
| 59 | + return self.__class__(None, None, self.a, self.b) |
| 60 | + |
| 61 | + # Case 2: self.x ≠ other.x |
| 62 | + # Formula (x3,y3)==(x1,y1)+(x2,y2) |
| 63 | + # s=(y2-y1)/(x2-x1) |
| 64 | + # x3=s**2-x1-x2 |
| 65 | + # y3=s*(x1-x3)-y1 |
| 66 | + if self.x != other.x: |
| 67 | + s = (other.y - self.y) / (other.x - self.x) |
| 68 | + x = s ** 2 - self.x - other.x |
| 69 | + y = s * (self.x - x) - self.y |
| 70 | + return self.__class__(x, y, self.a, self.b) |
| 71 | + |
| 72 | + # Case 4: if we are tangent to the vertical line, |
| 73 | + # we return the point at infinity |
| 74 | + # note instead of figuring out what 0 is for each type |
| 75 | + # we just use 0 * self.x |
| 76 | + if self == other and self.y == 0 * self.x: |
| 77 | + return self.__class__(None, None, self.a, self.b) |
| 78 | + |
| 79 | + # Case 3: self == other |
| 80 | + # Formula (x3,y3)=(x1,y1)+(x1,y1) |
| 81 | + # s=(3*x1**2+a)/(2*y1) |
| 82 | + # x3=s**2-2*x1 |
| 83 | + # y3=s*(x1-x3)-y1 |
| 84 | + if self == other: |
| 85 | + s = (3 * self.x ** 2 + self.a) / (2 * self.y) |
| 86 | + x = s ** 2 - 2 * self.x |
| 87 | + y = s * (self.x - x) - self.y |
| 88 | + return self.__class__(x, y, self.a, self.b) |
| 89 | + |
| 90 | + # tag::source3[] |
| 91 | + def __rmul__(self, coefficient): |
| 92 | + coef = coefficient |
| 93 | + current = self # <1> |
| 94 | + result = self.__class__(None, None, self.a, self.b) # <2> |
| 95 | + while coef: |
| 96 | + if coef & 1: # <3> |
| 97 | + result += current |
| 98 | + current += current # <4> |
| 99 | + coef >>= 1 # <5> |
| 100 | + return result |
0 commit comments