LCOV - code coverage report
Current view: top level - src/test/librust - sapling_note_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 37 37 100.0 %
Date: 2025-04-02 01:23:23 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2016-2020 The ZCash developers
       2             : // Copyright (c) 2020 The PIVX Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include "test/test_pivx.h"
       7             : 
       8             : #include "sapling/address.h"
       9             : #include "sapling/note.h"
      10             : #include "sapling/sapling_util.h"
      11             : 
      12             : #include "amount.h"
      13             : #include "random.h"
      14             : #include <librustzcash.h>
      15             : 
      16             : #include <array>
      17             : 
      18             : #include <boost/test/unit_test.hpp>
      19             : 
      20             : BOOST_FIXTURE_TEST_SUITE(sapling_note_tests, BasicTestingSetup)
      21             : 
      22             : // Test data from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_key_components.py
      23           2 : BOOST_AUTO_TEST_CASE(testVectors) {
      24           1 :     uint64_t v = 0;
      25           1 :     uint64_t note_pos = 0;
      26           1 :     std::array<uint8_t, 11> diversifier{0xf1, 0x9d, 0x9b, 0x79, 0x7e, 0x39, 0xf3, 0x37, 0x44, 0x58, 0x39};
      27           1 :     std::vector<uint8_t> v_sk{
      28             :         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      29             :         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      30           1 :         0x00, 0x00, 0x00, 0x00};
      31           1 :     std::vector<uint8_t> v_pk_d{
      32             :         0xdb, 0x4c, 0xd2, 0xb0, 0xaa, 0xc4, 0xf7, 0xeb, 0x8c, 0xa1, 0x31, 0xf1, 0x65, 0x67,
      33             :         0xc4, 0x45, 0xa9, 0x55, 0x51, 0x26, 0xd3, 0xc2, 0x9f, 0x14, 0xe3, 0xd7, 0x76, 0xe8,
      34           2 :         0x41, 0xae, 0x74, 0x15};
      35           1 :     std::vector<uint8_t> v_r{
      36             :         0x39, 0x17, 0x6d, 0xac, 0x39, 0xac, 0xe4, 0x98, 0x0e, 0xcc, 0x8d, 0x77, 0x8e, 0x89,
      37             :         0x86, 0x02, 0x55, 0xec, 0x36, 0x15, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      38           2 :         0x00, 0x00, 0x00, 0x00};
      39           1 :     std::vector<uint8_t> v_cm{
      40             :         0xcb, 0x3c, 0xf9, 0x15, 0x32, 0x70, 0xd5, 0x7e, 0xb9, 0x14, 0xc6, 0xc2, 0xbc, 0xc0,
      41             :         0x18, 0x50, 0xc9, 0xfe, 0xd4, 0x4f, 0xce, 0x08, 0x06, 0x27, 0x8f, 0x08, 0x3e, 0xf2,
      42           2 :         0xdd, 0x07, 0x64, 0x39};
      43           1 :     std::vector<uint8_t> v_nf{
      44             :         0x44, 0xfa, 0xd6, 0x56, 0x4f, 0xfd, 0xec, 0x9f, 0xa1, 0x9c, 0x43, 0xa2, 0x8f, 0x86,
      45             :         0x1d, 0x5e, 0xbf, 0x60, 0x23, 0x46, 0x00, 0x7d, 0xe7, 0x62, 0x67, 0xd9, 0x75, 0x27,
      46           2 :         0x47, 0xab, 0x40, 0x63};
      47           1 :     uint256 sk(v_sk);
      48           1 :     uint256 pk_d(v_pk_d);
      49           1 :     uint256 r(v_r);
      50           1 :     uint256 cm(v_cm);
      51           1 :     uint256 nf(v_nf);
      52             : 
      53             :     // Test commitment
      54           2 :     libzcash::SaplingNote note = libzcash::SaplingNote(diversifier, pk_d, v, r);
      55           2 :     BOOST_CHECK(note.cmu().get() == cm);
      56             : 
      57             :     // Test nullifier
      58           1 :     libzcash::SaplingSpendingKey spendingKey(sk);
      59           3 :     BOOST_CHECK(note.nullifier(spendingKey.full_viewing_key(), note_pos) == nf);
      60           1 : }
      61             : 
      62           2 : BOOST_AUTO_TEST_CASE(random) {
      63           1 :     CAmount MAX_MONEY_OUT = 21000000 * COIN;
      64             :     // Test creating random notes using the same spending key
      65           1 :     auto address = libzcash::SaplingSpendingKey::random().default_address();
      66           1 :     libzcash::SaplingNote note1(address, GetRand(MAX_MONEY_OUT));
      67           1 :     libzcash::SaplingNote note2(address, GetRand(MAX_MONEY_OUT));
      68             : 
      69           2 :     BOOST_CHECK(note1.d == note2.d);
      70           2 :     BOOST_CHECK(note1.pk_d == note2.pk_d);
      71           2 :     BOOST_CHECK(note1.value() != note2.value());
      72           2 :     BOOST_CHECK(note1.r != note2.r);
      73             : 
      74             :     // Test diversifier and pk_d are not the same for different spending keys
      75           1 :     libzcash::SaplingNote note3(libzcash::SaplingSpendingKey::random().default_address(), GetRand(MAX_MONEY_OUT));
      76           2 :     BOOST_CHECK(note1.d != note3.d);
      77           2 :     BOOST_CHECK(note1.pk_d != note3.pk_d);
      78           1 : }
      79             : 
      80             : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14