Skip to content

Commit 9024327

Browse files
committed
use C++ style helper in test file
1 parent 65eaa4c commit 9024327

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

tests/test_mpt_utility.cpp

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#include "test_utils.h"
22

3-
#include <cstring>
4-
#include <iomanip>
3+
#include <algorithm>
54
#include <iostream>
5+
#include <vector>
66
#include <secp256k1_mpt.h>
7-
#include <sstream>
8-
#include <string>
97
#include <utility/mpt_utility.h>
108

9+
// helper to create mock accounts and issuance IDs
10+
template <typename T>
11+
T create_mock_id(uint8_t fill) {
12+
T mock;
13+
std::fill(std::begin(mock.bytes), std::end(mock.bytes), fill);
14+
return mock;
15+
}
16+
1117
void
1218
test_encryption_decryption()
1319
{
@@ -39,10 +45,9 @@ test_encryption_decryption()
3945
void
4046
test_mpt_confidential_convert()
4147
{
42-
account_id acc;
43-
std::memset(acc.bytes, 0xAA, kMPT_ACCOUNT_ID_SIZE);
44-
mpt_issuance_id issuance;
45-
std::memset(issuance.bytes, 0xBB, kMPT_ISSUANCE_ID_SIZE);
48+
// Setup mock account, issuance and transaction details
49+
account_id acc = create_mock_id<account_id>(0xAA);
50+
mpt_issuance_id issuance = create_mock_id<mpt_issuance_id>(0xBB);
4651
uint32_t seq = 12345;
4752
uint64_t convert_amount = 750;
4853

@@ -76,15 +81,10 @@ test_mpt_confidential_convert()
7681
void
7782
test_mpt_confidential_send()
7883
{
79-
// Mock accounts and mpt issuance
80-
account_id sender_acc, dest_acc;
81-
std::memset(sender_acc.bytes, 0x11, kMPT_ACCOUNT_ID_SIZE);
82-
std::memset(dest_acc.bytes, 0x22, kMPT_ACCOUNT_ID_SIZE);
83-
84-
mpt_issuance_id issuance;
85-
std::memset(issuance.bytes, 0xBB, kMPT_ISSUANCE_ID_SIZE);
86-
87-
// Mock transaction details
84+
// Setup mock account, issuance and transaction details
85+
account_id sender_acc = create_mock_id<account_id>(0x11);
86+
account_id dest_acc = create_mock_id<account_id>(0x22);
87+
mpt_issuance_id issuance = create_mock_id<mpt_issuance_id>(0xBB);
8888
uint32_t seq = 54321;
8989
uint64_t amount_to_send = 100;
9090
uint64_t prev_balance = 2000;
@@ -115,8 +115,8 @@ test_mpt_confidential_send()
115115
std::vector<mpt_confidential_recipient> recipients;
116116
auto add_recipient = [&](uint8_t* p, uint8_t* c) {
117117
mpt_confidential_recipient r;
118-
std::memcpy(r.pubkey, p, kMPT_PUBKEY_SIZE);
119-
std::memcpy(r.encrypted_amount, c, kMPT_ELGAMAL_TOTAL_SIZE);
118+
std::copy(p, p + kMPT_PUBKEY_SIZE, r.pubkey);
119+
std::copy(c, c + kMPT_ELGAMAL_TOTAL_SIZE, r.encrypted_amount);
120120
recipients.push_back(r);
121121
};
122122
add_recipient(sender_pub, sender_ct);
@@ -143,20 +143,20 @@ test_mpt_confidential_send()
143143
// Prepare pedersen proof params for both amount and balance linkage proofs
144144
mpt_pedersen_proof_params amt_params;
145145
amt_params.amount = amount_to_send;
146-
std::memcpy(amt_params.blinding_factor, amount_bf, kMPT_BLINDING_FACTOR_SIZE);
147-
std::memcpy(amt_params.pedersen_commitment, amount_comm, kMPT_PEDERSEN_COMMIT_SIZE);
148-
std::memcpy(amt_params.encrypted_amount, sender_ct, kMPT_ELGAMAL_TOTAL_SIZE);
146+
std::copy(amount_bf, amount_bf + kMPT_BLINDING_FACTOR_SIZE, amt_params.blinding_factor);
147+
std::copy(amount_comm, amount_comm + kMPT_PEDERSEN_COMMIT_SIZE, amt_params.pedersen_commitment);
148+
std::copy(sender_ct, sender_ct + kMPT_ELGAMAL_TOTAL_SIZE, amt_params.encrypted_amount);
149149

150150
mpt_pedersen_proof_params bal_params;
151151
bal_params.amount = prev_balance;
152-
std::memcpy(bal_params.blinding_factor, balance_bf, kMPT_BLINDING_FACTOR_SIZE);
153-
std::memcpy(bal_params.pedersen_commitment, balance_comm, kMPT_PEDERSEN_COMMIT_SIZE);
152+
std::copy(balance_bf, balance_bf + kMPT_BLINDING_FACTOR_SIZE, bal_params.blinding_factor);
153+
std::copy(balance_comm, balance_comm + kMPT_PEDERSEN_COMMIT_SIZE, bal_params.pedersen_commitment);
154154

155155
uint8_t prev_bal_bf[kMPT_BLINDING_FACTOR_SIZE];
156156
uint8_t prev_bal_ct[kMPT_ELGAMAL_TOTAL_SIZE];
157157
EXPECT(mpt_generate_blinding_factor(prev_bal_bf) == 0);
158158
EXPECT(mpt_encrypt_amount(prev_balance, sender_pub, prev_bal_bf, prev_bal_ct) == 0);
159-
std::memcpy(bal_params.encrypted_amount, prev_bal_ct, kMPT_ELGAMAL_TOTAL_SIZE);
159+
std::copy(prev_bal_ct, prev_bal_ct + kMPT_ELGAMAL_TOTAL_SIZE, bal_params.encrypted_amount);
160160

161161
// Generate the confidential send proof
162162
size_t proof_len = get_confidential_send_proof_size(recipients.size());
@@ -244,12 +244,8 @@ void
244244
test_mpt_convert_back()
245245
{
246246
// Setup mock account, issuance and transaction details
247-
account_id acc;
248-
std::memset(acc.bytes, 0x55, kMPT_ACCOUNT_ID_SIZE);
249-
250-
mpt_issuance_id issuance;
251-
std::memset(issuance.bytes, 0xEE, kMPT_ISSUANCE_ID_SIZE);
252-
247+
account_id acc = create_mock_id<account_id>(0x55);
248+
mpt_issuance_id issuance = create_mock_id<mpt_issuance_id>(0xEE);
253249
uint32_t seq = 98765;
254250
uint64_t current_balance = 5000;
255251
uint64_t amount_to_convert_back = 1000;
@@ -280,9 +276,9 @@ test_mpt_convert_back()
280276
// Prepare pedersen proof params
281277
mpt_pedersen_proof_params pc_params;
282278
pc_params.amount = current_balance;
283-
std::memcpy(pc_params.blinding_factor, pcm_bf, kMPT_BLINDING_FACTOR_SIZE);
284-
std::memcpy(pc_params.pedersen_commitment, pcm_comm, kMPT_PEDERSEN_COMMIT_SIZE);
285-
std::memcpy(pc_params.encrypted_amount, spending_bal_ct, kMPT_ELGAMAL_TOTAL_SIZE);
279+
std::copy(pcm_bf, pcm_bf + kMPT_BLINDING_FACTOR_SIZE, pc_params.blinding_factor);
280+
std::copy(pcm_comm, pcm_comm + kMPT_PEDERSEN_COMMIT_SIZE, pc_params.pedersen_commitment);
281+
std::copy(spending_bal_ct, spending_bal_ct + kMPT_ELGAMAL_TOTAL_SIZE, pc_params.encrypted_amount);
286282

287283
// Generate proof
288284
uint8_t proof[kMPT_PEDERSEN_LINK_SIZE];
@@ -309,14 +305,9 @@ void
309305
test_mpt_clawback()
310306
{
311307
// Setup mock account, issuance and transaction details
312-
account_id issuer_acc;
313-
std::memset(issuer_acc.bytes, 0x11, kMPT_ACCOUNT_ID_SIZE);
314-
315-
account_id holder_acc;
316-
std::memset(holder_acc.bytes, 0x22, kMPT_ACCOUNT_ID_SIZE);
317-
318-
mpt_issuance_id issuance;
319-
std::memset(issuance.bytes, 0xCC, kMPT_ISSUANCE_ID_SIZE);
308+
account_id issuer_acc = create_mock_id<account_id>(0x11);
309+
account_id holder_acc = create_mock_id<account_id>(0x22);
310+
mpt_issuance_id issuance = create_mock_id<mpt_issuance_id>(0xCC);
320311

321312
uint32_t seq = 200;
322313
uint64_t claw_amount = 500;

0 commit comments

Comments
 (0)