forked from NicolasFlamel1/Secp256k1-zkp-NPM-Package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·1752 lines (1257 loc) · 46.9 KB
/
main.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Header files
#include <climits>
#include <cerrno>
#include <string>
#include <vector>
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Header files
#include <emscripten.h>
#include "secp256k1_aggsig.h"
#include "secp256k1_bulletproofs.h"
#include "secp256k1_commitment.h"
#include "secp256k1_ecdh.h"
// Otherwise
#else
// Header files
extern "C" {
#include "secp256k1_aggsig.h"
#include "secp256k1_bulletproofs.h"
#include "secp256k1_commitment.h"
#include "secp256k1_ecdh.h"
}
#endif
using namespace std;
// Definitions
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Export
#define EXPORT extern "C"
// Otherwise
#else
// Export
#define EXPORT static
// Emscripten keepalive
#define EMSCRIPTEN_KEEPALIVE
#endif
// Constants
// Blind size
static const size_t BLIND_SIZE = 32;
// Generator J public
static const secp256k1_pubkey GENERATOR_J_PUBLIC = {
{
0x5F, 0x15, 0x21, 0x36, 0x93, 0x93, 0x01, 0x2A, 0x8D, 0x8B, 0x39, 0x7E, 0x9B, 0xF4, 0x54, 0x29, 0x2F, 0x5A, 0x1B, 0x3D, 0x38, 0x85, 0x16, 0xC2, 0xF3, 0x03, 0xFC, 0x95, 0x67, 0xF5, 0x60, 0xB8, 0x3A, 0xC4, 0xC5, 0xA6, 0xDC, 0xA2, 0x01, 0x59, 0xFC, 0x56, 0xCF, 0x74, 0x9A, 0xA6, 0xA5, 0x65, 0x31, 0x6A, 0xA5, 0x03, 0x74, 0x42, 0x3F, 0x42, 0x53, 0x8F, 0xAA, 0x2C, 0xD3, 0x09, 0x3F, 0xA4
}
};
// Secret key size
static const size_t SECRET_KEY_SIZE = 32;
// Commit size
static const size_t COMMIT_SIZE = 33;
// Public key size
static const size_t PUBLIC_KEY_SIZE = 33;
// Uncompressed public key size
static const size_t UNCOMPRESSED_PUBLIC_KEY_SIZE = 65;
// Bulletproof proof size
static const size_t BULLETPROOF_PROOF_SIZE = 675;
// Bulletproof message size
static const size_t BULLETPROOF_MESSAGE_SIZE = 20;
// Decimal number base
static const int DECIMAL_NUMBER_BASE = 10;
// Scratch space size
static const size_t SCRATCH_SPACE_SIZE = 30 * 1024;
// Number of generators
static const size_t NUMBER_OF_GENERATORS = 256;
// Bits in a byte
static const size_t BITS_IN_A_BYTE = 8;
// Single-signer signature size
static const size_t SINGLE_SIGNER_SIGNATURE_SIZE = 64;
// Single-signer message size
static const size_t SINGLE_SIGNER_MESSAGE_SIZE = 32;
// Seed size
static const size_t SEED_SIZE = 32;
// Bits proven per range
static const size_t BITS_PROVEN_PER_RANGE = sizeof(uint64_t) * BITS_IN_A_BYTE;
// Nonce size
static const size_t NONCE_SIZE = 32;
// Tweak size
static const size_t TWEAK_SIZE = 32;
// Maximum message hash signature size
static const size_t MAXIMUM_MESSAGE_HASH_SIGNATURE_SIZE = 72;
// Message hash size
static const size_t MESSAGE_HASH_SIZE = 32;
// Tau x size
static const size_t TAU_X_SIZE = 32;
// Global variables
// Context
static secp256k1_context *context = nullptr;
// Scratch space
static secp256k1_scratch_space *scratchSpace = nullptr;
// Generators
static secp256k1_bulletproof_generators *generators = nullptr;
// Function prototypes
// Initialize
EXPORT bool EMSCRIPTEN_KEEPALIVE initialize(const uint8_t *seed, size_t seedSize);
// Uninistalize
EXPORT bool EMSCRIPTEN_KEEPALIVE uninitialize();
// Blind size
EXPORT size_t EMSCRIPTEN_KEEPALIVE blindSize();
// Blind switch
EXPORT bool EMSCRIPTEN_KEEPALIVE blindSwitch(uint8_t *result, const uint8_t *blind, size_t blindSize, const char *value);
// Blind sum
EXPORT bool EMSCRIPTEN_KEEPALIVE blindSum(uint8_t *result, const uint8_t *blinds, size_t blindsSizes[], size_t numberOfBlinds, size_t numberOfPositiveBlinds);
// Is valid secret key
EXPORT bool EMSCRIPTEN_KEEPALIVE isValidSecretKey(const uint8_t *secretKey, size_t secretKeySize);
// Is valid public key
EXPORT bool EMSCRIPTEN_KEEPALIVE isValidPublicKey(const uint8_t *publicKey, size_t publicKeySize);
// Is valid commit
EXPORT bool EMSCRIPTEN_KEEPALIVE isValidCommit(const uint8_t *commit, size_t commitSize);
// Is valid single-signer signature
EXPORT bool EMSCRIPTEN_KEEPALIVE isValidSingleSignerSignature(const uint8_t *signature, size_t signatureSize);
// Bulletproof size
EXPORT size_t EMSCRIPTEN_KEEPALIVE bulletproofProofSize();
// Create bulletproof
EXPORT bool EMSCRIPTEN_KEEPALIVE createBulletproof(uint8_t *proof, char *proofSize, const uint8_t *blind, size_t blindSize, const char *value, const uint8_t *nonce, size_t nonceSize, const uint8_t *privateNonce, size_t privateNonceSize, const uint8_t *extraCommit, size_t extraCommitSize, const uint8_t *message, size_t messageSize);
// Create bulletproof blindless
EXPORT bool EMSCRIPTEN_KEEPALIVE createBulletproofBlindless(uint8_t *proof, char *proofSize, uint8_t *tauX, size_t tauXSize, const uint8_t *tOne, size_t tOneSize, const uint8_t *tTwo, size_t tTwoSize, const uint8_t *commit, size_t commitSize, const char *value, const uint8_t *nonce, size_t nonceSize, const uint8_t *extraCommit, size_t extraCommitSize, const uint8_t *message, size_t messageSize);
// Bulletproof message size
EXPORT size_t EMSCRIPTEN_KEEPALIVE bulletproofMessageSize();
// Rewind bulletproof
EXPORT bool EMSCRIPTEN_KEEPALIVE rewindBulletproof(char *value, uint8_t *blind, uint8_t *message, const uint8_t *proof, size_t proofSize, const uint8_t *commit, size_t commitSize, const uint8_t *nonce, size_t nonceSize);
// Verify bulletproof
EXPORT bool EMSCRIPTEN_KEEPALIVE verifyBulletproof(const uint8_t *proof, size_t proofSize, const uint8_t *commit, size_t commitSize, const uint8_t *extraCommit, size_t extraCommitSize);
// Public key size
EXPORT size_t EMSCRIPTEN_KEEPALIVE publicKeySize();
// Public key from secret key
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyFromSecretKey(uint8_t *publicKey, const uint8_t *secretKey, size_t secretKeySize);
// Public key from data
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyFromData(uint8_t *publicKey, const uint8_t *data, size_t dataSize);
// Uncompressed public key size
EXPORT size_t EMSCRIPTEN_KEEPALIVE uncompressedPublicKeySize();
// Uncompress public key
EXPORT bool EMSCRIPTEN_KEEPALIVE uncompressPublicKey(uint8_t *uncompressedPublicKey, const uint8_t *publicKey, size_t publicKeySize);
// Secret key size
EXPORT size_t EMSCRIPTEN_KEEPALIVE secretKeySize();
// Secret key tweak add
EXPORT bool EMSCRIPTEN_KEEPALIVE secretKeyTweakAdd(uint8_t *result, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *tweak, size_t tweakSize);
// Public key tweak add
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyTweakAdd(uint8_t *result, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *tweak, size_t tweakSize);
// Secret key tweak multiply
EXPORT bool EMSCRIPTEN_KEEPALIVE secretKeyTweakMultiply(uint8_t *result, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *tweak, size_t tweakSize);
// Public key tweak multiply
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyTweakMultiply(uint8_t *result, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *tweak, size_t tweakSize);
// Shared secret key from secret key and public key
EXPORT bool EMSCRIPTEN_KEEPALIVE sharedSecretKeyFromSecretKeyAndPublicKey(uint8_t *sharedSecretKey, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *publicKey, size_t publicKeySize);
// Commit size
EXPORT size_t EMSCRIPTEN_KEEPALIVE commitSize();
// Pedersen commit
EXPORT bool EMSCRIPTEN_KEEPALIVE pedersenCommit(uint8_t *result, const uint8_t *blind, size_t blindSize, const char *value);
// Pedersen commit sum
EXPORT bool EMSCRIPTEN_KEEPALIVE pedersenCommitSum(uint8_t *result, const uint8_t *positiveCommits, size_t positiveCommitsSizes[], size_t numberOfPositiveCommits, const uint8_t *negativeCommits, size_t negativeCommitsSizes[], size_t numberOfNegativeCommits);
// Pedersen commit to public key
EXPORT bool EMSCRIPTEN_KEEPALIVE pedersenCommitToPublicKey(uint8_t *publicKey, const uint8_t *commit, size_t commitSize);
// Public key to Pedersen commit
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyToPedersenCommit(uint8_t *commit, const uint8_t *publicKey, size_t publicKeySize);
// Single-signer signature size
EXPORT size_t EMSCRIPTEN_KEEPALIVE singleSignerSignatureSize();
// Seed size
EXPORT size_t EMSCRIPTEN_KEEPALIVE seedSize();
// Create single-signer signature
EXPORT bool EMSCRIPTEN_KEEPALIVE createSingleSignerSignature(uint8_t *signature, const uint8_t *message, size_t messageSize, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *secretNonce, size_t secretNonceSize, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *publicNonce, size_t publicNonceSize, const uint8_t *publicNonceTotal, size_t publicNonceTotalSize, const uint8_t *seed, size_t seedSize);
// Add single-signer signatures
EXPORT bool EMSCRIPTEN_KEEPALIVE addSingleSignerSignatures(uint8_t *result, const uint8_t *signatures, size_t signaturesSizes[], size_t numberOfSignatures, const uint8_t *publicNonceTotal, size_t publicNonceTotalSize);
// Verify single-signer signature
EXPORT bool EMSCRIPTEN_KEEPALIVE verifySingleSignerSignature(const uint8_t *signature, size_t signatureSize, const uint8_t *message, size_t messageSize, const uint8_t *publicNonce, size_t publicNonceSize, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *publicKeyTotal, size_t publicKeyTotalSize, bool isPartial);
// Single-signer signature from data
EXPORT bool EMSCRIPTEN_KEEPALIVE singleSignerSignatureFromData(uint8_t *signature, const uint8_t *data, size_t dataSize);
// Uncompact single-signer signature size
EXPORT size_t EMSCRIPTEN_KEEPALIVE uncompactSingleSignerSignatureSize();
// Compact single-signer signature
EXPORT bool EMSCRIPTEN_KEEPALIVE compactSingleSignerSignature(uint8_t *result, const uint8_t *signature, size_t signatureSize);
// Uncompact single-signer signature
EXPORT bool EMSCRIPTEN_KEEPALIVE uncompactSingleSignerSignature(uint8_t *result, const uint8_t *signature, size_t signatureSize);
// Combine public keys
EXPORT bool EMSCRIPTEN_KEEPALIVE combinePublicKeys(uint8_t *result, const uint8_t *publicKeys, size_t publicKeysSizes[], size_t numberOfPublicKeys);
// Nonce size
EXPORT size_t EMSCRIPTEN_KEEPALIVE nonceSize();
// Create secret nonce
EXPORT bool EMSCRIPTEN_KEEPALIVE createSecretNonce(uint8_t *nonce, const uint8_t *seed, size_t seedSize);
// Maximum message hash signature size
EXPORT size_t EMSCRIPTEN_KEEPALIVE maximumMessageHashSignatureSize();
// Create message hash signature
EXPORT bool EMSCRIPTEN_KEEPALIVE createMessageHashSignature(uint8_t *signature, char *signatureSize, const uint8_t *messageHash, size_t messageHashSize, const uint8_t *secretKey, size_t secretKeySize);
// Verify message hash signature
EXPORT bool EMSCRIPTEN_KEEPALIVE verifyMessageHashSignature(const uint8_t *signature, size_t signatureSize, const uint8_t *messageHash, size_t messageHashSize, const uint8_t *publicKey, size_t publicKeySize);
// Is zero array
static bool isZeroArray(const void *value, size_t size);
// Supporting function implementation
// Initialize
bool initialize(const uint8_t *seed, size_t seedSize) {
// Check if seed is invalid
if(seedSize != SEED_SIZE) {
// Return false
return false;
}
// Check if creating context failed
context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
if(!context) {
// Return false
return false;
}
// Check if randomizing context failed
if(!secp256k1_context_randomize(context, seed)) {
// Return false
return false;
}
// Check if creating scratch space failed
scratchSpace = secp256k1_scratch_space_create(context, SCRATCH_SPACE_SIZE);
if(!scratchSpace) {
// Return false
return false;
}
// Check if creating generators failed
generators = secp256k1_bulletproof_generators_create(context, &secp256k1_generator_const_g, NUMBER_OF_GENERATORS);
if(!generators) {
// Return false
return false;
}
// Return true
return true;
}
// Uninitialize
bool uninitialize() {
// Check if generators exist
if(generators) {
// Destroy generators
secp256k1_bulletproof_generators_destroy(context, generators);
// Clear generators
generators = nullptr;
}
// Check if scratch space exists
if(scratchSpace) {
// Destroy scratch space
secp256k1_scratch_space_destroy(scratchSpace);
// Clear scratch space
scratchSpace = nullptr;
}
// Check if context exists
if(context) {
// Destroy context
secp256k1_context_destroy(context);
// Clear context
context = nullptr;
}
// Return true
return true;
}
// Blind size
size_t blindSize() {
// Return blind size
return BLIND_SIZE;
}
// Blind switch
bool blindSwitch(uint8_t *result, const uint8_t *blind, size_t blindSize, const char *value) {
// Check if blind is invalid
if(blindSize != BLIND_SIZE) {
// Return false
return false;
}
// Check if parsing value as a number failed
char *lastCharacter;
errno = 0;
uint64_t numericValue = strtoull(value, &lastCharacter, DECIMAL_NUMBER_BASE);
if(lastCharacter == value || *lastCharacter != '\0' || value[0] == '-' || value[0] == '+' || (numericValue == ULLONG_MAX && errno == ERANGE)) {
// Return false
return false;
}
// Check if performing blind switch failed
if(!secp256k1_blind_switch(context, result, blind, numericValue, &secp256k1_generator_const_h, &secp256k1_generator_const_g, &GENERATOR_J_PUBLIC)) {
// Return false
return false;
}
// Return true
return true;
}
// Blind sum
bool blindSum(uint8_t *result, const uint8_t *blinds, size_t blindsSizes[], size_t numberOfBlinds, size_t numberOfPositiveBlinds) {
// Go through all blinds
vector<const uint8_t *> blindsAddresses(numberOfBlinds);
for(size_t i = 0; i < numberOfBlinds; ++i) {
// Check if blind is invalid
if(blindsSizes[i] != BLIND_SIZE) {
// Return false
return false;
}
// Store blind's address
blindsAddresses[i] = &blinds[i * BLIND_SIZE];
}
// Check if performing pedersen blind sum failed
if(!secp256k1_pedersen_blind_sum(context, result, blindsAddresses.data(), numberOfBlinds, numberOfPositiveBlinds))
// Return false
return false;
// Return true
return true;
}
// Is valid secret key
bool isValidSecretKey(const uint8_t *secretKey, size_t secretKeySize) {
// Check if secret key size is invalid
if(secretKeySize != SECRET_KEY_SIZE) {
// Return false
return false;
}
// Check if cannot verify secret key
if(!secp256k1_ec_seckey_verify(context, secretKey)) {
// Return false
return false;
}
// Return true
return true;
}
// Is valid public key
bool isValidPublicKey(const uint8_t *publicKey, size_t publicKeySize) {
// Check if public key size is invalid
if(publicKeySize != PUBLIC_KEY_SIZE) {
// Return false
return false;
}
// Check if cannot verify public key
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_parse(context, &publicKeyData, publicKey, publicKeySize)) {
// Clear memory
explicit_bzero(&publicKeyData, sizeof(publicKeyData));
// Return false
return false;
}
// Clear memory
explicit_bzero(&publicKeyData, sizeof(publicKeyData));
// Return true
return true;
}
// Is valid commit
bool isValidCommit(const uint8_t *commit, size_t commitSize) {
// Check if commit size is invalid
if(commitSize != COMMIT_SIZE) {
// Return false
return false;
}
// Check if cannot verify commit
secp256k1_pedersen_commitment commitData;
if(!secp256k1_pedersen_commitment_parse(context, &commitData, commit)) {
// Return false
return false;
}
// Return true
return true;
}
// Is valid single-signer signature
bool isValidSingleSignerSignature(const uint8_t *signature, size_t signatureSize) {
// Check if signature size is invalid
if(signatureSize != SINGLE_SIGNER_SIGNATURE_SIZE) {
// Return false
return false;
}
// Check if cannot verify signature
secp256k1_ecdsa_signature signatureData;
if(!secp256k1_ecdsa_signature_parse_compact(context, &signatureData, signature)) {
// Return false
return false;
}
// Return true
return true;
}
// Bulletproof proof size
size_t bulletproofProofSize() {
// Return bulletproof proof size
return BULLETPROOF_PROOF_SIZE;
}
// Create bulletproof
bool createBulletproof(uint8_t *proof, char *proofSize, const uint8_t *blind, size_t blindSize, const char *value, const uint8_t *nonce, size_t nonceSize, const uint8_t *privateNonce, size_t privateNonceSize, const uint8_t *extraCommit, size_t extraCommitSize, const uint8_t *message, size_t messageSize) {
// Check if blind is invalid
if(blindSize != BLIND_SIZE) {
// Return false
return false;
}
// Check if parsing value as a number failed
char *lastCharacter;
errno = 0;
uint64_t numericValue = strtoull(value, &lastCharacter, DECIMAL_NUMBER_BASE);
if(lastCharacter == value || *lastCharacter != '\0' || value[0] == '-' || value[0] == '+' || (numericValue == ULLONG_MAX && errno == ERANGE)) {
// Return false
return false;
}
// Check if nonce is invalid
if(nonceSize != NONCE_SIZE) {
// Return false
return false;
}
// Check if private nonce is invalid
if(privateNonceSize != NONCE_SIZE) {
// Return false
return false;
}
// Check if message is invalid
if(messageSize != BULLETPROOF_MESSAGE_SIZE) {
// Return false
return false;
}
// Check if creating bulletproof failed
size_t numericProofSize = BULLETPROOF_PROOF_SIZE;
if(!secp256k1_bulletproof_rangeproof_prove(context, scratchSpace, generators, proof, &numericProofSize, nullptr, nullptr, nullptr, &numericValue, nullptr, &blind, nullptr, 1, &secp256k1_generator_const_h, BITS_PROVEN_PER_RANGE, nonce, privateNonce, extraCommitSize ? extraCommit : nullptr, extraCommitSize, message)) {
// Return false
return false;
}
// Copy numeric proof size to proof size
string stringProofSize = to_string(numericProofSize);
memcpy(proofSize, stringProofSize.c_str(), stringProofSize.length() + sizeof('\0'));
// Return true
return true;
}
// Create bulletproof blindless
bool createBulletproofBlindless(uint8_t *proof, char *proofSize, uint8_t *tauX, size_t tauXSize, const uint8_t *tOne, size_t tOneSize, const uint8_t *tTwo, size_t tTwoSize, const uint8_t *commit, size_t commitSize, const char *value, const uint8_t *nonce, size_t nonceSize, const uint8_t *extraCommit, size_t extraCommitSize, const uint8_t *message, size_t messageSize) {
// Check if tau x is invalid
if(tauXSize != TAU_X_SIZE) {
// Return false
return false;
}
// Check if parsing t one failed
secp256k1_pubkey tOneData;
if(!secp256k1_ec_pubkey_parse(context, &tOneData, tOne, tOneSize)) {
// Return false
return false;
}
// Check if parsing t two failed
secp256k1_pubkey tTwoData;
if(!secp256k1_ec_pubkey_parse(context, &tTwoData, tTwo, tTwoSize)) {
// Return false
return false;
}
// Check if parsing commit failed
secp256k1_pedersen_commitment commitData;
if(!secp256k1_pedersen_commitment_parse(context, &commitData, commit)) {
// Return false
return false;
}
// Check if parsing value as a number failed
char *lastCharacter;
errno = 0;
uint64_t numericValue = strtoull(value, &lastCharacter, DECIMAL_NUMBER_BASE);
if(lastCharacter == value || *lastCharacter != '\0' || value[0] == '-' || value[0] == '+' || (numericValue == ULLONG_MAX && errno == ERANGE)) {
// Return false
return false;
}
// Check if nonce is invalid
if(nonceSize != NONCE_SIZE) {
// Return false
return false;
}
// Check if message is invalid
if(messageSize != BULLETPROOF_MESSAGE_SIZE) {
// Return false
return false;
}
// Check if creating bulletproof blindless failed
size_t numericProofSize = BULLETPROOF_PROOF_SIZE;
const secp256k1_pedersen_commitment *commits[] = {
&commitData
};
if(!secp256k1_bulletproof_rangeproof_prove(context, scratchSpace, generators, proof, &numericProofSize, tauX, &tOneData, &tTwoData, &numericValue, nullptr, nullptr, commits, 1, &secp256k1_generator_const_h, BITS_PROVEN_PER_RANGE, nonce, nullptr, extraCommitSize ? extraCommit : nullptr, extraCommitSize, message)) {
// Return false
return false;
}
// Copy numeric proof size to proof size
string stringProofSize = to_string(numericProofSize);
memcpy(proofSize, stringProofSize.c_str(), stringProofSize.length() + sizeof('\0'));
// Return true
return true;
}
// Bulletproof message size
size_t bulletproofMessageSize() {
// Return bulletproof message size
return BULLETPROOF_MESSAGE_SIZE;
}
// Rewind bulletproof
bool rewindBulletproof(char *value, uint8_t *blind, uint8_t *message, const uint8_t *proof, size_t proofSize, const uint8_t *commit, size_t commitSize, const uint8_t *nonce, size_t nonceSize) {
// Check if commit is invalid
if(commitSize != COMMIT_SIZE) {
// Return false
return false;
}
// Check if nonce is invalid
if(nonceSize != NONCE_SIZE) {
// Return false
return false;
}
// Check if parsing commit failed
secp256k1_pedersen_commitment commitData;
if(!secp256k1_pedersen_commitment_parse(context, &commitData, commit)) {
// Return false
return false;
}
// Check if performing bulletproof rangeproof rewind failed
uint64_t numericValue;
if(!secp256k1_bulletproof_rangeproof_rewind(context, &numericValue, blind, proof, proofSize, 0, &commitData, &secp256k1_generator_const_h, nonce, nullptr, 0, message)) {
// Return false
return false;
}
// Copy numeric value to value
string stringValue = to_string(numericValue);
memcpy(value, stringValue.c_str(), stringValue.length() + sizeof('\0'));
// Return true
return true;
}
// Verify bulletproof
bool verifyBulletproof(const uint8_t *proof, size_t proofSize, const uint8_t *commit, size_t commitSize, const uint8_t *extraCommit, size_t extraCommitSize) {
// Check if commit is invalid
if(commitSize != COMMIT_SIZE) {
// Return false
return false;
}
// Check if parsing commit failed
secp256k1_pedersen_commitment commitData;
if(!secp256k1_pedersen_commitment_parse(context, &commitData, commit)) {
// Return false
return false;
}
// Check if cannot verify bulletproof
if(!secp256k1_bulletproof_rangeproof_verify(context, scratchSpace, generators, proof, proofSize, nullptr, &commitData, 1, BITS_PROVEN_PER_RANGE, &secp256k1_generator_const_h, extraCommitSize ? extraCommit : nullptr, extraCommitSize)) {
// Return false
return false;
}
// Return true
return true;
}
// Public key size
size_t publicKeySize() {
// Return public key size
return PUBLIC_KEY_SIZE;
}
// Public key from secret key
bool publicKeyFromSecretKey(uint8_t *publicKey, const uint8_t *secretKey, size_t secretKeySize) {
// Check if secret key is invalid
if(!isValidSecretKey(secretKey, secretKeySize)) {
// Return false
return false;
}
// Check if creating public key from secret key failed
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_create(context, &publicKeyData, secretKey)) {
// Clear memory
explicit_bzero(&publicKeyData, sizeof(publicKeyData));
// Return false
return false;
}
// Check if serializing public key failed
size_t publicKeySize = PUBLIC_KEY_SIZE;
if(!secp256k1_ec_pubkey_serialize(context, publicKey, &publicKeySize, &publicKeyData, SECP256K1_EC_COMPRESSED)) {
// Clear memory
explicit_bzero(&publicKeyData, sizeof(publicKeyData));
// Return false
return false;
}
// Clear memory
explicit_bzero(&publicKeyData, sizeof(publicKeyData));
// Return true
return true;
}
// Public key from data
bool publicKeyFromData(uint8_t *publicKey, const uint8_t *data, size_t dataSize) {
// Check if creating public key from data failed
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_parse(context, &publicKeyData, data, dataSize)) {
// Return false
return false;
}
// Check if serializing public key failed
size_t publicKeySize = PUBLIC_KEY_SIZE;
if(!secp256k1_ec_pubkey_serialize(context, publicKey, &publicKeySize, &publicKeyData, SECP256K1_EC_COMPRESSED)) {
// Return false
return false;
}
// Return true
return true;
}
// Uncompressed public key size
size_t uncompressedPublicKeySize() {
// Return uncompressed public key size
return UNCOMPRESSED_PUBLIC_KEY_SIZE;
}
// Uncompress public key
bool uncompressPublicKey(uint8_t *uncompressedPublicKey, const uint8_t *publicKey, size_t publicKeySize) {
// Check if public key size is invalid
if(publicKeySize != PUBLIC_KEY_SIZE) {
// Return false
return false;
}
// Check if parsing public key failed
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_parse(context, &publicKeyData, publicKey, publicKeySize)) {
// Return false
return false;
}
// Check if serializing public key failed
size_t uncompressedPublicKeySize = UNCOMPRESSED_PUBLIC_KEY_SIZE;
if(!secp256k1_ec_pubkey_serialize(context, uncompressedPublicKey, &uncompressedPublicKeySize, &publicKeyData, SECP256K1_EC_UNCOMPRESSED)) {
// Return false
return false;
}
// Return true
return true;
}
// Secret key size
size_t secretKeySize() {
// Return secret key size
return SECRET_KEY_SIZE;
}
// Secret key tweak add
bool secretKeyTweakAdd(uint8_t *result, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *tweak, size_t tweakSize) {
// Check if secret key is invalid and it's not zero
if((!isValidSecretKey(secretKey, secretKeySize) && !isZeroArray(secretKey, secretKeySize)) || secretKeySize != SECRET_KEY_SIZE) {
// Return false
return false;
}
// Check if tweak is invalid
if(tweakSize != TWEAK_SIZE) {
// Return false
return false;
}
// Set result to secret key
memcpy(result, secretKey, secretKeySize);
// Check if performing secret key tweak add failed
if(!secp256k1_ec_privkey_tweak_add(context, result, tweak)) {
// Clear memory
explicit_bzero(result, secretKeySize);
// Return false
return false;
}
// Check if result is invalid
if(!isValidSecretKey(result, secretKeySize)) {
// Clear memory
explicit_bzero(result, secretKeySize);
// Return false
return false;
}
// Return true
return true;
}
// Public key tweak add
bool publicKeyTweakAdd(uint8_t *result, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *tweak, size_t tweakSize) {
// Check if parsing public key failed
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_parse(context, &publicKeyData, publicKey, publicKeySize)) {
// Return false
return false;
}
// Check if tweak is invalid
if(tweakSize != TWEAK_SIZE) {
// Return false
return false;
}
// Check if performing public key tweak add failed
if(!secp256k1_ec_pubkey_tweak_add(context, &publicKeyData, tweak)) {
// Return false
return false;
}
// Check if serializing public key failed
size_t newPublicKeySize = PUBLIC_KEY_SIZE;
if(!secp256k1_ec_pubkey_serialize(context, result, &newPublicKeySize, &publicKeyData, SECP256K1_EC_COMPRESSED)) {
// Return false
return false;
}
// Return true
return true;
}
// Secret key tweak multiply
bool secretKeyTweakMultiply(uint8_t *result, const uint8_t *secretKey, size_t secretKeySize, const uint8_t *tweak, size_t tweakSize) {
// Check if secret key is invalid
if(!isValidSecretKey(secretKey, secretKeySize)) {
// Return false
return false;
}
// Check if tweak is invalid
if(tweakSize != TWEAK_SIZE) {
// Return false
return false;
}
// Set result to secret key
memcpy(result, secretKey, secretKeySize);
// Check if performing secret key tweak multiply failed
if(!secp256k1_ec_privkey_tweak_mul(context, result, tweak)) {
// Clear memory
explicit_bzero(result, secretKeySize);
// Return false
return false;
}
// Check if result is still invalid
if(!isValidSecretKey(result, secretKeySize)) {
// Clear memory
explicit_bzero(result, secretKeySize);
// Return false
return false;
}
// Return true
return true;
}
// Public key tweak multiply
bool publicKeyTweakMultiply(uint8_t *result, const uint8_t *publicKey, size_t publicKeySize, const uint8_t *tweak, size_t tweakSize) {
// Check if parsing public key failed
secp256k1_pubkey publicKeyData;
if(!secp256k1_ec_pubkey_parse(context, &publicKeyData, publicKey, publicKeySize)) {
// Return false
return false;
}
// Check if tweak is invalid
if(tweakSize != TWEAK_SIZE) {
// Return false
return false;
}
// Check if performing public key tweak multiply failed
if(!secp256k1_ec_pubkey_tweak_mul(context, &publicKeyData, tweak)) {
// Return false
return false;
}