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