diff options
| -rw-r--r-- | docs/MONZERO_ASSETS_V1_SPEC.md | 43 | ||||
| -rw-r--r-- | src/blockchain_db/asset_db.cpp | 67 | ||||
| -rw-r--r-- | src/blockchain_db/asset_db.h | 14 | ||||
| -rw-r--r-- | src/cryptonote_basic/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/cryptonote_basic/asset_wire.cpp | 355 | ||||
| -rw-r--r-- | src/cryptonote_basic/asset_wire.h | 68 | ||||
| -rw-r--r-- | tests/unit_tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/unit_tests/asset_wire.cpp | 169 | ||||
| -rw-r--r-- | tests/unit_tests/blockchain_db.cpp | 77 |
9 files changed, 791 insertions, 4 deletions
diff --git a/docs/MONZERO_ASSETS_V1_SPEC.md b/docs/MONZERO_ASSETS_V1_SPEC.md index 5a5b499b6..8ef42f6db 100644 --- a/docs/MONZERO_ASSETS_V1_SPEC.md +++ b/docs/MONZERO_ASSETS_V1_SPEC.md @@ -284,10 +284,45 @@ abort, duplicate-key-image, and chain-pop tests cover this storage layer. This still does **not** provide end-to-end global double-spend prevention. The active transaction and mempool paths do not yet call the verifier or reserve -key images, and accepted transactions do not yet derive and write canonical -asset output records. Canonical wire serialization is also outstanding. Until -those layers exist and are reviewed, these proofs cannot make an asset -transaction valid on any Monzero network. +key images. The canonical wire and atomic state-application prototypes below +are not yet embedded in native transactions or invoked by active block and +mempool validation. Until those layers are integrated and reviewed, these +proofs cannot make an asset transaction valid on any Monzero network. + +### 6.3 Inactive canonical transaction payload + +The source tree contains a version-1 canonical binary payload prototype. It is +still detached from active transaction parsing. Its byte order and field order +are fixed as follows: + +1. one-byte payload version and one-byte network type; +2. 32-byte carrier-prefix hash; +3. one-byte issuance-present flag, followed when set by a two-byte + little-endian issuance length and the canonical authenticated issuance; +4. one-byte balance-group count, then for each group: asset ID, counted pseudo + inputs, counted destination-key/commitment pairs, counted burn commitments, + and counted Bulletproof+ objects; +5. one-byte ownership-proof count, then each asset ID, pseudo commitment, key + image, exactly 16 ring members, and the canonical CLSAG fields (`c1`, `D`, + and exactly 16 responses). The redundant CLSAG `I` field is reconstructed + from the separately encoded key image and is not serialized. + +All integer lengths and output indexes use explicit little-endian encoding; +all point, hash, UUID, and signature values use their fixed byte arrays. The +decoder rejects truncation, trailing bytes, unsupported versions, invalid +flags, noncanonical ring sizes, and counts over the per-group and aggregate +limits. The payload is capped at 256 KiB, eight asset groups, 64 total inputs, +64 total destinations, and 64 ownership proofs. A fixed 492-byte test vector +has canonical fast-hash +`7ad38e0c9b90d1d458f69df1ca5c4c27689ba0c86e0fbcb08a2149166b3d999b`. + +Output identities are derived from a domain label, network UUID, carrier hash, +asset ID, global output index, destination key, and commitment. State +application validates the entire payload, resolves every ring member, checks +collection authority and collisions, then atomically writes issuance records, +spent key images, and outputs. The carrier is defined as the native transaction +prefix hash with this envelope omitted; the active transaction representation +and exact stripping procedure remain an activation prerequisite. ## 7. Metadata diff --git a/src/blockchain_db/asset_db.cpp b/src/blockchain_db/asset_db.cpp index bb2389d24..800ea78fb 100644 --- a/src/blockchain_db/asset_db.cpp +++ b/src/blockchain_db/asset_db.cpp @@ -117,5 +117,72 @@ bool verify_asset_ownership_against_db(const BlockchainDB& db, return verify_asset_ownership_proof( proof, expected_network, carrier_prefix_hash, error); } + +bool apply_asset_transaction_to_db(BlockchainDB& db, + const asset_transaction_payload& payload, network_type expected_network, + const crypto::hash& expected_carrier_prefix_hash, uint64_t height, + std::vector<crypto::hash>& output_ids, std::string* error) +{ + asset_registry registry; + if (!load_registry_from_db(db, expected_network, registry, error) + || !verify_asset_transaction_payload(payload, registry.known_assets(), + expected_network, expected_carrier_prefix_hash, error)) + return false; + for (const asset_ownership_proof& proof : payload.ownership_proofs) + if (!verify_asset_ownership_against_db(db, proof, expected_network, + expected_carrier_prefix_hash, error)) + return false; + + crypto::hash issued_id{}; + std::vector<uint8_t> encoded_issuance; + if (payload.issuance) + { + if (!registry.apply_issuance(*payload.issuance, height, issued_id, error) + || !encode_issuance_payload(*payload.issuance, encoded_issuance, error)) + return false; + } + + std::vector<crypto::hash> candidate_ids; + uint32_t global_output_index = 0; + for (size_t group = 0; group < payload.balances.size(); ++group) + { + const confidential_asset_balance& balance = payload.balances[group]; + for (size_t index = 0; index < balance.outputs.size(); ++index) + { + confidential_asset_output output{ + payload.output_destinations[group][index], balance.outputs[index]}; + crypto::hash output_id{}; + if (!derive_asset_output_id(expected_network, expected_carrier_prefix_hash, + balance.asset_id, global_output_index++, output, output_id, error)) + return false; + asset_output_data_t existing{}; + if (db.get_asset_output(output_id, existing)) + return fail(error, "asset output identity already exists"); + candidate_ids.push_back(output_id); + } + } + + if (payload.issuance) + { + const blobdata_ref bytes{reinterpret_cast<const char*>(encoded_issuance.data()), + encoded_issuance.size()}; + db.add_asset_record(issued_id, height, bytes); + } + for (const asset_ownership_proof& proof : payload.ownership_proofs) + db.add_asset_key_image(proof.key_image, height); + size_t candidate_index = 0; + for (size_t group = 0; group < payload.balances.size(); ++group) + { + const confidential_asset_balance& balance = payload.balances[group]; + for (size_t index = 0; index < balance.outputs.size(); ++index) + { + const asset_output_data_t output{balance.asset_id, + payload.output_destinations[group][index], balance.outputs[index], height}; + db.add_asset_output(candidate_ids[candidate_index++], output); + } + } + output_ids = std::move(candidate_ids); + return true; +} } } diff --git a/src/blockchain_db/asset_db.h b/src/blockchain_db/asset_db.h index b5a763b75..a43d86585 100644 --- a/src/blockchain_db/asset_db.h +++ b/src/blockchain_db/asset_db.h @@ -6,6 +6,7 @@ #include "blockchain_db.h" #include "cryptonote_basic/asset_confidential.h" #include "cryptonote_basic/asset_types.h" +#include "cryptonote_basic/asset_wire.h" namespace cryptonote { @@ -38,5 +39,18 @@ namespace assets network_type expected_network, const crypto::hash& carrier_prefix_hash, std::string* error = nullptr); + + // The caller owns the outer blockchain write transaction. Every semantic, + // ownership, collision, and collection-authority check completes before the + // first write so an accepted payload changes registry, output, and spent-key + // state atomically. + bool apply_asset_transaction_to_db( + BlockchainDB& db, + const asset_transaction_payload& payload, + network_type expected_network, + const crypto::hash& expected_carrier_prefix_hash, + uint64_t height, + std::vector<crypto::hash>& output_ids, + std::string* error = nullptr); } } diff --git a/src/cryptonote_basic/CMakeLists.txt b/src/cryptonote_basic/CMakeLists.txt index 84e86803c..c8b41a03e 100644 --- a/src/cryptonote_basic/CMakeLists.txt +++ b/src/cryptonote_basic/CMakeLists.txt @@ -46,6 +46,7 @@ target_link_libraries(cryptonote_format_utils_basic set(cryptonote_basic_sources asset_confidential.cpp + asset_wire.cpp account.cpp asset_types.cpp connection_context.cpp diff --git a/src/cryptonote_basic/asset_wire.cpp b/src/cryptonote_basic/asset_wire.cpp new file mode 100644 index 000000000..a0d77bf33 --- /dev/null +++ b/src/cryptonote_basic/asset_wire.cpp @@ -0,0 +1,355 @@ +#include "asset_wire.h" + +#include <cstring> +#include <limits> + +#include "ringct/rctOps.h" + +namespace cryptonote +{ +namespace assets +{ +namespace +{ + constexpr char OUTPUT_ID_DOMAIN[] = "MonzeroAssetOutputIdV1"; + + bool fail(std::string* error, const std::string& message) + { + if (error) + *error = message; + return false; + } + + class writer + { + public: + template<typename T> void pod(const T& value) + { + const auto* begin = reinterpret_cast<const uint8_t*>(&value); + bytes.insert(bytes.end(), begin, begin + sizeof(value)); + } + void count(size_t value) { pod(static_cast<uint8_t>(value)); } + void u16(uint16_t value) + { + bytes.push_back(static_cast<uint8_t>(value)); + bytes.push_back(static_cast<uint8_t>(value >> 8)); + } + void u32(uint32_t value) + { + for (unsigned shift = 0; shift < 32; shift += 8) + bytes.push_back(static_cast<uint8_t>(value >> shift)); + } + std::vector<uint8_t> bytes; + }; + + class reader + { + public: + explicit reader(const std::vector<uint8_t>& source) : source_(source) {} + template<typename T> bool pod(T& value) + { + if (offset_ > source_.size() || sizeof(value) > source_.size() - offset_) + return false; + std::memcpy(&value, source_.data() + offset_, sizeof(value)); + offset_ += sizeof(value); + return true; + } + bool count(size_t limit, size_t& value) + { + uint8_t encoded = 0; + if (!pod(encoded) || encoded > limit) + return false; + value = encoded; + return true; + } + bool u16(uint16_t& value) + { + uint8_t low = 0, high = 0; + if (!pod(low) || !pod(high)) + return false; + value = static_cast<uint16_t>(low) | (static_cast<uint16_t>(high) << 8); + return true; + } + bool done() const { return offset_ == source_.size(); } + private: + const std::vector<uint8_t>& source_; + size_t offset_ = 0; + }; + + void write_keys(writer& out, const rct::keyV& keys) + { + out.count(keys.size()); + for (const rct::key& key : keys) + out.pod(key); + } + + bool read_keys(reader& in, size_t limit, rct::keyV& keys) + { + size_t count = 0; + if (!in.count(limit, count)) + return false; + keys.resize(count); + for (rct::key& key : keys) + if (!in.pod(key)) + return false; + return true; + } + + void write_range_proof(writer& out, const rct::BulletproofPlus& proof) + { + write_keys(out, proof.V); + out.pod(proof.A); out.pod(proof.A1); out.pod(proof.B); + out.pod(proof.r1); out.pod(proof.s1); out.pod(proof.d1); + write_keys(out, proof.L); + write_keys(out, proof.R); + } + + bool read_range_proof(reader& in, rct::BulletproofPlus& proof) + { + return read_keys(in, MAX_CONFIDENTIAL_ASSET_OUTPUTS, proof.V) + && in.pod(proof.A) && in.pod(proof.A1) && in.pod(proof.B) + && in.pod(proof.r1) && in.pod(proof.s1) && in.pod(proof.d1) + && read_keys(in, 16, proof.L) && read_keys(in, 16, proof.R) + && !proof.L.empty() && proof.L.size() == proof.R.size(); + } +} + +bool validate_asset_transaction_payload_shape(const asset_transaction_payload& payload, + std::string* error) +{ + if (payload.version != ASSET_TRANSACTION_WIRE_VERSION) + return fail(error, "unsupported asset transaction wire version"); + if (payload.network != MAINNET && payload.network != TESTNET && payload.network != STAGENET) + return fail(error, "asset transaction requires a public network"); + if (payload.carrier_prefix_hash == crypto::null_hash) + return fail(error, "asset transaction has a zero carrier hash"); + if (payload.balances.empty() || payload.balances.size() > MAX_ASSET_BALANCE_GROUPS) + return fail(error, "asset transaction has an invalid balance-group count"); + if (payload.output_destinations.size() != payload.balances.size()) + return fail(error, "asset transaction output destinations do not match balance groups"); + if (payload.ownership_proofs.size() > MAX_ASSET_OWNERSHIP_PROOFS) + return fail(error, "asset transaction has too many ownership proofs"); + size_t total_inputs = 0, total_destinations = 0; + for (size_t group = 0; group < payload.balances.size(); ++group) + { + const confidential_asset_balance& balance = payload.balances[group]; + if (balance.pseudo_inputs.empty() + || balance.pseudo_inputs.size() > MAX_CONFIDENTIAL_ASSET_INPUTS + || balance.outputs.size() + balance.burns.size() > MAX_CONFIDENTIAL_ASSET_OUTPUTS + || balance.range_proofs.empty() + || balance.range_proofs.size() > MAX_ASSET_RANGE_PROOFS) + return fail(error, "asset balance group exceeds canonical limits"); + if (payload.output_destinations[group].size() != balance.outputs.size()) + return fail(error, "asset output destination count does not match commitments"); + total_inputs += balance.pseudo_inputs.size(); + total_destinations += balance.outputs.size() + balance.burns.size(); + if (total_inputs > MAX_ASSET_TOTAL_INPUTS + || total_destinations > MAX_ASSET_TOTAL_DESTINATIONS) + return fail(error, "asset transaction exceeds aggregate input or destination limits"); + } + for (const asset_ownership_proof& proof : payload.ownership_proofs) + if (proof.ring.size() != CONFIDENTIAL_ASSET_RING_SIZE + || proof.signature.s.size() != CONFIDENTIAL_ASSET_RING_SIZE) + return fail(error, "asset ownership proof has a noncanonical ring size"); + return true; +} + +bool encode_asset_transaction_payload(const asset_transaction_payload& payload, + std::vector<uint8_t>& encoded, std::string* error) +{ + if (!validate_asset_transaction_payload_shape(payload, error)) + return false; + writer out; + out.pod(payload.version); + out.pod(static_cast<uint8_t>(payload.network)); + out.pod(payload.carrier_prefix_hash); + out.pod(static_cast<uint8_t>(payload.issuance ? 1 : 0)); + if (payload.issuance) + { + std::vector<uint8_t> issuance; + if (!encode_issuance_payload(*payload.issuance, issuance, error)) + return false; + if (issuance.size() > std::numeric_limits<uint16_t>::max()) + return fail(error, "asset issuance payload is too large"); + out.u16(static_cast<uint16_t>(issuance.size())); + out.bytes.insert(out.bytes.end(), issuance.begin(), issuance.end()); + } + out.count(payload.balances.size()); + for (size_t group = 0; group < payload.balances.size(); ++group) + { + const confidential_asset_balance& balance = payload.balances[group]; + out.pod(balance.asset_id); + out.count(balance.pseudo_inputs.size()); + for (const confidential_pseudo_input& input : balance.pseudo_inputs) + { + out.pod(input.source_asset_id); + out.pod(input.commitment); + } + out.count(balance.outputs.size()); + for (size_t index = 0; index < balance.outputs.size(); ++index) + { + out.pod(payload.output_destinations[group][index]); + out.pod(balance.outputs[index]); + } + write_keys(out, balance.burns); + out.count(balance.range_proofs.size()); + for (const rct::BulletproofPlus& proof : balance.range_proofs) + write_range_proof(out, proof); + } + out.count(payload.ownership_proofs.size()); + for (const asset_ownership_proof& proof : payload.ownership_proofs) + { + out.pod(proof.asset_id); + out.pod(proof.pseudo_input); + out.pod(proof.key_image); + for (const asset_ring_member& member : proof.ring) + { + out.pod(member.asset_id); out.pod(member.output_id); + out.pod(member.public_output.dest); out.pod(member.public_output.mask); + } + out.pod(proof.signature.c1); + out.pod(proof.signature.D); + for (const rct::key& response : proof.signature.s) + out.pod(response); + } + if (out.bytes.size() > MAX_ASSET_WIRE_BYTES) + return fail(error, "asset transaction payload exceeds maximum size"); + encoded = std::move(out.bytes); + return true; +} + +bool decode_asset_transaction_payload(const std::vector<uint8_t>& encoded, + asset_transaction_payload& payload, std::string* error) +{ + if (encoded.empty() || encoded.size() > MAX_ASSET_WIRE_BYTES) + return fail(error, "asset transaction payload has an invalid size"); + reader in(encoded); + asset_transaction_payload candidate; + uint8_t network = 0, has_issuance = 0; + if (!in.pod(candidate.version) || !in.pod(network) + || !in.pod(candidate.carrier_prefix_hash) || !in.pod(has_issuance) + || has_issuance > 1) + return fail(error, "truncated asset transaction header"); + candidate.network = static_cast<network_type>(network); + if (has_issuance) + { + uint16_t size = 0; + if (!in.u16(size)) + return fail(error, "truncated asset issuance length"); + std::vector<uint8_t> issuance(size); + for (uint8_t& byte : issuance) + if (!in.pod(byte)) + return fail(error, "truncated asset issuance payload"); + issuance_payload decoded; + if (!decode_issuance_payload(issuance, decoded, error)) + return false; + candidate.issuance = decoded; + } + size_t groups = 0; + if (!in.count(MAX_ASSET_BALANCE_GROUPS, groups) || groups == 0) + return fail(error, "invalid asset balance-group count"); + candidate.balances.resize(groups); + candidate.output_destinations.resize(groups); + for (size_t group = 0; group < groups; ++group) + { + confidential_asset_balance& balance = candidate.balances[group]; + size_t inputs = 0, outputs = 0, proofs = 0; + if (!in.pod(balance.asset_id) || !in.count(MAX_CONFIDENTIAL_ASSET_INPUTS, inputs) || inputs == 0) + return fail(error, "truncated or invalid asset inputs"); + balance.pseudo_inputs.resize(inputs); + for (confidential_pseudo_input& input : balance.pseudo_inputs) + if (!in.pod(input.source_asset_id) || !in.pod(input.commitment)) + return fail(error, "truncated asset pseudo input"); + if (!in.count(MAX_CONFIDENTIAL_ASSET_OUTPUTS, outputs)) + return fail(error, "invalid asset output count"); + balance.outputs.resize(outputs); + candidate.output_destinations[group].resize(outputs); + for (size_t index = 0; index < outputs; ++index) + if (!in.pod(candidate.output_destinations[group][index]) || !in.pod(balance.outputs[index])) + return fail(error, "truncated asset output"); + if (!read_keys(in, MAX_CONFIDENTIAL_ASSET_OUTPUTS - outputs, balance.burns) + || !in.count(MAX_ASSET_RANGE_PROOFS, proofs) || proofs == 0) + return fail(error, "invalid asset burn or range-proof count"); + balance.range_proofs.resize(proofs); + for (rct::BulletproofPlus& proof : balance.range_proofs) + if (!read_range_proof(in, proof)) + return fail(error, "truncated or noncanonical asset range proof"); + } + size_t ownership_count = 0; + if (!in.count(MAX_ASSET_OWNERSHIP_PROOFS, ownership_count)) + return fail(error, "invalid asset ownership-proof count"); + candidate.ownership_proofs.resize(ownership_count); + for (asset_ownership_proof& proof : candidate.ownership_proofs) + { + if (!in.pod(proof.asset_id) || !in.pod(proof.pseudo_input) || !in.pod(proof.key_image)) + return fail(error, "truncated asset ownership proof"); + proof.ring.resize(CONFIDENTIAL_ASSET_RING_SIZE); + for (asset_ring_member& member : proof.ring) + if (!in.pod(member.asset_id) || !in.pod(member.output_id) + || !in.pod(member.public_output.dest) || !in.pod(member.public_output.mask)) + return fail(error, "truncated asset ownership ring"); + if (!in.pod(proof.signature.c1) || !in.pod(proof.signature.D)) + return fail(error, "truncated asset CLSAG header"); + proof.signature.s.resize(CONFIDENTIAL_ASSET_RING_SIZE); + for (rct::key& response : proof.signature.s) + if (!in.pod(response)) + return fail(error, "truncated asset CLSAG responses"); + std::memcpy(&proof.signature.I, &proof.key_image, sizeof(proof.signature.I)); + } + if (!in.done() || !validate_asset_transaction_payload_shape(candidate, error)) + return fail(error, in.done() ? "invalid asset transaction payload" : "trailing asset transaction bytes"); + payload = std::move(candidate); + return true; +} + +bool verify_asset_transaction_payload(const asset_transaction_payload& payload, + const std::set<crypto::hash>& known_assets, network_type expected_network, + const crypto::hash& expected_carrier_prefix_hash, std::string* error) +{ + if (!validate_asset_transaction_payload_shape(payload, error)) + return false; + if (payload.network != expected_network) + return fail(error, "asset transaction belongs to a different network"); + if (payload.carrier_prefix_hash != expected_carrier_prefix_hash) + return fail(error, "asset transaction carrier hash mismatch"); + boost::optional<issuance_descriptor> descriptor; + if (payload.issuance) + { + if (!verify_issuance_authorization(payload.issuance->descriptor, + payload.issuance->issuer_signature, error)) + return false; + descriptor = payload.issuance->descriptor; + } + for (const std::vector<rct::key>& destinations : payload.output_destinations) + for (const rct::key& destination : destinations) + if (!rct::isInMainSubgroup(destination)) + return fail(error, "asset transaction contains an invalid destination key"); + return verify_confidential_asset_transaction_with_ownership( + payload.balances, payload.ownership_proofs, known_assets, descriptor, + expected_network, expected_carrier_prefix_hash, error); +} + +bool derive_asset_output_id(network_type network, + const crypto::hash& carrier_prefix_hash, + const crypto::hash& asset_id, uint32_t output_index, + const confidential_asset_output& output, crypto::hash& output_id, + std::string* error) +{ + if (network != MAINNET && network != TESTNET && network != STAGENET) + return fail(error, "asset output identity requires a public network"); + if (carrier_prefix_hash == crypto::null_hash || asset_id == crypto::null_hash) + return fail(error, "asset output identity has a zero carrier or asset id"); + if (!rct::isInMainSubgroup(output.destination) + || !rct::isInMainSubgroup(output.commitment)) + return fail(error, "asset output identity contains an invalid point"); + writer bytes; + bytes.bytes.insert(bytes.bytes.end(), OUTPUT_ID_DOMAIN, + OUTPUT_ID_DOMAIN + sizeof(OUTPUT_ID_DOMAIN) - 1); + bytes.pod(get_config(network).NETWORK_ID); + bytes.pod(carrier_prefix_hash); bytes.pod(asset_id); + bytes.u32(output_index); bytes.pod(output.destination); bytes.pod(output.commitment); + output_id = crypto::cn_fast_hash(bytes.bytes.data(), bytes.bytes.size()); + return true; +} +} +} diff --git a/src/cryptonote_basic/asset_wire.h b/src/cryptonote_basic/asset_wire.h new file mode 100644 index 000000000..e5f63a01b --- /dev/null +++ b/src/cryptonote_basic/asset_wire.h @@ -0,0 +1,68 @@ +#pragma once + +#include <string> +#include <vector> + +#include <boost/optional.hpp> + +#include "asset_confidential.h" + +namespace cryptonote +{ +namespace assets +{ + constexpr uint8_t ASSET_TRANSACTION_WIRE_VERSION = 1; + constexpr size_t MAX_ASSET_BALANCE_GROUPS = 8; + constexpr size_t MAX_ASSET_TOTAL_INPUTS = 64; + constexpr size_t MAX_ASSET_TOTAL_DESTINATIONS = 64; + constexpr size_t MAX_ASSET_OWNERSHIP_PROOFS = 64; + constexpr size_t MAX_ASSET_RANGE_PROOFS = 16; + constexpr size_t MAX_ASSET_WIRE_BYTES = 256 * 1024; + + struct confidential_asset_output + { + rct::key destination{}; + rct::key commitment{}; + }; + + // Inactive canonical payload prototype. The carrier hash is computed from + // the native transaction prefix with this envelope omitted; activation code + // must enforce that procedure to avoid a self-referential hash. + struct asset_transaction_payload + { + uint8_t version = ASSET_TRANSACTION_WIRE_VERSION; + network_type network = UNDEFINED; + crypto::hash carrier_prefix_hash{}; + boost::optional<issuance_payload> issuance; + std::vector<confidential_asset_balance> balances; + std::vector<std::vector<rct::key>> output_destinations; + std::vector<asset_ownership_proof> ownership_proofs; + }; + + bool validate_asset_transaction_payload_shape( + const asset_transaction_payload& payload, + std::string* error = nullptr); + bool encode_asset_transaction_payload( + const asset_transaction_payload& payload, + std::vector<uint8_t>& encoded, + std::string* error = nullptr); + bool decode_asset_transaction_payload( + const std::vector<uint8_t>& encoded, + asset_transaction_payload& payload, + std::string* error = nullptr); + bool verify_asset_transaction_payload( + const asset_transaction_payload& payload, + const std::set<crypto::hash>& known_assets, + network_type expected_network, + const crypto::hash& expected_carrier_prefix_hash, + std::string* error = nullptr); + bool derive_asset_output_id( + network_type network, + const crypto::hash& carrier_prefix_hash, + const crypto::hash& asset_id, + uint32_t output_index, + const confidential_asset_output& output, + crypto::hash& output_id, + std::string* error = nullptr); +} +} diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index b3328fe74..52df31b4a 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -31,6 +31,7 @@ set(unit_tests_sources apply_permutation.cpp address_from_url.cpp asset_confidential.cpp + asset_wire.cpp asset_types.cpp base58.cpp blockchain_db.cpp diff --git a/tests/unit_tests/asset_wire.cpp b/tests/unit_tests/asset_wire.cpp new file mode 100644 index 000000000..d7c26101e --- /dev/null +++ b/tests/unit_tests/asset_wire.cpp @@ -0,0 +1,169 @@ +#include "gtest/gtest.h" + +#include "cryptonote_basic/asset_wire.h" +#include "ringct/bulletproofs_plus.h" +#include "ringct/rctOps.h" +#include "string_tools.h" + +namespace +{ + cryptonote::assets::asset_transaction_payload make_payload() + { + cryptonote::assets::asset_transaction_payload payload; + payload.network = cryptonote::TESTNET; + payload.carrier_prefix_hash.data[0] = 0xc1; + + crypto::public_key issuer{}; + crypto::secret_key issuer_secret{}; + crypto::generate_keys(issuer, issuer_secret); + cryptonote::assets::issuance_payload issuance; + issuance.descriptor.network = cryptonote::TESTNET; + issuance.descriptor.issuer_key = issuer; + issuance.descriptor.issuance_nonce.data[0] = 0xc2; + issuance.descriptor.atomic_supply = 10; + issuance.descriptor.display_decimals = 0; + issuance.descriptor.metadata_reference = "ipfs://monzero-wire-vector"; + crypto::hash authorization{}; + if (!cryptonote::assets::derive_issuance_authorization_hash( + issuance.descriptor, authorization)) + throw std::runtime_error("failed to derive issuance authorization"); + crypto::generate_signature(authorization, issuer, issuer_secret, + issuance.issuer_signature); + payload.issuance = issuance; + + crypto::hash id{}; + if (!cryptonote::assets::derive_asset_id(issuance.descriptor, id)) + throw std::runtime_error("failed to derive asset id"); + cryptonote::assets::confidential_asset_balance balance; + balance.asset_id = id; + balance.pseudo_inputs.push_back({id, rct::commit(10, rct::zero())}); + balance.outputs.push_back(rct::commit(10, rct::zero())); + balance.range_proofs.push_back( + rct::bulletproof_plus_PROVE(10, rct::zero())); + payload.balances.push_back(balance); + rct::key secret{}, destination{}; + rct::skpkGen(secret, destination); + payload.output_destinations.push_back({destination}); + return payload; + } +} + +TEST(asset_wire, canonical_round_trip_preserves_verified_issuance) +{ + const auto original = make_payload(); + std::vector<uint8_t> encoded, reencoded; + std::string error; + ASSERT_TRUE(cryptonote::assets::encode_asset_transaction_payload( + original, encoded, &error)) << error; + cryptonote::assets::asset_transaction_payload decoded; + ASSERT_TRUE(cryptonote::assets::decode_asset_transaction_payload( + encoded, decoded, &error)) << error; + ASSERT_TRUE(cryptonote::assets::encode_asset_transaction_payload( + decoded, reencoded, &error)) << error; + ASSERT_EQ(encoded, reencoded); + ASSERT_TRUE(decoded.issuance); + ASSERT_TRUE(cryptonote::assets::verify_confidential_asset_transaction( + decoded.balances, {}, decoded.issuance->descriptor, &error)) << error; + ASSERT_TRUE(cryptonote::assets::verify_asset_transaction_payload( + decoded, {}, cryptonote::TESTNET, decoded.carrier_prefix_hash, &error)) << error; + EXPECT_FALSE(cryptonote::assets::verify_asset_transaction_payload( + decoded, {}, cryptonote::MAINNET, decoded.carrier_prefix_hash, &error)); + crypto::hash other_carrier = decoded.carrier_prefix_hash; + other_carrier.data[1] = 1; + EXPECT_FALSE(cryptonote::assets::verify_asset_transaction_payload( + decoded, {}, cryptonote::TESTNET, other_carrier, &error)); +} + +TEST(asset_wire, rejects_every_truncation_trailing_bytes_and_noncanonical_counts) +{ + const auto payload = make_payload(); + std::vector<uint8_t> encoded; + std::string error; + ASSERT_TRUE(cryptonote::assets::encode_asset_transaction_payload( + payload, encoded, &error)) << error; + for (size_t size = 0; size < encoded.size(); ++size) + { + cryptonote::assets::asset_transaction_payload decoded; + const std::vector<uint8_t> truncated(encoded.begin(), encoded.begin() + size); + EXPECT_FALSE(cryptonote::assets::decode_asset_transaction_payload( + truncated, decoded, &error)) << "accepted truncation at " << size; + } + auto trailing = encoded; + trailing.push_back(0); + cryptonote::assets::asset_transaction_payload decoded; + EXPECT_FALSE(cryptonote::assets::decode_asset_transaction_payload( + trailing, decoded, &error)); + auto unsupported = encoded; + unsupported[0] = 2; + EXPECT_FALSE(cryptonote::assets::decode_asset_transaction_payload( + unsupported, decoded, &error)); + auto excessive = payload; + excessive.balances.resize(cryptonote::assets::MAX_ASSET_BALANCE_GROUPS + 1, + payload.balances.front()); + excessive.output_destinations.resize(excessive.balances.size(), + payload.output_destinations.front()); + EXPECT_FALSE(cryptonote::assets::encode_asset_transaction_payload( + excessive, trailing, &error)); +} + +TEST(asset_wire, deterministic_output_identity_binds_every_field) +{ + const auto payload = make_payload(); + cryptonote::assets::confidential_asset_output output{ + payload.output_destinations.front().front(), + payload.balances.front().outputs.front()}; + crypto::hash first{}, repeated{}, changed{}; + std::string error; + ASSERT_TRUE(cryptonote::assets::derive_asset_output_id( + cryptonote::TESTNET, payload.carrier_prefix_hash, payload.balances.front().asset_id, 0, + output, first, &error)) << error; + ASSERT_TRUE(cryptonote::assets::derive_asset_output_id( + cryptonote::TESTNET, payload.carrier_prefix_hash, payload.balances.front().asset_id, 0, + output, repeated, &error)); + EXPECT_EQ(first, repeated); + ASSERT_TRUE(cryptonote::assets::derive_asset_output_id( + cryptonote::MAINNET, payload.carrier_prefix_hash, payload.balances.front().asset_id, 0, + output, changed, &error)); + EXPECT_NE(first, changed); + ASSERT_TRUE(cryptonote::assets::derive_asset_output_id( + cryptonote::TESTNET, payload.carrier_prefix_hash, payload.balances.front().asset_id, 1, + output, changed, &error)); + EXPECT_NE(first, changed); + output.commitment = rct::commit(9, rct::zero()); + ASSERT_TRUE(cryptonote::assets::derive_asset_output_id( + cryptonote::TESTNET, payload.carrier_prefix_hash, payload.balances.front().asset_id, 0, + output, changed, &error)); + EXPECT_NE(first, changed); +} + +TEST(asset_wire, fixed_wire_vector_has_stable_size_and_digest) +{ + cryptonote::assets::asset_transaction_payload payload; + payload.network = cryptonote::STAGENET; + payload.carrier_prefix_hash.data[0] = 0xe1; + cryptonote::assets::confidential_asset_balance balance; + balance.asset_id.data[0] = 0xe2; + balance.pseudo_inputs.push_back({balance.asset_id, rct::key{}}); + balance.outputs.push_back(rct::key{}); + rct::BulletproofPlus proof; + proof.A = rct::key{}; + proof.A1 = rct::key{}; + proof.B = rct::key{}; + proof.r1 = rct::key{}; + proof.s1 = rct::key{}; + proof.d1 = rct::key{}; + proof.V.resize(1); + proof.L.resize(1); + proof.R.resize(1); + balance.range_proofs.push_back(proof); + payload.balances.push_back(balance); + payload.output_destinations.push_back({rct::key{}}); + std::vector<uint8_t> encoded; + std::string error; + ASSERT_TRUE(cryptonote::assets::encode_asset_transaction_payload( + payload, encoded, &error)) << error; + ASSERT_EQ(492u, encoded.size()); + const crypto::hash digest = crypto::cn_fast_hash(encoded.data(), encoded.size()); + ASSERT_EQ("7ad38e0c9b90d1d458f69df1ca5c4c27689ba0c86e0fbcb08a2149166b3d999b", + epee::string_tools::pod_to_hex(digest)); +} diff --git a/tests/unit_tests/blockchain_db.cpp b/tests/unit_tests/blockchain_db.cpp index 124a88724..a2261bc60 100644 --- a/tests/unit_tests/blockchain_db.cpp +++ b/tests/unit_tests/blockchain_db.cpp @@ -42,6 +42,7 @@ #include "cryptonote_basic/asset_confidential.h" #include "cryptonote_basic/cryptonote_format_utils.h" #include "device/device.hpp" +#include "ringct/bulletproofs_plus.h" #include "ringct/rctSigs.h" using namespace cryptonote; @@ -111,6 +112,30 @@ assets::asset_ownership_proof make_db_ownership_proof( return proof; } +assets::asset_transaction_payload make_db_asset_issuance( + const crypto::hash& carrier) +{ + const assets::transaction_extension extension = make_asset_extension(carrier); + assets::asset_transaction_payload payload; + payload.network = TESTNET; + payload.carrier_prefix_hash = carrier; + payload.issuance = extension.issuance; + crypto::hash id{}; + if (!assets::derive_asset_id(extension.issuance.descriptor, id)) + throw std::runtime_error("failed to derive test asset id"); + const uint64_t supply = extension.issuance.descriptor.atomic_supply; + assets::confidential_asset_balance balance; + balance.asset_id = id; + balance.pseudo_inputs.push_back({id, rct::commit(supply, rct::zero())}); + balance.outputs.push_back(rct::commit(supply, rct::zero())); + balance.range_proofs.push_back(rct::bulletproof_plus_PROVE(supply, rct::zero())); + payload.balances.push_back(balance); + rct::key secret{}, destination{}; + rct::skpkGen(secret, destination); + payload.output_destinations.push_back({destination}); + return payload; +} + const std::vector<std::string> t_blocks = { "0100d5adc49a053b8818b2b6023cd2d532c6774e164a8fcacd603651cb3ea0cb7f9340b28ec016b4bc4ca301aa0101ff6e08acbb2702eab03067870349139bee7eab2ca2e030a6bb73d4f68ab6a3b6ca937214054cdac0843d028bbe23b57ea9bae53f12da93bb57bf8a2e40598d9fccd10c2921576e987d93cd80b4891302468738e391f07c4f2b356f7957160968e0bfef6e907c3cee2d8c23cbf04b089680c6868f01025a0f41f063e195a966051e3a29e17130a9ce97d48f55285b9bb04bdd55a09ae78088aca3cf0202d0f26169290450fe17e08974789c3458910b4db18361cdc564f8f2d0bdd2cf568090cad2c60e02d6f3483ec45505cc3be841046c7a12bf953ac973939bc7b727e54258e1881d4d80e08d84ddcb0102dae6dfb16d3e28aaaf43e00170b90606b36f35f38f8a3dceb5ee18199dd8f17c80c0caf384a30202385d7e57a4daba4cdd9e550a92dcc188838386e7581f13f09de796cbed4716a42101c052492a077abf41996b50c1b2e67fd7288bcd8c55cdc657b4e22d0804371f6901beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd" @@ -452,6 +477,58 @@ TYPED_TEST(BlockchainDBTest, AssetOwnershipResolvesAuthoritativeRingAndSpentStat *this->m_db, proof, TESTNET, carrier, &error)); } +TYPED_TEST(BlockchainDBTest, AssetTransactionStateAppliesAtomicallyAndRejectsReplay) +{ + const boost::filesystem::path temp_path = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); + const std::string dir_path = temp_path.string(); + this->set_prefix(dir_path); + ASSERT_NO_THROW(this->m_db->open(dir_path)); + this->get_filenames(); + + crypto::hash carrier{}; + carrier.data[0] = 0xd1; + const auto payload = make_db_asset_issuance(carrier); + std::vector<crypto::hash> output_ids; + std::string error; + this->m_db->block_wtxn_start(); + ASSERT_TRUE(assets::apply_asset_transaction_to_db(*this->m_db, payload, + TESTNET, carrier, 50, output_ids, &error)) << error; + this->m_db->block_wtxn_stop(); + ASSERT_EQ(1u, output_ids.size()); + + crypto::hash asset_id{}; + ASSERT_TRUE(assets::derive_asset_id(payload.issuance->descriptor, asset_id)); + uint64_t issuance_height = 0; + blobdata issuance_bytes; + ASSERT_TRUE(this->m_db->get_asset_record(asset_id, issuance_height, issuance_bytes)); + ASSERT_EQ(50u, issuance_height); + asset_output_data_t stored{}; + ASSERT_TRUE(this->m_db->get_asset_output(output_ids.front(), stored)); + ASSERT_EQ(asset_id, stored.asset_id); + ASSERT_EQ(50u, stored.height); + + std::vector<crypto::hash> replay_outputs; + this->m_db->block_wtxn_start(); + EXPECT_FALSE(assets::apply_asset_transaction_to_db(*this->m_db, payload, + TESTNET, carrier, 51, replay_outputs, &error)); + this->m_db->block_wtxn_abort(); + ASSERT_TRUE(replay_outputs.empty()); + + auto invalid = make_db_asset_issuance(crypto::hash{}); + crypto::hash invalid_carrier{}; + invalid_carrier.data[0] = 0xd2; + invalid.carrier_prefix_hash = invalid_carrier; + invalid.balances.front().outputs.front() = rct::commit( + invalid.issuance->descriptor.atomic_supply + 1, rct::zero()); + this->m_db->block_wtxn_start(); + EXPECT_FALSE(assets::apply_asset_transaction_to_db(*this->m_db, invalid, + TESTNET, invalid_carrier, 52, replay_outputs, &error)); + this->m_db->block_wtxn_abort(); + crypto::hash invalid_id{}; + ASSERT_TRUE(assets::derive_asset_id(invalid.issuance->descriptor, invalid_id)); + ASSERT_FALSE(this->m_db->get_asset_record(invalid_id, issuance_height, issuance_bytes)); +} + TYPED_TEST(BlockchainDBTest, AssetBlockExtensionsRebuildFromPersistentState) { const boost::filesystem::path temp_path = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path(); |
