From 87ebb9a2e03cbb23960ccd7fa16a4ec100dcc1ab Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Mon, 27 Mar 2017 10:57:59 -0400 Subject: [PATCH] Move asset issuance funcionality to own file --- src/Makefile.am | 3 +++ src/issuance.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ src/issuance.h | 45 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 44 +------------------------------------------ src/main.h | 36 ----------------------------------- 5 files changed, 98 insertions(+), 79 deletions(-) create mode 100644 src/issuance.cpp create mode 100644 src/issuance.h diff --git a/src/Makefile.am b/src/Makefile.am index c745cab9bc..130a90da4e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -261,6 +261,8 @@ libbitcoin_consensus_a_SOURCES = \ core_write.cpp \ hash.cpp \ hash.h \ + issuance.h \ + issuance.cpp \ merkleblock.cpp \ pow.cpp \ prevector.h \ @@ -311,6 +313,7 @@ libbitcoin_common_a_SOURCES = \ global/common.cpp \ key.cpp \ keystore.cpp \ + issuance.cpp \ merkleblock.cpp \ netbase.cpp \ pow.cpp \ diff --git a/src/issuance.cpp b/src/issuance.cpp new file mode 100644 index 0000000000..48006e483c --- /dev/null +++ b/src/issuance.cpp @@ -0,0 +1,49 @@ +#include "issuance.h" + +#include "primitives/transaction.h" +#include "amount.h" + +void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint256& contracthash) +{ + // E : entropy + // I : prevout + // C : contract + // E = H( H(I) || H(C) ) + std::vector leaves; + leaves.reserve(2); + leaves.push_back(SerializeHash(prevout, SER_GETHASH, 0)); + leaves.push_back(contracthash); + entropy = ComputeFastMerkleRoot(leaves); +} + +void CalculateAsset(CAsset& asset, const uint256& entropy) +{ + static const uint256 kZero = uint256S("0x0000000000000000000000000000000000000000000000000000000000000000"); + // H_a : asset tag + // E : entropy + // H_a = H( E || 0 ) + std::vector leaves; + leaves.reserve(2); + leaves.push_back(entropy); + leaves.push_back(kZero); + asset = CAsset(ComputeFastMerkleRoot(leaves)); +} + +void CalculateReissuanceToken(CAsset& reissuanceToken, const uint256& entropy, bool fConfidential) +{ + static const uint256 kOne = uint256S("0x0000000000000000000000000000000000000000000000000000000000000001"); + static const uint256 kTwo = uint256S("0x0000000000000000000000000000000000000000000000000000000000000002"); + // H_a : asset reissuance tag + // E : entropy + // if not fConfidential: + // H_a = H( E || 1 ) + // else + // H_a = H( E || 2 ) + std::vector leaves; + leaves.reserve(2); + leaves.push_back(entropy); + leaves.push_back(fConfidential? kTwo: kOne); + reissuanceToken = CAsset(ComputeFastMerkleRoot(leaves)); +} + + diff --git a/src/issuance.h b/src/issuance.h new file mode 100644 index 0000000000..be313d9e66 --- /dev/null +++ b/src/issuance.h @@ -0,0 +1,45 @@ +#include "primitives/transaction.h" +#include "amount.h" +#include "hash.h" +#include "consensus/merkle.h" + +#ifndef BITCOIN_ISSUANCE_H +#define BITCOIN_ISSUANCE_H + +/** + * Calculate the asset entropy from an COutPoint and a tx-author specified + * Ricardian contract. See Definition 18 of the confidential assets paper. + * + * @param[out] entropy The asset entropy, which is used as input to + * CalculateAsset and CalculateReissuanceToken. + * @param[in] prevout Reference to the UTXO being spent. + * @param[in] contracthash Root hash of the issuer-specified Ricardian + * contract. + */ +void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint256& contracthash); + +/** + * Derive the asset from the entropy. See Definintion 19 of the confidential + * assets paper. + * + * @param[out] asset The nonce used as auxiliary input to the Pedersen + * commitment setup to derive the unblinded asset tag. + * @param[in] entropy The asset entropy returned by GenerateAssetEntropy. + */ +void CalculateAsset(CAsset& asset, const uint256& entropy); + +/** + * Derive the asset reissuance token asset from the entropy and reissuance + * parameters (confidential or explicit). See Definition 21 of the confidential + * assets paper. + * + * @param[out] reissuanceToken The nonce used as auxiliary input to the + * Pedersen commitment setup to derive the + * unblinded reissuance asset tag. + * @param[in] entropy The asset entropy returned by GenerateAssetEntropy. + * @param[in] fConfidential Set to true if the initial issuance was blinded, + * false otherwise. + */ +void CalculateReissuanceToken(CAsset& reissuanceToken, const uint256& entropy, bool fConfidential); + +#endif // BITCOIN_ISSUANCE_H diff --git a/src/main.cpp b/src/main.cpp index 14a5b1d67a..5a8af905ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "consensus/validation.h" #include "hash.h" #include "init.h" +#include "issuance.h" #include "merkleblock.h" #include "net.h" #include "policy/fees.h" @@ -2619,49 +2620,6 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins } }// namespace Consensus -void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint256& contracthash) -{ - // E : entropy - // I : prevout - // C : contract - // E = H( H(I) || H(C) ) - std::vector leaves; - leaves.reserve(2); - leaves.push_back(SerializeHash(prevout, SER_GETHASH, 0)); - leaves.push_back(contracthash); - entropy = ComputeFastMerkleRoot(leaves); -} - -void CalculateAsset(CAsset& asset, const uint256& entropy) -{ - static const uint256 kZero = uint256S("0x0000000000000000000000000000000000000000000000000000000000000000"); - // H_a : asset tag - // E : entropy - // H_a = H( E || 0 ) - std::vector leaves; - leaves.reserve(2); - leaves.push_back(entropy); - leaves.push_back(kZero); - asset = CAsset(ComputeFastMerkleRoot(leaves)); -} - -void CalculateReissuanceToken(CAsset& reissuanceToken, const uint256& entropy, bool fConfidential) -{ - static const uint256 kOne = uint256S("0x0000000000000000000000000000000000000000000000000000000000000001"); - static const uint256 kTwo = uint256S("0x0000000000000000000000000000000000000000000000000000000000000002"); - // H_a : asset reissuance tag - // E : entropy - // if not fConfidential: - // H_a = H( E || 1 ) - // else - // H_a = H( E || 2 ) - std::vector leaves; - leaves.reserve(2); - leaves.push_back(entropy); - leaves.push_back(fConfidential? kTwo: kOne); - reissuanceToken = CAsset(ComputeFastMerkleRoot(leaves)); -} - bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, set >& setWithdrawsSpent, std::vector *pvChecks) { if (!tx.IsCoinBase()) diff --git a/src/main.h b/src/main.h index a5b507d732..9c012202e5 100644 --- a/src/main.h +++ b/src/main.h @@ -372,42 +372,6 @@ bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoins } // namespace Consensus -/** - * Calculate the asset entropy from an COutPoint and a tx-author specified - * Ricardian contract. See Definition 18 of the confidential assets paper. - * - * @param[out] entropy The asset entropy, which is used as input to - * CalculateAsset and CalculateReissuanceToken. - * @param[in] prevout Reference to the UTXO being spent. - * @param[in] contracthash Root hash of the issuer-specified Ricardian - * contract. - */ -void GenerateAssetEntropy(uint256& entropy, const COutPoint& prevout, const uint256& contracthash); - -/** - * Derive the asset from the entropy. See Definintion 19 of the confidential - * assets paper. - * - * @param[out] asset The nonce used as auxiliary input to the Pedersen - * commitment setup to derive the unblinded asset tag. - * @param[in] entropy The asset entropy returned by GenerateAssetEntropy. - */ -void CalculateAsset(CAsset& asset, const uint256& entropy); - -/** - * Derive the asset reissuance token asset from the entropy and reissuance - * parameters (confidential or explicit). See Definition 21 of the confidential - * assets paper. - * - * @param[out] reissuanceToken The nonce used as auxiliary input to the - * Pedersen commitment setup to derive the - * unblinded reissuance asset tag. - * @param[in] entropy The asset entropy returned by GenerateAssetEntropy. - * @param[in] fConfidential Set to true if the initial issuance was blinded, - * false otherwise. - */ -void CalculateReissuanceToken(CAsset& reissuanceToken, const uint256& entropy, bool fConfidential); - /** * Verify the transaction's outputs spend exactly what its inputs provide, plus some excess amount. *