| 166 | } |
| 167 | |
| 168 | void static NegateSignatureS(std::vector<unsigned char>& vchSig) { |
| 169 | // Parse the signature. |
| 170 | std::vector<unsigned char> r, s; |
| 171 | r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]); |
| 172 | s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]); |
| 173 | |
| 174 | // Really ugly to implement mod-n negation here, but it would be feature creep to expose such functionality from libsecp256k1. |
| 175 | static const unsigned char order[33] = { |
| 176 | 0x00, |
| 177 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 178 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, |
| 179 | 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B, |
| 180 | 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41 |
| 181 | }; |
| 182 | while (s.size() < 33) { |
| 183 | s.insert(s.begin(), 0x00); |
| 184 | } |
| 185 | int carry = 0; |
| 186 | for (int p = 32; p >= 1; p--) { |
| 187 | int n = (int)order[p] - s[p] - carry; |
| 188 | s[p] = (n + 256) & 0xFF; |
| 189 | carry = (n < 0); |
| 190 | } |
| 191 | assert(carry == 0); |
| 192 | if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) { |
| 193 | s.erase(s.begin()); |
| 194 | } |
| 195 | |
| 196 | // Reconstruct the signature. |
| 197 | vchSig.clear(); |
| 198 | vchSig.push_back(0x30); |
| 199 | vchSig.push_back(4 + r.size() + s.size()); |
| 200 | vchSig.push_back(0x02); |
| 201 | vchSig.push_back(r.size()); |
| 202 | vchSig.insert(vchSig.end(), r.begin(), r.end()); |
| 203 | vchSig.push_back(0x02); |
| 204 | vchSig.push_back(s.size()); |
| 205 | vchSig.insert(vchSig.end(), s.begin(), s.end()); |
| 206 | } |
| 207 | |
| 208 | namespace |
| 209 | { |