Add a Jacobian tuple p1 and an affine tuple p2 See https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates - Point Addition (with affine point)
(self, p1, p2)
| 130 | return (x2, y2, z2) |
| 131 | |
| 132 | def add_mixed(self, p1, p2): |
| 133 | """Add a Jacobian tuple p1 and an affine tuple p2 |
| 134 | |
| 135 | See https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates - Point Addition (with affine point)""" |
| 136 | x1, y1, z1 = p1 |
| 137 | x2, y2, z2 = p2 |
| 138 | assert(z2 == 1) |
| 139 | # Adding to the point at infinity is a no-op |
| 140 | if z1 == 0: |
| 141 | return p2 |
| 142 | z1_2 = (z1**2) % self.p |
| 143 | z1_3 = (z1_2 * z1) % self.p |
| 144 | u2 = (x2 * z1_2) % self.p |
| 145 | s2 = (y2 * z1_3) % self.p |
| 146 | if x1 == u2: |
| 147 | if (y1 != s2): |
| 148 | # p1 and p2 are inverses. Return the point at infinity. |
| 149 | return (0, 1, 0) |
| 150 | # p1 == p2. The formulas below fail when the two points are equal. |
| 151 | return self.double(p1) |
| 152 | h = u2 - x1 |
| 153 | r = s2 - y1 |
| 154 | h_2 = (h**2) % self.p |
| 155 | h_3 = (h_2 * h) % self.p |
| 156 | u1_h_2 = (x1 * h_2) % self.p |
| 157 | x3 = (r**2 - h_3 - 2*u1_h_2) % self.p |
| 158 | y3 = (r*(u1_h_2 - x3) - y1*h_3) % self.p |
| 159 | z3 = (h*z1) % self.p |
| 160 | return (x3, y3, z3) |
| 161 | |
| 162 | def add(self, p1, p2): |
| 163 | """Add two Jacobian tuples p1 and p2 |