forked from monero-project/monero
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialization_demo_utils.cpp
705 lines (652 loc) · 35.2 KB
/
serialization_demo_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// Copyright (c) 2023, 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.
//paired header
#include "serialization_demo_utils.h"
//local headers
#include "common/container_helpers.h"
#include "crypto/crypto.h"
#include "ringct/rctOps.h"
#include "ringct/rctTypes.h"
#include "seraphis_core/binned_reference_set.h"
#include "seraphis_core/discretized_fee.h"
#include "seraphis_core/jamtis_destination.h"
#include "seraphis_crypto/bulletproofs_plus2.h"
#include "seraphis_crypto/grootle.h"
#include "seraphis_crypto/sp_composition_proof.h"
#include "seraphis_impl/serialization_demo_types.h"
#include "seraphis_main/tx_builders_inputs.h"
#include "seraphis_main/tx_component_types.h"
#include "seraphis_main/tx_component_types_legacy.h"
#include "seraphis_main/txtype_coinbase_v1.h"
#include "seraphis_main/txtype_squashed_v1.h"
//third party headers
//standard headers
#include <vector>
#undef MONERO_DEFAULT_LOG_CATEGORY
#define MONERO_DEFAULT_LOG_CATEGORY "seraphis_impl"
namespace sp
{
namespace serialization
{
//-------------------------------------------------------------------------------------------------------------------
// array2 copies array1 by invoking copy_func() on each element
//-------------------------------------------------------------------------------------------------------------------
template <typename CopyFuncT, typename Type1, typename Type2>
static void copy_array(const CopyFuncT ©_func, const std::vector<Type1> &array1, std::vector<Type2> &array2_out)
{
array2_out.clear();
array2_out.reserve(array1.size());
for (const Type1 &obj : array1)
copy_func(obj, tools::add_element(array2_out));
}
//-------------------------------------------------------------------------------------------------------------------
// array2 consumes array1 by invoking relay_func() on each element
//-------------------------------------------------------------------------------------------------------------------
template <typename RelayFuncT, typename Type1, typename Type2>
static void relay_array(const RelayFuncT &relay_func, std::vector<Type1> &array1_in, std::vector<Type2> &array2_out)
{
array2_out.clear();
array2_out.reserve(array1_in.size());
for (Type1 &obj_in : array1_in)
relay_func(obj_in, tools::add_element(array2_out));
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void collect_sp_balance_proof_commitments_v1(const std::vector<SpEnoteImageV1> &seraphis_input_images,
const std::vector<SpEnoteV1> &output_enotes,
std::vector<rct::key> &commitments_out)
{
commitments_out.clear();
commitments_out.reserve(seraphis_input_images.size() + output_enotes.size());
for (const SpEnoteImageV1 &input_image : seraphis_input_images)
commitments_out.emplace_back(rct::scalarmultKey(masked_commitment_ref(input_image), rct::INV_EIGHT));
for (const SpEnoteV1 &output_enote : output_enotes)
commitments_out.emplace_back(rct::scalarmultKey(output_enote.core.amount_commitment, rct::INV_EIGHT));
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void indices_to_offsets(std::vector<std::uint64_t> &indices_inout)
{
if (indices_inout.size() == 0)
return;
for (std::size_t i{indices_inout.size() - 1}; i != 0; --i)
indices_inout[i] -= indices_inout[i - 1];
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void indices_from_offsets(std::vector<std::uint64_t> &indices_inout)
{
for (std::size_t i{1}; i < indices_inout.size(); ++i)
indices_inout[i] += indices_inout[i - 1];
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void recover_legacy_ring_signatures_v4(
std::vector<ser_LegacyRingSignatureV4_PARTIAL> &serializable_legacy_ring_signatures_in,
const std::vector<LegacyEnoteImageV2> &legacy_enote_images,
std::vector<LegacyRingSignatureV4> &legacy_ring_signatures_out)
{
CHECK_AND_ASSERT_THROW_MES(legacy_enote_images.size() == serializable_legacy_ring_signatures_in.size(),
"recovering legacy ring signature v4s: legacy input images don't line up with legacy ring signatures.");
legacy_ring_signatures_out.clear();
legacy_ring_signatures_out.reserve(serializable_legacy_ring_signatures_in.size());
for (std::size_t legacy_input_index{0}; legacy_input_index < legacy_enote_images.size(); ++legacy_input_index)
{
recover_legacy_ring_signature_v4(serializable_legacy_ring_signatures_in[legacy_input_index],
legacy_enote_images[legacy_input_index].key_image,
tools::add_element(legacy_ring_signatures_out));
}
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void recover_sp_membership_proofs_v1(
std::vector<ser_SpMembershipProofV1_PARTIAL> &serializable_membership_proofs_in,
const std::vector<SpEnoteImageV1> &enote_images,
const SpBinnedReferenceSetConfigV1 &sp_refset_bin_config,
const std::size_t sp_ref_set_decomp_n,
const std::size_t sp_ref_set_decomp_m,
std::vector<SpMembershipProofV1> &membership_proofs_out)
{
CHECK_AND_ASSERT_THROW_MES(enote_images.size() == serializable_membership_proofs_in.size(),
"recovering seraphis membership proof v1s: seraphis input images don't line up with seraphis membership proofs.");
membership_proofs_out.clear();
membership_proofs_out.reserve(serializable_membership_proofs_in.size());
rct::key generator_seed_temp;
for (std::size_t sp_input_index{0}; sp_input_index < enote_images.size(); ++sp_input_index)
{
make_binned_ref_set_generator_seed_v1(masked_address_ref(enote_images[sp_input_index]),
masked_commitment_ref(enote_images[sp_input_index]),
generator_seed_temp);
recover_sp_membership_proof_v1(serializable_membership_proofs_in[sp_input_index],
sp_refset_bin_config,
generator_seed_temp,
sp_ref_set_decomp_n,
sp_ref_set_decomp_m,
tools::add_element(membership_proofs_out));
}
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void make_serializable_legacy_ring_signatures_v4(const std::vector<LegacyRingSignatureV4> &legacy_ring_signatures,
std::vector<ser_LegacyRingSignatureV4_PARTIAL> &serializable_legacy_ring_signatures_out)
{
serializable_legacy_ring_signatures_out.clear();
serializable_legacy_ring_signatures_out.reserve(legacy_ring_signatures.size());
for (const LegacyRingSignatureV4 &legacy_ring_signature : legacy_ring_signatures)
{
make_serializable_legacy_ring_signature_v4(legacy_ring_signature,
tools::add_element(serializable_legacy_ring_signatures_out));
}
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
static void make_serializable_sp_membership_proofs_v1(const std::vector<SpMembershipProofV1> &membership_proofs,
std::vector<ser_SpMembershipProofV1_PARTIAL> &serializable_membership_proofs_out)
{
serializable_membership_proofs_out.clear();
serializable_membership_proofs_out.reserve(membership_proofs.size());
for (const SpMembershipProofV1 &membership_proof : membership_proofs)
{
make_serializable_sp_membership_proof_v1(membership_proof,
tools::add_element(serializable_membership_proofs_out));
}
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_bpp2(const BulletproofPlus2 &bpp2, ser_BulletproofPlus2_PARTIAL &serializable_bpp2_out)
{
serializable_bpp2_out.A = bpp2.A;
serializable_bpp2_out.A1 = bpp2.A1;
serializable_bpp2_out.B = bpp2.B;
serializable_bpp2_out.r1 = bpp2.r1;
serializable_bpp2_out.s1 = bpp2.s1;
serializable_bpp2_out.d1 = bpp2.d1;
serializable_bpp2_out.L = bpp2.L;
serializable_bpp2_out.R = bpp2.R;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_clsag(const rct::clsag &clsag, ser_clsag_PARTIAL &serializable_clsag_out)
{
serializable_clsag_out.s = clsag.s;
serializable_clsag_out.c1 = clsag.c1;
serializable_clsag_out.D = clsag.D;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_grootle_proof(const GrootleProof &grootle, ser_GrootleProof &serializable_grootle_out)
{
serializable_grootle_out.A = grootle.A;
serializable_grootle_out.B = grootle.B;
serializable_grootle_out.f = grootle.f;
serializable_grootle_out.X = grootle.X;
serializable_grootle_out.zA = grootle.zA;
serializable_grootle_out.z = grootle.z;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_composition_proof(const SpCompositionProof &proof,
ser_SpCompositionProof &serializable_proof_out)
{
serializable_proof_out.c = proof.c;
serializable_proof_out.r_t1 = proof.r_t1;
serializable_proof_out.r_t2 = proof.r_t2;
serializable_proof_out.r_ki = proof.r_ki;
serializable_proof_out.K_t1 = proof.K_t1;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_coinbase_enote_core(const SpCoinbaseEnoteCore &enote,
ser_SpCoinbaseEnoteCore &serializable_enote_out)
{
serializable_enote_out.onetime_address = enote.onetime_address;
serializable_enote_out.amount = enote.amount;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_enote_core(const SpEnoteCore &enote, ser_SpEnoteCore &serializable_enote_out)
{
serializable_enote_out.onetime_address = enote.onetime_address;
serializable_enote_out.amount_commitment = enote.amount_commitment;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_enote_image_core(const SpEnoteImageCore &image, ser_SpEnoteImageCore &serializable_image_out)
{
serializable_image_out.masked_address = image.masked_address;
serializable_image_out.masked_commitment = image.masked_commitment;
serializable_image_out.key_image = image.key_image;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_binned_reference_set_v1(const SpBinnedReferenceSetV1 &refset,
ser_SpBinnedReferenceSetV1_PARTIAL &serializable_refset_out)
{
serializable_refset_out.bin_rotation_factor = refset.bin_rotation_factor;
serializable_refset_out.bin_loci_COMPACT = refset.bin_loci;
indices_to_offsets(serializable_refset_out.bin_loci_COMPACT);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_legacy_enote_image_v2(const LegacyEnoteImageV2 &image,
ser_LegacyEnoteImageV2 &serializable_image_out)
{
serializable_image_out.masked_commitment = image.masked_commitment;
serializable_image_out.key_image = image.key_image;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_coinbase_enote_v1(const SpCoinbaseEnoteV1 &enote, ser_SpCoinbaseEnoteV1 &serializable_enote_out)
{
make_serializable_sp_coinbase_enote_core(enote.core, serializable_enote_out.core);
memcpy(serializable_enote_out.addr_tag_enc.bytes,
enote.addr_tag_enc.bytes,
sizeof(enote.addr_tag_enc));
serializable_enote_out.view_tag = enote.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_enote_v1(const SpEnoteV1 &enote, ser_SpEnoteV1 &serializable_enote_out)
{
make_serializable_sp_enote_core(enote.core, serializable_enote_out.core);
memcpy(serializable_enote_out.encoded_amount.bytes,
enote.encoded_amount.bytes,
sizeof(enote.encoded_amount));
memcpy(serializable_enote_out.addr_tag_enc.bytes,
enote.addr_tag_enc.bytes,
sizeof(enote.addr_tag_enc));
serializable_enote_out.view_tag = enote.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_enote_image_v1(const SpEnoteImageV1 &image, ser_SpEnoteImageV1 &serializable_image_out)
{
make_serializable_sp_enote_image_core(image.core, serializable_image_out.core);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_balance_proof_v1(const SpBalanceProofV1 &proof,
ser_SpBalanceProofV1_PARTIAL &serializable_proof_out)
{
make_serializable_bpp2(proof.bpp2_proof, serializable_proof_out.bpp2_proof_PARTIAL);
serializable_proof_out.remainder_blinding_factor = proof.remainder_blinding_factor;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_legacy_ring_signature_v4(const LegacyRingSignatureV4 &signature,
ser_LegacyRingSignatureV4_PARTIAL &serializable_signature_out)
{
make_serializable_clsag(signature.clsag_proof, serializable_signature_out.clsag_proof_PARTIAL);
serializable_signature_out.reference_set_COMPACT = signature.reference_set;
indices_to_offsets(serializable_signature_out.reference_set_COMPACT);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_membership_proof_v1(const SpMembershipProofV1 &proof,
ser_SpMembershipProofV1_PARTIAL &serializable_proof_out)
{
make_serializable_grootle_proof(proof.grootle_proof, serializable_proof_out.grootle_proof);
make_serializable_sp_binned_reference_set_v1(proof.binned_reference_set,
serializable_proof_out.binned_reference_set_PARTIAL);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_image_proof_v1(const SpImageProofV1 &image_proof,
ser_SpImageProofV1 &serializable_image_proof_out)
{
make_serializable_sp_composition_proof(image_proof.composition_proof,
serializable_image_proof_out.composition_proof);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_tx_supplement_v1(const SpTxSupplementV1 &supplement,
ser_SpTxSupplementV1 &serializable_supplement_out)
{
serializable_supplement_out.output_enote_ephemeral_pubkeys = supplement.output_enote_ephemeral_pubkeys;
serializable_supplement_out.tx_extra = supplement.tx_extra;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_discretized_fee(const DiscretizedFee discretized_fee,
unsigned char &serializable_discretized_fee_out)
{
serializable_discretized_fee_out = discretized_fee.fee_encoding;
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_tx_coinbase_v1(const SpTxCoinbaseV1 &tx, ser_SpTxCoinbaseV1 &serializable_tx_out)
{
// semantic rules version
serializable_tx_out.tx_semantic_rules_version = tx.tx_semantic_rules_version;
// block height
serializable_tx_out.block_height = tx.block_height;
// block reward
serializable_tx_out.block_reward = tx.block_reward;
// tx outputs (new enotes)
copy_array(&make_serializable_sp_coinbase_enote_v1, tx.outputs, serializable_tx_out.outputs);
// supplemental data for tx
make_serializable_sp_tx_supplement_v1(tx.tx_supplement, serializable_tx_out.tx_supplement);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_tx_squashed_v1(const SpTxSquashedV1 &tx, ser_SpTxSquashedV1 &serializable_tx_out)
{
// semantic rules version
serializable_tx_out.tx_semantic_rules_version = tx.tx_semantic_rules_version;
// legacy tx input images (spent legacy enotes)
copy_array(&make_serializable_legacy_enote_image_v2, tx.legacy_input_images,
serializable_tx_out.legacy_input_images);
// seraphis tx input images (spent seraphis enotes)
copy_array(&make_serializable_sp_enote_image_v1, tx.sp_input_images, serializable_tx_out.sp_input_images);
// tx outputs (new enotes)
copy_array(&make_serializable_sp_enote_v1, tx.outputs, serializable_tx_out.outputs);
// balance proof (balance proof and range proofs)
make_serializable_sp_balance_proof_v1(tx.balance_proof, serializable_tx_out.balance_proof);
// ring signature proofs: membership and ownership/key-image-legitimacy for each legacy input
make_serializable_legacy_ring_signatures_v4(tx.legacy_ring_signatures,
serializable_tx_out.legacy_ring_signatures);
// composition proofs: ownership/key-image-legitimacy for each seraphis input
copy_array(&make_serializable_sp_image_proof_v1, tx.sp_image_proofs, serializable_tx_out.sp_image_proofs);
// Grootle proofs on squashed enotes: membership for each seraphis input
make_serializable_sp_membership_proofs_v1(tx.sp_membership_proofs, serializable_tx_out.sp_membership_proofs);
// supplemental data for tx
make_serializable_sp_tx_supplement_v1(tx.tx_supplement, serializable_tx_out.tx_supplement);
// the transaction fee (discretized representation)
make_serializable_discretized_fee(tx.tx_fee, serializable_tx_out.tx_fee);
}
//-------------------------------------------------------------------------------------------------------------------
void make_serializable_sp_destination_v1(const jamtis::JamtisDestinationV1 &dest, ser_JamtisDestinationV1 &serializable_dest_out)
{
serializable_dest_out.addr_K1 = dest.addr_K1;
serializable_dest_out.addr_K2 = dest.addr_K2;
serializable_dest_out.addr_K3 = dest.addr_K3;
memcpy(serializable_dest_out.addr_tag.bytes,
dest.addr_tag.bytes,
sizeof(dest.addr_tag));
}
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
void recover_bpp2(ser_BulletproofPlus2_PARTIAL &serializable_bpp2_in,
std::vector<rct::key> balance_proof_commitments_mulinv8,
BulletproofPlus2 &bpp2_out)
{
bpp2_out.V = std::move(balance_proof_commitments_mulinv8);
bpp2_out.A = serializable_bpp2_in.A;
bpp2_out.A1 = serializable_bpp2_in.A1;
bpp2_out.B = serializable_bpp2_in.B;
bpp2_out.r1 = serializable_bpp2_in.r1;
bpp2_out.s1 = serializable_bpp2_in.s1;
bpp2_out.d1 = serializable_bpp2_in.d1;
bpp2_out.L = std::move(serializable_bpp2_in.L);
bpp2_out.R = std::move(serializable_bpp2_in.R);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_clsag(ser_clsag_PARTIAL &serializable_clsag_in, const crypto::key_image &key_image, rct::clsag &clsag_out)
{
clsag_out.s = std::move(serializable_clsag_in.s);
clsag_out.c1 = serializable_clsag_in.c1;
clsag_out.I = rct::ki2rct(key_image);
clsag_out.D = serializable_clsag_in.D;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_grootle_proof(ser_GrootleProof &serializable_grootle_in, GrootleProof &grootle_out)
{
grootle_out.A = serializable_grootle_in.A;
grootle_out.B = serializable_grootle_in.B;
grootle_out.f = std::move(serializable_grootle_in.f);
grootle_out.X = std::move(serializable_grootle_in.X);
grootle_out.zA = serializable_grootle_in.zA;
grootle_out.z = serializable_grootle_in.z;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_composition_proof(const ser_SpCompositionProof &serializable_proof, SpCompositionProof &proof_out)
{
proof_out.c = serializable_proof.c;
proof_out.r_t1 = serializable_proof.r_t1;
proof_out.r_t2 = serializable_proof.r_t2;
proof_out.r_ki = serializable_proof.r_ki;
proof_out.K_t1 = serializable_proof.K_t1;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_coinbase_enote_core(const ser_SpCoinbaseEnoteCore &serializable_enote, SpCoinbaseEnoteCore &enote_out)
{
enote_out.onetime_address = serializable_enote.onetime_address;
enote_out.amount = serializable_enote.amount;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_enote_core(const ser_SpEnoteCore &serializable_enote, SpEnoteCore &enote_out)
{
enote_out.onetime_address = serializable_enote.onetime_address;
enote_out.amount_commitment = serializable_enote.amount_commitment;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_enote_image_core(const ser_SpEnoteImageCore &serializable_image, SpEnoteImageCore &image_out)
{
image_out.masked_address = serializable_image.masked_address;
image_out.masked_commitment = serializable_image.masked_commitment;
image_out.key_image = serializable_image.key_image;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_binned_reference_set_v1(ser_SpBinnedReferenceSetV1_PARTIAL &serializable_refset_in,
const SpBinnedReferenceSetConfigV1 &bin_config,
const rct::key &generator_seed,
SpBinnedReferenceSetV1 &refset_out)
{
// bin configuration details
refset_out.bin_config = bin_config;
// bin generator seed
refset_out.bin_generator_seed = generator_seed;
// rotation factor
refset_out.bin_rotation_factor = serializable_refset_in.bin_rotation_factor;
// bin loci
refset_out.bin_loci = std::move(serializable_refset_in.bin_loci_COMPACT);
indices_from_offsets(refset_out.bin_loci);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_legacy_enote_image_v2(const ser_LegacyEnoteImageV2 &serializable_image, LegacyEnoteImageV2 &image_out)
{
image_out.masked_commitment = serializable_image.masked_commitment;
image_out.key_image = serializable_image.key_image;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_coinbase_enote_v1(const ser_SpCoinbaseEnoteV1 &serializable_enote, SpCoinbaseEnoteV1 &enote_out)
{
recover_sp_coinbase_enote_core(serializable_enote.core, enote_out.core);
memcpy(enote_out.addr_tag_enc.bytes,
serializable_enote.addr_tag_enc.bytes,
sizeof(serializable_enote.addr_tag_enc));
enote_out.view_tag = serializable_enote.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_enote_v1(const ser_SpEnoteV1 &serializable_enote, SpEnoteV1 &enote_out)
{
recover_sp_enote_core(serializable_enote.core, enote_out.core);
memcpy(enote_out.encoded_amount.bytes,
serializable_enote.encoded_amount.bytes,
sizeof(serializable_enote.encoded_amount));
memcpy(enote_out.addr_tag_enc.bytes,
serializable_enote.addr_tag_enc.bytes,
sizeof(serializable_enote.addr_tag_enc));
enote_out.view_tag = serializable_enote.view_tag;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_enote_image_v1(const ser_SpEnoteImageV1 &serializable_image, SpEnoteImageV1 &image_out)
{
recover_sp_enote_image_core(serializable_image.core, image_out.core);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_balance_proof_v1(ser_SpBalanceProofV1_PARTIAL &serializable_proof_in,
std::vector<rct::key> commitments_inv8,
SpBalanceProofV1 &proof_out)
{
// bpp2
recover_bpp2(serializable_proof_in.bpp2_proof_PARTIAL, std::move(commitments_inv8), proof_out.bpp2_proof);
// remainder blinding factor
proof_out.remainder_blinding_factor = serializable_proof_in.remainder_blinding_factor;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_legacy_ring_signature_v4(ser_LegacyRingSignatureV4_PARTIAL &serializable_signature_in,
const crypto::key_image &key_image,
LegacyRingSignatureV4 &signature_out)
{
// clsag
recover_clsag(serializable_signature_in.clsag_proof_PARTIAL, key_image, signature_out.clsag_proof);
// reference set
signature_out.reference_set = std::move(serializable_signature_in.reference_set_COMPACT);
indices_from_offsets(signature_out.reference_set);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_membership_proof_v1(ser_SpMembershipProofV1_PARTIAL &serializable_proof_in,
const SpBinnedReferenceSetConfigV1 &bin_config,
const rct::key &generator_seed,
const std::size_t ref_set_decomp_n,
const std::size_t ref_set_decomp_m,
SpMembershipProofV1 &proof_out)
{
// grootle proof
recover_grootle_proof(serializable_proof_in.grootle_proof, proof_out.grootle_proof);
// binned reference set
recover_sp_binned_reference_set_v1(serializable_proof_in.binned_reference_set_PARTIAL,
bin_config,
generator_seed,
proof_out.binned_reference_set);
// ref set size decomposition
proof_out.ref_set_decomp_n = ref_set_decomp_n;
proof_out.ref_set_decomp_m = ref_set_decomp_m;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_image_proof_v1(const ser_SpImageProofV1 &serializable_image_proof, SpImageProofV1 &image_proof_out)
{
recover_sp_composition_proof(serializable_image_proof.composition_proof, image_proof_out.composition_proof);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_tx_supplement_v1(ser_SpTxSupplementV1 &serializable_supplement_in, SpTxSupplementV1 &supplement_out)
{
supplement_out.output_enote_ephemeral_pubkeys =
std::move(serializable_supplement_in.output_enote_ephemeral_pubkeys);
supplement_out.tx_extra = std::move(serializable_supplement_in.tx_extra);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_discretized_fee(const unsigned char serializable_discretized_fee, DiscretizedFee &discretized_fee_out)
{
discretized_fee_out.fee_encoding = serializable_discretized_fee;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_tx_coinbase_v1(ser_SpTxCoinbaseV1 &serializable_tx_in, SpTxCoinbaseV1 &tx_out)
{
// semantic rules version
tx_out.tx_semantic_rules_version = serializable_tx_in.tx_semantic_rules_version;
// block height
tx_out.block_height = serializable_tx_in.block_height;
// block reward
tx_out.block_reward = serializable_tx_in.block_reward;
// tx outputs (new enotes)
relay_array(&recover_sp_coinbase_enote_v1, serializable_tx_in.outputs, tx_out.outputs);
// supplemental data for tx
recover_sp_tx_supplement_v1(serializable_tx_in.tx_supplement, tx_out.tx_supplement);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_tx_squashed_v1(ser_SpTxSquashedV1 &serializable_tx_in,
const SpBinnedReferenceSetConfigV1 &sp_refset_bin_config,
const std::size_t sp_ref_set_decomp_n,
const std::size_t sp_ref_set_decomp_m,
SpTxSquashedV1 &tx_out)
{
// semantic rules version
tx_out.tx_semantic_rules_version = serializable_tx_in.tx_semantic_rules_version;
// legacy tx input images (spent legacy enotes)
relay_array(&recover_legacy_enote_image_v2, serializable_tx_in.legacy_input_images, tx_out.legacy_input_images);
// seraphis tx input images (spent seraphis enotes)
relay_array(&recover_sp_enote_image_v1, serializable_tx_in.sp_input_images, tx_out.sp_input_images);
// tx outputs (new enotes)
relay_array(&recover_sp_enote_v1, serializable_tx_in.outputs, tx_out.outputs);
// balance proof (balance proof and range proofs)
std::vector<rct::key> balance_proof_commitments_mulinv8;
collect_sp_balance_proof_commitments_v1(tx_out.sp_input_images,
tx_out.outputs,
balance_proof_commitments_mulinv8);
recover_sp_balance_proof_v1(serializable_tx_in.balance_proof,
std::move(balance_proof_commitments_mulinv8),
tx_out.balance_proof);
// ring signature proofs: membership and ownership/key-image-legitimacy for each legacy input
recover_legacy_ring_signatures_v4(serializable_tx_in.legacy_ring_signatures,
tx_out.legacy_input_images,
tx_out.legacy_ring_signatures);
// composition proofs: ownership/key-image-legitimacy for each seraphis input
relay_array(&recover_sp_image_proof_v1, serializable_tx_in.sp_image_proofs, tx_out.sp_image_proofs);
// Grootle proofs on squashed enotes: membership for each seraphis input
recover_sp_membership_proofs_v1(serializable_tx_in.sp_membership_proofs,
tx_out.sp_input_images,
sp_refset_bin_config,
sp_ref_set_decomp_n,
sp_ref_set_decomp_m,
tx_out.sp_membership_proofs);
// supplemental data for tx
recover_sp_tx_supplement_v1(serializable_tx_in.tx_supplement, tx_out.tx_supplement);
// the transaction fee (discretized representation)
recover_discretized_fee(serializable_tx_in.tx_fee, tx_out.tx_fee);
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_tx_squashed_v1(ser_SpTxSquashedV1 &serializable_tx_in, SpTxSquashedV1 &tx_out)
{
// get config for seraphis reference sets (assume the minimum values are needed; use raw API for other variations)
const SemanticConfigSpRefSetV1 seraphis_ref_set_config{
semantic_config_sp_ref_sets_v1(serializable_tx_in.tx_semantic_rules_version)
};
// finish recovering
recover_sp_tx_squashed_v1(serializable_tx_in,
SpBinnedReferenceSetConfigV1{
.bin_radius = static_cast<ref_set_bin_dimension_v1_t>(seraphis_ref_set_config.bin_radius_min),
.num_bin_members = static_cast<ref_set_bin_dimension_v1_t>(seraphis_ref_set_config.num_bin_members_min)
},
seraphis_ref_set_config.decomp_n_min,
seraphis_ref_set_config.decomp_m_min,
tx_out);
}
//-------------------------------------------------------------------------------------------------------------------
bool try_recover_sp_tx_squashed_v1(ser_SpTxSquashedV1 &serializable_tx_in,
const SpBinnedReferenceSetConfigV1 &sp_refset_bin_config,
const std::size_t sp_ref_set_decomp_n,
const std::size_t sp_ref_set_decomp_m,
SpTxSquashedV1 &tx_out)
{
try
{
recover_sp_tx_squashed_v1(serializable_tx_in,
sp_refset_bin_config,
sp_ref_set_decomp_n,
sp_ref_set_decomp_m,
tx_out);
}
catch (...) { return false; }
return true;
}
//-------------------------------------------------------------------------------------------------------------------
bool try_recover_sp_tx_squashed_v1(ser_SpTxSquashedV1 &serializable_tx_in, SpTxSquashedV1 &tx_out)
{
try
{
recover_sp_tx_squashed_v1(serializable_tx_in, tx_out);
}
catch (...) { return false; }
return true;
}
//-------------------------------------------------------------------------------------------------------------------
void recover_sp_destination_v1(const ser_JamtisDestinationV1 &serializable_destination, jamtis::JamtisDestinationV1 &dest_out)
{
dest_out.addr_K1 = serializable_destination.addr_K1;
dest_out.addr_K2 = serializable_destination.addr_K2;
dest_out.addr_K3 = serializable_destination.addr_K3;
memcpy(dest_out.addr_tag.bytes,
serializable_destination.addr_tag.bytes,
sizeof(serializable_destination.addr_tag));
}
//-------------------------------------------------------------------------------------------------------------------
} //namespace serialization
} //namespace sp