forked from NoahBz/Easy-BigInt
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCryptographyAsymmetricKey.cpp
More file actions
984 lines (808 loc) · 27.9 KB
/
Copy pathCryptographyAsymmetricKey.cpp
File metadata and controls
984 lines (808 loc) · 27.9 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
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
/*
MIT License
Copyright (c) 2024-2050 Twilight-Dream & With-Sky
https://github.com/Twilight-Dream-Of-Magic/
https://github.com/With-Sky
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "CryptographyAsymmetricKey.hpp"
namespace TwilightDream::CryptographyAsymmetric
{
void RSA::GeneratePrimeNumbers(std::vector<FindPrimeState>& primes, size_t primes_index)
{
BigInteger PrimeNumber = BigInteger::RandomGenerateNBit(bit_count);
PrimeNumber = MIN + (PrimeNumber % RANGE_INTEGER);
//Prime numbers cannot be found inside an even number except two.
if(PrimeNumber.IsEven())
{
PrimeNumber |= ONE;
}
bool IsPrime = Tester.IsPrime(PrimeNumber);
while ( !IsPrime )
{
/*
https://crypto.stackexchange.com/questions/1970/how-are-primes-generated-for-rsa
Generate a random 512 bit odd number, say p
Test to see if p is prime; if it is, return p; this is expected to occur after testing about Log(p)/2∼177 candidates
Otherwise set p=p+2, goto 2
*/
PrimeNumber += TWO;
if (PrimeNumber > MAX)
{
PrimeNumber = MIN;
if (PrimeNumber.IsEven())
{
PrimeNumber += ONE;
}
}
IsPrime = Tester.IsPrime(PrimeNumber);
}
primes[primes_index].IsPrime = IsPrime;
primes[primes_index].Number = PrimeNumber;
}
std::optional<RSA::FindPrimeState> RSA::GeneratePrimesInParallelFunctions()
{
std::vector<std::future<void>> futures;
const size_t max_thread_count = 4; //std::thread::hardware_concurrency();
std::vector<FindPrimeState> prime_map( max_thread_count, FindPrimeState() );
for ( size_t i = 0; i < prime_map.size(); ++i )
{
futures.push_back( std::async( std::launch::async, &RSA::GeneratePrimeNumbers, this, std::ref( prime_map ), i ) );
}
// Wait for every prime-search worker and propagate any worker exception.
for ( auto& Future : futures )
{
Future.get();
}
for ( const auto& state : prime_map )
{
if ( state.IsPrime )
{
//std::cout << "Generate BigInteger Is Prime: True" << std::endl;
return state;
}
else
{
//std::cout << "Generate BigInteger Is Prime: False" << std::endl;
continue;
}
}
return std::nullopt; // Return std::nullopt if no prime is found
}
RSA::BigInteger RSA::GeneratePrimeNumber( size_t bit_count )
{
if ( bit_count == 0 )
return BigInteger( 0 );
this->bit_count = bit_count;
MIN = BigInteger(TWO).Power(bit_count - 1);
MAX = BigInteger(TWO).Power(bit_count) - ONE;
RANGE_INTEGER = MAX - MIN + ONE;
std::optional<RSA::FindPrimeState> state_data = GeneratePrimesInParallelFunctions();
if(state_data.has_value())
return state_data.value().Number;
else
return BigInteger(0);
}
RSA::RSA_AlgorithmNumbers RSA::GenerateKeys( size_t bit_count, bool is_pkcs )
{
if(bit_count == 0 || bit_count == 1)
{
throw std::invalid_argument("Invalid bit_count values.");
}
// Generate two large prime numbers
BigInteger PrimeNumberA = 0;
BigInteger PrimeNumberB = 0;
GeneratePrimesAgain:
do
{
PrimeNumberA = GeneratePrimeNumber(bit_count / 2);
PrimeNumberB = GeneratePrimeNumber(bit_count - bit_count / 2);
}
while (PrimeNumberA.IsZero() || PrimeNumberB.IsZero() || PrimeNumberA == PrimeNumberB);
// Calculate n = p * q
BigInteger AlgorithmModulus = PrimeNumberA * PrimeNumberB;
if(AlgorithmModulus.BitLength() != bit_count)
{
goto GeneratePrimesAgain;
}
// Calculate totient(n) = phi(n) = (p - 1) * (q - 1)
BigInteger Totient_PhiFunctionValue = (PrimeNumberA - ONE) * (PrimeNumberB - ONE);
//std::cout << "---------------------------------------------------------------------------------------------------\n";
//std::cout << "RSA Algorithm: Number Prime A is: " << PrimeNumberA.ToString(10) << "\n\n";
//std::cout << "RSA Algorithm: Number Prime B is: " << PrimeNumberB.ToString(10) << "\n\n";
//std::cout << "RSA Algorithm: Number Modulus is: " << AlgorithmModulus.ToString(10) << "\n\n";
//std::cout << "RSA Algorithm: Number Totient_PhiFunctionValue is: " << Totient_PhiFunctionValue.ToString(10) << "\n\n\n";
BigInteger EncryptExponent = 0;
//Enable security and performance optimization?
if(is_pkcs)
{
EncryptExponent = 65537;
if (BigInteger::GCD(EncryptExponent, Totient_PhiFunctionValue) != ONE)
{
throw std::runtime_error("PKCS exponent 65537 is not coprime with phi(n).");
}
}
else
{
BigInteger min = 2;
BigInteger max = (BigInteger {1} << bit_count - 1) - 1;
BigInteger range_integer = max - min + 1;
// Ensure that e is odd and greater than 2
while ( EncryptExponent <= 2 )
{
// Choosing exponents for RSA encryption
// encryption_exponents = RandomNumberRange(2, 2^{bit\_count} - 1)
EncryptExponent = BigInteger::RandomGenerateNBit(bit_count + 1) % range_integer + min;
// Check if e is not 3
// encryption_exponents equal 3 is not safe
if(EncryptExponent == THREE)
{
continue;
}
if(EncryptExponent.IsEven())
{
EncryptExponent.SetBit(0);
}
}
// Check if gcd(e, totient(n)) = 1, result is false then repeat this loop.
do
{
if (BigInteger::GCD(EncryptExponent, Totient_PhiFunctionValue) != ONE)
{
EncryptExponent += TWO;
//std::cout << "Updated Temporary Number EncryptExponent is:" << EncryptExponent.ToString(10) << "\n";
continue;
}
break;
} while (true);
//std::cout << "The large odd number EncryptExponent is computed." << "\n";
}
// Choosing exponents for RSA decryption
// Need calculate d such that decryption_exponents = encryption_exponents^{-1} (mod totient(n))
BigSignedInteger DecryptExponent = BigSignedInteger::ModuloInverse(EncryptExponent, Totient_PhiFunctionValue);
//std::cout << "The large odd number DecryptExponent is computed." << "\n\n\n";
//std::cout << "RSA Algorithm: EncryptExponent is: " << EncryptExponent.ToString(10) << "\n\n";
//std::cout << "RSA Algorithm: DecryptExponent is: " << DecryptExponent.ToString(10) << "\n\n";
//std::cout << "---------------------------------------------------------------------------------------------------\n";
RSA::RSA_AlgorithmNumbers AlgorithmNumbers = RSA::RSA_AlgorithmNumbers();
AlgorithmNumbers.EncryptExponent = EncryptExponent;
AlgorithmNumbers.DecryptExponent = static_cast<BigInteger>(DecryptExponent);
AlgorithmNumbers.AlgorithmModulus = AlgorithmModulus;
return AlgorithmNumbers;
}
void RSA::Encryption(BigInteger& PlainMessage, const BigInteger& EncryptExponent, const BigInteger& AlgorithmModulus )
{
if(PlainMessage >= AlgorithmModulus)
{
return;
}
//std::cout << "---------------------------------\n";
//std::cout << "PlainMessage: " << PlainMessage.ToString( 10 ) << "\n";
//std::cout << "EncryptExponent: " << EncryptExponent.ToString( 10 ) << "\n";
//std::cout << "AlgorithmModulus: " << AlgorithmModulus.ToString( 10 ) << "\n";
PlainMessage.PowerWithModulo(EncryptExponent, AlgorithmModulus);
//std::cout << "CipherText: " << PlainMessage.ToString( 10 ) << "\n";
//std::cout << "---------------------------------\n";
}
void RSA::Decryption(BigInteger& CipherMessage, const BigInteger& DecryptExponent, const BigInteger& AlgorithmModulus )
{
if(CipherMessage >= AlgorithmModulus)
{
return;
}
//std::cout << "---------------------------------\n";
//std::cout << "CipherText: " << CipherMessage.ToString( 10 ) << "\n";
//std::cout << "DecryptExponent: " << DecryptExponent.ToString( 10 ) << "\n";
//std::cout << "AlgorithmModulus: " << AlgorithmModulus.ToString( 10 ) << "\n";
CipherMessage.PowerWithModulo(DecryptExponent, AlgorithmModulus);
//std::cout << "PlainMessage: " << CipherMessage.ToString( 10 ) << "\n";
//std::cout << "---------------------------------\n";
}
void FastRSA::GeneratePrimeNumbers(std::vector<FindPrimeState>& primes, size_t primes_index)
{
BigInteger PrimeNumber = BigInteger::RandomGenerateNBit(bit_count) % RANGE_INTEGER + MIN;
if (PrimeNumber.IsEven())
{
PrimeNumber += ONE;
}
bool isPrime = Tester.IsPrime(PrimeNumber);
while (!isPrime)
{
PrimeNumber += TWO;
if (PrimeNumber > MAX)
{
PrimeNumber = MIN;
if (PrimeNumber.IsEven())
{
PrimeNumber += ONE;
}
}
isPrime = Tester.IsPrime(PrimeNumber);
}
primes[primes_index].IsPrime = true;
primes[primes_index].Number = PrimeNumber;
}
std::optional<FastRSA::FindPrimeState> FastRSA::GeneratePrimesInParallelFunctions()
{
std::vector<std::future<void>> futures;
const size_t max_thread_count = 4;
std::vector<FindPrimeState> prime_map(max_thread_count, FindPrimeState());
for (size_t i = 0; i < prime_map.size(); ++i)
{
futures.push_back(
std::async(
std::launch::async,
&FastRSA::GeneratePrimeNumbers,
this,
std::ref(prime_map),
i
)
);
}
for ( auto& Future : futures )
{
Future.get();
}
for (const auto& state : prime_map)
{
if (state.IsPrime)
{
return state;
}
}
return std::nullopt;
}
FastRSA::BigInteger FastRSA::GeneratePrimeNumber(size_t bit_count)
{
if ( bit_count == 0 )
{
return BigInteger( 0 );
}
this->bit_count = bit_count;
MIN = BigInteger(TWO).Power(bit_count - 1);
MAX = BigInteger(TWO).Power(bit_count) - ONE;
RANGE_INTEGER = MAX - MIN + ONE;
std::optional<FindPrimeState> state_data = GeneratePrimesInParallelFunctions();
if (state_data.has_value())
{
return state_data.value().Number;
}
return BigInteger(0);
}
FastRSA::KeyPair FastRSA::GenerateKeys(size_t bit_count, bool use_pkcs_public_exponent)
{
if (bit_count <= 1)
{
throw std::invalid_argument("Invalid bit_count values.");
}
BigInteger p = 0;
BigInteger q = 0;
GeneratePrimesAgain:
do
{
p = GeneratePrimeNumber(bit_count / 2);
q = GeneratePrimeNumber(bit_count - bit_count / 2);
}
while (p.IsZero() || q.IsZero() || p == q);
BigInteger n = p * q;
if(n.BitLength() != bit_count)
{
goto GeneratePrimesAgain;
}
BigInteger phi = (p - ONE) * (q - ONE);
BigInteger e = 0;
if (use_pkcs_public_exponent)
{
e = 65537;
if (BigInteger::GCD(e, phi) != ONE)
{
throw std::runtime_error("PKCS exponent 65537 is not coprime with phi(n).");
}
}
else
{
BigInteger min = 2;
BigInteger max = (BigInteger{1} << (bit_count - 1)) - 1;
BigInteger range_integer = max - min + 1;
while (e <= 2)
{
e = BigInteger::RandomGenerateNBit(bit_count + 1) % range_integer + min;
if (e == THREE)
{
continue;
}
if (e.IsEven())
{
e.SetBit(0);
}
}
while (BigInteger::GCD(e, phi) != ONE)
{
e += TWO;
}
}
BigSignedInteger d_signed = BigSignedInteger::ModuloInverse(
BigSignedInteger(e),
BigSignedInteger(phi)
);
BigInteger d = NormalizeModulo(d_signed, phi);
KeyPair keyPair;
keyPair.PublicExponent = e;
keyPair.PrivateExponent = d;
keyPair.Modulus = n;
keyPair.PrimeP = p;
keyPair.PrimeQ = q;
keyPair.DP = d % (p - ONE);
keyPair.DQ = d % (q - ONE);
keyPair.QInverseModP = ComputeQInverseModP(q, p);
return keyPair;
}
void FastRSA::Encrypt(BigInteger& message, const BigInteger& publicExponent, const BigInteger& modulus) const
{
if (modulus.IsZero())
{
throw std::invalid_argument("modulus cannot be zero.");
}
if (message >= modulus)
{
message %= modulus;
}
message.PowerWithModulo(publicExponent, modulus);
}
void FastRSA::DecryptSlow(BigInteger& cipher, const BigInteger& privateExponent, const BigInteger& modulus) const
{
if (modulus.IsZero())
{
throw std::invalid_argument("modulus cannot be zero.");
}
if (cipher >= modulus)
{
cipher %= modulus;
}
cipher.PowerWithModulo(privateExponent, modulus);
}
void FastRSA::DecryptCRT(BigInteger& cipher, const KeyPair& keyPair) const
{
if (keyPair.Modulus.IsZero() || keyPair.PrimeP.IsZero() || keyPair.PrimeQ.IsZero())
{
throw std::invalid_argument("invalid CRT key.");
}
if (cipher >= keyPair.Modulus)
{
cipher %= keyPair.Modulus;
}
const BigInteger originalCipher = cipher;
BigInteger mp = cipher % keyPair.PrimeP;
mp.PowerWithModulo(keyPair.DP, keyPair.PrimeP);
BigInteger mq = cipher % keyPair.PrimeQ;
mq.PowerWithModulo(keyPair.DQ, keyPair.PrimeQ);
BigInteger diff = NonNegativeModuloDifference(mp, mq, keyPair.PrimeP);
BigInteger h = keyPair.QInverseModP;
h *= diff;
h %= keyPair.PrimeP;
BigInteger message = mq + h * keyPair.PrimeQ;
// Optional fault / correctness verification
BigInteger verify = message;
verify.PowerWithModulo(keyPair.PublicExponent, keyPair.Modulus);
if (verify != originalCipher)
{
throw std::runtime_error("FastRSA CRT verification failed.");
}
cipher = std::move(message);
}
bool FastRSA::ValidateCRTKeyPair(const KeyPair& keyPair) const
{
if (keyPair.PrimeP.IsZero() || keyPair.PrimeQ.IsZero() || keyPair.Modulus.IsZero())
{
return false;
}
if (keyPair.PrimeP == keyPair.PrimeQ)
{
return false;
}
if (keyPair.PrimeP * keyPair.PrimeQ != keyPair.Modulus)
{
return false;
}
BigInteger phi = (keyPair.PrimeP - ONE) * (keyPair.PrimeQ - ONE);
if (BigInteger::GCD(keyPair.PublicExponent, phi) != ONE)
{
return false;
}
if ((keyPair.PrivateExponent * keyPair.PublicExponent) % phi != ONE)
{
return false;
}
if (keyPair.DP != keyPair.PrivateExponent % (keyPair.PrimeP - ONE))
{
return false;
}
if (keyPair.DQ != keyPair.PrivateExponent % (keyPair.PrimeQ - ONE))
{
return false;
}
if ((keyPair.PrimeQ * keyPair.QInverseModP) % keyPair.PrimeP != ONE)
{
return false;
}
return true;
}
} // namespace TwilightDream::CryptographyAsymmetric
namespace TwilightDream::CryptographyAsymmetric::SM2
{
bool EllipticCurvePoint::CheckCurveFormula() const
{
using EllitpticCurvePrimeField = TwilightDream::CryptographyAsymmetric::SM2::EllipticCurvePrimeField;
EllitpticCurvePrimeField x = this->X();
EllitpticCurvePrimeField y = this->Y();
// Check the curve equation in Affine coordinates: y^2 (mod prime) = x^3 + ax + b (mod prime)
return y.power(2) == x.power(3) + EllitpticCurvePrimeField(EllipticCurve::a) * x + EllitpticCurvePrimeField(EllipticCurve::b);
}
EllipticCurvePoint EllipticCurvePoint::ConvertAffineToJaccobian() const
{
// Handle special case: invalid point (0, 0, 0)
if (IsInvalidPoint(*this))
{
return EllipticCurvePoint(); // Return the invalid point (0, 0, 0)
}
// Check if it is the point at infinity
if (this->IsInfinityPoint())
{
// In Jacobian coordinates, the point at infinity is typically represented as (1, 1, 0)
// or any point with Z = 0
return MakeJaccobianInfinity();
}
// For an affine point (x, y), convert it to a Jacobian point (x, y, 1)
return EllipticCurvePoint(x, y, EllipticCurvePrimeField(1));
}
EllipticCurvePoint EllipticCurvePoint::ConvertJaccobianToAffine() const
{
// Handle special case: invalid point (0, 0, 0)
if (IsInvalidPoint(*this))
{
return EllipticCurvePoint(); // Return the invalid point (0, 0, 0)
}
BigSignedInteger x = this->x.GetValue();
BigSignedInteger y = this->y.GetValue();
BigSignedInteger z = this->z.GetValue();
// Use Fermat's Little Theorem to calculate the modular inverse in the prime field
// (z^{-1} ≡ z^(p-2) mod p)
BigSignedInteger count = EllipticCurvePrimeField::Prime;
count -= 2;
BigSignedInteger z_inverse = z;
while (!count.IsZero())
{
if (count & 1)
{
z_inverse = (z_inverse * z) % EllipticCurvePrimeField::Prime;
}
z_inverse = (z_inverse * z_inverse) % EllipticCurvePrimeField::Prime;
count >>= 1;
}
BigSignedInteger z_inverse_square = (z_inverse * z_inverse) % EllipticCurvePrimeField::Prime;
BigSignedInteger z_inverse_cube = (z_inverse_square * z_inverse) % EllipticCurvePrimeField::Prime;
BigSignedInteger x_affine = (x * z_inverse_square) % EllipticCurvePrimeField::Prime;
BigSignedInteger y_affine = (y * z_inverse_cube) % EllipticCurvePrimeField::Prime;
BigSignedInteger z_affine = (this->z.GetValue() * z) % EllipticCurvePrimeField::Prime;
if (z_affine == 1)
{
return EllipticCurvePoint(x_affine, y_affine, EllipticCurvePrimeField(1), false); // Return affine point
}
else
{
return EllipticCurvePoint(); // Return invalid point (0, 0, 0)
}
}
EllipticCurvePoint EllipticCurvePoint::operator+(const EllipticCurvePoint& other) const
{
// First, check if either point is the point at infinity
if (this->IsInfinityPoint())
return other; // If *this is the point at infinity, return the other point
if (other.IsInfinityPoint())
return *this; // If the other point is the point at infinity, return *this
EllipticCurvePrimeField x1 = this->x;
EllipticCurvePrimeField y1 = this->y;
EllipticCurvePrimeField z1 = this->z;
EllipticCurvePrimeField x2 = other.x;
EllipticCurvePrimeField y2 = other.y;
EllipticCurvePrimeField z2 = other.z;
// Compute intermediate values
EllipticCurvePrimeField w1 = x1 * z2;
EllipticCurvePrimeField w2 = x2 * z1;
EllipticCurvePrimeField w3 = w1 - w2;
EllipticCurvePrimeField w4 = y1 * z2;
EllipticCurvePrimeField w5 = y2 * z1;
EllipticCurvePrimeField w6 = w4 - w5;
if (w3.GetValue().IsZero())
{
if (w6.GetValue().IsZero())
{
// If this == other, return the result of point doubling
return this->Twice();
}
// If this == -other, return the point at infinity
return EllipticCurvePoint();
}
EllipticCurvePrimeField w7 = w1 + w2;
EllipticCurvePrimeField w8 = z1 * z2;
EllipticCurvePrimeField w9 = w3.power(2);
EllipticCurvePrimeField w10 = w3 * w9;
EllipticCurvePrimeField w11 = w8 * w6.power(2) - w7 * w9;
// Compute the resulting coordinates
EllipticCurvePrimeField x3 = w3 * w11;
EllipticCurvePrimeField y3 = w6 * (w9 * w1 - w11) - w4 * w10;
EllipticCurvePrimeField z3 = w10 * w8;
return EllipticCurvePoint(x3.GetValue(), y3.GetValue(), z3.GetValue());
}
EllipticCurvePoint EllipticCurvePoint::Twice() const
{
// Handle special case: the point at infinity
if (this->IsInfinityPoint())
return *this;
// Handle special case: vertical tangent (y = 0)
if (this->y.GetValue().IsZero())
return EllipticCurvePoint();
EllipticCurvePrimeField x1 = x;
EllipticCurvePrimeField y1 = y;
EllipticCurvePrimeField z1 = z;
// Compute intermediate values
EllipticCurvePrimeField w1 = (x1.power(2) * EllipticCurvePrimeField(3) + EllipticCurvePrimeField(EllipticCurve::a) * z1.power(2));
EllipticCurvePrimeField w2 = (y1 << 1) * z1;
EllipticCurvePrimeField w3 = y1.power(2);
EllipticCurvePrimeField w4 = w3 * x1 * z1;
EllipticCurvePrimeField w5 = w2.power(2);
EllipticCurvePrimeField w6 = w1.power(2) - (w4 << 3);
// Compute resulting coordinates
EllipticCurvePrimeField x3 = w2 * w6;
EllipticCurvePrimeField y3 = w1 * ((w4 << 2) - w6) - ((w5 << 1) * w3);
EllipticCurvePrimeField z3 = w2 * w5;
return EllipticCurvePoint(x3.GetValue(), y3.GetValue(), z3.GetValue());
}
EllipticCurvePoint EllipticCurvePoint::operator*(const BigInteger& scalar) const
{
// Handle special case: the point at infinity
if (this->IsInfinityPoint())
return *this;
// Handle special case: scalar is zero
if (scalar.IsZero())
return EllipticCurvePoint();
// Compute k3 = 3 * scalar
BigInteger k3 = scalar * 3;
size_t k3_bit_length = k3.BitLength();
size_t k_bit_length = scalar.BitLength();
// Define the negation of the point
EllipticCurvePoint negate_point = -(*this);
// Initialize the result point
EllipticCurvePoint Q = *this;
bool k3_bit = false;
bool k_bit = false;
// Iterate through the bits of k3, from the second most significant to the least significant
for (int64_t i = k3_bit_length - 2; i >= 0; i--)
{
// Double the current point
Q = Q.Twice();
// Get the current bits of k3 and scalar
k3_bit = k3.GetBit(i);
// If k3_bit_length > k_bit_length and i > k_bit_length - 1, then k_bit is always false.
// because for a bit block longer than k_bit, the "non-existent" most significant bit of k_bit is always zero.
if(k3_bit_length > k_bit_length && (i > k_bit_length - 1))
{
k_bit = false;
}
else
{
k_bit = scalar.GetBit(i);
}
// Add the appropriate point based on the bit difference
if (k3_bit != k_bit)
{
Q = Q + (k3_bit ? *this : negate_point);
}
}
return Q;
}
bool EllipticCurvePoint::operator==(const EllipticCurvePoint& other) const
{
// Check if the two points are the same object in memory
if (this == &other)
{
return true;
}
// Check if either point is at infinity
if (this->IsInfinityPoint())
return other.IsInfinityPoint();
if (other.IsInfinityPoint())
return this->IsInfinityPoint();
// Compare the Y-coordinates using cross-multiplication
EllipticCurvePrimeField u = other.y * this->z - this->y * other.z;
if (!u.GetValue().IsZero())
return false;
// Compare the X-coordinates using cross-multiplication
EllipticCurvePrimeField v = other.x * this->z - this->x * other.z;
return v.GetValue().IsZero();
}
bool EllipticCurvePoint::operator!=( const EllipticCurvePoint& other ) const
{
return !(*this == other);
}
bool EllipticCurvePoint::IsOnCurve(const EllipticCurvePoint& point)
{
// Check if the point is invalid
if (IsInvalidPoint(point))
{
return false;
}
// Internally converts projective coordinates to affine coordinates using X() and Y(),
// and validates the point against the elliptic curve equation in affine form.
return point.CheckCurveFormula();
}
void SM2KeyPair::GenerateKeyPair()
{
// Curve order n
// min_random_k = 1
// max_random_k = n - 2
const BigInteger& min = 1;
const BigInteger& max = EllipticCurve::n - 2;
// Generate a random private key in the range [1, n-2]
BigInteger range = max - min + 1;
BigInteger random;
TryAgain:
do
{
random = BigInteger::RandomGenerateNBit(range.BitLength());
}
while (random >= range);
privateKey = random + min;
// Compute the public key P = [d]G
publicKey = G * privateKey;
// Test if the public key point lies on the curve
if (!EllipticCurvePoint::IsOnCurve(publicKey))
{
std::cout << "Key pair generation verification not passed: public key is not on SM2 algorithm curve!" << "\n";
goto TryAgain;
}
std::cout << "Key pair generation verification passed: public key is on the SM2 algorithm curve! \n";
}
/*
Papers: Efficient and Secure Elliptic Curve Cryptography Implementation of Curve P-256
inline constexpr bool EllipticCurvePoint::IsOptimized = false;
EllipticCurvePoint EllipticCurvePoint::operator+( const EllipticCurvePoint& other ) const
{
if (this->IsInfinityPoint())
return other;
if (other.IsInfinityPoint())
return *this;
if(*this == other)
return this->Twice();
if constexpr(!IsOptimized)
{
const EllipticCurvePoint& P_affine = *this;
const EllipticCurvePoint& Q_affine = other;
// 如果 x1 == x2 且 y1 == -y2,则返回无穷远点
if (P_affine.x == Q_affine.x && P_affine.y == -Q_affine.y)
{
return EllipticCurvePoint(EllipticCurvePrimeField("0", 10), EllipticCurvePrimeField("0", 10), EllipticCurvePrimeField(1), true);
}
// 点加公式
EllipticCurvePrimeField lambda = (Q_affine.y - P_affine.y) * (Q_affine.x - P_affine.x).inverse();
EllipticCurvePrimeField x3 = lambda * lambda - P_affine.x - Q_affine.x;
EllipticCurvePrimeField y3 = lambda * (P_affine.x - x3) - P_affine.y;
return EllipticCurvePoint(x3, y3);
}
else
{
// 提取点 this 和点 other 的坐标
const EllipticCurvePrimeField& X1 = this->x;
const EllipticCurvePrimeField& Y1 = this->y;
const EllipticCurvePrimeField& Z1 = this->z;
const EllipticCurvePrimeField& X2 = other.x;
const EllipticCurvePrimeField& Y2 = other.y;
const EllipticCurvePrimeField& Z2 = other.z;
// 临时变量
EllipticCurvePrimeField U1 = X1;
EllipticCurvePrimeField S1 = Y1;
EllipticCurvePrimeField U2 = X2;
EllipticCurvePrimeField S2 = Y2;
if (Z1 != EllipticCurvePrimeField(BigInteger(1)))
{
U1 = X1 * (Z2.power(BigInteger(2)));
S1 = Y1 * (Z2.power(BigInteger(3)));
}
if (Z2 != EllipticCurvePrimeField(BigInteger(1)))
{
U2 = X2 * (Z1.power(BigInteger(2)));
S2 = Y2 * (Z1.power(BigInteger(3)));
}
// H = U2 - U1
EllipticCurvePrimeField H = U2 - U1;
// R = S2 - S1
EllipticCurvePrimeField R = S2 - S1;
if (H == EllipticCurvePrimeField("0", 10))
{
if (R == EllipticCurvePrimeField("0", 10))
{
// P == Q,进行点倍加
return this->Twice();
}
else
{
// P == -Q,返回无穷远点
return EllipticCurvePoint();
}
}
// HSquared = H**2
EllipticCurvePrimeField HSquared = H.power(BigInteger(2));
// G = HSquared * H
EllipticCurvePrimeField G = HSquared * H;
// V = HSquared * U1
EllipticCurvePrimeField V = HSquared * U1;
// X3 = R**2 - G - 2V
EllipticCurvePrimeField RSquared = R.power(BigInteger(2));
EllipticCurvePrimeField X3 = RSquared - G - (V * EllipticCurvePrimeField("2", 10));
// Y3 = R * (V - X3) - S1 * G
EllipticCurvePrimeField V_minus_X3 = V - X3;
EllipticCurvePrimeField Y3 = (R * V_minus_X3) - (S1 * G);
// Z3 = H * Z1 * Z2
EllipticCurvePrimeField Z3 = H * Z1 * Z2;
return EllipticCurvePoint(X3, Y3, Z3);
}
}
EllipticCurvePoint EllipticCurvePoint::Twice() const
{
if (this->IsInfinityPoint())
return *this;
if constexpr(!IsOptimized)
{
const EllipticCurvePoint& P_affine = *this;
// 点倍加公式
EllipticCurvePrimeField lambda = (EllipticCurvePrimeField(3) * P_affine.x * P_affine.x + P256V1Curve::a) * (P_affine.y * EllipticCurvePrimeField(2)).inverse();
EllipticCurvePrimeField x3 = lambda * lambda - P_affine.x * EllipticCurvePrimeField(2);
EllipticCurvePrimeField y3 = lambda * (P_affine.x - x3) - P_affine.y;
return EllipticCurvePoint(x3, y3);
}
else
{
const EllipticCurvePrimeField& X1 = this->x;
const EllipticCurvePrimeField& Y1 = this->y;
const EllipticCurvePrimeField& Z1 = this->z;
if (Y1 == EllipticCurvePrimeField(BigInteger(0)))
return EllipticCurvePoint(); // 无穷远点
// S = 4 * X1 * Y1**2
EllipticCurvePrimeField Y1Squared = Y1.power(BigInteger(2));
EllipticCurvePrimeField S = X1 * Y1Squared;
S *= EllipticCurvePrimeField("4", 10);
// M = 3 * X1**2 + a * Z1**4
EllipticCurvePrimeField X1Squared = X1.power(BigInteger(2));
EllipticCurvePrimeField M = X1Squared * EllipticCurvePrimeField("3", 10);
EllipticCurvePrimeField Z1Squared = Z1.power(BigInteger(2));
EllipticCurvePrimeField Z1Fourth = Z1Squared.power(BigInteger(2));
EllipticCurvePrimeField a_times_Z1Fourth = P256V1Curve::a * Z1Fourth.GetValue() % EllipticCurvePrimeField::EllipticCurvePrimeFieldPrime;
M += a_times_Z1Fourth;
// X3 = M**2 - 2 * S
EllipticCurvePrimeField MSquared = M.power(BigInteger(2));
EllipticCurvePrimeField X3 = MSquared - (S * EllipticCurvePrimeField("2", 10));
// Y3 = M * (S - X3) - 8 * Y1**4
EllipticCurvePrimeField S_minus_X3 = S - X3;
EllipticCurvePrimeField Y1Fourth = Y1Squared.power(BigInteger(2));
EllipticCurvePrimeField Y3 = (M * S_minus_X3) - (Y1Fourth * EllipticCurvePrimeField("8", 10));
// Z3 = 2 * Y1 * Z1
EllipticCurvePrimeField Z3 = Y1 * EllipticCurvePrimeField("2", 10) * Z1;
return EllipticCurvePoint(X3, Y3, Z3);
}
}
*/
} // namespace TwilightDream::CryptographyAsymmetric::SM2