| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include "gtest/gtest.h" |
| 31 | |
| 32 | #include "crypto/crypto.h" |
| 33 | extern "C" { |
| 34 | #include "crypto/crypto-ops.h" |
| 35 | } |
| 36 | #include "crypto/hash.h" |
| 37 | #include <boost/algorithm/string.hpp> |
| 38 | |
| 39 | static inline unsigned char *operator &(crypto::ec_point &point) { |
| 40 | return &reinterpret_cast<unsigned char &>(point); |
| 41 | } |
| 42 | |
| 43 | static inline unsigned char *operator &(crypto::ec_scalar &scalar) { |
| 44 | return &reinterpret_cast<unsigned char &>(scalar); |
| 45 | } |
| 46 | |
| 47 | TEST(tx_proof, prove_verify_v2) |
| 48 | { |
| 49 | crypto::secret_key r; |
| 50 | crypto::random32_unbiased(&r); |
| 51 | |
| 52 | // A = aG |
| 53 | // B = bG |
| 54 | crypto::secret_key a,b; |
| 55 | crypto::public_key A,B; |
| 56 | crypto::generate_keys(A, a, a, false); |
| 57 | crypto::generate_keys(B, b, b, false); |
| 58 | |
| 59 | // R_B = rB |
| 60 | crypto::public_key R_B; |
| 61 | ge_p3 B_p3; |
| 62 | ASSERT_EQ(ge_frombytes_vartime(&B_p3,&B), 0); |
| 63 | ge_p2 R_B_p2; |
| 64 | ge_scalarmult(&R_B_p2, &unwrap(r), &B_p3); |
| 65 | ge_tobytes(&R_B, &R_B_p2); |
| 66 | |
| 67 | // R_G = rG |
| 68 | crypto::public_key R_G; |
| 69 | ASSERT_EQ(ge_frombytes_vartime(&B_p3,&B), 0); |
| 70 | ge_p3 R_G_p3; |
| 71 | ge_scalarmult_base(&R_G_p3, &unwrap(r)); |
| 72 | ge_p3_tobytes(&R_G, &R_G_p3); |
| 73 | |
| 74 | // D = rA |
| 75 | crypto::public_key D; |
| 76 | ge_p3 A_p3; |
| 77 | ASSERT_EQ(ge_frombytes_vartime(&A_p3,&A), 0); |
| 78 | ge_p2 D_p2; |
| 79 | ge_scalarmult(&D_p2, &unwrap(r), &A_p3); |
| 80 | ge_tobytes(&D, &D_p2); |
| 81 | |
| 82 | crypto::signature sig; |
| 83 | |
| 84 | // Message data |
| 85 | crypto::hash prefix_hash; |
| 86 | char data[] = "hash input"; |
| 87 | crypto::cn_fast_hash(data,sizeof(data)-1,prefix_hash); |
| 88 | |
| 89 | // Generate/verify valid v1 proof with standard address |
| 90 | crypto::generate_tx_proof_v1(prefix_hash, R_G, A, boost::none, D, r, sig); |
| 91 | ASSERT_TRUE(crypto::check_tx_proof(prefix_hash, R_G, A, boost::none, D, sig, 1)); |
| 92 | |
| 93 | // Generate/verify valid v1 proof with subaddress |
| 94 | crypto::generate_tx_proof_v1(prefix_hash, R_B, A, B, D, r, sig); |
| 95 | ASSERT_TRUE(crypto::check_tx_proof(prefix_hash, R_B, A, B, D, sig, 1)); |
| 96 | |
| 97 | // Generate/verify valid v2 proof with standard address |
| 98 | crypto::generate_tx_proof(prefix_hash, R_G, A, boost::none, D, r, sig); |
| 99 | ASSERT_TRUE(crypto::check_tx_proof(prefix_hash, R_G, A, boost::none, D, sig, 2)); |
| 100 | |
| 101 | // Generate/verify valid v2 proof with subaddress |
| 102 | crypto::generate_tx_proof(prefix_hash, R_B, A, B, D, r, sig); |
| 103 | ASSERT_TRUE(crypto::check_tx_proof(prefix_hash, R_B, A, B, D, sig, 2)); |
| 104 | |
| 105 | // Try to verify valid v2 proofs as v1 proof (bad) |
| 106 | crypto::generate_tx_proof(prefix_hash, R_G, A, boost::none, D, r, sig); |
| 107 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_G, A, boost::none, D, sig, 1)); |
| 108 | crypto::generate_tx_proof(prefix_hash, R_B, A, B, D, r, sig); |
| 109 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_B, A, B, D, sig, 1)); |
| 110 | |
| 111 | // Randomly-distributed test points |
| 112 | crypto::secret_key evil_a, evil_b, evil_d, evil_r; |
| 113 | crypto::public_key evil_A, evil_B, evil_D, evil_R; |
| 114 | crypto::generate_keys(evil_A, evil_a, evil_a, false); |
| 115 | crypto::generate_keys(evil_B, evil_b, evil_b, false); |
| 116 | crypto::generate_keys(evil_D, evil_d, evil_d, false); |
| 117 | crypto::generate_keys(evil_R, evil_r, evil_r, false); |
| 118 | |
| 119 | // Selectively choose bad point in v2 proof (bad) |
| 120 | crypto::generate_tx_proof(prefix_hash, R_B, A, B, D, r, sig); |
| 121 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, evil_R, A, B, D, sig, 2)); |
| 122 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_B, evil_A, B, D, sig, 2)); |
| 123 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_B, A, evil_B, D, sig, 2)); |
| 124 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_B, A, B, evil_D, sig, 2)); |
| 125 | |
| 126 | // Try to verify valid v1 proofs as v2 proof (bad) |
| 127 | crypto::generate_tx_proof_v1(prefix_hash, R_G, A, boost::none, D, r, sig); |
| 128 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_G, A, boost::none, D, sig, 2)); |
| 129 | crypto::generate_tx_proof_v1(prefix_hash, R_B, A, B, D, r, sig); |
| 130 | ASSERT_FALSE(crypto::check_tx_proof(prefix_hash, R_B, A, B, D, sig, 2)); |
| 131 | } |