| 1 | // Copyright (c) 2016, Monero Research Labs |
| 2 | // |
| 3 | // Author: Shen Noether <[email protected]> |
| 4 | // |
| 5 | // All rights reserved. |
| 6 | // |
| 7 | // Redistribution and use in source and binary forms, with or without modification, are |
| 8 | // permitted provided that the following conditions are met: |
| 9 | // |
| 10 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | // conditions and the following disclaimer. |
| 12 | // |
| 13 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 14 | // of conditions and the following disclaimer in the documentation and/or other |
| 15 | // materials provided with the distribution. |
| 16 | // |
| 17 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 18 | // used to endorse or promote products derived from this software without specific |
| 19 | // prior written permission. |
| 20 | // |
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 22 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 24 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 28 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 29 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | #include "misc_log_ex.h" |
| 32 | #include "cryptonote_config.h" |
| 33 | #include "rctTypes.h" |
| 34 | #include "int-util.h" |
| 35 | using namespace crypto; |
| 36 | using namespace std; |
| 37 | |
| 38 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 39 | #define MONERO_DEFAULT_LOG_CATEGORY "ringct" |
| 40 | |
| 41 | namespace rct { |
| 42 | |
| 43 | //dp |
| 44 | //Debug printing for the above types |
| 45 | //Actually use DP(value) and #define DBG |
| 46 | |
| 47 | void dp(key a) { |
| 48 | int j = 0; |
| 49 | printf("\""); |
| 50 | for (j = 0; j < 32; j++) { |
| 51 | printf("%02x", (unsigned char)a.bytes[j]); |
| 52 | } |
| 53 | printf("\""); |
| 54 | printf("\n"); |
| 55 | } |
| 56 | |
| 57 | void dp(bool a) { |
| 58 | printf(" ... %s ... ", a ? "true" : "false"); |
| 59 | printf("\n"); |
| 60 | } |
| 61 | |
| 62 | void dp(const char * a, int l) { |
| 63 | int j = 0; |
| 64 | printf("\""); |
| 65 | for (j = 0; j < l; j++) { |
| 66 | printf("%02x", (unsigned char)a[j]); |
| 67 | } |
| 68 | printf("\""); |
| 69 | printf("\n"); |
| 70 | } |
| 71 | void dp(keyV a) { |
| 72 | size_t j = 0; |
| 73 | printf("["); |
| 74 | for (j = 0; j < a.size(); j++) { |
| 75 | dp(a[j]); |
| 76 | if (j < a.size() - 1) { |
| 77 | printf(","); |
| 78 | } |
| 79 | } |
| 80 | printf("]"); |
| 81 | printf("\n"); |
| 82 | } |
| 83 | void dp(keyM a) { |
| 84 | size_t j = 0; |
| 85 | printf("["); |
| 86 | for (j = 0; j < a.size(); j++) { |
| 87 | dp(a[j]); |
| 88 | if (j < a.size() - 1) { |
| 89 | printf(","); |
| 90 | } |
| 91 | } |
| 92 | printf("]"); |
| 93 | printf("\n"); |
| 94 | } |
| 95 | void dp(xmr_amount vali) { |
| 96 | printf("x: "); |
| 97 | std::cout << vali; |
| 98 | printf("\n\n"); |
| 99 | } |
| 100 | |
| 101 | void dp(int vali) { |
| 102 | printf("x: %d\n", vali); |
| 103 | printf("\n"); |
| 104 | } |
| 105 | void dp(bits amountb) { |
| 106 | for (int i = 0; i < 64; i++) { |
| 107 | printf("%d", amountb[i]); |
| 108 | } |
| 109 | printf("\n"); |
| 110 | |
| 111 | } |
| 112 | |
| 113 | void dp(const char * st) { |
| 114 | printf("%s\n", st); |
| 115 | } |
| 116 | |
| 117 | //Various Conversions |
| 118 | |
| 119 | //uint long long to 32 byte key |
| 120 | void d2h(key & amounth, const xmr_amount in) { |
| 121 | sc_0(amounth.bytes); |
| 122 | memcpy_swap64le(amounth.bytes, &in, 1); |
| 123 | } |
| 124 | |
| 125 | //uint long long to 32 byte key |
| 126 | key d2h(const xmr_amount in) { |
| 127 | key amounth; |
| 128 | d2h(amounth, in); |
| 129 | return amounth; |
| 130 | } |
| 131 | |
| 132 | //uint long long to int[64] |
| 133 | void d2b(bits amountb, xmr_amount val) { |
| 134 | int i = 0; |
| 135 | while (i < 64) { |
| 136 | amountb[i++] = val & 1; |
| 137 | val >>= 1; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | //32 byte key to uint long long |
| 142 | // if the key holds a value > 2^64 |
| 143 | // then the value in the first 8 bytes is returned |
| 144 | xmr_amount h2d(const key & test) { |
| 145 | xmr_amount vali = 0; |
| 146 | int j = 0; |
| 147 | for (j = 7; j >= 0; j--) { |
| 148 | vali = (xmr_amount)(vali * 256 + (unsigned char)test.bytes[j]); |
| 149 | } |
| 150 | return vali; |
| 151 | } |
| 152 | |
| 153 | //32 byte key to int[64] |
| 154 | void h2b(bits amountb2, const key & test) { |
| 155 | int val = 0, i = 0, j = 0; |
| 156 | for (j = 0; j < 8; j++) { |
| 157 | val = (unsigned char)test.bytes[j]; |
| 158 | i = 0; |
| 159 | while (i < 8) { |
| 160 | amountb2[j*8+i++] = val & 1; |
| 161 | val >>= 1; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | //int[64] to 32 byte key |
| 167 | void b2h(key & amountdh, const bits amountb2) { |
| 168 | int byte, i, j; |
| 169 | for (j = 0; j < 8; j++) { |
| 170 | byte = 0; |
| 171 | for (i = 7; i > -1; i--) { |
| 172 | byte = byte * 2 + amountb2[8 * j + i]; |
| 173 | } |
| 174 | amountdh[j] = (unsigned char)byte; |
| 175 | } |
| 176 | for (j = 8; j < 32; j++) { |
| 177 | amountdh[j] = (unsigned char)(0x00); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | //int[64] to uint long long |
| 182 | xmr_amount b2d(bits amountb) { |
| 183 | xmr_amount vali = 0; |
| 184 | int j = 0; |
| 185 | for (j = 63; j >= 0; j--) { |
| 186 | vali = (xmr_amount)(vali * 2 + amountb[j]); |
| 187 | } |
| 188 | return vali; |
| 189 | } |
| 190 | |
| 191 | bool is_rct_simple(int type) |
| 192 | { |
| 193 | switch (type) |
| 194 | { |
| 195 | case RCTTypeSimple: |
| 196 | case RCTTypeBulletproof: |
| 197 | case RCTTypeBulletproof2: |
| 198 | case RCTTypeCLSAG: |
| 199 | case RCTTypeBulletproofPlus: |
| 200 | return true; |
| 201 | default: |
| 202 | return false; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | bool is_rct_bulletproof(int type) |
| 207 | { |
| 208 | switch (type) |
| 209 | { |
| 210 | case RCTTypeBulletproof: |
| 211 | case RCTTypeBulletproof2: |
| 212 | case RCTTypeCLSAG: |
| 213 | return true; |
| 214 | default: |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | bool is_rct_bulletproof_plus(int type) |
| 220 | { |
| 221 | switch (type) |
| 222 | { |
| 223 | case RCTTypeBulletproofPlus: |
| 224 | return true; |
| 225 | default: |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | bool is_rct_borromean(int type) |
| 231 | { |
| 232 | switch (type) |
| 233 | { |
| 234 | case RCTTypeSimple: |
| 235 | case RCTTypeFull: |
| 236 | return true; |
| 237 | default: |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool is_rct_clsag(int type) |
| 243 | { |
| 244 | switch (type) |
| 245 | { |
| 246 | case RCTTypeCLSAG: |
| 247 | case RCTTypeBulletproofPlus: |
| 248 | return true; |
| 249 | default: |
| 250 | return false; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static size_t n_bulletproof_amounts_base(const size_t L_size, const size_t R_size, const size_t V_size, const size_t max_outputs) |
| 255 | { |
| 256 | CHECK_AND_ASSERT_MES(L_size >= 6, 0, "Invalid bulletproof L size"); |
| 257 | CHECK_AND_ASSERT_MES(L_size == R_size, 0, "Mismatched bulletproof L/R size"); |
| 258 | static const size_t extra_bits = 4; |
| 259 | CHECK_AND_ASSERT_MES((1 << extra_bits) == max_outputs, 0, "log2(max_outputs) is out of date"); |
| 260 | CHECK_AND_ASSERT_MES(L_size <= 6 + extra_bits, 0, "Invalid bulletproof L size"); |
| 261 | CHECK_AND_ASSERT_MES(V_size <= (1u<<(L_size-6)), 0, "Invalid bulletproof V/L"); |
| 262 | CHECK_AND_ASSERT_MES(V_size * 2 > (1u<<(L_size-6)), 0, "Invalid bulletproof V/L"); |
| 263 | CHECK_AND_ASSERT_MES(V_size > 0, 0, "Empty bulletproof"); |
| 264 | return V_size; |
| 265 | } |
| 266 | |
| 267 | size_t n_bulletproof_amounts(const Bulletproof &proof) { return n_bulletproof_amounts_base(proof.L.size(), proof.R.size(), proof.V.size(), BULLETPROOF_MAX_OUTPUTS); } |
| 268 | size_t n_bulletproof_plus_amounts(const BulletproofPlus &proof) { return n_bulletproof_amounts_base(proof.L.size(), proof.R.size(), proof.V.size(), BULLETPROOF_PLUS_MAX_OUTPUTS); } |
| 269 | |
| 270 | size_t n_bulletproof_amounts(const std::vector<Bulletproof> &proofs) |
| 271 | { |
| 272 | size_t n = 0; |
| 273 | for (const Bulletproof &proof: proofs) |
| 274 | { |
| 275 | size_t n2 = n_bulletproof_amounts(proof); |
| 276 | CHECK_AND_ASSERT_MES(n2 < std::numeric_limits<uint32_t>::max() - n, 0, "Invalid number of bulletproofs"); |
| 277 | if (n2 == 0) |
| 278 | return 0; |
| 279 | n += n2; |
| 280 | } |
| 281 | return n; |
| 282 | } |
| 283 | |
| 284 | size_t n_bulletproof_plus_amounts(const std::vector<BulletproofPlus> &proofs) |
| 285 | { |
| 286 | size_t n = 0; |
| 287 | for (const BulletproofPlus &proof: proofs) |
| 288 | { |
| 289 | size_t n2 = n_bulletproof_plus_amounts(proof); |
| 290 | CHECK_AND_ASSERT_MES(n2 < std::numeric_limits<uint32_t>::max() - n, 0, "Invalid number of bulletproofs"); |
| 291 | if (n2 == 0) |
| 292 | return 0; |
| 293 | n += n2; |
| 294 | } |
| 295 | return n; |
| 296 | } |
| 297 | |
| 298 | static size_t n_bulletproof_max_amounts_base(size_t L_size, size_t R_size, size_t max_outputs) |
| 299 | { |
| 300 | CHECK_AND_ASSERT_MES(L_size >= 6, 0, "Invalid bulletproof L size"); |
| 301 | CHECK_AND_ASSERT_MES(L_size == R_size, 0, "Mismatched bulletproof L/R size"); |
| 302 | static const size_t extra_bits = 4; |
| 303 | CHECK_AND_ASSERT_MES((1 << extra_bits) == max_outputs, 0, "log2(max_outputs) is out of date"); |
| 304 | CHECK_AND_ASSERT_MES(L_size <= 6 + extra_bits, 0, "Invalid bulletproof L size"); |
| 305 | return 1 << (L_size - 6); |
| 306 | } |
| 307 | size_t n_bulletproof_max_amounts(const Bulletproof &proof) { return n_bulletproof_max_amounts_base(proof.L.size(), proof.R.size(), BULLETPROOF_MAX_OUTPUTS); } |
| 308 | size_t n_bulletproof_plus_max_amounts(const BulletproofPlus &proof) { return n_bulletproof_max_amounts_base(proof.L.size(), proof.R.size(), BULLETPROOF_PLUS_MAX_OUTPUTS); } |
| 309 | |
| 310 | size_t n_bulletproof_max_amounts(const std::vector<Bulletproof> &proofs) |
| 311 | { |
| 312 | size_t n = 0; |
| 313 | for (const Bulletproof &proof: proofs) |
| 314 | { |
| 315 | size_t n2 = n_bulletproof_max_amounts(proof); |
| 316 | CHECK_AND_ASSERT_MES(n2 < std::numeric_limits<uint32_t>::max() - n, 0, "Invalid number of bulletproofs"); |
| 317 | if (n2 == 0) |
| 318 | return 0; |
| 319 | n += n2; |
| 320 | } |
| 321 | return n; |
| 322 | } |
| 323 | |
| 324 | size_t n_bulletproof_plus_max_amounts(const std::vector<BulletproofPlus> &proofs) |
| 325 | { |
| 326 | size_t n = 0; |
| 327 | for (const BulletproofPlus &proof: proofs) |
| 328 | { |
| 329 | size_t n2 = n_bulletproof_plus_max_amounts(proof); |
| 330 | CHECK_AND_ASSERT_MES(n2 < std::numeric_limits<uint32_t>::max() - n, 0, "Invalid number of bulletproofs"); |
| 331 | if (n2 == 0) |
| 332 | return 0; |
| 333 | n += n2; |
| 334 | } |
| 335 | return n; |
| 336 | } |
| 337 | |
| 338 | } |