| 1 | // Copyright (c) 2017-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 "string_tools.h" |
| 34 | #include "ringct/rctOps.h" |
| 35 | #include "ringct/rctSigs.h" |
| 36 | #include "ringct/bulletproofs_plus.h" |
| 37 | #include "cryptonote_basic/blobdatatype.h" |
| 38 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 39 | #include "device/device.hpp" |
| 40 | #include "misc_log_ex.h" |
| 41 | |
| 42 | TEST(bulletproofs_plus, valid_zero) |
| 43 | { |
| 44 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(0, rct::skGen()); |
| 45 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proof)); |
| 46 | } |
| 47 | |
| 48 | TEST(bulletproofs_plus, valid_max) |
| 49 | { |
| 50 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(0xffffffffffffffff, rct::skGen()); |
| 51 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proof)); |
| 52 | } |
| 53 | |
| 54 | TEST(bulletproofs_plus, valid_random) |
| 55 | { |
| 56 | for (int n = 0; n < 8; ++n) |
| 57 | { |
| 58 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(crypto::rand<uint64_t>(), rct::skGen()); |
| 59 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proof)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | TEST(bulletproofs_plus, valid_multi_random) |
| 64 | { |
| 65 | for (int n = 0; n < 8; ++n) |
| 66 | { |
| 67 | size_t outputs = 2 + n; |
| 68 | std::vector<uint64_t> amounts; |
| 69 | rct::keyV gamma; |
| 70 | for (size_t i = 0; i < outputs; ++i) |
| 71 | { |
| 72 | amounts.push_back(crypto::rand<uint64_t>()); |
| 73 | gamma.push_back(rct::skGen()); |
| 74 | } |
| 75 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(amounts, gamma); |
| 76 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proof)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | TEST(bulletproofs_plus, valid_aggregated) |
| 81 | { |
| 82 | static const size_t N_PROOFS = 8; |
| 83 | std::vector<rct::BulletproofPlus> proofs(N_PROOFS); |
| 84 | for (size_t n = 0; n < N_PROOFS; ++n) |
| 85 | { |
| 86 | size_t outputs = 2 + n; |
| 87 | std::vector<uint64_t> amounts; |
| 88 | rct::keyV gamma; |
| 89 | for (size_t i = 0; i < outputs; ++i) |
| 90 | { |
| 91 | amounts.push_back(crypto::rand<uint64_t>()); |
| 92 | gamma.push_back(rct::skGen()); |
| 93 | } |
| 94 | proofs[n] = bulletproof_plus_PROVE(amounts, gamma); |
| 95 | } |
| 96 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proofs)); |
| 97 | } |
| 98 | |
| 99 | TEST(bulletproofs_plus, invalid_8) |
| 100 | { |
| 101 | rct::key invalid_amount = rct::zero(); |
| 102 | invalid_amount[8] = 1; |
| 103 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(invalid_amount, rct::skGen()); |
| 104 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 105 | } |
| 106 | |
| 107 | TEST(bulletproofs_plus, invalid_31) |
| 108 | { |
| 109 | rct::key invalid_amount = rct::zero(); |
| 110 | invalid_amount[31] = 1; |
| 111 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(invalid_amount, rct::skGen()); |
| 112 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 113 | } |
| 114 | |
| 115 | static const char * const torsion_elements[] = |
| 116 | { |
| 117 | "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa", |
| 118 | "0000000000000000000000000000000000000000000000000000000000000000", |
| 119 | "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85", |
| 120 | "ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f", |
| 121 | "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05", |
| 122 | "0000000000000000000000000000000000000000000000000000000000000080", |
| 123 | "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a", |
| 124 | }; |
| 125 | |
| 126 | TEST(bulletproofs_plus, invalid_torsion) |
| 127 | { |
| 128 | rct::BulletproofPlus proof = bulletproof_plus_PROVE(7329838943733, rct::skGen()); |
| 129 | ASSERT_TRUE(rct::bulletproof_plus_VERIFY(proof)); |
| 130 | for (const auto &xs: torsion_elements) |
| 131 | { |
| 132 | rct::key x; |
| 133 | ASSERT_TRUE(epee::string_tools::hex_to_pod(xs, x)); |
| 134 | ASSERT_FALSE(rct::isInMainSubgroup(x)); |
| 135 | for (auto &k: proof.V) |
| 136 | { |
| 137 | const rct::key org_k = k; |
| 138 | rct::addKeys(k, org_k, x); |
| 139 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 140 | k = org_k; |
| 141 | } |
| 142 | for (auto &k: proof.L) |
| 143 | { |
| 144 | const rct::key org_k = k; |
| 145 | rct::addKeys(k, org_k, x); |
| 146 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 147 | k = org_k; |
| 148 | } |
| 149 | for (auto &k: proof.R) |
| 150 | { |
| 151 | const rct::key org_k = k; |
| 152 | rct::addKeys(k, org_k, x); |
| 153 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 154 | k = org_k; |
| 155 | } |
| 156 | const rct::key org_A = proof.A; |
| 157 | rct::addKeys(proof.A, org_A, x); |
| 158 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 159 | proof.A = org_A; |
| 160 | const rct::key org_A1 = proof.A1; |
| 161 | rct::addKeys(proof.A1, org_A1, x); |
| 162 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 163 | proof.A1 = org_A1; |
| 164 | const rct::key org_B = proof.B; |
| 165 | rct::addKeys(proof.B, org_B, x); |
| 166 | ASSERT_FALSE(rct::bulletproof_plus_VERIFY(proof)); |
| 167 | proof.B = org_B; |
| 168 | } |
| 169 | } |