| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include "gtest/gtest.h" |
| 32 | |
| 33 | #include <cstdint> |
| 34 | #include <algorithm> |
| 35 | #include <sstream> |
| 36 | |
| 37 | #include "ringct/rctTypes.h" |
| 38 | #include "ringct/rctSigs.h" |
| 39 | #include "ringct/rctOps.h" |
| 40 | #include "device/device.hpp" |
| 41 | #include "string_tools.h" |
| 42 | |
| 43 | using namespace std; |
| 44 | using namespace crypto; |
| 45 | using namespace rct; |
| 46 | |
| 47 | TEST(ringct, Borromean) |
| 48 | { |
| 49 | int j = 0; |
| 50 | |
| 51 | //Tests for Borromean signatures |
| 52 | //#boro true one, false one, C != sum Ci, and one out of the range.. |
| 53 | int N = 64; |
| 54 | key64 xv; |
| 55 | key64 P1v; |
| 56 | key64 P2v; |
| 57 | bits indi; |
| 58 | |
| 59 | for (j = 0 ; j < N ; j++) { |
| 60 | indi[j] = (int)randXmrAmount(2); |
| 61 | |
| 62 | xv[j] = skGen(); |
| 63 | if ( (int)indi[j] == 0 ) { |
| 64 | scalarmultBase(P1v[j], xv[j]); |
| 65 | } else { |
| 66 | addKeys1(P1v[j], xv[j], H2[j]); |
| 67 | } |
| 68 | subKeys(P2v[j], P1v[j], H2[j]); |
| 69 | } |
| 70 | |
| 71 | //#true one |
| 72 | boroSig bb = genBorromean(xv, P1v, P2v, indi); |
| 73 | ASSERT_TRUE(verifyBorromean(bb, P1v, P2v)); |
| 74 | |
| 75 | //#false one |
| 76 | indi[3] = (indi[3] + 1) % 2; |
| 77 | bb = genBorromean(xv, P1v, P2v, indi); |
| 78 | ASSERT_FALSE(verifyBorromean(bb, P1v, P2v)); |
| 79 | |
| 80 | //#true one again |
| 81 | indi[3] = (indi[3] + 1) % 2; |
| 82 | bb = genBorromean(xv, P1v, P2v, indi); |
| 83 | ASSERT_TRUE(verifyBorromean(bb, P1v, P2v)); |
| 84 | |
| 85 | //#false one |
| 86 | bb = genBorromean(xv, P2v, P1v, indi); |
| 87 | ASSERT_FALSE(verifyBorromean(bb, P1v, P2v)); |
| 88 | } |
| 89 | |
| 90 | TEST(ringct, MG_sigs) |
| 91 | { |
| 92 | int j = 0; |
| 93 | int N = 0; |
| 94 | |
| 95 | //Tests for MG Sigs |
| 96 | //#MG sig: true one |
| 97 | N = 3;// #cols |
| 98 | int R = 3;// #rows |
| 99 | keyV xtmp = skvGen(R); |
| 100 | keyM xm = keyMInit(R, N);// = [[None]*N] #just used to generate test public keys |
| 101 | keyV sk = skvGen(R); |
| 102 | keyM P = keyMInit(R, N);// = keyM[[None]*N] #stores the public keys; |
| 103 | int ind = 2; |
| 104 | int i = 0; |
| 105 | for (j = 0 ; j < R ; j++) { |
| 106 | for (i = 0 ; i < N ; i++) |
| 107 | { |
| 108 | xm[i][j] = skGen(); |
| 109 | P[i][j] = scalarmultBase(xm[i][j]); |
| 110 | } |
| 111 | } |
| 112 | for (j = 0 ; j < R ; j++) { |
| 113 | sk[j] = xm[ind][j]; |
| 114 | } |
| 115 | key message = identity(); |
| 116 | mgSig IIccss = MLSAG_Gen(message, P, sk, ind, R, hw::get_device("default")); |
| 117 | ASSERT_TRUE(MLSAG_Ver(message, P, IIccss, R)); |
| 118 | |
| 119 | //#MG sig: false one |
| 120 | N = 3;// #cols |
| 121 | R = 3;// #rows |
| 122 | xtmp = skvGen(R); |
| 123 | keyM xx(N, xtmp);// = [[None]*N] #just used to generate test public keys |
| 124 | sk = skvGen(R); |
| 125 | //P (N, xtmp);// = keyM[[None]*N] #stores the public keys; |
| 126 | |
| 127 | ind = 2; |
| 128 | for (j = 0 ; j < R ; j++) { |
| 129 | for (i = 0 ; i < N ; i++) |
| 130 | { |
| 131 | xx[i][j] = skGen(); |
| 132 | P[i][j] = scalarmultBase(xx[i][j]); |
| 133 | } |
| 134 | sk[j] = xx[ind][j]; |
| 135 | } |
| 136 | sk[2] = skGen();//assume we don't know one of the private keys.. |
| 137 | IIccss = MLSAG_Gen(message, P, sk, ind, R, hw::get_device("default")); |
| 138 | ASSERT_FALSE(MLSAG_Ver(message, P, IIccss, R)); |
| 139 | } |
| 140 | |
| 141 | TEST(ringct, CLSAG) |
| 142 | { |
| 143 | const size_t N = 11; |
| 144 | const size_t idx = 5; |
| 145 | ctkeyV pubs; |
| 146 | key p, t, t2, u; |
| 147 | const key message = identity(); |
| 148 | ctkey backup; |
| 149 | clsag clsag; |
| 150 | |
| 151 | for (size_t i = 0; i < N; ++i) |
| 152 | { |
| 153 | key sk; |
| 154 | ctkey tmp; |
| 155 | |
| 156 | skpkGen(sk, tmp.dest); |
| 157 | skpkGen(sk, tmp.mask); |
| 158 | |
| 159 | pubs.push_back(tmp); |
| 160 | } |
| 161 | |
| 162 | // Set P[idx] |
| 163 | skpkGen(p, pubs[idx].dest); |
| 164 | |
| 165 | // Set C[idx] |
| 166 | t = skGen(); |
| 167 | u = skGen(); |
| 168 | addKeys2(pubs[idx].mask,t,u,H); |
| 169 | |
| 170 | // Set commitment offset |
| 171 | key Cout; |
| 172 | t2 = skGen(); |
| 173 | addKeys2(Cout,t2,u,H); |
| 174 | |
| 175 | // Prepare generation inputs |
| 176 | ctkey insk; |
| 177 | insk.dest = p; |
| 178 | insk.mask = t; |
| 179 | |
| 180 | // bad message |
| 181 | clsag = rct::proveRctCLSAGSimple(zero(),pubs,insk,t2,Cout,idx,hw::get_device("default")); |
| 182 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 183 | |
| 184 | // bad index at creation |
| 185 | try |
| 186 | { |
| 187 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk,t2,Cout,(idx + 1) % N,hw::get_device("default")); |
| 188 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 189 | } |
| 190 | catch (...) { /* either exception, or failure to verify above */ } |
| 191 | |
| 192 | // bad z at creation |
| 193 | try |
| 194 | { |
| 195 | ctkey insk2; |
| 196 | insk2.dest = insk.dest; |
| 197 | insk2.mask = skGen(); |
| 198 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk2,t2,Cout,idx,hw::get_device("default")); |
| 199 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 200 | } |
| 201 | catch (...) { /* either exception, or failure to verify above */ } |
| 202 | |
| 203 | // bad C at creation |
| 204 | backup = pubs[idx]; |
| 205 | pubs[idx].mask = scalarmultBase(skGen()); |
| 206 | try |
| 207 | { |
| 208 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk,t2,Cout,idx,hw::get_device("default")); |
| 209 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 210 | } |
| 211 | catch (...) { /* either exception, or failure to verify above */ } |
| 212 | pubs[idx] = backup; |
| 213 | |
| 214 | // bad p at creation |
| 215 | try |
| 216 | { |
| 217 | ctkey insk2; |
| 218 | insk2.dest = skGen(); |
| 219 | insk2.mask = insk.mask; |
| 220 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk2,t2,Cout,idx,hw::get_device("default")); |
| 221 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 222 | } |
| 223 | catch (...) { /* either exception, or failure to verify above */ } |
| 224 | |
| 225 | // bad P at creation |
| 226 | backup = pubs[idx]; |
| 227 | pubs[idx].dest = scalarmultBase(skGen()); |
| 228 | try |
| 229 | { |
| 230 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk,t2,Cout,idx,hw::get_device("default")); |
| 231 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 232 | } |
| 233 | catch (...) { /* either exception, or failure to verify above */ } |
| 234 | pubs[idx] = backup; |
| 235 | |
| 236 | // Test correct signature |
| 237 | clsag = rct::proveRctCLSAGSimple(message,pubs,insk,t2,Cout,idx,hw::get_device("default")); |
| 238 | ASSERT_TRUE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 239 | |
| 240 | // empty s |
| 241 | auto sbackup = clsag.s; |
| 242 | clsag.s.clear(); |
| 243 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 244 | clsag.s = sbackup; |
| 245 | |
| 246 | // too few s elements |
| 247 | key backup_key; |
| 248 | backup_key = clsag.s.back(); |
| 249 | clsag.s.pop_back(); |
| 250 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 251 | clsag.s.push_back(backup_key); |
| 252 | |
| 253 | // too many s elements |
| 254 | clsag.s.push_back(skGen()); |
| 255 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 256 | clsag.s.pop_back(); |
| 257 | |
| 258 | // bad s in clsag at verification |
| 259 | for (auto &s: clsag.s) |
| 260 | { |
| 261 | backup_key = s; |
| 262 | s = skGen(); |
| 263 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 264 | s = backup_key; |
| 265 | } |
| 266 | |
| 267 | // bad c1 in clsag at verification |
| 268 | backup_key = clsag.c1; |
| 269 | clsag.c1 = skGen(); |
| 270 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 271 | clsag.c1 = backup_key; |
| 272 | |
| 273 | // bad I in clsag at verification |
| 274 | backup_key = clsag.I; |
| 275 | clsag.I = scalarmultBase(skGen()); |
| 276 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 277 | clsag.I = backup_key; |
| 278 | |
| 279 | // bad D in clsag at verification |
| 280 | backup_key = clsag.D; |
| 281 | clsag.D = scalarmultBase(skGen()); |
| 282 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 283 | clsag.D = backup_key; |
| 284 | |
| 285 | // D not in main subgroup in clsag at verification |
| 286 | backup_key = clsag.D; |
| 287 | rct::key x; |
| 288 | ASSERT_TRUE(epee::string_tools::hex_to_pod("c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", x)); |
| 289 | clsag.D = rct::addKeys(clsag.D, x); |
| 290 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 291 | clsag.D = backup_key; |
| 292 | |
| 293 | // swapped I and D in clsag at verification |
| 294 | std::swap(clsag.I, clsag.D); |
| 295 | ASSERT_FALSE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 296 | std::swap(clsag.I, clsag.D); |
| 297 | |
| 298 | // check it's still good, in case we failed to restore |
| 299 | ASSERT_TRUE(rct::verRctCLSAGSimple(message,clsag,pubs,Cout)); |
| 300 | } |
| 301 | |
| 302 | TEST(ringct, range_proofs) |
| 303 | { |
| 304 | //Ring CT Stuff |
| 305 | //ct range proofs |
| 306 | ctkeyV sc, pc; |
| 307 | ctkey sctmp, pctmp; |
| 308 | std::vector<uint64_t> inamounts; |
| 309 | //add fake input 6000 |
| 310 | inamounts.push_back(6000); |
| 311 | tie(sctmp, pctmp) = ctskpkGen(inamounts.back()); |
| 312 | sc.push_back(sctmp); |
| 313 | pc.push_back(pctmp); |
| 314 | |
| 315 | |
| 316 | inamounts.push_back(7000); |
| 317 | tie(sctmp, pctmp) = ctskpkGen(inamounts.back()); |
| 318 | sc.push_back(sctmp); |
| 319 | pc.push_back(pctmp); |
| 320 | vector<xmr_amount >amounts; |
| 321 | rct::keyV amount_keys; |
| 322 | key mask; |
| 323 | |
| 324 | //add output 500 |
| 325 | amounts.push_back(500); |
| 326 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 327 | keyV destinations; |
| 328 | key Sk, Pk; |
| 329 | skpkGen(Sk, Pk); |
| 330 | destinations.push_back(Pk); |
| 331 | |
| 332 | |
| 333 | //add output for 12500 |
| 334 | amounts.push_back(12500); |
| 335 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 336 | skpkGen(Sk, Pk); |
| 337 | destinations.push_back(Pk); |
| 338 | |
| 339 | const rct::RCTConfig rct_config { RangeProofBorromean, 0 }; |
| 340 | |
| 341 | //compute rct data with mixin 3 - should fail since full type with > 1 input |
| 342 | bool ok = false; |
| 343 | try { genRct(rct::zero(), sc, pc, destinations, amounts, amount_keys, 3, rct_config, hw::get_device("default")); } |
| 344 | catch(...) { ok = true; } |
| 345 | ASSERT_TRUE(ok); |
| 346 | |
| 347 | //compute rct data with mixin 3 |
| 348 | rctSig s = genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 0, 3, rct_config, hw::get_device("default")); |
| 349 | |
| 350 | //verify rct data |
| 351 | ASSERT_TRUE(verRctSimple(s)); |
| 352 | |
| 353 | //decode received amount |
| 354 | decodeRctSimple(s, amount_keys[1], 1, mask, hw::get_device("default")); |
| 355 | |
| 356 | // Ring CT with failing MG sig part should not verify! |
| 357 | // Since sum of inputs != outputs |
| 358 | |
| 359 | amounts[1] = 12501; |
| 360 | skpkGen(Sk, Pk); |
| 361 | destinations[1] = Pk; |
| 362 | |
| 363 | |
| 364 | //compute rct data with mixin 3 |
| 365 | s = genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 0, 3, rct_config, hw::get_device("default")); |
| 366 | |
| 367 | //verify rct data |
| 368 | ASSERT_FALSE(verRctSimple(s)); |
| 369 | |
| 370 | //decode received amount |
| 371 | decodeRctSimple(s, amount_keys[1], 1, mask, hw::get_device("default")); |
| 372 | } |
| 373 | |
| 374 | TEST(ringct, range_proofs_with_fee) |
| 375 | { |
| 376 | //Ring CT Stuff |
| 377 | //ct range proofs |
| 378 | ctkeyV sc, pc; |
| 379 | ctkey sctmp, pctmp; |
| 380 | std::vector<uint64_t> inamounts; |
| 381 | //add fake input 6001 |
| 382 | inamounts.push_back(6001); |
| 383 | tie(sctmp, pctmp) = ctskpkGen(inamounts.back()); |
| 384 | sc.push_back(sctmp); |
| 385 | pc.push_back(pctmp); |
| 386 | |
| 387 | |
| 388 | inamounts.push_back(7000); |
| 389 | tie(sctmp, pctmp) = ctskpkGen(inamounts.back()); |
| 390 | sc.push_back(sctmp); |
| 391 | pc.push_back(pctmp); |
| 392 | vector<xmr_amount >amounts; |
| 393 | keyV amount_keys; |
| 394 | key mask; |
| 395 | |
| 396 | //add output 500 |
| 397 | amounts.push_back(500); |
| 398 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 399 | keyV destinations; |
| 400 | key Sk, Pk; |
| 401 | skpkGen(Sk, Pk); |
| 402 | destinations.push_back(Pk); |
| 403 | |
| 404 | //add output for 12500 |
| 405 | amounts.push_back(12500); |
| 406 | amount_keys.push_back(hash_to_scalar(zero())); |
| 407 | skpkGen(Sk, Pk); |
| 408 | destinations.push_back(Pk); |
| 409 | |
| 410 | const rct::RCTConfig rct_config { RangeProofBorromean, 0 }; |
| 411 | |
| 412 | //compute rct data with mixin 3 |
| 413 | rctSig s = genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 1, 3, rct_config, hw::get_device("default")); |
| 414 | |
| 415 | //verify rct data |
| 416 | ASSERT_TRUE(verRctSimple(s)); |
| 417 | |
| 418 | //decode received amount |
| 419 | decodeRctSimple(s, amount_keys[1], 1, mask, hw::get_device("default")); |
| 420 | |
| 421 | // Ring CT with failing MG sig part should not verify! |
| 422 | // Since sum of inputs != outputs |
| 423 | |
| 424 | amounts[1] = 12501; |
| 425 | skpkGen(Sk, Pk); |
| 426 | destinations[1] = Pk; |
| 427 | |
| 428 | |
| 429 | //compute rct data with mixin 3 |
| 430 | s = genRctSimple(rct::zero(), sc, pc, destinations, inamounts, amounts, amount_keys, 500, 3, rct_config, hw::get_device("default")); |
| 431 | |
| 432 | //verify rct data |
| 433 | ASSERT_FALSE(verRctSimple(s)); |
| 434 | |
| 435 | //decode received amount |
| 436 | decodeRctSimple(s, amount_keys[1], 1, mask, hw::get_device("default")); |
| 437 | } |
| 438 | |
| 439 | TEST(ringct, simple) |
| 440 | { |
| 441 | ctkeyV sc, pc; |
| 442 | ctkey sctmp, pctmp; |
| 443 | //this vector corresponds to output amounts |
| 444 | vector<xmr_amount>outamounts; |
| 445 | //this vector corresponds to input amounts |
| 446 | vector<xmr_amount>inamounts; |
| 447 | //this keyV corresponds to destination pubkeys |
| 448 | keyV destinations; |
| 449 | keyV amount_keys; |
| 450 | key mask; |
| 451 | |
| 452 | //add fake input 3000 |
| 453 | //the sc is secret data |
| 454 | //pc is public data |
| 455 | tie(sctmp, pctmp) = ctskpkGen(3000); |
| 456 | sc.push_back(sctmp); |
| 457 | pc.push_back(pctmp); |
| 458 | inamounts.push_back(3000); |
| 459 | |
| 460 | //add fake input 3000 |
| 461 | //the sc is secret data |
| 462 | //pc is public data |
| 463 | tie(sctmp, pctmp) = ctskpkGen(3000); |
| 464 | sc.push_back(sctmp); |
| 465 | pc.push_back(pctmp); |
| 466 | inamounts.push_back(3000); |
| 467 | |
| 468 | //add output 5000 |
| 469 | outamounts.push_back(5000); |
| 470 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 471 | //add the corresponding destination pubkey |
| 472 | key Sk, Pk; |
| 473 | skpkGen(Sk, Pk); |
| 474 | destinations.push_back(Pk); |
| 475 | |
| 476 | //add output 999 |
| 477 | outamounts.push_back(999); |
| 478 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 479 | //add the corresponding destination pubkey |
| 480 | skpkGen(Sk, Pk); |
| 481 | destinations.push_back(Pk); |
| 482 | |
| 483 | key message = skGen(); //real message later (hash of txn..) |
| 484 | |
| 485 | //compute sig with mixin 2 |
| 486 | xmr_amount txnfee = 1; |
| 487 | |
| 488 | const rct::RCTConfig rct_config { RangeProofBorromean, 0 }; |
| 489 | rctSig s = genRctSimple(message, sc, pc, destinations,inamounts, outamounts, amount_keys, txnfee, 2, rct_config, hw::get_device("default")); |
| 490 | |
| 491 | //verify ring ct signature |
| 492 | ASSERT_TRUE(verRctSimple(s)); |
| 493 | |
| 494 | //decode received amount corresponding to output pubkey index 1 |
| 495 | decodeRctSimple(s, amount_keys[1], 1, mask, hw::get_device("default")); |
| 496 | } |
| 497 | |
| 498 | static rct::rctSig make_sample_rct_sig(int n_inputs, const uint64_t input_amounts[], int n_outputs, const uint64_t output_amounts[], bool last_is_fee) |
| 499 | { |
| 500 | ctkeyV sc, pc; |
| 501 | ctkey sctmp, pctmp; |
| 502 | vector<xmr_amount >amounts; |
| 503 | keyV destinations; |
| 504 | keyV amount_keys; |
| 505 | key Sk, Pk; |
| 506 | |
| 507 | for (int n = 0; n < n_inputs; ++n) { |
| 508 | tie(sctmp, pctmp) = ctskpkGen(input_amounts[n]); |
| 509 | sc.push_back(sctmp); |
| 510 | pc.push_back(pctmp); |
| 511 | } |
| 512 | |
| 513 | for (int n = 0; n < n_outputs; ++n) { |
| 514 | amounts.push_back(output_amounts[n]); |
| 515 | skpkGen(Sk, Pk); |
| 516 | if (n < n_outputs - 1 || !last_is_fee) |
| 517 | { |
| 518 | destinations.push_back(Pk); |
| 519 | amount_keys.push_back(rct::hash_to_scalar(rct::zero())); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | const rct::RCTConfig rct_config { RangeProofBorromean, 0 }; |
| 524 | return genRct(rct::zero(), sc, pc, destinations, amounts, amount_keys, 3, rct_config, hw::get_device("default")); |
| 525 | } |
| 526 | |
| 527 | static rct::rctSig make_sample_simple_rct_sig(int n_inputs, const uint64_t input_amounts[], int n_outputs, const uint64_t output_amounts[], uint64_t fee) |
| 528 | { |
| 529 | ctkeyV sc, pc; |
| 530 | ctkey sctmp, pctmp; |
| 531 | vector<xmr_amount> inamounts, outamounts; |
| 532 | keyV destinations; |
| 533 | keyV amount_keys; |
| 534 | key Sk, Pk; |
| 535 | |
| 536 | for (int n = 0; n < n_inputs; ++n) { |
| 537 | inamounts.push_back(input_amounts[n]); |
| 538 | tie(sctmp, pctmp) = ctskpkGen(input_amounts[n]); |
| 539 | sc.push_back(sctmp); |
| 540 | pc.push_back(pctmp); |
| 541 | } |
| 542 | |
| 543 | for (int n = 0; n < n_outputs; ++n) { |
| 544 | outamounts.push_back(output_amounts[n]); |
| 545 | amount_keys.push_back(hash_to_scalar(zero())); |
| 546 | skpkGen(Sk, Pk); |
| 547 | destinations.push_back(Pk); |
| 548 | } |
| 549 | |
| 550 | const rct::RCTConfig rct_config { RangeProofBorromean, 0 }; |
| 551 | return genRctSimple(rct::zero(), sc, pc, destinations, inamounts, outamounts, amount_keys, fee, 3, rct_config, hw::get_device("default")); |
| 552 | } |
| 553 | |
| 554 | static bool range_proof_test(bool expected_valid, |
| 555 | int n_inputs, const uint64_t input_amounts[], int n_outputs, const uint64_t output_amounts[], bool last_is_fee, bool simple) |
| 556 | { |
| 557 | //compute rct data |
| 558 | bool valid; |
| 559 | try { |
| 560 | rctSig s; |
| 561 | // simple takes fee as a parameter, non-simple takes it as an extra element to output amounts |
| 562 | if (simple) { |
| 563 | s = make_sample_simple_rct_sig(n_inputs, input_amounts, last_is_fee ? n_outputs - 1 : n_outputs, output_amounts, last_is_fee ? output_amounts[n_outputs - 1] : 0); |
| 564 | valid = verRctSimple(s); |
| 565 | } |
| 566 | else { |
| 567 | s = make_sample_rct_sig(n_inputs, input_amounts, n_outputs, output_amounts, last_is_fee); |
| 568 | valid = verRct(s); |
| 569 | } |
| 570 | } |
| 571 | catch (const std::exception &e) { |
| 572 | valid = false; |
| 573 | } |
| 574 | |
| 575 | if (valid == expected_valid) { |
| 576 | return testing::AssertionSuccess(); |
| 577 | } |
| 578 | else { |
| 579 | return testing::AssertionFailure(); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | #define NELTS(array) (sizeof(array)/sizeof(array[0])) |
| 584 | |
| 585 | TEST(ringct, range_proofs_reject_empty_outs) |
| 586 | { |
| 587 | const uint64_t inputs[] = {5000}; |
| 588 | const uint64_t outputs[] = {}; |
| 589 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 590 | } |
| 591 | |
| 592 | TEST(ringct, range_proofs_reject_empty_outs_simple) |
| 593 | { |
| 594 | const uint64_t inputs[] = {5000}; |
| 595 | const uint64_t outputs[] = {}; |
| 596 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 597 | } |
| 598 | |
| 599 | TEST(ringct, range_proofs_reject_empty_ins) |
| 600 | { |
| 601 | const uint64_t inputs[] = {}; |
| 602 | const uint64_t outputs[] = {5000}; |
| 603 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 604 | } |
| 605 | |
| 606 | TEST(ringct, range_proofs_reject_empty_ins_simple) |
| 607 | { |
| 608 | const uint64_t inputs[] = {}; |
| 609 | const uint64_t outputs[] = {5000}; |
| 610 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 611 | } |
| 612 | |
| 613 | TEST(ringct, range_proofs_reject_all_empty) |
| 614 | { |
| 615 | const uint64_t inputs[] = {}; |
| 616 | const uint64_t outputs[] = {}; |
| 617 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 618 | } |
| 619 | |
| 620 | TEST(ringct, range_proofs_reject_all_empty_simple) |
| 621 | { |
| 622 | const uint64_t inputs[] = {}; |
| 623 | const uint64_t outputs[] = {}; |
| 624 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 625 | } |
| 626 | |
| 627 | TEST(ringct, range_proofs_accept_zero_empty) |
| 628 | { |
| 629 | const uint64_t inputs[] = {0}; |
| 630 | const uint64_t outputs[] = {}; |
| 631 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 632 | } |
| 633 | |
| 634 | TEST(ringct, range_proofs_accept_zero_empty_simple) |
| 635 | { |
| 636 | const uint64_t inputs[] = {0}; |
| 637 | const uint64_t outputs[] = {}; |
| 638 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 639 | } |
| 640 | |
| 641 | TEST(ringct, range_proofs_reject_empty_zero) |
| 642 | { |
| 643 | const uint64_t inputs[] = {}; |
| 644 | const uint64_t outputs[] = {0}; |
| 645 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 646 | } |
| 647 | |
| 648 | TEST(ringct, range_proofs_reject_empty_zero_simple) |
| 649 | { |
| 650 | const uint64_t inputs[] = {}; |
| 651 | const uint64_t outputs[] = {0}; |
| 652 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 653 | } |
| 654 | |
| 655 | TEST(ringct, range_proofs_accept_zero_zero) |
| 656 | { |
| 657 | const uint64_t inputs[] = {0}; |
| 658 | const uint64_t outputs[] = {0}; |
| 659 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 660 | } |
| 661 | |
| 662 | TEST(ringct, range_proofs_accept_zero_zero_simple) |
| 663 | { |
| 664 | const uint64_t inputs[] = {0}; |
| 665 | const uint64_t outputs[] = {0}; |
| 666 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 667 | } |
| 668 | |
| 669 | TEST(ringct, range_proofs_accept_zero_out_first) |
| 670 | { |
| 671 | const uint64_t inputs[] = {5000}; |
| 672 | const uint64_t outputs[] = {0, 5000}; |
| 673 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 674 | } |
| 675 | |
| 676 | TEST(ringct, range_proofs_accept_zero_out_first_simple) |
| 677 | { |
| 678 | const uint64_t inputs[] = {5000}; |
| 679 | const uint64_t outputs[] = {0, 5000}; |
| 680 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 681 | } |
| 682 | |
| 683 | TEST(ringct, range_proofs_accept_zero_out_last) |
| 684 | { |
| 685 | const uint64_t inputs[] = {5000}; |
| 686 | const uint64_t outputs[] = {5000, 0}; |
| 687 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 688 | } |
| 689 | |
| 690 | TEST(ringct, range_proofs_accept_zero_out_last_simple) |
| 691 | { |
| 692 | const uint64_t inputs[] = {5000}; |
| 693 | const uint64_t outputs[] = {5000, 0}; |
| 694 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 695 | } |
| 696 | |
| 697 | TEST(ringct, range_proofs_accept_zero_out_middle) |
| 698 | { |
| 699 | const uint64_t inputs[] = {5000}; |
| 700 | const uint64_t outputs[] = {2500, 0, 2500}; |
| 701 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 702 | } |
| 703 | |
| 704 | TEST(ringct, range_proofs_accept_zero_out_middle_simple) |
| 705 | { |
| 706 | const uint64_t inputs[] = {5000}; |
| 707 | const uint64_t outputs[] = {2500, 0, 2500}; |
| 708 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 709 | } |
| 710 | |
| 711 | TEST(ringct, range_proofs_accept_zero) |
| 712 | { |
| 713 | const uint64_t inputs[] = {0}; |
| 714 | const uint64_t outputs[] = {0}; |
| 715 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 716 | } |
| 717 | |
| 718 | TEST(ringct, range_proofs_accept_zero_in_first_simple) |
| 719 | { |
| 720 | const uint64_t inputs[] = {0, 5000}; |
| 721 | const uint64_t outputs[] = {5000}; |
| 722 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 723 | } |
| 724 | |
| 725 | TEST(ringct, range_proofs_accept_zero_in_last_simple) |
| 726 | { |
| 727 | const uint64_t inputs[] = {5000, 0}; |
| 728 | const uint64_t outputs[] = {5000}; |
| 729 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 730 | } |
| 731 | |
| 732 | TEST(ringct, range_proofs_accept_zero_in_middle_simple) |
| 733 | { |
| 734 | const uint64_t inputs[] = {2500, 0, 2500}; |
| 735 | const uint64_t outputs[] = {5000}; |
| 736 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 737 | } |
| 738 | |
| 739 | TEST(ringct, range_proofs_reject_single_lower) |
| 740 | { |
| 741 | const uint64_t inputs[] = {5000}; |
| 742 | const uint64_t outputs[] = {1}; |
| 743 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 744 | } |
| 745 | |
| 746 | TEST(ringct, range_proofs_reject_single_lower_simple) |
| 747 | { |
| 748 | const uint64_t inputs[] = {5000}; |
| 749 | const uint64_t outputs[] = {1}; |
| 750 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 751 | } |
| 752 | |
| 753 | TEST(ringct, range_proofs_reject_single_higher) |
| 754 | { |
| 755 | const uint64_t inputs[] = {5000}; |
| 756 | const uint64_t outputs[] = {5001}; |
| 757 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 758 | } |
| 759 | |
| 760 | TEST(ringct, range_proofs_reject_single_higher_simple) |
| 761 | { |
| 762 | const uint64_t inputs[] = {5000}; |
| 763 | const uint64_t outputs[] = {5001}; |
| 764 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 765 | } |
| 766 | |
| 767 | TEST(ringct, range_proofs_reject_single_out_negative) |
| 768 | { |
| 769 | const uint64_t inputs[] = {5000}; |
| 770 | const uint64_t outputs[] = {(uint64_t)-1000ll}; |
| 771 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 772 | } |
| 773 | |
| 774 | TEST(ringct, range_proofs_reject_single_out_negative_simple) |
| 775 | { |
| 776 | const uint64_t inputs[] = {5000}; |
| 777 | const uint64_t outputs[] = {(uint64_t)-1000ll}; |
| 778 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 779 | } |
| 780 | |
| 781 | TEST(ringct, range_proofs_reject_out_negative_first) |
| 782 | { |
| 783 | const uint64_t inputs[] = {5000}; |
| 784 | const uint64_t outputs[] = {(uint64_t)-1000ll, 6000}; |
| 785 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 786 | } |
| 787 | |
| 788 | TEST(ringct, range_proofs_reject_out_negative_first_simple) |
| 789 | { |
| 790 | const uint64_t inputs[] = {5000}; |
| 791 | const uint64_t outputs[] = {(uint64_t)-1000ll, 6000}; |
| 792 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 793 | } |
| 794 | |
| 795 | TEST(ringct, range_proofs_reject_out_negative_last) |
| 796 | { |
| 797 | const uint64_t inputs[] = {5000}; |
| 798 | const uint64_t outputs[] = {6000, (uint64_t)-1000ll}; |
| 799 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 800 | } |
| 801 | |
| 802 | TEST(ringct, range_proofs_reject_out_negative_last_simple) |
| 803 | { |
| 804 | const uint64_t inputs[] = {5000}; |
| 805 | const uint64_t outputs[] = {6000, (uint64_t)-1000ll}; |
| 806 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 807 | } |
| 808 | |
| 809 | TEST(ringct, range_proofs_reject_out_negative_middle) |
| 810 | { |
| 811 | const uint64_t inputs[] = {5000}; |
| 812 | const uint64_t outputs[] = {3000, (uint64_t)-1000ll, 3000}; |
| 813 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 814 | } |
| 815 | |
| 816 | TEST(ringct, range_proofs_reject_out_negative_middle_simple) |
| 817 | { |
| 818 | const uint64_t inputs[] = {5000}; |
| 819 | const uint64_t outputs[] = {3000, (uint64_t)-1000ll, 3000}; |
| 820 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 821 | } |
| 822 | |
| 823 | TEST(ringct, range_proofs_reject_single_in_negative) |
| 824 | { |
| 825 | const uint64_t inputs[] = {(uint64_t)-1000ll}; |
| 826 | const uint64_t outputs[] = {5000}; |
| 827 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 828 | } |
| 829 | |
| 830 | TEST(ringct, range_proofs_reject_single_in_negative_simple) |
| 831 | { |
| 832 | const uint64_t inputs[] = {(uint64_t)-1000ll}; |
| 833 | const uint64_t outputs[] = {5000}; |
| 834 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 835 | } |
| 836 | |
| 837 | TEST(ringct, range_proofs_reject_in_negative_first) |
| 838 | { |
| 839 | const uint64_t inputs[] = {(uint64_t)-1000ll, 6000}; |
| 840 | const uint64_t outputs[] = {5000}; |
| 841 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 842 | } |
| 843 | |
| 844 | TEST(ringct, range_proofs_reject_in_negative_first_simple) |
| 845 | { |
| 846 | const uint64_t inputs[] = {(uint64_t)-1000ll, 6000}; |
| 847 | const uint64_t outputs[] = {5000}; |
| 848 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 849 | } |
| 850 | |
| 851 | TEST(ringct, range_proofs_reject_in_negative_last) |
| 852 | { |
| 853 | const uint64_t inputs[] = {6000, (uint64_t)-1000ll}; |
| 854 | const uint64_t outputs[] = {5000}; |
| 855 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 856 | } |
| 857 | |
| 858 | TEST(ringct, range_proofs_reject_in_negative_last_simple) |
| 859 | { |
| 860 | const uint64_t inputs[] = {6000, (uint64_t)-1000ll}; |
| 861 | const uint64_t outputs[] = {5000}; |
| 862 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 863 | } |
| 864 | |
| 865 | TEST(ringct, range_proofs_reject_in_negative_middle) |
| 866 | { |
| 867 | const uint64_t inputs[] = {3000, (uint64_t)-1000ll, 3000}; |
| 868 | const uint64_t outputs[] = {5000}; |
| 869 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 870 | } |
| 871 | |
| 872 | TEST(ringct, range_proofs_reject_in_negative_middle_simple) |
| 873 | { |
| 874 | const uint64_t inputs[] = {3000, (uint64_t)-1000ll, 3000}; |
| 875 | const uint64_t outputs[] = {5000}; |
| 876 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 877 | } |
| 878 | |
| 879 | TEST(ringct, range_proofs_reject_higher_list) |
| 880 | { |
| 881 | const uint64_t inputs[] = {5000}; |
| 882 | const uint64_t outputs[] = {1000, 1000, 1000, 1000, 1000, 1000}; |
| 883 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 884 | } |
| 885 | |
| 886 | TEST(ringct, range_proofs_reject_higher_list_simple) |
| 887 | { |
| 888 | const uint64_t inputs[] = {5000}; |
| 889 | const uint64_t outputs[] = {1000, 1000, 1000, 1000, 1000, 1000}; |
| 890 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 891 | } |
| 892 | |
| 893 | TEST(ringct, range_proofs_accept_1_to_1) |
| 894 | { |
| 895 | const uint64_t inputs[] = {5000}; |
| 896 | const uint64_t outputs[] = {5000}; |
| 897 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 898 | } |
| 899 | |
| 900 | TEST(ringct, range_proofs_accept_1_to_1_simple) |
| 901 | { |
| 902 | const uint64_t inputs[] = {5000}; |
| 903 | const uint64_t outputs[] = {5000}; |
| 904 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 905 | } |
| 906 | |
| 907 | TEST(ringct, range_proofs_accept_1_to_N) |
| 908 | { |
| 909 | const uint64_t inputs[] = {5000}; |
| 910 | const uint64_t outputs[] = {1000, 1000, 1000, 1000, 1000}; |
| 911 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, false)); |
| 912 | } |
| 913 | |
| 914 | TEST(ringct, range_proofs_accept_1_to_N_simple) |
| 915 | { |
| 916 | const uint64_t inputs[] = {5000}; |
| 917 | const uint64_t outputs[] = {1000, 1000, 1000, 1000, 1000}; |
| 918 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false,true)); |
| 919 | } |
| 920 | |
| 921 | TEST(ringct, range_proofs_accept_N_to_1_simple) |
| 922 | { |
| 923 | const uint64_t inputs[] = {1000, 1000, 1000, 1000, 1000}; |
| 924 | const uint64_t outputs[] = {5000}; |
| 925 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 926 | } |
| 927 | |
| 928 | TEST(ringct, range_proofs_accept_N_to_N_simple) |
| 929 | { |
| 930 | const uint64_t inputs[] = {1000, 1000, 1000, 1000, 1000}; |
| 931 | const uint64_t outputs[] = {1000, 1000, 1000, 1000, 1000}; |
| 932 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 933 | } |
| 934 | |
| 935 | TEST(ringct, range_proofs_accept_very_long_simple) |
| 936 | { |
| 937 | const size_t N=12; |
| 938 | uint64_t inputs[N]; |
| 939 | uint64_t outputs[N]; |
| 940 | for (size_t n = 0; n < N; ++n) { |
| 941 | inputs[n] = n; |
| 942 | outputs[n] = n; |
| 943 | } |
| 944 | std::shuffle(inputs, inputs + N, crypto::random_device{}); |
| 945 | std::shuffle(outputs, outputs + N, crypto::random_device{}); |
| 946 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, false, true)); |
| 947 | } |
| 948 | |
| 949 | TEST(ringct, HPow2) |
| 950 | { |
| 951 | key G = scalarmultBase(d2h(1)); |
| 952 | |
| 953 | // Note that H is computed differently than standard hashing |
| 954 | // This method is not guaranteed to return a curvepoint for all inputs |
| 955 | // Don't use it elsewhere |
| 956 | key H = cn_fast_hash(G); |
| 957 | ge_p3 H_p3; |
| 958 | int decode = ge_frombytes_vartime(&H_p3, H.bytes); |
| 959 | ASSERT_EQ(decode, 0); // this is known to pass for the particular value G |
| 960 | ge_p2 H_p2; |
| 961 | ge_p3_to_p2(&H_p2, &H_p3); |
| 962 | ge_p1p1 H8_p1p1; |
| 963 | ge_mul8(&H8_p1p1, &H_p2); |
| 964 | ge_p1p1_to_p3(&H_p3, &H8_p1p1); |
| 965 | ge_p3_tobytes(H.bytes, &H_p3); |
| 966 | |
| 967 | for (int j = 0 ; j < ATOMS ; j++) { |
| 968 | ASSERT_TRUE(equalKeys(H, H2[j])); |
| 969 | addKeys(H, H, H); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | static const xmr_amount test_amounts[]={0, 1, 2, 3, 4, 5, 10000, 10000000000000000000ull, 10203040506070809000ull, 123456789123456789}; |
| 974 | |
| 975 | TEST(ringct, d2h) |
| 976 | { |
| 977 | key k, P1; |
| 978 | skpkGen(k, P1); |
| 979 | for (auto amount: test_amounts) { |
| 980 | d2h(k, amount); |
| 981 | ASSERT_TRUE(amount == h2d(k)); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | TEST(ringct, d2b) |
| 986 | { |
| 987 | for (auto amount: test_amounts) { |
| 988 | bits b; |
| 989 | d2b(b, amount); |
| 990 | ASSERT_TRUE(amount == b2d(b)); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | TEST(ringct, prooveRange_is_non_deterministic) |
| 995 | { |
| 996 | key C[2], mask[2]; |
| 997 | for (int n = 0; n < 2; ++n) |
| 998 | proveRange(C[n], mask[n], 80); |
| 999 | ASSERT_TRUE(memcmp(C[0].bytes, C[1].bytes, sizeof(C[0].bytes))); |
| 1000 | ASSERT_TRUE(memcmp(mask[0].bytes, mask[1].bytes, sizeof(mask[0].bytes))); |
| 1001 | } |
| 1002 | |
| 1003 | TEST(ringct, fee_0_valid) |
| 1004 | { |
| 1005 | const uint64_t inputs[] = {2000}; |
| 1006 | const uint64_t outputs[] = {2000, 0}; |
| 1007 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1008 | } |
| 1009 | |
| 1010 | TEST(ringct, fee_0_valid_simple) |
| 1011 | { |
| 1012 | const uint64_t inputs[] = {1000, 1000}; |
| 1013 | const uint64_t outputs[] = {2000, 0}; |
| 1014 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1015 | } |
| 1016 | |
| 1017 | TEST(ringct, fee_non_0_valid) |
| 1018 | { |
| 1019 | const uint64_t inputs[] = {2000}; |
| 1020 | const uint64_t outputs[] = {1900, 100}; |
| 1021 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1022 | } |
| 1023 | |
| 1024 | TEST(ringct, fee_non_0_valid_simple) |
| 1025 | { |
| 1026 | const uint64_t inputs[] = {1000, 1000}; |
| 1027 | const uint64_t outputs[] = {1900, 100}; |
| 1028 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1029 | } |
| 1030 | |
| 1031 | TEST(ringct, fee_non_0_invalid_higher) |
| 1032 | { |
| 1033 | const uint64_t inputs[] = {1000, 1000}; |
| 1034 | const uint64_t outputs[] = {1990, 100}; |
| 1035 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1036 | } |
| 1037 | |
| 1038 | TEST(ringct, fee_non_0_invalid_higher_simple) |
| 1039 | { |
| 1040 | const uint64_t inputs[] = {1000, 1000}; |
| 1041 | const uint64_t outputs[] = {1990, 100}; |
| 1042 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1043 | } |
| 1044 | |
| 1045 | TEST(ringct, fee_non_0_invalid_lower) |
| 1046 | { |
| 1047 | const uint64_t inputs[] = {1000, 1000}; |
| 1048 | const uint64_t outputs[] = {1000, 100}; |
| 1049 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1050 | } |
| 1051 | |
| 1052 | TEST(ringct, fee_non_0_invalid_lower_simple) |
| 1053 | { |
| 1054 | const uint64_t inputs[] = {1000, 1000}; |
| 1055 | const uint64_t outputs[] = {1000, 100}; |
| 1056 | EXPECT_TRUE(range_proof_test(false, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1057 | } |
| 1058 | |
| 1059 | TEST(ringct, fee_burn_valid_one_out) |
| 1060 | { |
| 1061 | const uint64_t inputs[] = {2000}; |
| 1062 | const uint64_t outputs[] = {0, 2000}; |
| 1063 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1064 | } |
| 1065 | |
| 1066 | TEST(ringct, fee_burn_valid_one_out_simple) |
| 1067 | { |
| 1068 | const uint64_t inputs[] = {1000, 1000}; |
| 1069 | const uint64_t outputs[] = {0, 2000}; |
| 1070 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1071 | } |
| 1072 | |
| 1073 | TEST(ringct, fee_burn_valid_zero_out) |
| 1074 | { |
| 1075 | const uint64_t inputs[] = {2000}; |
| 1076 | const uint64_t outputs[] = {2000}; |
| 1077 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, false)); |
| 1078 | } |
| 1079 | |
| 1080 | TEST(ringct, fee_burn_valid_zero_out_simple) |
| 1081 | { |
| 1082 | const uint64_t inputs[] = {1000, 1000}; |
| 1083 | const uint64_t outputs[] = {2000}; |
| 1084 | EXPECT_TRUE(range_proof_test(true, NELTS(inputs), inputs, NELTS(outputs), outputs, true, true)); |
| 1085 | } |
| 1086 | |
| 1087 | static rctSig make_sig() |
| 1088 | { |
| 1089 | static const uint64_t inputs[] = {2000}; |
| 1090 | static const uint64_t outputs[] = {1000, 1000}; |
| 1091 | static rct::rctSig sig = make_sample_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, true); |
| 1092 | return sig; |
| 1093 | } |
| 1094 | |
| 1095 | #define TEST_rctSig_elements(name, op) \ |
| 1096 | TEST(ringct, rctSig_##name) \ |
| 1097 | { \ |
| 1098 | rct::rctSig sig = make_sig(); \ |
| 1099 | ASSERT_TRUE(rct::verRct(sig)); \ |
| 1100 | op; \ |
| 1101 | ASSERT_FALSE(rct::verRct(sig)); \ |
| 1102 | } |
| 1103 | |
| 1104 | TEST_rctSig_elements(rangeSigs_empty, sig.p.rangeSigs.resize(0)); |
| 1105 | TEST_rctSig_elements(rangeSigs_too_many, sig.p.rangeSigs.push_back(sig.p.rangeSigs.back())); |
| 1106 | TEST_rctSig_elements(rangeSigs_too_few, sig.p.rangeSigs.pop_back()); |
| 1107 | TEST_rctSig_elements(mgSig_MG_empty, sig.p.MGs.resize(0)); |
| 1108 | TEST_rctSig_elements(mgSig_ss_empty, sig.p.MGs[0].ss.resize(0)); |
| 1109 | TEST_rctSig_elements(mgSig_ss_too_many, sig.p.MGs[0].ss.push_back(sig.p.MGs[0].ss.back())); |
| 1110 | TEST_rctSig_elements(mgSig_ss_too_few, sig.p.MGs[0].ss.pop_back()); |
| 1111 | TEST_rctSig_elements(mgSig_ss0_empty, sig.p.MGs[0].ss[0].resize(0)); |
| 1112 | TEST_rctSig_elements(mgSig_ss0_too_many, sig.p.MGs[0].ss[0].push_back(sig.p.MGs[0].ss[0].back())); |
| 1113 | TEST_rctSig_elements(mgSig_ss0_too_few, sig.p.MGs[0].ss[0].pop_back()); |
| 1114 | TEST_rctSig_elements(mgSig_II_empty, sig.p.MGs[0].II.resize(0)); |
| 1115 | TEST_rctSig_elements(mgSig_II_too_many, sig.p.MGs[0].II.push_back(sig.p.MGs[0].II.back())); |
| 1116 | TEST_rctSig_elements(mgSig_II_too_few, sig.p.MGs[0].II.pop_back()); |
| 1117 | TEST_rctSig_elements(mixRing_empty, sig.mixRing.resize(0)); |
| 1118 | TEST_rctSig_elements(mixRing_too_many, sig.mixRing.push_back(sig.mixRing.back())); |
| 1119 | TEST_rctSig_elements(mixRing_too_few, sig.mixRing.pop_back()); |
| 1120 | TEST_rctSig_elements(mixRing0_empty, sig.mixRing[0].resize(0)); |
| 1121 | TEST_rctSig_elements(mixRing0_too_many, sig.mixRing[0].push_back(sig.mixRing[0].back())); |
| 1122 | TEST_rctSig_elements(mixRing0_too_few, sig.mixRing[0].pop_back()); |
| 1123 | TEST_rctSig_elements(ecdhInfo_empty, sig.ecdhInfo.resize(0)); |
| 1124 | TEST_rctSig_elements(ecdhInfo_too_many, sig.ecdhInfo.push_back(sig.ecdhInfo.back())); |
| 1125 | TEST_rctSig_elements(ecdhInfo_too_few, sig.ecdhInfo.pop_back()); |
| 1126 | TEST_rctSig_elements(outPk_empty, sig.outPk.resize(0)); |
| 1127 | TEST_rctSig_elements(outPk_too_many, sig.outPk.push_back(sig.outPk.back())); |
| 1128 | TEST_rctSig_elements(outPk_too_few, sig.outPk.pop_back()); |
| 1129 | |
| 1130 | static rct::rctSig make_sig_simple() |
| 1131 | { |
| 1132 | static const uint64_t inputs[] = {1000, 1000}; |
| 1133 | static const uint64_t outputs[] = {1000}; |
| 1134 | static rct::rctSig sig = make_sample_simple_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, 1000); |
| 1135 | return sig; |
| 1136 | } |
| 1137 | |
| 1138 | #define TEST_rctSig_elements_simple(name, op) \ |
| 1139 | TEST(ringct, rctSig_##name##_simple) \ |
| 1140 | { \ |
| 1141 | rct::rctSig sig = make_sig_simple(); \ |
| 1142 | ASSERT_TRUE(rct::verRctSimple(sig)); \ |
| 1143 | op; \ |
| 1144 | ASSERT_FALSE(rct::verRctSimple(sig)); \ |
| 1145 | } |
| 1146 | |
| 1147 | TEST_rctSig_elements_simple(rangeSigs_empty, sig.p.rangeSigs.resize(0)); |
| 1148 | TEST_rctSig_elements_simple(rangeSigs_too_many, sig.p.rangeSigs.push_back(sig.p.rangeSigs.back())); |
| 1149 | TEST_rctSig_elements_simple(rangeSigs_too_few, sig.p.rangeSigs.pop_back()); |
| 1150 | TEST_rctSig_elements_simple(mgSig_empty, sig.p.MGs.resize(0)); |
| 1151 | TEST_rctSig_elements_simple(mgSig_too_many, sig.p.MGs.push_back(sig.p.MGs.back())); |
| 1152 | TEST_rctSig_elements_simple(mgSig_too_few, sig.p.MGs.pop_back()); |
| 1153 | TEST_rctSig_elements_simple(mgSig0_ss_empty, sig.p.MGs[0].ss.resize(0)); |
| 1154 | TEST_rctSig_elements_simple(mgSig0_ss_too_many, sig.p.MGs[0].ss.push_back(sig.p.MGs[0].ss.back())); |
| 1155 | TEST_rctSig_elements_simple(mgSig0_ss_too_few, sig.p.MGs[0].ss.pop_back()); |
| 1156 | TEST_rctSig_elements_simple(mgSig_ss0_empty, sig.p.MGs[0].ss[0].resize(0)); |
| 1157 | TEST_rctSig_elements_simple(mgSig_ss0_too_many, sig.p.MGs[0].ss[0].push_back(sig.p.MGs[0].ss[0].back())); |
| 1158 | TEST_rctSig_elements_simple(mgSig_ss0_too_few, sig.p.MGs[0].ss[0].pop_back()); |
| 1159 | TEST_rctSig_elements_simple(mgSig0_II_empty, sig.p.MGs[0].II.resize(0)); |
| 1160 | TEST_rctSig_elements_simple(mgSig0_II_too_many, sig.p.MGs[0].II.push_back(sig.p.MGs[0].II.back())); |
| 1161 | TEST_rctSig_elements_simple(mgSig0_II_too_few, sig.p.MGs[0].II.pop_back()); |
| 1162 | TEST_rctSig_elements_simple(mixRing_empty, sig.mixRing.resize(0)); |
| 1163 | TEST_rctSig_elements_simple(mixRing_too_many, sig.mixRing.push_back(sig.mixRing.back())); |
| 1164 | TEST_rctSig_elements_simple(mixRing_too_few, sig.mixRing.pop_back()); |
| 1165 | TEST_rctSig_elements_simple(mixRing0_empty, sig.mixRing[0].resize(0)); |
| 1166 | TEST_rctSig_elements_simple(mixRing0_too_many, sig.mixRing[0].push_back(sig.mixRing[0].back())); |
| 1167 | TEST_rctSig_elements_simple(mixRing0_too_few, sig.mixRing[0].pop_back()); |
| 1168 | TEST_rctSig_elements_simple(pseudoOuts_empty, sig.pseudoOuts.resize(0)); |
| 1169 | TEST_rctSig_elements_simple(pseudoOuts_too_many, sig.pseudoOuts.push_back(sig.pseudoOuts.back())); |
| 1170 | TEST_rctSig_elements_simple(pseudoOuts_too_few, sig.pseudoOuts.pop_back()); |
| 1171 | TEST_rctSig_elements_simple(ecdhInfo_empty, sig.ecdhInfo.resize(0)); |
| 1172 | TEST_rctSig_elements_simple(ecdhInfo_too_many, sig.ecdhInfo.push_back(sig.ecdhInfo.back())); |
| 1173 | TEST_rctSig_elements_simple(ecdhInfo_too_few, sig.ecdhInfo.pop_back()); |
| 1174 | TEST_rctSig_elements_simple(outPk_empty, sig.outPk.resize(0)); |
| 1175 | TEST_rctSig_elements_simple(outPk_too_many, sig.outPk.push_back(sig.outPk.back())); |
| 1176 | TEST_rctSig_elements_simple(outPk_too_few, sig.outPk.pop_back()); |
| 1177 | |
| 1178 | TEST(ringct, reject_gen_simple_ver_non_simple) |
| 1179 | { |
| 1180 | const uint64_t inputs[] = {1000, 1000}; |
| 1181 | const uint64_t outputs[] = {1000}; |
| 1182 | rct::rctSig sig = make_sample_simple_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, 1000); |
| 1183 | ASSERT_FALSE(rct::verRct(sig)); |
| 1184 | } |
| 1185 | |
| 1186 | TEST(ringct, reject_gen_non_simple_ver_simple) |
| 1187 | { |
| 1188 | const uint64_t inputs[] = {2000}; |
| 1189 | const uint64_t outputs[] = {1000, 1000}; |
| 1190 | rct::rctSig sig = make_sample_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, true); |
| 1191 | ASSERT_FALSE(rct::verRctSimple(sig)); |
| 1192 | } |
| 1193 | |
| 1194 | TEST(ringct, key_ostream) |
| 1195 | { |
| 1196 | std::stringstream out; |
| 1197 | out << "BEGIN" << rct::H << "END"; |
| 1198 | EXPECT_EQ( |
| 1199 | std::string{"BEGIN<8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94>END"}, |
| 1200 | out.str() |
| 1201 | ); |
| 1202 | } |
| 1203 | |
| 1204 | TEST(ringct, zeroCommmit) |
| 1205 | { |
| 1206 | static const uint64_t amount = crypto::rand<uint64_t>(); |
| 1207 | const rct::key z = rct::zeroCommit(amount); |
| 1208 | const rct::key a = rct::scalarmultBase(rct::identity()); |
| 1209 | const rct::key b = rct::scalarmultH(rct::d2h(amount)); |
| 1210 | const rct::key manual = rct::addKeys(a, b); |
| 1211 | ASSERT_EQ(z, manual); |
| 1212 | } |
| 1213 | |
| 1214 | static rct::key uncachedZeroCommit(uint64_t amount) |
| 1215 | { |
| 1216 | const rct::key am = rct::d2h(amount); |
| 1217 | const rct::key bH = rct::scalarmultH(am); |
| 1218 | return rct::addKeys(rct::G, bH); |
| 1219 | } |
| 1220 | |
| 1221 | TEST(ringct, zeroCommitCache) |
| 1222 | { |
| 1223 | ASSERT_EQ(rct::zeroCommit(0), uncachedZeroCommit(0)); |
| 1224 | ASSERT_EQ(rct::zeroCommit(1), uncachedZeroCommit(1)); |
| 1225 | ASSERT_EQ(rct::zeroCommit(2), uncachedZeroCommit(2)); |
| 1226 | ASSERT_EQ(rct::zeroCommit(10), uncachedZeroCommit(10)); |
| 1227 | ASSERT_EQ(rct::zeroCommit(200), uncachedZeroCommit(200)); |
| 1228 | ASSERT_EQ(rct::zeroCommit(1000000000), uncachedZeroCommit(1000000000)); |
| 1229 | ASSERT_EQ(rct::zeroCommit(3000000000000), uncachedZeroCommit(3000000000000)); |
| 1230 | ASSERT_EQ(rct::zeroCommit(900000000000000), uncachedZeroCommit(900000000000000)); |
| 1231 | } |
| 1232 | |
| 1233 | TEST(ringct, H) |
| 1234 | { |
| 1235 | ge_p3 p3; |
| 1236 | ASSERT_EQ(ge_frombytes_vartime(&p3, rct::H.bytes), 0); |
| 1237 | ASSERT_EQ(memcmp(&p3, &ge_p3_H, sizeof(ge_p3)), 0); |
| 1238 | } |
| 1239 | |
| 1240 | TEST(ringct, mul8) |
| 1241 | { |
| 1242 | ge_p3 p3; |
| 1243 | rct::key key; |
| 1244 | ASSERT_EQ(rct::scalarmult8(rct::identity()), rct::identity()); |
| 1245 | rct::scalarmult8(p3,rct::identity()); |
| 1246 | ge_p3_tobytes(key.bytes, &p3); |
| 1247 | ASSERT_EQ(key, rct::identity()); |
| 1248 | ASSERT_EQ(rct::scalarmult8(rct::H), rct::scalarmultKey(rct::H, rct::EIGHT)); |
| 1249 | rct::scalarmult8(p3,rct::H); |
| 1250 | ge_p3_tobytes(key.bytes, &p3); |
| 1251 | ASSERT_EQ(key, rct::scalarmultKey(rct::H, rct::EIGHT)); |
| 1252 | ASSERT_EQ(rct::scalarmultKey(rct::scalarmultKey(rct::H, rct::INV_EIGHT), rct::EIGHT), rct::H); |
| 1253 | } |
| 1254 | |
| 1255 | TEST(ringct, aggregated) |
| 1256 | { |
| 1257 | static const size_t N_PROOFS = 16; |
| 1258 | std::vector<rctSig> s(N_PROOFS); |
| 1259 | std::vector<const rctSig*> sp(N_PROOFS); |
| 1260 | |
| 1261 | for (size_t n = 0; n < N_PROOFS; ++n) |
| 1262 | { |
| 1263 | static const uint64_t inputs[] = {1000, 1000}; |
| 1264 | static const uint64_t outputs[] = {500, 1500}; |
| 1265 | s[n] = make_sample_simple_rct_sig(NELTS(inputs), inputs, NELTS(outputs), outputs, 0); |
| 1266 | sp[n] = &s[n]; |
| 1267 | } |
| 1268 | |
| 1269 | ASSERT_TRUE(verRctSemanticsSimple(sp)); |
| 1270 | } |