forked from ElementsProject/elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblindpsbt.cpp
More file actions
603 lines (519 loc) · 27.8 KB
/
blindpsbt.cpp
File metadata and controls
603 lines (519 loc) · 27.8 KB
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
// Copyright (c) 2017-2019 The Elements Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <blindpsbt.h>
#include <hash.h>
#include <primitives/transaction.h>
#include <primitives/confidential.h>
#include <psbt.h>
#include <issuance.h>
#include <random.h>
#include <util/system.h>
std::string GetBlindingStatusError(const BlindingStatus& status)
{
switch(status) {
case BlindingStatus::OK:
return "No error";
case BlindingStatus::NEEDS_UTXOS:
return "Inputs are missing UTXOs (or peg-in data for peg-in inputs)";
case BlindingStatus::INVALID_ASSET:
return "Provided asset tag is invalid";
case BlindingStatus::INVALID_ASSET_COMMITMENT:
return "Provided asset commitment is invalid";
case BlindingStatus::SCALAR_UNABLE:
return "Unable to compute the scalars for the final blinder";
case BlindingStatus::INVALID_BLINDER:
return "Computed blinding factor is invalid";
case BlindingStatus::ASP_UNABLE:
return "Unable to create an asset surjection proof";
case BlindingStatus::NO_BLIND_OUTPUTS:
return "Transaction has blind inputs belonging to this blinder but does not have outputs to blind";
}
assert(false);
}
// Create surjection proof
bool CreateAssetSurjectionProof(std::vector<unsigned char>& output_proof, const std::vector<secp256k1_fixed_asset_tag>& fixed_input_tags, const std::vector<secp256k1_generator>& ephemeral_input_tags, const std::vector<uint256>& input_asset_blinders, const uint256& output_asset_blinder, const secp256k1_generator& output_asset_tag, const CAsset& asset, size_t num_targets)
{
int ret;
// 1 to 3 targets
size_t inputs_to_select = std::min(num_targets, fixed_input_tags.size());
unsigned char randseed[32];
GetStrongRandBytes(randseed, 32);
size_t input_index;
secp256k1_surjectionproof proof;
secp256k1_fixed_asset_tag fixed_output_tag;
memcpy(&fixed_output_tag, asset.begin(), 32);
// Find correlation between asset tag and listed input tags
if (secp256k1_surjectionproof_initialize(secp256k1_blind_context, &proof, &input_index, &fixed_input_tags[0], fixed_input_tags.size(), inputs_to_select, &fixed_output_tag, 100, randseed) == 0) {
return false;
}
// Using the input chosen, build proof
ret = secp256k1_surjectionproof_generate(secp256k1_blind_context, &proof, &ephemeral_input_tags[0], ephemeral_input_tags.size(), &output_asset_tag, input_index, input_asset_blinders[input_index].begin(), output_asset_blinder.begin());
assert(ret == 1);
// Double-check answer
ret = secp256k1_surjectionproof_verify(secp256k1_blind_context, &proof, &ephemeral_input_tags[0], ephemeral_input_tags.size(), &output_asset_tag);
assert(ret == 1);
// Serialize into output witness structure
size_t output_len = secp256k1_surjectionproof_serialized_size(secp256k1_blind_context, &proof);
output_proof.resize(output_len);
secp256k1_surjectionproof_serialize(secp256k1_blind_context, &output_proof[0], &output_len, &proof);
assert(output_len == output_proof.size());
return true;
}
bool VerifyBlindAssetProof(const uint256& asset, const std::vector<unsigned char>& proof, const CConfidentialAsset& conf_asset)
{
secp256k1_surjectionproof surj_proof;
if (secp256k1_surjectionproof_parse(secp256k1_blind_context, &surj_proof, proof.data(), proof.size()) == 0) {
return false;
}
secp256k1_generator blinded_asset_gen;
if (secp256k1_generator_parse(secp256k1_blind_context, &blinded_asset_gen, conf_asset.vchCommitment.data()) == 0) {
return false;
}
secp256k1_generator asset_gen;
if (secp256k1_generator_generate(secp256k1_blind_context, &asset_gen, asset.data()) == 0) {
return false;
}
return secp256k1_surjectionproof_verify(secp256k1_blind_context, &surj_proof, &asset_gen, 1, &blinded_asset_gen);
}
uint256 GenerateRangeproofECDHKey(CPubKey& ephemeral_pubkey, const CPubKey blinding_pubkey)
{
// Generate ephemeral key for ECDH nonce generation
CKey ephemeral_key;
ephemeral_key.MakeNewKey(true);
ephemeral_pubkey = ephemeral_key.GetPubKey();
assert(ephemeral_pubkey.size() == CConfidentialNonce::nCommittedSize);
// Generate nonce
uint256 nonce = ephemeral_key.ECDH(blinding_pubkey);
CSHA256().Write(nonce.begin(), 32).Finalize(nonce.begin());
return nonce;
}
bool CreateValueRangeProof(std::vector<unsigned char>& rangeproof, const uint256& value_blinder, const uint256& nonce, const CAmount amount, const CScript& scriptPubKey, const secp256k1_pedersen_commitment& value_commit, const secp256k1_generator& gen, const CAsset& asset, const uint256& asset_blinder)
{
// Prep range proof
size_t rangeproof_len = 5134;
rangeproof.resize(rangeproof_len);
// Compose sidechannel message to convey asset info (ID and asset blinds)
unsigned char asset_message[SIDECHANNEL_MSG_SIZE];
memcpy(asset_message, asset.begin(), 32);
memcpy(asset_message + 32, asset_blinder.begin(), 32);
// Sign rangeproof
int ct_exponent = (int)gArgs.GetArg("-ct_exponent", 0);
int ct_bits = (int)gArgs.GetArg("-ct_bits", 52);
// If min_value is 0, scriptPubKey must be unspendable
uint64_t min_value = scriptPubKey.IsUnspendable() ? 0 : 1;
int res = secp256k1_rangeproof_sign(secp256k1_blind_context, rangeproof.data(), &rangeproof_len, min_value, &value_commit, value_blinder.begin(), nonce.begin(), ct_exponent, ct_bits, amount, asset_message, sizeof(asset_message), scriptPubKey.size() ? &scriptPubKey.front() : NULL, scriptPubKey.size(), &gen);
rangeproof.resize(rangeproof_len);
return (res == 1);
}
// Create an explicit value rangeproof which proves that the commitment commits to an explicit value
static bool CreateBlindValueProof(std::vector<unsigned char>& rangeproof, const uint256& value_blinder, const CAmount amount, const secp256k1_pedersen_commitment& value_commit, const secp256k1_generator& gen)
{
// Prep rangeproof
size_t rangeproof_len = 5134;
rangeproof.resize(rangeproof_len);
// Generate a new random nonce
uint256 nonce;
GetStrongRandBytes(nonce.begin(), nonce.size());
// Make the rangeproof
int res = secp256k1_rangeproof_sign(secp256k1_blind_context, rangeproof.data(), &rangeproof_len, /* min_value */ amount, &value_commit, value_blinder.begin(), nonce.begin(), /* exp */ -1, /* min_bits */ 0, amount, /* message */ nullptr, /* message_len */ 0, /* extra_commit */ nullptr, /* extra_commit_len */ 0, &gen);
rangeproof.resize(rangeproof_len);
return res == 1;
}
// Create an explicit value rangeproof which proves that the commitment commits to an explicit value
static bool CreateBlindAssetProof(std::vector<unsigned char>& assetproof, const CAsset& asset, const CConfidentialAsset& asset_commit, const uint256& asset_blinder)
{
const unsigned char zero32[32] = {0};
secp256k1_surjectionproof proof;
size_t input_index;
secp256k1_generator asset_gen;
secp256k1_generator blinded_asset_gen;
secp256k1_fixed_asset_tag fixed_tag;
memcpy(&fixed_tag, asset.begin(), 32);
if (!secp256k1_generator_generate(secp256k1_blind_context, &asset_gen, asset.begin())) {
return false;
}
if (secp256k1_generator_parse(secp256k1_blind_context, &blinded_asset_gen, asset_commit.vchCommitment.data()) == 0) {
return false;
}
if (!secp256k1_surjectionproof_initialize(secp256k1_blind_context, &proof, &input_index, &fixed_tag, 1, 1, &fixed_tag, 1, zero32)) {
return false;
}
assert(input_index == 0);
if (!secp256k1_surjectionproof_generate(secp256k1_blind_context, &proof, &asset_gen, 1, &blinded_asset_gen, 0, zero32, asset_blinder.data())) {
return false;
}
if (!secp256k1_surjectionproof_verify(secp256k1_blind_context, &proof, &asset_gen, 1, &blinded_asset_gen)) {
return false;
}
size_t output_len = secp256k1_surjectionproof_serialized_size(secp256k1_blind_context, &proof);
assetproof.resize(output_len);
secp256k1_surjectionproof_serialize(secp256k1_blind_context, &assetproof[0], &output_len, &proof);
assert(output_len == assetproof.size());
return true;
}
bool VerifyBlindValueProof(CAmount value, const CConfidentialValue& conf_value, const std::vector<unsigned char>& proof, const CConfidentialAsset& conf_asset)
{
secp256k1_pedersen_commitment value_commit;
if (secp256k1_pedersen_commitment_parse(secp256k1_blind_context, &value_commit, conf_value.vchCommitment.data()) == 0) {
return false;
}
secp256k1_generator gen;
if (secp256k1_generator_parse(secp256k1_blind_context, &gen, conf_asset.vchCommitment.data()) == 0) {
return false;
}
uint64_t min_value;
uint64_t max_value;
if (secp256k1_rangeproof_verify(secp256k1_blind_context, &min_value, &max_value, &value_commit, proof.data(), proof.size(), /* extra_commit */ nullptr, /* extra_commit_len */ 0, &gen) == 0) {
return false;
}
return min_value == (uint64_t)value;
}
BlindProofResult VerifyBlindProofs(const PSBTOutput& o) {
// No blinding, no problem
if (!o.IsBlinded()) {
return BlindProofResult::OK;
} else if (!o.IsFullyBlinded()) {
return BlindProofResult::NOT_FULLY_BLINDED;
}
if (o.amount != std::nullopt) {
if (o.m_blind_value_proof.empty()) {
return BlindProofResult::MISSING_VALUE_PROOF;
} else if (!VerifyBlindValueProof(*o.amount, o.m_value_commitment, o.m_blind_value_proof, o.m_asset_commitment)) {
return BlindProofResult::INVALID_VALUE_PROOF;
}
}
if (!o.m_asset.IsNull()) {
if (o.m_blind_asset_proof.empty()) {
return BlindProofResult::MISSING_ASSET_PROOF;
} else if (!VerifyBlindAssetProof(o.m_asset, o.m_blind_asset_proof, o.m_asset_commitment)) {
return BlindProofResult::INVALID_ASSET_PROOF;
}
}
return BlindProofResult::OK;
}
void CreateAssetCommitment(CConfidentialAsset& conf_asset, secp256k1_generator& asset_gen, const CAsset& asset, const uint256& asset_blinder)
{
conf_asset.vchCommitment.resize(CConfidentialAsset::nCommittedSize);
int ret = secp256k1_generator_generate_blinded(secp256k1_blind_context, &asset_gen, asset.begin(), asset_blinder.begin());
assert(ret == 1);
ret = secp256k1_generator_serialize(secp256k1_blind_context, conf_asset.vchCommitment.data(), &asset_gen);
assert(ret == 1);
}
void CreateValueCommitment(CConfidentialValue& conf_value, secp256k1_pedersen_commitment& value_commit, const uint256& value_blinder, const secp256k1_generator& asset_gen, const CAmount amount)
{
int ret;
conf_value.vchCommitment.resize(CConfidentialValue::nCommittedSize);
ret = secp256k1_pedersen_commit(secp256k1_blind_context, &value_commit, value_blinder.begin(), amount, &asset_gen);
assert(ret == 1);
secp256k1_pedersen_commitment_serialize(secp256k1_blind_context, conf_value.vchCommitment.data(), &value_commit);
assert(conf_value.IsValid());
}
// Subtract b from a in place
bool SubtractScalars(uint256& a, const uint256& b)
{
// If b is 0, then the result of this subtraction is just a
if (b.IsNull()) {
return true;
}
uint256 sub(b);
if (secp256k1_ec_seckey_negate(secp256k1_blind_context, sub.begin()) != 1) return false;
// If a is 0, then the result of this subtraction is the negation of b (i.e. sub)
if (a.IsNull()) {
a = sub;
return true;
}
// Neither a nor b are null, do a = a - b
if (secp256k1_ec_seckey_tweak_add(secp256k1_blind_context, a.begin(), sub.begin()) != 1) return false;
return true;
}
// Compute the scalar offset used for the final blinder computation
// value * asset_blinder + value_blinder
// FIXME this method should be in libsecp, as should `ComputeAndAddToScalarOffset`
bool CalculateScalarOffset(uint256& out, CAmount value, const uint256& asset_blinder, const uint256& value_blinder)
{
// If the asset_blinder is 0, then the equation resolves to just the value_blinder
if (asset_blinder.IsNull()) {
out = value_blinder;
return true;
}
out = asset_blinder;
uint256 val;
// tweak_mul expects a 32 byte, big endian tweak.
// We need to pack the 8 byte CAmount into a uint256 with the correct padding, so start it at 24 bytes from the front
WriteBE64(val.begin() + 24, value);
if (value > 0) {
if (secp256k1_ec_seckey_tweak_mul(secp256k1_blind_context, out.begin(), val.begin()) != 1) return false;
} else {
out = value_blinder;
return true;
}
if (!value_blinder.IsNull()) {
uint256 value_negated = value_blinder;
if (secp256k1_ec_seckey_negate(secp256k1_blind_context, value_negated.begin()) != 1) {
return false;
}
// Special-case zero, which would otherwise cause `secp256k1_ec_seckey_tweak_add` to fail
if (value_negated == out) {
out = uint256{};
return true;
}
if (secp256k1_ec_seckey_tweak_add(secp256k1_blind_context, out.begin(), value_blinder.begin()) != 1) return false;
}
return true;
}
// Computes a scalar offset and adds it to another existing one
bool ComputeAndAddToScalarOffset(uint256& a, CAmount value, const uint256& asset_blinder, const uint256& value_blinder)
{
// If both asset and value blinders are null, 0 is added to the offset, so nothing actually happens
if (asset_blinder.IsNull() && value_blinder.IsNull()) return true;
uint256 scalar;
if (!CalculateScalarOffset(scalar, value, asset_blinder, value_blinder)) return false;
// When we start out, the result (a) is 0, so just set it to the scalar we just computed.
if (a.IsNull()) {
a = scalar;
} else {
uint256 scalar_negated = scalar;
if (secp256k1_ec_seckey_negate(secp256k1_blind_context, scalar_negated.begin()) != 1) {
return false;
}
// Special-case zero, which would otherwise cause `secp256k1_ec_seckey_tweak_add` to fail
if (scalar_negated == a) {
a = uint256{};
} else {
// If we have a, then add the scalar to it.
if (secp256k1_ec_seckey_tweak_add(secp256k1_blind_context, a.begin(), scalar.begin()) != 1) return false;
}
}
return true;
}
BlindingStatus BlindPSBT(PartiallySignedTransaction& psbt, std::map<uint32_t, std::tuple<CAmount, CAsset, uint256, uint256>> our_input_data, std::map<uint32_t, std::pair<CKey, CKey>> our_issuances_to_blind)
{
unsigned int num_blinded = 0;
std::vector<uint32_t> to_blind;
for (unsigned int i = 0; i < psbt.outputs.size(); ++i) {
PSBTOutput& output = psbt.outputs[i];
if (output.IsFullyBlinded()) num_blinded++;
if (output.IsBlinded()) to_blind.push_back(i);
}
if (num_blinded == to_blind.size()) {
// All outputs are blinded, nothing left to do
return BlindingStatus::OK;
}
std::vector<secp256k1_fixed_asset_tag> fixed_input_tags; // Explicit Asset IDs for the inputs we know. Blinded for unknown ones
std::vector<secp256k1_generator> ephemeral_input_tags; // Blinded Asset IDs. Explicit Asset ID blinded with 0 if not blinded
std::vector<uint256> input_asset_blinders; // Blinding factors for the input asset tags
uint256 input_scalar;
for (unsigned int i = 0; i < psbt.inputs.size(); ++i) {
PSBTInput& input = psbt.inputs[i];
CTxOut utxo;
if (!input.GetUTXO(utxo)) {
return BlindingStatus::NEEDS_UTXOS;
}
CConfidentialAsset& asset = utxo.nAsset;
ephemeral_input_tags.emplace_back();
if (asset.IsExplicit()) {
// Explicit asset
if (secp256k1_generator_generate(secp256k1_blind_context, &ephemeral_input_tags.back(), asset.GetAsset().begin()) != 1) {
return BlindingStatus::INVALID_ASSET;
}
} else if (asset.IsCommitment()) {
// Parse the asset commitment as a generator (because it is)
if (secp256k1_generator_parse(secp256k1_blind_context, &ephemeral_input_tags.back(), asset.vchCommitment.data()) != 1) {
return BlindingStatus::INVALID_ASSET_COMMITMENT;
}
} else {
return BlindingStatus::INVALID_ASSET; // Missing asset
}
fixed_input_tags.emplace_back();
auto it = our_input_data.find(i);
if (it != our_input_data.end()) {
memcpy(fixed_input_tags.back().data, std::get<1>(it->second).begin(), 32);
input_asset_blinders.push_back(std::get<2>(it->second));
// Add the value blinder to the input scalar
if (!ComputeAndAddToScalarOffset(input_scalar, std::get<0>(it->second), std::get<2>(it->second), std::get<3>(it->second))) return BlindingStatus::SCALAR_UNABLE;
} else if (asset.IsExplicit()) {
memcpy(fixed_input_tags.back().data, asset.GetAsset().begin(), 32);
input_asset_blinders.emplace_back(); // No blinding factor, put 0
} else {
memcpy(fixed_input_tags.back().data, asset.vchCommitment.data() + 1, 32);
input_asset_blinders.emplace_back(); // We don't know the blinding factor, put 0
}
// Handle issuances
if (input.m_issuance_value) {
if (!input.m_issuance_value_commitment.IsCommitment() && input.m_issuance_rangeproof.size() == 0 && input.m_issuance_inflation_keys_rangeproof.size() == 0) {
CAsset issuance_asset;
CAsset reissuance_asset;
uint256 entropy;
if (!input.m_issuance_blinding_nonce.IsNull()) {
// Reissuance, use assetEntropy as the asset entropy
entropy = input.m_issuance_asset_entropy;
} else {
// New issuance, make new entropy
GenerateAssetEntropy(entropy, input.GetOutPoint(), input.m_issuance_asset_entropy);
}
// Asset isn't blinded yet. Add it to the list of input assets
CalculateAsset(issuance_asset, entropy);
fixed_input_tags.emplace_back();
memcpy(fixed_input_tags.back().data, issuance_asset.begin(), 32);
ephemeral_input_tags.emplace_back();
if (secp256k1_generator_generate(secp256k1_blind_context, &ephemeral_input_tags.back(), issuance_asset.begin()) != 1) {
return BlindingStatus::INVALID_ASSET;
}
unsigned int iss_to_blind = 1; // Always do the first issuance blinding iteration for the issuance value
bool blind_issuance = our_issuances_to_blind.count(i) > 0;
if (input.m_issuance_blinding_nonce.IsNull() && input.m_issuance_inflation_keys_amount) {
// New issuance, do reissuance token things
CalculateReissuanceToken(reissuance_asset, entropy, blind_issuance);
// Add the reissuance_asset to the list of input assets
fixed_input_tags.emplace_back();
memcpy(fixed_input_tags.back().data, reissuance_asset.begin(), 32);
ephemeral_input_tags.emplace_back();
if (secp256k1_generator_generate(secp256k1_blind_context, &ephemeral_input_tags.back(), reissuance_asset.begin()) != 1) {
return BlindingStatus::INVALID_ASSET;
}
iss_to_blind++; // If we have a reissuance, do the second blinding iteration for the inflation keys
}
if (blind_issuance) {
for (unsigned int blind_i = 0; blind_i < iss_to_blind; ++blind_i) {
// To blind an issuance, both the issuance value and the number of inflation keys need to be blinded
// Since this process is basically the same for both, do it in a loop and switch based on the index
bool blind_value = blind_i == 0; // True for blinding the value, false for blinding the inflation keys
CAmount value = blind_value ? *input.m_issuance_value : *input.m_issuance_inflation_keys_amount;
CAsset asset = blind_value ? issuance_asset : reissuance_asset;
CKey blinding_privkey = blind_value ? our_issuances_to_blind.at(i).first : our_issuances_to_blind.at(i).second;
uint256 value_blinder;
GetStrongRandBytes(value_blinder.begin(), value_blinder.size());
// Create unblinded generator. Throw away everything except asset_gen
uint256 asset_blinder;
CConfidentialAsset conf_asset;
secp256k1_generator asset_gen;
CreateAssetCommitment(conf_asset, asset_gen, asset, asset_blinder);
input_asset_blinders.push_back(asset_blinder);
// Compute the scalar for this blinding and add to the input scalar
if (!ComputeAndAddToScalarOffset(input_scalar, value, asset_blinder, value_blinder)) return BlindingStatus::SCALAR_UNABLE;
// Create value commitment
secp256k1_pedersen_commitment value_commit;
CConfidentialValue conf_value;
CreateValueCommitment(conf_value, value_commit, value_blinder, asset_gen, value);
// Nonce is the blinding key
uint256 nonce = uint256(std::vector<unsigned char>(blinding_privkey.begin(), blinding_privkey.end()));
// Generate rangeproof
std::vector<unsigned char> rangeproof;
bool rangeresult = CreateValueRangeProof(rangeproof, value_blinder, nonce, value, CScript(), value_commit, asset_gen, asset, asset_blinder);
assert(rangeresult);
// Create explicit value rangeproofs
std::vector<unsigned char> blind_value_proof;
rangeresult = CreateBlindValueProof(blind_value_proof, value_blinder, value, value_commit, asset_gen);
assert(rangeresult);
if (blind_value) {
input.m_issuance_value_commitment = conf_value;
input.m_issuance_rangeproof = rangeproof;
input.m_blind_issuance_value_proof = blind_value_proof;
} else {
input.m_issuance_inflation_keys_commitment = conf_value;
input.m_issuance_inflation_keys_rangeproof = rangeproof;
input.m_blind_issuance_inflation_keys_proof = blind_value_proof;
}
}
}
else {
input_asset_blinders.emplace_back();
}
}
}
}
uint256 output_scalar;
bool did_last_blind = false;
int our_blinds = 0;
for (uint32_t i : to_blind) {
PSBTOutput& output = psbt.outputs[i];
if (output.IsFullyBlinded()) {
our_blinds++;
continue;
}
// Check this is our output to blind
if (output.m_blinder_index == std::nullopt || our_input_data.count(*output.m_blinder_index) == 0) continue;
// Things we are going to stuff into the PSBTOutput if everything is successful
CConfidentialValue value_commitment;
CConfidentialAsset asset_commitment;
std::vector<unsigned char> rangeproof;
std::vector<unsigned char> asp;
CPubKey ecdh_key;
// Generate the blinders
uint256 value_blinder;
uint256 asset_blinder;
GetStrongRandBytes(value_blinder.begin(), value_blinder.size());
GetStrongRandBytes(asset_blinder.begin(), asset_blinder.size());
// Compute the scalar for this blinding and add to the output scalar
if (!ComputeAndAddToScalarOffset(output_scalar, *output.amount, asset_blinder, value_blinder)) return BlindingStatus::SCALAR_UNABLE;
// For the last blinder
num_blinded++;
if (num_blinded == to_blind.size()) {
did_last_blind = true;
// For the last blinder, we need to first compute a scalar offset for the inputs and outputs that haven't already been
// accounted for in a scalar. Then for this last output, a randomly generated value blinder is created and all of the scalar
// offsets subtracted from this.
// First compute a scalar offset for the stuff we've already blinded and subtract that scalar from value_blinder
if (!SubtractScalars(output_scalar, input_scalar)) return BlindingStatus::SCALAR_UNABLE;
if (!SubtractScalars(value_blinder, output_scalar)) return BlindingStatus::SCALAR_UNABLE;
// Now subtract ever other scalar from value_blinder
for (const uint256& s : psbt.m_scalar_offsets) {
if (!SubtractScalars(value_blinder, s)) return BlindingStatus::SCALAR_UNABLE;
}
// Make sure our blinder isn't 0 as this has privacy implications.
// This can occur if the transaction has one input and one output.
// This can also occur if another party is being malicious.
// Or just bad luck.
if (value_blinder.IsNull()) return BlindingStatus::INVALID_BLINDER;
// Remove all scalar offsets
psbt.m_scalar_offsets.clear();
}
CAsset asset(output.m_asset);
// Blind the asset ID
secp256k1_generator asset_generator;
CreateAssetCommitment(asset_commitment, asset_generator, asset, asset_blinder);
// Blind the value
secp256k1_pedersen_commitment value_commit;
CreateValueCommitment(value_commitment, value_commit, value_blinder, asset_generator, *output.amount);
// Generate rangproof nonce
uint256 nonce = GenerateRangeproofECDHKey(ecdh_key, output.m_blinding_pubkey);
// Generate rangeproof
bool rangeresult = CreateValueRangeProof(rangeproof, value_blinder, nonce, *output.amount, *output.script, value_commit, asset_generator, asset, asset_blinder);
assert(rangeresult);
// Create explicit value rangeproof
std::vector<unsigned char> blind_value_proof;
rangeresult = CreateBlindValueProof(blind_value_proof, value_blinder, *output.amount, value_commit, asset_generator);
assert(rangeresult);
// Create surjection proof for this output
if (!CreateAssetSurjectionProof(asp, fixed_input_tags, ephemeral_input_tags, input_asset_blinders, asset_blinder, asset_generator, asset)) {
return BlindingStatus::ASP_UNABLE;
}
// Create explicit asset surjection proof
std::vector<unsigned char> blind_asset_proof;
if (!CreateBlindAssetProof(blind_asset_proof, asset, asset_commitment, asset_blinder)) {
return BlindingStatus::ASP_UNABLE;
}
// Fill output
output.m_asset_commitment = asset_commitment;
output.m_value_commitment = value_commitment;
output.m_ecdh_pubkey = ecdh_key;
output.m_value_rangeproof = rangeproof;
output.m_asset_surjection_proof = asp;
output.m_blind_value_proof = blind_value_proof;
output.m_blind_asset_proof = blind_asset_proof;
our_blinds++;
}
// Compute scalar and add to PSBT if it isn't null
if (!did_last_blind && !output_scalar.IsNull()) {
// Subtract input scalar from output scalar
if (!SubtractScalars(output_scalar, input_scalar)) return BlindingStatus::SCALAR_UNABLE;
// Add to PSBT
psbt.m_scalar_offsets.insert(output_scalar);
}
// Make sure that we blinded some outputs if we have blinded inputs
if (our_input_data.size() > 0 && our_blinds == 0) {
return BlindingStatus::NO_BLIND_OUTPUTS;
}
return BlindingStatus::OK;
}