Line data Source code
1 : // Copyright (c) 2016-2020 The ZCash developers 2 : // Copyright (c) 2021 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : /* 7 : See the Zcash protocol specification for more information. 8 : https://github.com/zcash/zips/blob/master/protocol/protocol.pdf 9 : */ 10 : 11 : #ifndef PIVX_SAPLING_NOTEENCRYPTION_H 12 : #define PIVX_SAPLING_NOTEENCRYPTION_H 13 : 14 : #include "optional.h" 15 : #include "sapling/sapling.h" 16 : #include "uint256.h" 17 : 18 : #include <array> 19 : 20 : namespace libzcash { 21 : 22 : // Ciphertext for the recipient to decrypt 23 : typedef std::array<unsigned char, ZC_SAPLING_ENCCIPHERTEXT_SIZE> SaplingEncCiphertext; 24 : typedef std::array<unsigned char, ZC_SAPLING_ENCPLAINTEXT_SIZE> SaplingEncPlaintext; 25 : 26 : // Ciphertext for outgoing viewing key to decrypt 27 : typedef std::array<unsigned char, ZC_SAPLING_OUTCIPHERTEXT_SIZE> SaplingOutCiphertext; 28 : typedef std::array<unsigned char, ZC_SAPLING_OUTPLAINTEXT_SIZE> SaplingOutPlaintext; 29 : 30 : //! This is not a thread-safe API. 31 : class SaplingNoteEncryption { 32 : protected: 33 : // Ephemeral public key 34 : uint256 epk; 35 : 36 : // Ephemeral secret key 37 : uint256 esk; 38 : 39 : bool already_encrypted_enc; 40 : bool already_encrypted_out; 41 : 42 1327 : SaplingNoteEncryption(uint256 epk, uint256 esk) : epk(epk), esk(esk), already_encrypted_enc(false), already_encrypted_out(false) { 43 : 44 : } 45 : 46 : public: 47 : 48 : static Optional<SaplingNoteEncryption> FromDiversifier(diversifier_t d); 49 : 50 : Optional<SaplingEncCiphertext> encrypt_to_recipient( 51 : const uint256 &pk_d, 52 : const SaplingEncPlaintext &message 53 : ); 54 : 55 : SaplingOutCiphertext encrypt_to_ourselves( 56 : const uint256 &ovk, 57 : const uint256 &cv, 58 : const uint256 &cm, 59 : const SaplingOutPlaintext &message 60 : ); 61 : 62 1327 : uint256 get_epk() const { 63 1327 : return epk; 64 : } 65 : 66 2648 : uint256 get_esk() const { 67 2648 : return esk; 68 : } 69 : }; 70 : 71 : // Attempts to decrypt a Sapling note. This will not check that the contents 72 : // of the ciphertext are correct. 73 : Optional<SaplingEncPlaintext> AttemptSaplingEncDecryption( 74 : const SaplingEncCiphertext &ciphertext, 75 : const uint256 &ivk, 76 : const uint256 &epk 77 : ); 78 : 79 : // Attempts to decrypt a Sapling note using outgoing plaintext. 80 : // This will not check that the contents of the ciphertext are correct. 81 : Optional<SaplingEncPlaintext> AttemptSaplingEncDecryption ( 82 : const SaplingEncCiphertext &ciphertext, 83 : const uint256 &epk, 84 : const uint256 &esk, 85 : const uint256 &pk_d 86 : ); 87 : 88 : // Attempts to decrypt a Sapling note. This will not check that the contents 89 : // of the ciphertext are correct. 90 : Optional<SaplingOutPlaintext> AttemptSaplingOutDecryption( 91 : const SaplingOutCiphertext &ciphertext, 92 : const uint256 &ovk, 93 : const uint256 &cv, 94 : const uint256 &cm, 95 : const uint256 &epk 96 : ); 97 : 98 : } 99 : 100 : #endif // PIVX_SAPLING_NOTEENCRYPTION_H