Skip to content

Conversation

@furszy
Copy link

@furszy furszy commented Sep 25, 2020

Another decoupling from #1798. Similar to #1870, these changes are part of the primitives unit test coverage back port work.
Commits included:

  • change cm() to cmu() in SaplingNote class —> d437276a4b922c6c70d810cff0719a2e76f1a4bc
  • Note Encryption unit tests back ported. —> e4c1bbf1618fd797599b6bf3511cea79e6d9d4d7

@furszy furszy self-assigned this Sep 25, 2020
@furszy furszy requested a review from random-zebra September 28, 2020 15:32
@Fuzzbawls Fuzzbawls added this to the 5.0.0 milestone Oct 2, 2020
random-zebra
random-zebra previously approved these changes Oct 3, 2020
Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 56266a653d89bd4eedd249cc8e0ee4ce434601b3

@furszy furszy requested a review from Fuzzbawls October 3, 2020 20:18
@Fuzzbawls
Copy link
Collaborator

needs rebase after #1870 merge.

Also, would prefer if we didn't import libzcash into the global namespace for these two tests.

Index: src/test/librust/noteencryption_tests.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/librust/noteencryption_tests.cpp	(revision b220dcb582a064dbf46bda23831d099606477687)
+++ src/test/librust/noteencryption_tests.cpp	(date 1601755420677)
@@ -33,11 +33,10 @@
 
 BOOST_AUTO_TEST_CASE(note_plain_text_test)
 {
-    using namespace libzcash;
-    auto xsk = SaplingSpendingKey(uint256()).expanded_spending_key();
+    auto xsk = libzcash::SaplingSpendingKey(uint256()).expanded_spending_key();
     auto fvk = xsk.full_viewing_key();
     auto ivk = fvk.in_viewing_key();
-    SaplingPaymentAddress addr = *ivk.address({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
+    libzcash::SaplingPaymentAddress addr = *ivk.address({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
 
     std::array<unsigned char, ZC_MEMO_SIZE> memo;
     for (size_t i = 0; i < ZC_MEMO_SIZE; i++) {
@@ -45,13 +44,13 @@
         memo[i] = (unsigned char) i;
     }
 
-    SaplingNote note(addr, 39393);
+    libzcash::SaplingNote note(addr, 39393);
     auto cmu_opt = note.cmu();
     if (!cmu_opt) {
         BOOST_ERROR("SaplingNote cm failed");
     }
     uint256 cmu = cmu_opt.get();
-    SaplingNotePlaintext pt(note, memo);
+    libzcash::SaplingNotePlaintext pt(note, memo);
 
     auto res = pt.encrypt(addr.pk_d);
     if (!res) {
@@ -65,7 +64,7 @@
     auto epk = encryptor.get_epk();
 
     // Try to decrypt with incorrect commitment
-    BOOST_CHECK(!SaplingNotePlaintext::decrypt(
+    BOOST_CHECK(!libzcash::SaplingNotePlaintext::decrypt(
         ct,
         ivk,
         epk,
@@ -73,7 +72,7 @@
     ));
 
     // Try to decrypt with correct commitment
-    auto foo = SaplingNotePlaintext::decrypt(
+    auto foo = libzcash::SaplingNotePlaintext::decrypt(
         ct,
         ivk,
         epk,
@@ -106,7 +105,7 @@
     BOOST_CHECK(note.r == new_note.r);
     BOOST_CHECK(note.cmu() == new_note.cmu());
 
-    SaplingOutgoingPlaintext out_pt;
+    libzcash::SaplingOutgoingPlaintext out_pt;
     out_pt.pk_d = note.pk_d;
     out_pt.esk = encryptor.get_esk();
 
@@ -139,7 +138,7 @@
     BOOST_CHECK(decrypted_out_ct_unwrapped.esk == out_pt.esk);
 
     // Test sender won't accept invalid commitments
-    BOOST_CHECK(!SaplingNotePlaintext::decrypt(
+    BOOST_CHECK(!libzcash::SaplingNotePlaintext::decrypt(
         ct,
         epk,
         decrypted_out_ct_unwrapped.esk,
@@ -148,7 +147,7 @@
     ));
 
     // Test sender can decrypt the note ciphertext.
-    foo = SaplingNotePlaintext::decrypt(
+    foo = libzcash::SaplingNotePlaintext::decrypt(
         ct,
         epk,
         decrypted_out_ct_unwrapped.esk,
@@ -170,14 +169,12 @@
 
 BOOST_AUTO_TEST_CASE(SaplingApi_test)
 {
-    using namespace libzcash;
-
     // Create recipient addresses
-    auto sk = SaplingSpendingKey(uint256()).expanded_spending_key();
+    auto sk = libzcash::SaplingSpendingKey(uint256()).expanded_spending_key();
     auto vk = sk.full_viewing_key();
     auto ivk = vk.in_viewing_key();
-    SaplingPaymentAddress pk_1 = *ivk.address({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
-    SaplingPaymentAddress pk_2 = *ivk.address({4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
+    libzcash::SaplingPaymentAddress pk_1 = *ivk.address({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
+    libzcash::SaplingPaymentAddress pk_2 = *ivk.address({4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
 
     // Blob of stuff we're encrypting
     std::array<unsigned char, ZC_SAPLING_ENCPLAINTEXT_SIZE> message;
@@ -193,10 +190,10 @@
     }
 
     // Invalid diversifier
-    BOOST_CHECK(boost::none == SaplingNoteEncryption::FromDiversifier({1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}));
+    BOOST_CHECK(boost::none == libzcash::SaplingNoteEncryption::FromDiversifier({1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}));
 
     // Encrypt to pk_1
-    auto enc = *SaplingNoteEncryption::FromDiversifier(pk_1.d);
+    auto enc = *libzcash::SaplingNoteEncryption::FromDiversifier(pk_1.d);
     auto ciphertext_1 = *enc.encrypt_to_recipient(
             pk_1.pk_d,
             message
@@ -218,7 +215,7 @@
     );
 
     // Encrypt to pk_2
-    enc = *SaplingNoteEncryption::FromDiversifier(pk_2.d);
+    enc = *libzcash::SaplingNoteEncryption::FromDiversifier(pk_2.d);
     auto ciphertext_2 = *enc.encrypt_to_recipient(
             pk_2.pk_d,
             message
@@ -236,7 +233,7 @@
 
     // Test nonce-reuse resistance of API
     {
-        auto tmp_enc = *SaplingNoteEncryption::FromDiversifier(pk_1.d);
+        auto tmp_enc = *libzcash::SaplingNoteEncryption::FromDiversifier(pk_1.d);
 
         tmp_enc.encrypt_to_recipient(
                 pk_1.pk_d,
@@ -271,7 +268,7 @@
     );
     BOOST_CHECK(message == plaintext_1);
 
-    auto small_plaintext_1 = *AttemptSaplingOutDecryption(
+    auto small_plaintext_1 = *libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_1,
             sk.ovk,
             cv_1,
@@ -287,7 +284,7 @@
     );
     BOOST_CHECK(message == plaintext_2);
 
-    auto small_plaintext_2 = *AttemptSaplingOutDecryption(
+    auto small_plaintext_2 = *libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_2,
             sk.ovk,
             cv_2,
@@ -297,28 +294,28 @@
     BOOST_CHECK(small_message == small_plaintext_2);
 
     // Try to decrypt out ciphertext with wrong key material
-    BOOST_CHECK(!AttemptSaplingOutDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_1,
             random_uint256(),
             cv_1,
             cm_1,
             epk_1
     ));
-    BOOST_CHECK(!AttemptSaplingOutDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_1,
             sk.ovk,
             random_uint256(),
             cm_1,
             epk_1
     ));
-    BOOST_CHECK(!AttemptSaplingOutDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_1,
             sk.ovk,
             cv_1,
             random_uint256(),
             epk_1
     ));
-    BOOST_CHECK(!AttemptSaplingOutDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingOutDecryption(
             out_ciphertext_1,
             sk.ovk,
             cv_1,
@@ -339,12 +336,12 @@
     ));
 
     // Try to decrypt with wrong ivk
-    BOOST_CHECK(!AttemptSaplingEncDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingEncDecryption(
             ciphertext_1,
             uint256(),
             epk_1
     ));
-    BOOST_CHECK(!AttemptSaplingEncDecryption(
+    BOOST_CHECK(!libzcash::AttemptSaplingEncDecryption(
             ciphertext_2,
             uint256(),
             epk_2
Index: src/test/librust/sapling_note_tests.cpp
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/test/librust/sapling_note_tests.cpp	(revision b220dcb582a064dbf46bda23831d099606477687)
+++ src/test/librust/sapling_note_tests.cpp	(date 1601755420683)
@@ -20,7 +20,6 @@
 
 // Test data from https://github.com/zcash-hackworks/zcash-test-vectors/blob/master/sapling_key_components.py
 BOOST_AUTO_TEST_CASE(testVectors) {
-    using namespace libzcash;
     uint64_t v = 0;
     uint64_t note_pos = 0;
     std::array<uint8_t, 11> diversifier{0xf1, 0x9d, 0x9b, 0x79, 0x7e, 0x39, 0xf3, 0x37, 0x44, 0x58, 0x39};
@@ -51,21 +50,20 @@
     uint256 nf(v_nf);
 
     // Test commitment
-    SaplingNote note = SaplingNote(diversifier, pk_d, v, r);
+    libzcash::SaplingNote note = libzcash::SaplingNote(diversifier, pk_d, v, r);
     BOOST_CHECK(note.cmu().get() == cm);
 
     // Test nullifier
-    SaplingSpendingKey spendingKey(sk);
+    libzcash::SaplingSpendingKey spendingKey(sk);
     BOOST_CHECK(note.nullifier(spendingKey.full_viewing_key(), note_pos) == nf);
 }
 
 BOOST_AUTO_TEST_CASE(random) {
-    using namespace libzcash;
     CAmount MAX_MONEY_OUT = 21000000 * COIN;
     // Test creating random notes using the same spending key
-    auto address = SaplingSpendingKey::random().default_address();
-    SaplingNote note1(address, GetRand(MAX_MONEY_OUT));
-    SaplingNote note2(address, GetRand(MAX_MONEY_OUT));
+    auto address = libzcash::SaplingSpendingKey::random().default_address();
+    libzcash::SaplingNote note1(address, GetRand(MAX_MONEY_OUT));
+    libzcash::SaplingNote note2(address, GetRand(MAX_MONEY_OUT));
 
     BOOST_CHECK(note1.d == note2.d);
     BOOST_CHECK(note1.pk_d == note2.pk_d);
@@ -73,7 +71,7 @@
     BOOST_CHECK(note1.r != note2.r);
 
     // Test diversifier and pk_d are not the same for different spending keys
-    SaplingNote note3(SaplingSpendingKey::random().default_address(), GetRand(MAX_MONEY_OUT));
+    libzcash::SaplingNote note3(libzcash::SaplingSpendingKey::random().default_address(), GetRand(MAX_MONEY_OUT));
     BOOST_CHECK(note1.d != note3.d);
     BOOST_CHECK(note1.pk_d != note3.pk_d);
 }

@furszy
Copy link
Author

furszy commented Oct 3, 2020

done 👍 , global namespace usage removed.

Copy link
Collaborator

@Fuzzbawls Fuzzbawls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK b11b357

Copy link

@random-zebra random-zebra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-utACK b11b357

@furszy furszy merged commit 3c05a7b into PIVX-Project:master Oct 3, 2020
@furszy furszy deleted the 2020_notes_base_backports branch November 29, 2022 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants