forked from iceland2k14/secp256k1
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecp256k1.cs
1012 lines (853 loc) · 34.2 KB
/
secp256k1.cs
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
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using System.IO.Compression;
using System.Numerics;
using System.Drawing;
namespace secp256k1
{
public class secp256k1
{
private const string libFile = "ice_secp256k1.so";
private const string DllPath = "ice_secp256k1.dll";
private static bool isLinux = false;
private static readonly BigInteger N = BigInteger.Parse("115792089237316195423570985008687907852837564279074904382605163141518161494337");
//# Coin type
//COIN_BTC = 0
//COIN_BSV = 1
//COIN_BTCD = 2
//COIN_ARG = 3
//COIN_AXE = 4
//COIN_BC = 5
//COIN_BCH = 6
//COIN_BSD = 7
//COIN_BTDX = 8
//COIN_BTG = 9
//COIN_BTX = 10
//COIN_CHA = 11
//COIN_DASH = 12
//COIN_DCR = 13
//COIN_DFC = 14
//COIN_DGB = 15
//COIN_DOGE = 16
//COIN_FAI = 17
//COIN_FTC = 18
//COIN_GRS = 19
//COIN_JBS = 20
//COIN_LTC = 21
//COIN_MEC = 22
//COIN_MONA = 23
//COIN_MZC = 24
//COIN_PIVX = 25
//COIN_POLIS= 26
//COIN_RIC = 27
//COIN_STRAT= 28
//COIN_SMART= 29
//COIN_VIA = 30
//COIN_XMY = 31
//COIN_ZEC = 32
//COIN_ZCL = 33
//COIN_ZERO = 34
//COIN_ZEN = 35
//COIN_TENT = 36
//COIN_ZEIT = 37
//COIN_VTC = 38
//COIN_UNO = 39
//COIN_SKC = 40
//COIN_RVN = 41
//COIN_PPC = 42
//COIN_OMC = 43
//COIN_OK = 44
//COIN_NMC = 45
//COIN_NLG = 46
//COIN_LBRY = 47
//COIN_DNR = 48
//COIN_BWK = 49
// type = 0 [p2pkh], 1 [p2sh], 2 [bech32]
static void Initialization()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
//libFile = "ice_secp256k1.dll";
isLinux = false;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
//libFile = "ice_secp256k1.so";
isLinux = true;
}
else
{
Console.WriteLine("[-] Unsupported Platform currently for dll method. Only [Windows and Linux] is working");
Environment.Exit(1);
}
}
private static class IceLibrary_linux
{
[DllImport(libFile)]
public static extern void get_x_to_y(byte[] x, bool isEven, byte[] ret);
[DllImport(libFile)]
public static extern void pbkdf2_hmac_sha512_dll(IntPtr ret, IntPtr words, int len);
[DllImport(libFile)]
public static extern void pbkdf2_hmac_sha512_list(IntPtr ret, IntPtr words, ulong wordsLen, int mnemSize, ulong total);
[DllImport(libFile)]
public static extern IntPtr scalar_multiplication(byte[] pvk, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr scalar_multiplications(byte[] pvks, int len, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr privatekey_to_coinaddress(int coinType, int addrType, bool isCompressed, string pvkInt);
[DllImport(libFile)]
public static extern IntPtr privatekey_to_address(int addrType, bool isCompressed, string pvkInt);
[DllImport(libFile)]
public static extern IntPtr hash_to_address(int addrType, bool isCompressed, string hash);
[DllImport(libFile)]
public static extern IntPtr pubkey_to_address(int addrType, bool isCompressed, string pubkey);
[DllImport(libFile)]
public static extern void privatekey_to_h160(int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(libFile)]
public static extern void privatekey_loop_h160(ulong num, int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(libFile)]
public static extern void privatekey_loop_h160_sse(ulong num, int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(libFile)]
public static extern void pubkey_to_h160(int addrType, bool isCompressed, byte[] pubkey, byte[] ret);
[DllImport(libFile)]
public static extern void pub_endo1(string pubkey, byte[] ret);
[DllImport(libFile)]
public static extern void pub_endo2(string pubkey, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr b58_encode(byte[] input);
[DllImport(libFile)]
public static extern IntPtr b58_decode(string addr);
[DllImport(libFile)]
public static extern void bech32_address_decode(int coinType, string b32Addr, byte[] h160);
[DllImport(libFile)]
public static extern void get_sha256(byte[] input, int len, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr pubkeyxy_to_ETH_address(string upubXY);
[DllImport(libFile)]
public static extern void pubkeyxy_to_ETH_address_bytes(string upubXY, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr privatekey_to_ETH_address(string pvk);
[DllImport(libFile)]
public static extern void privatekey_to_ETH_address_bytes(string pvk, byte[] ret);
[DllImport(libFile)]
public static extern IntPtr privatekey_group_to_ETH_address(string pvk, int m);
[DllImport(libFile)]
public static extern void privatekey_group_to_ETH_address_bytes(string pvk, int m, byte[] ret);
[DllImport(libFile)]
public static extern void init_P2_Group(string upub);
[DllImport(libFile)]
public static extern void init_secp256_lib();
[DllImport(libFile)]
public static extern void free_memory(IntPtr pointer);
}
private static class IceLibrary
{
[DllImport(DllPath)]
public static extern void get_x_to_y(byte[] x, bool isEven, byte[] ret);
[DllImport(DllPath)]
public static extern void pbkdf2_hmac_sha512_dll(IntPtr ret, IntPtr words, int len);
[DllImport(DllPath)]
public static extern void pbkdf2_hmac_sha512_list(IntPtr ret, IntPtr words, ulong wordsLen, int mnemSize, ulong total);
[DllImport(DllPath)]
public static extern IntPtr scalar_multiplication(byte[] pvk, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr scalar_multiplications(byte[] pvks, int len, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr privatekey_to_coinaddress(int coinType, int addrType, bool isCompressed, string pvkInt);
[DllImport(DllPath)]
public static extern IntPtr privatekey_to_address(int addrType, bool isCompressed, string pvkInt);
[DllImport(DllPath)]
public static extern IntPtr hash_to_address(int addrType, bool isCompressed, string hash);
[DllImport(DllPath)]
public static extern IntPtr pubkey_to_address(int addrType, bool isCompressed, string pubkey);
[DllImport(DllPath)]
public static extern void privatekey_to_h160(int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(DllPath)]
public static extern void privatekey_loop_h160(ulong num, int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(DllPath)]
public static extern void privatekey_loop_h160_sse(ulong num, int addrType, bool isCompressed, string pvkInt, byte[] ret);
[DllImport(DllPath)]
public static extern void pubkey_to_h160(int addrType, bool isCompressed, byte[] pubkey, byte[] ret);
[DllImport(DllPath)]
public static extern void pub_endo1(string pubkey, byte[] ret);
[DllImport(DllPath)]
public static extern void pub_endo2(string pubkey, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr b58_encode(byte[] input);
[DllImport(DllPath)]
public static extern IntPtr b58_decode(string addr);
[DllImport(DllPath)]
public static extern void bech32_address_decode(int coinType, string b32Addr, byte[] h160);
[DllImport(DllPath)]
public static extern void get_sha256(byte[] input, int len, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr pubkeyxy_to_ETH_address(string upubXY);
[DllImport(DllPath)]
public static extern void pubkeyxy_to_ETH_address_bytes(string upubXY, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr privatekey_to_ETH_address(string pvk);
[DllImport(DllPath)]
public static extern void privatekey_to_ETH_address_bytes(string pvk, byte[] ret);
[DllImport(DllPath)]
public static extern IntPtr privatekey_group_to_ETH_address(string pvk, int m);
[DllImport(DllPath)]
public static extern void privatekey_group_to_ETH_address_bytes(string pvk, int m, byte[] ret);
[DllImport(DllPath)]
public static extern void init_P2_Group(string upub);
[DllImport(DllPath)]
public static extern void init_secp256_lib();
[DllImport(DllPath)]
public static extern void free_memory(IntPtr pointer);
}
public (byte[], byte[]) PvkToPubs_Bytes(string pvkHex)
{
if (BigInteger.TryParse(pvkHex, System.Globalization.NumberStyles.HexNumber, null, out BigInteger pvkInt))
{
if (pvkInt < 0) pvkInt = N + pvkInt;
//string[] PVKs = new string[1];
//PVKs[0] = Fl(pvkInt);
byte[] UPub = _scalar_multiplication(Fl(pvkInt));
byte[] Cpub = PointToCPub(UPub);
return (UPub, Cpub);
}
throw new ArgumentException("Invalid hex string.", nameof(pvkHex));
}
public (string, string) PvkToPubs(string pvkHex)
{
if (BigInteger.TryParse(pvkHex, System.Globalization.NumberStyles.HexNumber, null, out BigInteger pvkInt))
{
if (pvkInt < 0) pvkInt = N + pvkInt;
//string[] PVKs = new string[1];
//PVKs[0] = Fl(pvkInt);
byte[] UPub = _scalar_multiplication(Fl(pvkInt));
string Cpub = PointToCPub(UPub, false);
return (BytesToHexString(UPub), Cpub);
}
throw new ArgumentException("Invalid hex string.", nameof(pvkHex));
}
private byte[] _scalar_multiplication(string pvkHex)
{
Encoding utf8 = Encoding.UTF8;
byte[] res = new byte[65];
byte[] pvk = utf8.GetBytes(pvkHex);//HexStringToByteArray(pvkHex);
//Console.WriteLine(pvkHex + " Ключ, который пришёл в прогу");
if (isLinux)
{ IceLibrary_linux.scalar_multiplication(pvk, res); }
else
{ IceLibrary.scalar_multiplication(pvk, res); }
//Console.WriteLine( BytesToHexString(pvk));
return res;
}
public byte[] ScalarMultiplication(string pvkHex)
{
//if (BigInteger.TryParse(pvkHex, System.Globalization.NumberStyles.HexNumber, null, out BigInteger pvkInt))
//{
// if (pvkInt < 0) pvkInt = N + pvkInt;
// //string[] PVKs = new string[1];
// //PVKs[0] = Fl(pvkInt);
// return _scalar_multiplication(Fl(pvkInt));
//}
return _scalar_multiplication(Fl(pvkHex));
}
private byte[] _scalar_multiplications(string[] pvkHexList)
{
Encoding utf8 = Encoding.UTF8;
int sz = pvkHexList.Length;
byte[] res = new byte[65 * sz];
byte[] pvks = new byte[65 * sz];
for (int i = 0; i < sz; i++)
{
byte[] pvkBytes = utf8.GetBytes(pvkHexList[i]);
Array.Copy(pvkBytes, 0, pvks, i * 65, 65);
}
if (isLinux)
{
IceLibrary_linux.scalar_multiplications(pvks, sz, res);
}
else
{
IceLibrary.scalar_multiplications(pvks, sz, res);
}
//Console.WriteLine(BytesToHexString(pvks));
return res;
}
public byte[] ScalarMultiplications(string[] pvkHexList)
{
string[] normalizedPvkHexList = new string[pvkHexList.Length];
for (int i = 0; i < pvkHexList.Length; i++)
{
if (int.TryParse(pvkHexList[i], System.Globalization.NumberStyles.HexNumber, null, out int pvkInt))
{
normalizedPvkHexList[i] = pvkInt < 0 ? Fl(N + pvkInt) : Fl(pvkInt);
}
else
{
throw new ArgumentException("Invalid hex string in the list.", nameof(pvkHexList));
}
}
return _scalar_multiplications(normalizedPvkHexList);
}
//private string Fl(BigInteger sstr, int length = 64)
//{
// if (sstr < 0) sstr = N + sstr;
// return sstr.ToString("x").PadLeft(length, '0');
//}
public string Fl(object sstr, int length = 64)
{
string fixedStr = "";
if (sstr is int intValue)
{
fixedStr = intValue.ToString("x").PadLeft(length, '0');
}
if (sstr is BigInteger BigValue)
{
fixedStr = BigValue.ToString("x").PadLeft(length, '0');
}
else if (sstr is string strValue)
{
if (strValue.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
{
fixedStr = strValue.Substring(2).PadLeft(length, '0');
}
else
{
fixedStr = strValue.PadLeft(length, '0');
}
}
else if (sstr is byte[] bytesValue)
{
byte[] fixedBytes = new byte[length];
Array.Copy(bytesValue, 0, fixedBytes, length - bytesValue.Length, bytesValue.Length);
return BytesToHexString(fixedBytes);
}
else
{
Console.WriteLine("[Error] Input format [Integer] [Hex] [Bytes] allowed only. Detected : " + sstr.GetType());
}
//Console.WriteLine( fixedStr);
return fixedStr;
}
private byte[] HexStringToByteArray(string hex)
{
if (hex.Length % 2 != 0)
{
throw new ArgumentException("Hex string must have an even number of characters.");
}
byte[] byteArray = new byte[hex.Length / 2];
for (int i = 0; i < byteArray.Length; i++)
{
byteArray[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return byteArray;
}
public string PrivateKeyToCoinAddress(int coinType, int addrType, bool isCompressed, string pvkInt)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.privatekey_to_coinaddress(coinType, addrType, isCompressed, pvkInt);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.privatekey_to_coinaddress(coinType, addrType, isCompressed, pvkInt);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public string PrivateKeyToAddress(int addrType, bool isCompressed, string pvkInt)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.privatekey_to_address(addrType, isCompressed, pvkInt);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.privatekey_to_address(addrType, isCompressed, pvkInt);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public string HashToAddress(int addrType, bool isCompressed, string hash)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.hash_to_address(addrType, isCompressed, hash);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.hash_to_address(addrType, isCompressed, hash);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public string PubKeyToAddress(int addrType, bool isCompressed, string pubkey)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.pubkey_to_address(addrType, isCompressed, pubkey);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.pubkey_to_address(addrType, isCompressed, pubkey);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public byte[] PrivateKeyToH160(int addrType, bool isCompressed, string pvkInt)
{
if (isLinux)
{
byte[] h160 = new byte[20];
IceLibrary_linux.privatekey_to_h160(addrType, isCompressed, pvkInt, h160);
return h160;
}
else
{
byte[] h160 = new byte[20];
IceLibrary.privatekey_to_h160(addrType, isCompressed, pvkInt, h160);
return h160;
}
}
public byte[] PrivateKeyLoopH160(ulong num, int addrType, bool isCompressed, string pvkInt)
{
if (isLinux)
{
byte[] h160 = new byte[20 * num];
IceLibrary_linux.privatekey_loop_h160(num, addrType, isCompressed, pvkInt, h160);
return h160;
}
else
{
byte[] h160 = new byte[20 * num];
IceLibrary.privatekey_loop_h160(num, addrType, isCompressed, pvkInt, h160);
return h160;
}
}
public byte[] PrivateKeyLoopH160SSE(ulong num, int addrType, bool isCompressed, string pvkInt)
{
if (isLinux)
{
byte[] h160 = new byte[20 * num];
IceLibrary_linux.privatekey_loop_h160_sse(num, addrType, isCompressed, pvkInt, h160);
return h160;
}
else
{
byte[] h160 = new byte[20 * num];
IceLibrary.privatekey_loop_h160_sse(num, addrType, isCompressed, pvkInt, h160);
return h160;
}
}
public byte[] PubKeyToH160(int addrType, bool isCompressed, byte[] pubkey)
{
if (isLinux)
{
byte[] h160 = new byte[20];
IceLibrary_linux.pubkey_to_h160(addrType, isCompressed, pubkey, h160);
return h160;
}
else
{
byte[] h160 = new byte[20];
IceLibrary.pubkey_to_h160(addrType, isCompressed, pubkey, h160);
return h160;
}
}
public byte[] PubEndo1(string pubkey)
{
if (isLinux)
{
byte[] ret = new byte[65];
IceLibrary_linux.pub_endo1(pubkey, ret);
return ret;
}
else
{
byte[] ret = new byte[65];
IceLibrary.pub_endo1(pubkey, ret);
return ret;
}
}
public byte[] PubEndo2(string pubkey)
{
if (isLinux)
{
byte[] ret = new byte[65];
IceLibrary_linux.pub_endo2(pubkey, ret);
return ret;
}
else
{
byte[] ret = new byte[65];
IceLibrary.pub_endo2(pubkey, ret);
return ret;
}
}
public string B58Encode(byte[] input)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.b58_encode(input);
string encoded = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return encoded;
}
else
{
IntPtr resultPtr = IceLibrary.b58_encode(input);
string encoded = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return encoded;
}
}
public string B58Decode(string addr)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.b58_decode(addr);
string decoded = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return decoded;
}
else
{
IntPtr resultPtr = IceLibrary.b58_decode(addr);
string decoded = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return decoded;
}
}
public string Bech32AddressDecode(int coinType, string b32Addr)
{
if (isLinux)
{
byte[] h160 = new byte[20];
IceLibrary_linux.bech32_address_decode(coinType, b32Addr, h160);
return BytesToHexString(h160);
}
else
{
byte[] h160 = new byte[20];
IceLibrary.bech32_address_decode(coinType, b32Addr, h160);
return BytesToHexString(h160);//BitConverter.ToString(h160).Replace("-", "").ToLower();
}
}
public byte[] GetSha256(byte[] input)
{
if (isLinux)
{
byte[] ret = new byte[32];
IceLibrary_linux.get_sha256(input, input.Length, ret);
return ret;
}
else
{
byte[] ret = new byte[32];
IceLibrary.get_sha256(input, input.Length, ret);
return ret;
}
}
public byte[] GetSha256(string IN)
{
Encoding utf8 = Encoding.UTF8;
byte[] input = utf8.GetBytes(IN);
if (isLinux)
{
byte[] ret = new byte[32];
IceLibrary_linux.get_sha256(input, input.Length, ret);
return ret;
}
else
{
byte[] ret = new byte[32];
IceLibrary.get_sha256(input, input.Length, ret);
return ret;
}
}
public byte[] Checksum(byte[] input)
{
byte[] res = GetSha256(input);
byte[] res2 = GetSha256(res);
return new Span<byte>(res2, 0, 4).ToArray();
}
public byte[] Checksum(string input)
{
byte[] res = GetSha256(input);
byte[] res2 = GetSha256(res);
return new Span<byte>(res2, 0, 4).ToArray();
}
public string PubKeyToEthAddress(string pubkey)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.pubkeyxy_to_ETH_address(pubkey);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.pubkeyxy_to_ETH_address(pubkey);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public byte[] PubKeyToEthAddressBytes(string pubkey)
{
if (isLinux)
{
byte[] ret = new byte[20];
IceLibrary_linux.pubkeyxy_to_ETH_address_bytes(pubkey, ret);
return ret;
}
else
{
byte[] ret = new byte[20];
IceLibrary.pubkeyxy_to_ETH_address_bytes(pubkey, ret);
return ret;
}
}
public string PrivateKeyToEthAddress(string pvk)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary_linux.privatekey_to_ETH_address(pvk);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return address;
}
else
{
IntPtr resultPtr = IceLibrary.privatekey_to_ETH_address(pvk);
string address = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return address;
}
}
public byte[] PrivateKeyToEthAddressBytes(string pvk)
{
if (isLinux)
{
byte[] ret = new byte[20];
IceLibrary_linux.privatekey_to_ETH_address_bytes(pvk, ret);
return ret;
}
else
{
byte[] ret = new byte[20];
IceLibrary.privatekey_to_ETH_address_bytes(pvk, ret);
return ret;
}
}
public string PrivateKeyGroupToEthAddress(string pvk, int m)
{
if (isLinux)
{
IntPtr resultPtr = IceLibrary.privatekey_group_to_ETH_address(pvk, m);
string addresses = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary.free_memory(resultPtr);
return addresses;
}
else
{
IntPtr resultPtr = IceLibrary_linux.privatekey_group_to_ETH_address(pvk, m);
string addresses = Marshal.PtrToStringAnsi(resultPtr);
IceLibrary_linux.free_memory(resultPtr);
return addresses;
}
}
public byte[] PrivateKeyGroupToEthAddressBytes(string pvk, int m)
{
if (isLinux)
{
byte[] ret = new byte[20 * m];
IceLibrary_linux.privatekey_group_to_ETH_address_bytes(pvk, m, ret);
return ret;
}
else
{
byte[] ret = new byte[20 * m];
IceLibrary.privatekey_group_to_ETH_address_bytes(pvk, m, ret);
return ret;
}
}
public byte[] Pbkdf2HmacSha512Dll(string words)
{
if (isLinux)
{
byte[] seedBytes = new byte[64];
IntPtr seedPtr = Marshal.AllocHGlobal(seedBytes.Length);
IntPtr wordsPtr = Marshal.StringToHGlobalAnsi(words);
IceLibrary_linux.pbkdf2_hmac_sha512_dll(seedPtr, wordsPtr, words.Length);
Marshal.Copy(seedPtr, seedBytes, 0, seedBytes.Length);
Marshal.FreeHGlobal(seedPtr);
Marshal.FreeHGlobal(wordsPtr);
return seedBytes;
}
else
{
byte[] seedBytes = new byte[64];
IntPtr seedPtr = Marshal.AllocHGlobal(seedBytes.Length);
IntPtr wordsPtr = Marshal.StringToHGlobalAnsi(words);
IceLibrary.pbkdf2_hmac_sha512_dll(seedPtr, wordsPtr, words.Length);
Marshal.Copy(seedPtr, seedBytes, 0, seedBytes.Length);
Marshal.FreeHGlobal(seedPtr);
Marshal.FreeHGlobal(wordsPtr);
return seedBytes;
}
}
public byte[] Pbkdf2HmacSha512List(string[] wordsList)
{
if (isLinux)
{
int wl = wordsList.Length;
int strength = wordsList[0].Split().Length;
string words = string.Join(" ", wordsList);
byte[] seedBytes = new byte[64 * wl];
IntPtr seedPtr = Marshal.AllocHGlobal(seedBytes.Length);
IntPtr wordsPtr = Marshal.StringToHGlobalAnsi(words);
IceLibrary_linux.pbkdf2_hmac_sha512_list(seedPtr, wordsPtr, (ulong)words.Length, strength, (ulong)wl);
Marshal.Copy(seedPtr, seedBytes, 0, seedBytes.Length);
Marshal.FreeHGlobal(seedPtr);
Marshal.FreeHGlobal(wordsPtr);
return seedBytes;
}
else
{
int wl = wordsList.Length;
int strength = wordsList[0].Split().Length;
string words = string.Join(" ", wordsList);
byte[] seedBytes = new byte[64 * wl];
IntPtr seedPtr = Marshal.AllocHGlobal(seedBytes.Length);
IntPtr wordsPtr = Marshal.StringToHGlobalAnsi(words);
IceLibrary.pbkdf2_hmac_sha512_list(seedPtr, wordsPtr, (ulong)words.Length, strength, (ulong)wl);
Marshal.Copy(seedPtr, seedBytes, 0, seedBytes.Length);
Marshal.FreeHGlobal(seedPtr);
Marshal.FreeHGlobal(wordsPtr);
return seedBytes;
}
}
public string ToCPub(string pubHex)
{
string P = pubHex;
if (pubHex.Length > 70)
{
P = (BigInteger.Parse(pubHex.Substring(66), System.Globalization.NumberStyles.HexNumber) % 2 == 0 ? "02" : "03") + pubHex.Substring(2, 64);
}
return P;
}
public dynamic PointToCPub(byte[] pubkeyBytes, bool returnByteArray = true)
{
string P = BytesToHexString(pubkeyBytes);
if (P.Length > 70)
{
string x = P.Substring(2, 64);
string y = P.Substring(66);
P = (BigInteger.Parse(y, System.Globalization.NumberStyles.HexNumber) % 2 == 0 ? "02" : "03") + x;
}
if (returnByteArray)
{
return StringToByteArray(P);
}
else
{
return P;
}
}
public byte[] PubToUPub(string pubHex)
{
Encoding utf8 = Encoding.UTF8;
string x = pubHex.Substring(2, 64);
string y;
if (pubHex.Length < 70)
{
bool isEven = BigInteger.Parse(pubHex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) % 2 == 0;
y = BytesToHexString(GetXToY(utf8.GetBytes(x), isEven)).PadLeft(64, '0');
}
else
{
y = pubHex.Substring(66).PadLeft(64, '0');
}
//Console.WriteLine("04" + x + y);
return StringToByteArray("04" + x + y);
}
public byte[] GetXToY(byte[] xBytes, bool isEven)
{
if (isLinux)
{
byte[] res = new byte[32];
IceLibrary_linux.get_x_to_y(xBytes, isEven, res);
return res;
}
else
{
byte[] res = new byte[32];
IceLibrary.get_x_to_y(xBytes, isEven, res);
return res;
}
}
//public byte[] StringToByteArray(string hex)
//{
// return Enumerable.Range(0, hex.Length)
// .Where(x => x % 2 == 0)
// .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
// .ToArray();
//}
public byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length / 2)
.Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16))
.ToArray();
}
public void InitP2Group(string pubkeyBytes)
{
if (isLinux)
{
IceLibrary_linux.init_P2_Group(pubkeyBytes);
}
else
{
IceLibrary.init_P2_Group(pubkeyBytes);
}
}
static string BytesToHexString(byte[] byteArray)
{
string hexString = "";
foreach (byte b in byteArray)
{
hexString += b.ToString("X2").ToLowerInvariant(); // X2 указывает на двузначное шестнадцатеричное число
}
return hexString;
}
public void InitSecp256Lib()
{
Initialization();
if (isLinux)