Files
wownero/tests/unit_tests/carrot_fcmp.cpp
+3 a9145f9d38 FCMP++/Carrot mega commit [9/14/26]
Co-authored-by: jeffro256 <jeffro256@tutanota.com>
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
Co-authored-by: Lee *!* Clagett <code@leeclagett.com>
Co-authored-by: SyntheticBird45 <someoneelse.is_on.github.rio7x@simplelogin.com>
Co-authored-by: jeetrex17 <jeetrex@tutamail.com>
Co-authored-by: UkoeHB <ukoehb@users.noreply.github.com>
Co-authored-by: MrCyjaneK
Co-authored-by: tobtoht
Co-authored-by: selsta
Co-authored-by: Thomas <thomas.giudici@proton.me>
Co-authored-by: navidR
Co-authored-by: Boog900 <boog900@tutanota.com>
2026-09-17 18:33:24 -07:00

650 lines
29 KiB
C++

// Copyright (c) 2025-2026, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "carrot_core/config.h"
#include "carrot_core/payment_proposal.h"
#include "carrot_impl/format_utils.h"
#include "carrot_impl/tx_builder_inputs.h"
#include "carrot_impl/tx_builder_outputs.h"
#include "carrot_impl/tx_proposal_utils.h"
#include "carrot_mock_helpers.h"
#include "crypto/crypto.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "cryptonote_core/blockchain.h"
#include "curve_trees.h"
#include "fcmp_pp/prove.h"
#include "ringct/bulletproofs_plus.h"
#include "ringct/rctOps.h"
#include "ringct/rctSigs.h"
#include <variant>
using namespace carrot;
namespace
{
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
static constexpr rct::xmr_amount MAX_AMOUNT_FCMP_PP = MONEY_SUPPLY /
(FCMP_PLUS_PLUS_MAX_INPUTS + FCMP_PLUS_PLUS_MAX_OUTPUTS + 1);
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
using CarrotEnoteVariant = std::variant<CarrotCoinbaseEnoteV1, CarrotEnoteV1>;
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
struct CarrotUnifiedOutputsAndKeys
{
std::vector<CarrotEnoteVariant> enotes;
std::vector<encrypted_payment_id_t> encrypted_payment_ids;
std::vector<fcmp_pp::UnifiedOutput> output_pairs;
};
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
static const CarrotUnifiedOutputsAndKeys generate_random_carrot_outputs(
const mock::mock_carrot_and_legacy_keys &keys,
const std::size_t old_n_leaf_tuples,
const std::size_t new_n_leaf_tuples)
{
CarrotUnifiedOutputsAndKeys outs;
outs.enotes.reserve(new_n_leaf_tuples);
outs.encrypted_payment_ids.reserve(new_n_leaf_tuples);
outs.output_pairs.reserve(new_n_leaf_tuples);
for (std::size_t i = 0; i < new_n_leaf_tuples; ++i)
{
const std::uint64_t unified_id = old_n_leaf_tuples + i;
fcmp_pp::UnifiedOutput output_pair{
.unified_id = unified_id
};
CarrotPaymentProposalV1 normal_payment_proposal{
.destination = keys.cryptonote_address(),
.amount = rct::randXmrAmount(MAX_AMOUNT_FCMP_PP),
.randomness = gen_janus_anchor()
};
CarrotPaymentProposalSelfSendV1 selfsend_payment_proposal{
.destination_address_spend_pubkey = keys.cryptonote_address().address_spend_pubkey,
.is_subaddress = false,
.amount = rct::randXmrAmount(MAX_AMOUNT_FCMP_PP),
.enote_type = i % 2 ? CarrotEnoteType::CHANGE : CarrotEnoteType::PAYMENT,
.enote_ephemeral_privkey = mock::gen_secret_key()
};
bool push_coinbase = false;
CarrotCoinbaseEnoteV1 coinbase_enote;
RCTOutputEnoteProposal rct_output_enote_proposal;
encrypted_payment_id_t encrypted_payment_id;
const unsigned int enote_derive_type = i % 7;
switch (enote_derive_type)
{
case 0: // coinbase enote
get_coinbase_enote_v1(normal_payment_proposal,
mock::gen_block_index(),
coinbase_enote);
push_coinbase = true;
break;
case 1: // normal enote main address
get_output_proposal_normal_v1(normal_payment_proposal,
mock::gen_key_image(),
rct_output_enote_proposal,
encrypted_payment_id);
break;
case 2: // normal enote subaddress
normal_payment_proposal.destination = keys.subaddress({mock::gen_subaddress_index()});
get_output_proposal_normal_v1(normal_payment_proposal,
mock::gen_key_image(),
rct_output_enote_proposal,
encrypted_payment_id);
break;
case 3: // special enote main address
get_output_proposal_special_v1(selfsend_payment_proposal,
keys.k_view_incoming_dev,
mock::gen_key_image(),
nullptr,
nullptr,
rct_output_enote_proposal);
break;
case 4: // special enote subaddress
selfsend_payment_proposal.destination_address_spend_pubkey
= keys.subaddress(mock::gen_subaddress_index_extended()).address_spend_pubkey;
selfsend_payment_proposal.is_subaddress = false;
get_output_proposal_special_v1(selfsend_payment_proposal,
keys.k_view_incoming_dev,
mock::gen_key_image(),
nullptr,
nullptr,
rct_output_enote_proposal);
break;
case 5: // internal main address
get_output_proposal_internal_v1(selfsend_payment_proposal,
keys.s_view_balance_dev,
mock::gen_key_image(),
nullptr,
nullptr,
rct_output_enote_proposal);
break;
case 6: // internal subaddress
selfsend_payment_proposal.destination_address_spend_pubkey
= keys.subaddress(mock::gen_subaddress_index_extended()).address_spend_pubkey;
selfsend_payment_proposal.is_subaddress = false;
get_output_proposal_internal_v1(selfsend_payment_proposal,
keys.s_view_balance_dev,
mock::gen_key_image(),
nullptr,
nullptr,
rct_output_enote_proposal);
break;
}
crypto::public_key O;
carrot::amount_commitment_t C;
if (push_coinbase)
{
O = coinbase_enote.onetime_address;
static_cast<crypto::ec_point&>(C) = rct::rct2pt(rct::zeroCommitVartime(coinbase_enote.amount));
outs.enotes.push_back(coinbase_enote);
outs.encrypted_payment_ids.emplace_back();
}
else
{
O = rct_output_enote_proposal.enote.onetime_address;
C = rct_output_enote_proposal.enote.amount_commitment;
outs.enotes.push_back(rct_output_enote_proposal.enote);
}
output_pair.output_pair = fcmp_pp::CarrotOutputPairV1{{O, C}};
outs.encrypted_payment_ids.push_back(encrypted_payment_id);
outs.output_pairs.push_back(output_pair);
}
return outs;
}
} //anonymous namespace
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
TEST(carrot_fcmp, receive_scan_spend_and_verify_serialized_carrot_tx)
{
// In this test we:
// 1. Populate a curve tree with Carrot-derived enotes to Alice
// 2. Scan those enotes and construct a transfer-style tx to Bob
// 3. Serialize that tx, then deserialize it
// 4. Verify non-input consensus rules on the deserialized tx
// 5. Verify FCMP membership in the curve tree on the deserialized tx
// 6. Scan the deserialized tx to Bob
mock::mock_carrot_and_legacy_keys alice;
mock::mock_carrot_and_legacy_keys bob;
alice.generate();
bob.generate();
const size_t n_inputs = crypto::rand_range<size_t>(CARROT_MIN_TX_INPUTS, FCMP_PLUS_PLUS_MAX_INPUTS);
const size_t n_outputs = crypto::rand_range<size_t>(CARROT_MIN_TX_OUTPUTS, FCMP_PLUS_PLUS_MAX_OUTPUTS);
const std::size_t selene_chunk_width = fcmp_pp::curve_trees::SELENE_CHUNK_WIDTH;
const std::size_t helios_chunk_width = fcmp_pp::curve_trees::HELIOS_CHUNK_WIDTH;
const std::size_t tree_depth = 3;
const std::size_t n_tree_layers = tree_depth + 1;
const size_t expected_num_selene_branch_blinds = (tree_depth + 1) / 2;
const size_t expected_num_helios_branch_blinds = tree_depth / 2;
LOG_PRINT_L1("Test carrot_impl.receive_scan_spend_and_verify_serialized_carrot_tx with selene chunk width "
<< selene_chunk_width << ", helios chunk width " << helios_chunk_width << ", tree depth " << tree_depth
<< ", number of inputs " << n_inputs << ", number of outputs " << n_outputs);
// Tree params
uint64_t min_leaves_needed_for_tree_depth = 0;
const auto curve_trees = test::init_curve_trees_test(selene_chunk_width,
helios_chunk_width,
tree_depth,
min_leaves_needed_for_tree_depth);
// Generate enotes...
LOG_PRINT_L1("Generating carrot-derived enotes to Alice");
const auto new_outputs = generate_random_carrot_outputs(alice,
0,
min_leaves_needed_for_tree_depth
);
ASSERT_GT(min_leaves_needed_for_tree_depth, n_inputs);
// generate output ids to use as inputs...
std::set<size_t> picked_output_ids_set;
while (picked_output_ids_set.size() < n_inputs)
picked_output_ids_set.insert(crypto::rand_idx(min_leaves_needed_for_tree_depth));
std::vector<size_t> picked_output_ids(picked_output_ids_set.cbegin(), picked_output_ids_set.cend());
std::shuffle(picked_output_ids.begin(), picked_output_ids.end(), crypto::random_device{});
// scan inputs and make key images and opening hints...
// a z opening hint output id
using input_info_t = std::tuple<rct::xmr_amount, rct::key, OutputOpeningHintVariant, std::uint64_t>;
LOG_PRINT_L1("Alice scanning inputs");
std::unordered_map<crypto::key_image, input_info_t> input_info_by_ki;
rct::xmr_amount input_amount_sum = 0;
for (const size_t picked_output_id : picked_output_ids)
{
// find index into new_outputs based on picked_output_id
size_t new_outputs_idx;
for (new_outputs_idx = 0; new_outputs_idx < new_outputs.output_pairs.size(); ++new_outputs_idx)
{
if (new_outputs.output_pairs.at(new_outputs_idx).unified_id == picked_output_id)
break;
}
ASSERT_LT(new_outputs_idx, new_outputs.enotes.size());
// compile information about this enote
const CarrotEnoteVariant &enote_v = new_outputs.enotes.at(new_outputs_idx);
OutputOpeningHintVariant opening_hint;
std::vector<mock::mock_scan_result_t> scan_results;
if (std::holds_alternative<CarrotEnoteV1>(enote_v))
{
const CarrotEnoteV1 &enote = std::get<CarrotEnoteV1>(enote_v);
const encrypted_payment_id_t encrypted_payment_id = new_outputs.encrypted_payment_ids.at(new_outputs_idx);
mock::mock_scan_enote_set({enote},
encrypted_payment_id,
alice,
scan_results);
ASSERT_EQ(1, scan_results.size());
const mock::mock_scan_result_t &scan_result = scan_results.front();
const auto subaddr_it = alice.subaddress_map.get_index_for_address_spend_pubkey(scan_result.address_spend_pubkey);
ASSERT_TRUE(subaddr_it);
opening_hint = CarrotOutputOpeningHintV1{
.source_enote = enote,
.encrypted_payment_id = encrypted_payment_id,
.subaddr_index = *subaddr_it
};
}
else // is coinbase
{
const CarrotCoinbaseEnoteV1 &enote = std::get<CarrotCoinbaseEnoteV1>(enote_v);
mock::mock_scan_coinbase_enote_set({enote},
alice,
scan_results);
ASSERT_EQ(1, scan_results.size());
const mock::mock_scan_result_t &scan_result = scan_results.front();
ASSERT_EQ(alice.cryptonote_address().address_spend_pubkey, scan_result.address_spend_pubkey);
opening_hint = CarrotCoinbaseOutputOpeningHintV1{
.source_enote = enote,
.derive_type = AddressDeriveType::Carrot
};
}
ASSERT_EQ(1, scan_results.size());
const mock::mock_scan_result_t &scan_result = scan_results.front();
const auto &output_pair = new_outputs.output_pairs.at(new_outputs_idx).output_pair;
const crypto::key_image ki = alice.derive_key_image(scan_result.address_spend_pubkey,
scan_result.sender_extension_g,
scan_result.sender_extension_t,
output_pubkey_cref(output_pair),
use_biased_hash_to_point(opening_hint));
const crypto::key_image ki2 = alice.key_image_dev->derive_key_image(opening_hint);
ASSERT_EQ(ki, ki2);
ASSERT_EQ(0, input_info_by_ki.count(ki));
input_info_by_ki[ki] = {scan_result.amount,
rct::sk2rct(scan_result.amount_blinding_factor),
opening_hint,
new_outputs.output_pairs.at(new_outputs_idx).unified_id};
input_amount_sum += scan_result.amount;
}
// generate n_outputs-1 payment proposals to bob ...
LOG_PRINT_L1("Generating payment proposals to Bob");
rct::xmr_amount output_amount_remaining = rct::randXmrAmount(input_amount_sum);
std::vector<CarrotPaymentProposalV1> bob_payment_proposals;
for (size_t i = 0; i < n_outputs - 1; ++i)
{
const bool use_subaddress = i % 2 == 1;
const CarrotDestinationV1 addr = use_subaddress ?
bob.subaddress({mock::gen_subaddress_index()}) :
bob.cryptonote_address();
const rct::xmr_amount amount = rct::randXmrAmount(output_amount_remaining);
bob_payment_proposals.push_back(CarrotPaymentProposalV1{
.destination = addr,
.amount = amount,
.randomness = gen_janus_anchor()
});
output_amount_remaining -= amount;
}
// make a transfer-type tx proposal
// @TODO: this can fail sporadically if fee exceeds remaining funds
LOG_PRINT_L1("Creating transaction proposal");
const rct::xmr_amount fee_per_weight = 1;
CarrotTransactionProposalV1 tx_proposal;
make_carrot_transaction_proposal_v1_transfer(bob_payment_proposals,
/*selfsend_payment_proposals=*/{},
fee_per_weight,
/*extra=*/{},
[&input_info_by_ki]
(
const boost::multiprecision::int128_t&,
const std::map<std::size_t, rct::xmr_amount>&,
const std::size_t,
const std::size_t,
std::vector<CarrotSelectedInput>& selected_inputs_out)
{
selected_inputs_out.clear();
selected_inputs_out.reserve(input_info_by_ki.size());
for (const auto &info : input_info_by_ki)
{
selected_inputs_out.push_back(CarrotSelectedInput{
.amount = std::get<0>(info.second),
.input = std::get<2>(info.second)
});
}
},
alice.carrot_account_spend_pubkey,
{{0, 0}, AddressDeriveType::Carrot},
{},
{},
tx_proposal);
ASSERT_EQ(n_outputs, tx_proposal.normal_payment_proposals.size() + tx_proposal.selfsend_payment_proposals.size());
// derive input key images
std::vector<crypto::key_image> sorted_input_key_images;
carrot::get_sorted_input_key_images_from_proposal_v1(tx_proposal,
*alice.key_image_dev,
sorted_input_key_images);
// derive output enote set
LOG_PRINT_L1("Deriving enotes");
std::vector<RCTOutputEnoteProposal> output_enote_proposals;
encrypted_payment_id_t encrypted_payment_id;
get_output_enote_proposals_from_proposal_v1(tx_proposal,
&alice.s_view_balance_dev,
&alice.k_view_incoming_dev,
sorted_input_key_images.at(0),
output_enote_proposals,
encrypted_payment_id);
// Collect balance info and enotes
std::vector<rct::xmr_amount> output_amounts;
std::vector<rct::key> output_amount_blinding_factors;
std::vector<CarrotEnoteV1> output_enotes;
for (const RCTOutputEnoteProposal &output_enote_proposal : output_enote_proposals)
{
output_amounts.push_back(output_enote_proposal.amount);
output_amount_blinding_factors.push_back(rct::sk2rct(output_enote_proposal.amount_blinding_factor));
output_enotes.push_back(output_enote_proposal.enote);
}
// make pruned tx
LOG_PRINT_L1("Storing carrot to transaction");
cryptonote::transaction tx = store_carrot_to_transaction_v1(output_enotes,
sorted_input_key_images,
tx_proposal.fee,
encrypted_payment_id);
ASSERT_EQ(2, tx.version);
ASSERT_EQ(0, tx.unlock_time);
ASSERT_EQ(n_inputs, tx.vin.size());
ASSERT_EQ(n_outputs, tx.vout.size());
ASSERT_EQ(n_outputs, tx.rct_signatures.outPk.size());
// Generate bulletproof+
LOG_PRINT_L1("Generating Bulletproof+");
rct::BulletproofPlus bpp = rct::bulletproof_plus_PROVE(output_amounts, output_amount_blinding_factors);
ASSERT_EQ(n_outputs, bpp.V.size());
// expand tx and calculate signable tx hash
LOG_PRINT_L1("Calculating signable tx hash");
hw::device &hwdev = hw::get_device("default");
ASSERT_TRUE(cryptonote::expand_transaction_1(tx, /*base_only=*/true));
const crypto::hash tx_prefix_hash = cryptonote::get_transaction_prefix_hash(tx);
tx.rct_signatures.message = rct::hash2rct(tx_prefix_hash);
tx.rct_signatures.p.pseudoOuts.resize(n_inputs); // @TODO: make this not necessary to call get_mlsag_hash
const crypto::hash signable_tx_hash = rct::rct2hash(rct::get_pre_mlsag_hash(tx.rct_signatures, hwdev));
// collect opening hits and rerandomize inputs
LOG_PRINT_L1("Making rerandomized inputs");
std::vector<OutputOpeningHintVariant> sorted_opening_hints;
sorted_opening_hints.reserve(n_inputs);
for (const crypto::key_image &ki : sorted_input_key_images)
sorted_opening_hints.push_back(std::get<2>(input_info_by_ki.at(ki)));
std::vector<FcmpRerandomizedOutputCompressed> rerandomized_outputs = generate_rerandomized_inputs_nonrefundable(
epee::to_span(output_enote_proposals),
epee::to_span(sorted_opening_hints),
*alice.addr_dev,
&alice.s_view_balance_dev,
alice.k_view_incoming_dev);
ASSERT_EQ(n_inputs, rerandomized_outputs.size());
// Make SA/L proofs
LOG_PRINT_L1("Generating FCMP++ SA/L proofs");
std::vector<crypto::key_image> actual_key_images;
std::vector<fcmp_pp::FcmpPpSalProof> sal_proofs;
for (size_t i = 0; i < n_inputs; ++i)
{
make_sal_proof_any_to_carrot_v1(signable_tx_hash,
rerandomized_outputs.at(i),
std::get<2>(input_info_by_ki.at(sorted_input_key_images.at(i))),
alice.k_prove_spend,
alice.k_generate_image,
*alice.addr_dev,
alice.s_view_balance_dev,
alice.k_view_incoming_dev,
sal_proofs.emplace_back(),
actual_key_images.emplace_back());
}
// Init tree in memory
LOG_PRINT_L1("Initializing tree with " << min_leaves_needed_for_tree_depth << " leaves");
CurveTreesGlobalTree global_tree(*curve_trees);
ASSERT_TRUE(global_tree.grow_tree(0, min_leaves_needed_for_tree_depth, new_outputs.output_pairs));
LOG_PRINT_L1("Finished initializing tree with " << min_leaves_needed_for_tree_depth << " leaves");
// Make FCMP paths
LOG_PRINT_L1("Calculating FCMP paths");
std::vector<fcmp_pp::ProofInput> fcmp_proof_inputs(n_inputs);
for (size_t i = 0; i < n_inputs; ++i)
{
const size_t leaf_idx = std::get<3>(input_info_by_ki.at(sorted_input_key_images.at(i)));
const auto path = global_tree.get_path_at_leaf_idx(leaf_idx);
const auto &opening_hint = std::get<2>(input_info_by_ki.at(sorted_input_key_images.at(i)));
const auto output_pair = to_output_pair(opening_hint);
const auto output_tuple = fcmp_pp::curve_trees::output_to_tuple(output_pair);
const auto path_for_proof = curve_trees->path_for_proof(path, output_tuple);
const auto helios_scalar_chunks = fcmp_pp::tower_cycle::scalar_chunks_to_chunk_vector<fcmp_pp::HeliosT>(
path_for_proof.c2_scalar_chunks);
const auto selene_scalar_chunks = fcmp_pp::tower_cycle::scalar_chunks_to_chunk_vector<fcmp_pp::SeleneT>(
path_for_proof.c1_scalar_chunks);
auto path_rust = fcmp_pp::path_new({path_for_proof.leaves.data(), path_for_proof.leaves.size()},
path_for_proof.output_idx,
{helios_scalar_chunks.data(), helios_scalar_chunks.size()},
{selene_scalar_chunks.data(), selene_scalar_chunks.size()});
fcmp_proof_inputs[i].path = std::move(path_rust);
}
// make FCMP blinds
LOG_PRINT_L1("Calculating branch and output blinds");
for (size_t i = 0; i < n_inputs; ++i)
{
fcmp_pp::ProofInput &proof_input = fcmp_proof_inputs[i];
const FcmpRerandomizedOutputCompressed &rerandomized_output = rerandomized_outputs.at(i);
// calculate individual blinds
auto blinded_o_blind = fcmp_pp::blind_o_blind(fcmp_pp::o_blind(rerandomized_output));
auto blinded_i_blind = fcmp_pp::blind_i_blind(fcmp_pp::i_blind(rerandomized_output));
auto blinded_i_blind_blind = fcmp_pp::blind_i_blind_blind(fcmp_pp::i_blind_blind(rerandomized_output));
auto blinded_c_blind = fcmp_pp::blind_c_blind(fcmp_pp::c_blind(rerandomized_output));
// make output blinds
proof_input.output_blinds = fcmp_pp::output_blinds_new(blinded_o_blind,
blinded_i_blind,
blinded_i_blind_blind,
blinded_c_blind);
// generate selene blinds
proof_input.selene_branch_blinds.reserve(expected_num_selene_branch_blinds);
for (size_t j = 0; j < expected_num_selene_branch_blinds; ++j)
proof_input.selene_branch_blinds.push_back(fcmp_pp::gen_selene_branch_blind());
// generate helios blinds
proof_input.helios_branch_blinds.reserve(expected_num_helios_branch_blinds);
for (size_t j = 0; j < expected_num_helios_branch_blinds; ++j)
proof_input.helios_branch_blinds.push_back(fcmp_pp::gen_helios_branch_blind());
}
// Make FCMP membership proof
LOG_PRINT_L1("Generating FCMP++ membership proofs");
std::vector<fcmp_pp::FcmpPpProveMembershipInput> fcmp_proof_inputs_rust;
for (size_t i = 0; i < n_inputs; ++i)
{
fcmp_pp::ProofInput &proof_input = fcmp_proof_inputs.at(i);
fcmp_proof_inputs_rust.push_back(fcmp_pp::fcmp_pp_prove_input_new(
proof_input.path,
proof_input.output_blinds,
proof_input.selene_branch_blinds,
proof_input.helios_branch_blinds));
}
const fcmp_pp::FcmpMembershipProof membership_proof = fcmp_pp::prove_membership(fcmp_proof_inputs_rust,
n_tree_layers);
// Attach rctSigPrunable to tx
LOG_PRINT_L1("Storing rctSig prunable");
const std::uint64_t fcmp_block_reference_index = mock::gen_block_index();
tx.rct_signatures.p = store_fcmp_proofs_to_rct_prunable_v1(std::move(bpp),
rerandomized_outputs,
sal_proofs,
membership_proof,
fcmp_block_reference_index,
n_tree_layers);
tx.pruned = false;
// Serialize tx to bytes
LOG_PRINT_L1("Serializing & deserializing transaction");
const cryptonote::blobdata tx_blob = cryptonote::tx_to_blob(tx);
// Deserialize tx
cryptonote::transaction deserialized_tx;
ASSERT_TRUE(cryptonote::parse_and_validate_tx_from_blob(tx_blob, deserialized_tx));
// Expand tx
auto tree_root = global_tree.get_tree_root();
const crypto::hash tx_prefix_hash_2 = cryptonote::get_transaction_prefix_hash(deserialized_tx);
ASSERT_TRUE(cryptonote::Blockchain::expand_transaction_2(deserialized_tx, tx_prefix_hash_2, {}, tree_root));
// Verify non-input consensus rules on tx
LOG_PRINT_L1("Verifying non-input consensus rules");
cryptonote::tx_verification_context tvc{};
const bool r = cryptonote::ver_non_input_consensus(deserialized_tx, tvc, HF_VERSION_FCMP_PLUS_PLUS);
ASSERT_FALSE(tvc.m_added_to_pool);
ASSERT_FALSE(tvc.m_low_mixin);
ASSERT_FALSE(tvc.m_double_spend);
ASSERT_FALSE(tvc.m_invalid_input);
ASSERT_FALSE(tvc.m_invalid_output);
ASSERT_FALSE(tvc.m_too_big);
ASSERT_FALSE(tvc.m_overspend);
ASSERT_FALSE(tvc.m_fee_too_low);
ASSERT_FALSE(tvc.m_too_few_outputs);
ASSERT_FALSE(tvc.m_tx_extra_too_big);
ASSERT_FALSE(tvc.m_nonzero_unlock_time);
ASSERT_TRUE(r);
ASSERT_FALSE(tvc.m_verifivation_failed);
ASSERT_FALSE(tvc.m_verifivation_impossible);
// Recalculate signable tx hash from deserialized tx and check
const crypto::hash signable_tx_hash_2 = rct::rct2hash(rct::get_pre_mlsag_hash(deserialized_tx.rct_signatures, hwdev));
ASSERT_EQ(signable_tx_hash, signable_tx_hash_2);
// Pre-verify SAL proofs
LOG_PRINT_L1("Verify SA/L proofs");
ASSERT_EQ(deserialized_tx.vin.size(), n_inputs);
ASSERT_EQ(deserialized_tx.vin.size(), deserialized_tx.rct_signatures.p.fcmp_ver_helper_data.key_images.size());
ASSERT_EQ(deserialized_tx.vin.size(), deserialized_tx.rct_signatures.p.pseudoOuts.size());
ASSERT_GT(deserialized_tx.rct_signatures.p.fcmp_pp.size(), (3*32 + FCMP_PP_SAL_PROOF_SIZE_V1) * n_inputs);
for (size_t i = 0; i < n_inputs; ++i)
{
const uint8_t * const pbytes = deserialized_tx.rct_signatures.p.fcmp_pp.data() +
(3*32 + FCMP_PP_SAL_PROOF_SIZE_V1) * i;
FcmpInputCompressed input;
fcmp_pp::FcmpPpSalProof sal_proof(FCMP_PP_SAL_PROOF_SIZE_V1);
memcpy(&input, pbytes, 3*32);
memcpy(&sal_proof[0], pbytes + 3*32, FCMP_PP_SAL_PROOF_SIZE_V1);
memcpy(input.C_tilde, deserialized_tx.rct_signatures.p.pseudoOuts.at(i).bytes, 32);
const crypto::key_image &ki = deserialized_tx.rct_signatures.p.fcmp_ver_helper_data.key_images.at(i);
ASSERT_TRUE(fcmp_pp::verify_sal(signable_tx_hash_2, input, ki, sal_proof));
}
// Verify all RingCT non-semantics
LOG_PRINT_L1("Verify RingCT non-semantics consensus rules");
ASSERT_TRUE(rct::verRctNonSemanticsSimple(deserialized_tx.rct_signatures));
// Load carrot from tx
LOG_PRINT_L1("Parsing carrot info from deserialized transaction");
std::vector<CarrotEnoteV1> parsed_enotes;
std::vector<crypto::key_image> parsed_key_images;
rct::xmr_amount parsed_fee;
std::optional<encrypted_payment_id_t> parsed_encrypted_payment_id;
ASSERT_TRUE(try_load_carrot_from_transaction_v1(deserialized_tx,
parsed_enotes,
parsed_key_images,
parsed_fee,
parsed_encrypted_payment_id));
// Bob scan
LOG_PRINT_L1("Bob scanning");
std::vector<mock::mock_scan_result_t> bob_scan_results;
mock::mock_scan_enote_set(parsed_enotes,
parsed_encrypted_payment_id.value_or(encrypted_payment_id_t{}),
bob,
bob_scan_results);
ASSERT_EQ(bob_payment_proposals.size(), bob_scan_results.size());
// Compare bob scan results to bob payment proposals
std::unordered_set<size_t> matched_scan_results;
for (size_t i = 0; i < bob_payment_proposals.size(); ++i)
{
bool matched = false;
for (size_t j = 0; j < bob_scan_results.size(); ++j)
{
if (matched_scan_results.count(j))
continue;
else if (compare_scan_result(bob_scan_results.at(j),
bob_payment_proposals.at(i)))
{
matched = true;
matched_scan_results.insert(j);
break;
}
}
ASSERT_TRUE(matched);
}
}
//----------------------------------------------------------------------------------------------------------------------