| 196 | } |
| 197 | |
| 198 | void static NegateSignatureS(std::vector<unsigned char>& vchSig) { |
| 199 | // Parse the signature. |
| 200 | std::vector<unsigned char> r, s; |
| 201 | r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]); |
| 202 | s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]); |
| 203 | |
| 204 | // Really ugly to implement mod-n negation here, but it would be feature creep to expose such functionality from libsecp256k1. |
| 205 | static const unsigned char order[33] = { |
| 206 | 0x00, |
| 207 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 208 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 209 | 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, |
| 210 | 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41 |
| 211 | }; |
| 212 | while (s.size() < 33) { |
| 213 | s.insert(s.begin(), 0x00); |
| 214 | } |
| 215 | int carry = 0; |
| 216 | for (int p = 32; p >= 1; p--) { |
| 217 | int n = (int)order[p] - s[p] - carry; |
| 218 | s[p] = (n + 256) & 0xFF; |
| 219 | carry = (n < 0); |
| 220 | } |
| 221 | assert(carry == 0); |
| 222 | if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) { |
| 223 | s.erase(s.begin()); |
| 224 | } |
| 225 | |
| 226 | // Reconstruct the signature. |
| 227 | vchSig.clear(); |
| 228 | vchSig.push_back(0x30); |
| 229 | vchSig.push_back(4 + r.size() + s.size()); |
| 230 | vchSig.push_back(0x02); |
| 231 | vchSig.push_back(r.size()); |
| 232 | vchSig.insert(vchSig.end(), r.begin(), r.end()); |
| 233 | vchSig.push_back(0x02); |
| 234 | vchSig.push_back(s.size()); |
| 235 | vchSig.insert(vchSig.end(), s.begin(), s.end()); |
| 236 | } |
| 237 | |
| 238 | namespace |
| 239 | { |