Add two Jacobian tuples p1 and p2 See https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates - Point Addition
(self, p1, p2)
| 160 | return (x3, y3, z3) |
| 161 | |
| 162 | def add(self, p1, p2): |
| 163 | """Add two Jacobian tuples p1 and p2 |
| 164 | |
| 165 | See https://en.wikibooks.org/wiki/Cryptography/Prime_Curve/Jacobian_Coordinates - Point Addition""" |
| 166 | x1, y1, z1 = p1 |
| 167 | x2, y2, z2 = p2 |
| 168 | # Adding the point at infinity is a no-op |
| 169 | if z1 == 0: |
| 170 | return p2 |
| 171 | if z2 == 0: |
| 172 | return p1 |
| 173 | # Adding an Affine to a Jacobian is more efficient since we save field multiplications and squarings when z = 1 |
| 174 | if z1 == 1: |
| 175 | return self.add_mixed(p2, p1) |
| 176 | if z2 == 1: |
| 177 | return self.add_mixed(p1, p2) |
| 178 | z1_2 = (z1**2) % self.p |
| 179 | z1_3 = (z1_2 * z1) % self.p |
| 180 | z2_2 = (z2**2) % self.p |
| 181 | z2_3 = (z2_2 * z2) % self.p |
| 182 | u1 = (x1 * z2_2) % self.p |
| 183 | u2 = (x2 * z1_2) % self.p |
| 184 | s1 = (y1 * z2_3) % self.p |
| 185 | s2 = (y2 * z1_3) % self.p |
| 186 | if u1 == u2: |
| 187 | if (s1 != s2): |
| 188 | # p1 and p2 are inverses. Return the point at infinity. |
| 189 | return (0, 1, 0) |
| 190 | # p1 == p2. The formulas below fail when the two points are equal. |
| 191 | return self.double(p1) |
| 192 | h = u2 - u1 |
| 193 | r = s2 - s1 |
| 194 | h_2 = (h**2) % self.p |
| 195 | h_3 = (h_2 * h) % self.p |
| 196 | u1_h_2 = (u1 * h_2) % self.p |
| 197 | x3 = (r**2 - h_3 - 2*u1_h_2) % self.p |
| 198 | y3 = (r*(u1_h_2 - x3) - s1*h_3) % self.p |
| 199 | z3 = (h*z1*z2) % self.p |
| 200 | return (x3, y3, z3) |
| 201 | |
| 202 | def mul(self, ps): |
| 203 | """Compute a (multi) point multiplication |