forked from 1inch/profanity2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofanity.cl
More file actions
1954 lines (1703 loc) · 78.5 KB
/
Copy pathprofanity.cl
File metadata and controls
1954 lines (1703 loc) · 78.5 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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* profanity.cl
* ============
* Contains multi-precision arithmetic functions and iterative elliptical point
* addition which is the heart of profanity.
*
* Terminology
* ===========
*
*
* Cutting corners
* ===============
* In some instances this code will produce the incorrect results. The elliptical
* point addition does for example not properly handle the case of two points
* sharing the same X-coordinate. The reason the code doesn't handle it properly
* is because it is very unlikely to ever occur and the performance penalty for
* doing it right is too severe. In the future I'll introduce a periodic check
* after N amount of cycles that verifies the integrity of all the points to
* make sure that even very unlikely event are at some point rectified.
*
* Currently, if any of the points in the kernels experiences the unlikely event
* of an error then that point is forever garbage and your runtime-performance
* will in practice be (i*I-N) / (i*I). i and I here refers to the values given
* to the program via the -i and -I switches (default values of 255 and 16384
* respectively) and N is the number of errornous points.
*
* So if a single error occurs you'll lose 1/(i*I) of your performance. That's
* around 0.00002%. The program will still report the same hashrate of course,
* only that some of that work is entirely wasted on this errornous point.
*
* Initialization of main structure
* ================================
*
* Iteration
* =========
*
*
* TODO
* ====
* * Update comments to reflect new optimizations and structure
*
*/
/* ------------------------------------------------------------------------ */
/* Multiprecision functions */
/* ------------------------------------------------------------------------ */
// How many addresses one point addition is worth, in the order they are taken:
//
// 1 P the point itself
// 2 -P its negation, (x, -y), free but for a subtraction
// 3 ψ(P) (βx, y), one modular multiplication
// 4 -ψ(P)
// 5 ψ²(P) (β²x, y), one more multiplication
// 6 -ψ²(P)
//
// Six is the ceiling and not a choice of implementation. These are the
// automorphisms of the curve — the maps E → E that cost no point arithmetic —
// and secp256k1 is y² = x³ + 7, so its j-invariant is zero and its automorphism
// group is the sixth roots of unity, {±1, ±ω, ±ω²}. That group has order six, so
// there is no seventh such map to find. Anything further wants an endomorphism
// of degree above one, which is point arithmetic again by another name.
//
// The host passes the count in, and knows what to do with the private key behind
// each of them — see profanity_iterate.
#if PROFANITY_VARIANTS < 1 || PROFANITY_VARIANTS > 6
#error "PROFANITY_VARIANTS must be between 1 and 6"
#endif
// How many point additions a launch does per point before handing back. The
// state a point carries between them — its delta and its previous lambda — stays
// in the kernel across all of them, so the global memory traffic that state
// costs, and the launch it costs, are divided by this.
#if PROFANITY_ROUNDS < 1
#error "PROFANITY_ROUNDS must be at least 1"
#endif
#define MP_WORDS 8
#define MP_BITS 32
#define bswap32(n) (rotate(n & 0x00FF00FF, 24U)|(rotate(n, 8U) & 0x00FF00FF))
// The same for eight bytes, out of two of the above. Used where a value written
// big endian has to go into a state read little endian, which is a CREATE2
// search putting its counter into the salt.
static inline ulong bswap64(const ulong n) {
return ((ulong)bswap32(((uint)n)) << 32) | (ulong)bswap32(((uint)(n >> 32)));
}
// Whether the multiprecision routines below may use the carry flag directly.
//
// Every carry in this file is detected by comparison — `c = t > a ? 1 : (t == a
// ? c : 0)` and its variants — because OpenCL C has no way to name the flag the
// hardware sets for free. That costs a setp and a selp per word per carry.
// NVIDIA's OpenCL frontend shares NVVM with CUDA and accepts inline PTX, which
// does have a way to name it, so on that one vendor the same arithmetic can be
// written in about a fifth of the instructions.
//
// __NV_CL_C_VERSION is defined by that frontend and by no other, which is what
// this has to key off rather than anything the host knows: profanity.cpp builds
// one program for every device in the context at once, so a machine with an
// NVIDIA card and an AMD one compiles this file twice from the same string and
// only the vendor's own macro tells the two compilations apart.
//
// Inline PTX in OpenCL is not something NVIDIA documents, so -D PROFANITY_NO_PTX
// turns it off and takes the portable path on every device.
#if defined(__NV_CL_C_VERSION) && !defined(PROFANITY_NO_PTX)
#define PROFANITY_PTX_MP 1
#endif
typedef uint mp_word;
typedef struct __attribute__((aligned(16))) {
mp_word d[MP_WORDS];
} mp_number;
// mod = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
__constant const mp_number mod = { {0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
// tripleNegativeGx = 0x92c4cc831269ccfaff1ed83e946adeeaf82c096e76958573f2287becbb17b196
__constant const mp_number tripleNegativeGx = { {0xbb17b196, 0xf2287bec, 0x76958573, 0xf82c096e, 0x946adeea, 0xff1ed83e, 0x1269ccfa, 0x92c4cc83 } };
// doubleNegativeGy = 0x6f8a4b11b2b8773544b60807e3ddeeae05d0976eb2f557ccc7705edf09de52bf
__constant const mp_number doubleNegativeGy = { {0x09de52bf, 0xc7705edf, 0xb2f557cc, 0x05d0976e, 0xe3ddeeae, 0x44b60807, 0xb2b87735, 0x6f8a4b11} };
// negativeGy = 0xb7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777
__constant const mp_number negativeGy = { {0x04ef2777, 0x63b82f6f, 0x597aabe6, 0x02e84bb7, 0xf1eef757, 0xa25b0403, 0xd95c3b9a, 0xb7c52588 } };
// Multiprecision subtraction. Underflow signalled via return value.
mp_word mp_sub(mp_number * const r, const mp_number * const a, const mp_number * const b) {
mp_word t, c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t > a->d[i] ? 1 : (t == a->d[i] ? c : 0);
r->d[i] = t;
}
return c;
}
// Multiprecision subtraction of the modulus saved in mod. Underflow signalled via return value.
mp_word mp_sub_mod(mp_number * const r) {
mp_number mod = { {0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
mp_word t, c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
t = r->d[i] - mod.d[i] - c;
c = t > r->d[i] ? 1 : (t == r->d[i] ? c : 0);
r->d[i] = t;
}
return c;
}
// Multiprecision subtraction modulo M, M = mod.
// This function is often also used for additions by subtracting a negative number. I've chosen
// to do this because:
// 1. It's easier to re-use an already existing function
// 2. A modular addition would have more overhead since it has to determine if the result of
// the addition (r) is in the gap M <= r < 2^256. This overhead doesn't exist in a
// subtraction. We immediately know at the end of a subtraction if we had underflow
// or not by inspecting the carry value. M refers to the modulus saved in variable mod.
void mp_mod_sub(mp_number * const r, const mp_number * const a, const mp_number * const b) {
mp_word i, t, c = 0;
for (i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t < a->d[i] ? 0 : (t == a->d[i] ? c : 1);
r->d[i] = t;
}
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
// Multiprecision subtraction modulo M from a constant number.
// I made this in the belief that using constant address space instead of private address space for any
// constant numbers would lead to increase in performance. Judges are still out on this one.
void mp_mod_sub_const(mp_number * const r, __constant const mp_number * const a, const mp_number * const b) {
mp_word i, t, c = 0;
for (i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t < a->d[i] ? 0 : (t == a->d[i] ? c : 1);
r->d[i] = t;
}
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
// Multiprecision subtraction modulo M of G_x from a number.
// Specialization of mp_mod_sub in hope of performance gain.
void mp_mod_sub_gx(mp_number * const r, const mp_number * const a) {
mp_word i, t, c = 0;
t = a->d[0] - 0x16f81798; c = t < a->d[0] ? 0 : (t == a->d[0] ? c : 1); r->d[0] = t;
t = a->d[1] - 0x59f2815b - c; c = t < a->d[1] ? 0 : (t == a->d[1] ? c : 1); r->d[1] = t;
t = a->d[2] - 0x2dce28d9 - c; c = t < a->d[2] ? 0 : (t == a->d[2] ? c : 1); r->d[2] = t;
t = a->d[3] - 0x029bfcdb - c; c = t < a->d[3] ? 0 : (t == a->d[3] ? c : 1); r->d[3] = t;
t = a->d[4] - 0xce870b07 - c; c = t < a->d[4] ? 0 : (t == a->d[4] ? c : 1); r->d[4] = t;
t = a->d[5] - 0x55a06295 - c; c = t < a->d[5] ? 0 : (t == a->d[5] ? c : 1); r->d[5] = t;
t = a->d[6] - 0xf9dcbbac - c; c = t < a->d[6] ? 0 : (t == a->d[6] ? c : 1); r->d[6] = t;
t = a->d[7] - 0x79be667e - c; c = t < a->d[7] ? 0 : (t == a->d[7] ? c : 1); r->d[7] = t;
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
// Multiprecision subtraction modulo M of G_y from a number.
// Specialization of mp_mod_sub in hope of performance gain.
void mp_mod_sub_gy(mp_number * const r, const mp_number * const a) {
mp_word i, t, c = 0;
t = a->d[0] - 0xfb10d4b8; c = t < a->d[0] ? 0 : (t == a->d[0] ? c : 1); r->d[0] = t;
t = a->d[1] - 0x9c47d08f - c; c = t < a->d[1] ? 0 : (t == a->d[1] ? c : 1); r->d[1] = t;
t = a->d[2] - 0xa6855419 - c; c = t < a->d[2] ? 0 : (t == a->d[2] ? c : 1); r->d[2] = t;
t = a->d[3] - 0xfd17b448 - c; c = t < a->d[3] ? 0 : (t == a->d[3] ? c : 1); r->d[3] = t;
t = a->d[4] - 0x0e1108a8 - c; c = t < a->d[4] ? 0 : (t == a->d[4] ? c : 1); r->d[4] = t;
t = a->d[5] - 0x5da4fbfc - c; c = t < a->d[5] ? 0 : (t == a->d[5] ? c : 1); r->d[5] = t;
t = a->d[6] - 0x26a3c465 - c; c = t < a->d[6] ? 0 : (t == a->d[6] ? c : 1); r->d[6] = t;
t = a->d[7] - 0x483ada77 - c; c = t < a->d[7] ? 0 : (t == a->d[7] ? c : 1); r->d[7] = t;
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
// Multiprecision addition. Overflow signalled via return value.
mp_word mp_add(mp_number * const r, const mp_number * const a) {
mp_word c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
r->d[i] += a->d[i] + c;
c = r->d[i] < a->d[i] ? 1 : (r->d[i] == a->d[i] ? c : 0);
}
return c;
}
// Multiprecision addition of the modulus saved in mod. Overflow signalled via return value.
mp_word mp_add_mod(mp_number * const r) {
mp_word c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
return c;
}
// Multiprecision addition of two numbers with one extra word each. Overflow signalled via return value.
mp_word mp_add_more(mp_number * const r, mp_word * const extraR, const mp_number * const a, const mp_word * const extraA) {
const mp_word c = mp_add(r, a);
*extraR += *extraA + c;
return *extraR < *extraA ? 1 : (*extraR == *extraA ? c : 0);
}
// Multiprecision greater than or equal (>=) operator
mp_word mp_gte(const mp_number * const a, const mp_number * const b) {
mp_word l = 0, g = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
if (a->d[i] < b->d[i]) l |= (1 << i);
if (a->d[i] > b->d[i]) g |= (1 << i);
}
return g >= l;
}
// Bit shifts a number with an extra word to the right one step
void mp_shr_extra(mp_number * const r, mp_word * const e) {
r->d[0] = (r->d[1] << 31) | (r->d[0] >> 1);
r->d[1] = (r->d[2] << 31) | (r->d[1] >> 1);
r->d[2] = (r->d[3] << 31) | (r->d[2] >> 1);
r->d[3] = (r->d[4] << 31) | (r->d[3] >> 1);
r->d[4] = (r->d[5] << 31) | (r->d[4] >> 1);
r->d[5] = (r->d[6] << 31) | (r->d[5] >> 1);
r->d[6] = (r->d[7] << 31) | (r->d[6] >> 1);
r->d[7] = (*e << 31) | (r->d[7] >> 1);
*e >>= 1;
}
// Bit shifts a number to the right one step
void mp_shr(mp_number * const r) {
r->d[0] = (r->d[1] << 31) | (r->d[0] >> 1);
r->d[1] = (r->d[2] << 31) | (r->d[1] >> 1);
r->d[2] = (r->d[3] << 31) | (r->d[2] >> 1);
r->d[3] = (r->d[4] << 31) | (r->d[3] >> 1);
r->d[4] = (r->d[5] << 31) | (r->d[4] >> 1);
r->d[5] = (r->d[6] << 31) | (r->d[5] >> 1);
r->d[6] = (r->d[7] << 31) | (r->d[6] >> 1);
r->d[7] >>= 1;
}
// Multiplies a number with a word and adds it to an existing number with an extra word, overflow of the extra word is signalled in return value
// This is a special function only used for modular multiplication
mp_word mp_mul_word_add_extra_portable(mp_number * const r, const mp_number * const a, const mp_word w, mp_word * const extra) {
mp_word cM = 0; // Carry for multiplication
mp_word cA = 0; // Carry for addition
mp_word tM = 0; // Temporary storage for multiplication
for (mp_word i = 0; i < MP_WORDS; ++i) {
tM = (a->d[i] * w + cM);
cM = mul_hi(a->d[i], w) + (tM < cM);
r->d[i] += tM + cA;
cA = r->d[i] < tM ? 1 : (r->d[i] == tM ? cA : 0);
}
*extra += cM + cA;
return *extra < cM ? 1 : (*extra == cM ? cA : 0);
}
#ifdef PROFANITY_PTX_MP
/* The same nine-word accumulation, written so the carries stay in the flag.
*
* What the loop above computes is (r || extra) += a * w, where the product is
* built a word at a time and its top word falls out at the end as cM. Split by
* where each half of a partial product lands, that is
*
* (r || extra) += sum_i lo(a_i * w) << 32i -- words 0..7
* + sum_i hi(a_i * w) << 32(i+1) -- words 1..8
*
* and each of those sums is one straight carry chain. mad.lo.cc/madc.lo.cc runs
* the first and mad.hi.cc/madc.hi.cc the second, eighteen instructions against
* the eighty or so the comparisons cost.
*
* Both chains have to be in one asm block. CC.CF does not survive whatever the
* compiler decides to schedule between two of them, and nothing in the operand
* constraints tells it not to — splitting this in half is the way to get results
* that are wrong only sometimes, and only on some drivers.
*
* The overflow out of word 8 is a single bit for the same reason it is above:
* (r || extra) is below 2^288 and a * w is below 2^288, so their sum is below
* 2^289 and crosses 2^288 at most once, whichever chain happens to carry it.
*/
mp_word mp_mul_word_add_extra_ptx(mp_number * const r, const mp_number * const a, const mp_word w, mp_word * const extra) {
mp_word r0 = r->d[0], r1 = r->d[1], r2 = r->d[2], r3 = r->d[3];
mp_word r4 = r->d[4], r5 = r->d[5], r6 = r->d[6], r7 = r->d[7];
const mp_word a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3];
const mp_word a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
mp_word e = *extra;
mp_word overflow;
asm volatile(
"mad.lo.cc.u32 %0, %10, %18, %0;\n\t"
"madc.lo.cc.u32 %1, %11, %18, %1;\n\t"
"madc.lo.cc.u32 %2, %12, %18, %2;\n\t"
"madc.lo.cc.u32 %3, %13, %18, %3;\n\t"
"madc.lo.cc.u32 %4, %14, %18, %4;\n\t"
"madc.lo.cc.u32 %5, %15, %18, %5;\n\t"
"madc.lo.cc.u32 %6, %16, %18, %6;\n\t"
"madc.lo.cc.u32 %7, %17, %18, %7;\n\t"
"addc.cc.u32 %8, %8, 0;\n\t"
"addc.u32 %9, 0, 0;\n\t"
"mad.hi.cc.u32 %1, %10, %18, %1;\n\t"
"madc.hi.cc.u32 %2, %11, %18, %2;\n\t"
"madc.hi.cc.u32 %3, %12, %18, %3;\n\t"
"madc.hi.cc.u32 %4, %13, %18, %4;\n\t"
"madc.hi.cc.u32 %5, %14, %18, %5;\n\t"
"madc.hi.cc.u32 %6, %15, %18, %6;\n\t"
"madc.hi.cc.u32 %7, %16, %18, %7;\n\t"
"madc.hi.cc.u32 %8, %17, %18, %8;\n\t"
"addc.u32 %9, %9, 0;"
: "+r"(r0), "+r"(r1), "+r"(r2), "+r"(r3), "+r"(r4),
"+r"(r5), "+r"(r6), "+r"(r7), "+r"(e), "=r"(overflow)
: "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4),
"r"(a5), "r"(a6), "r"(a7), "r"(w));
r->d[0] = r0; r->d[1] = r1; r->d[2] = r2; r->d[3] = r3;
r->d[4] = r4; r->d[5] = r5; r->d[6] = r6; r->d[7] = r7;
*extra = e;
return overflow;
}
#endif /* PROFANITY_PTX_MP */
// Which of the two the rest of this file gets.
static inline mp_word mp_mul_word_add_extra(mp_number * const r, const mp_number * const a, const mp_word w, mp_word * const extra) {
#ifdef PROFANITY_PTX_MP
return mp_mul_word_add_extra_ptx(r, a, w, extra);
#else
return mp_mul_word_add_extra_portable(r, a, w, extra);
#endif
}
// Multiplies a number with a word, potentially adds modhigher to it, and then subtracts it from
// an existing number, no extra words, no overflow.
//
// This is a special function only used for modular multiplication.
//
// Optimization (secp256k1 fast reduction) contributed by Rodrigo Madera (@madera).
//
// The secp256k1 prime has the special form:
//
// p = 2^256 - pmod, where pmod = 2^32 + 977 = 0x1000003D1
//
// Therefore, working modulo 2^256:
//
// q * p = q * (2^256 - pmod) = q * 2^256 - q * pmod == -q * pmod (mod 2^256)
//
// (r - q * p) mod 2^256 == (r + q * pmod) mod 2^256
//
// So instead of multiplying q by the full 256-bit p and subtracting, we multiply q by the
// 33-bit pmod and add. This reduces the amount of bits used, giving us 20-35% speed improvements.
//
// Two implementations of that addition follow. They differ only in how the
// three words go into r and not in what the three words are, so what they are
// is worked out once, here.
// q * pmod, which is never more than three words wide: pmod is 33 bits, so its
// product with a single word is 65, and the modhigher term shifts one copy of
// it up by a word.
static inline void mp_mod_word_addend(const mp_word w, const bool withModHigher, mp_word * const p0, mp_word * const p1, mp_word * const p2) {
const mp_word lo977 = 977u * w;
const mp_word hi977 = mul_hi(977u, w);
*p0 = lo977;
const ulong p1_full = (ulong)w + hi977 + (withModHigher ? 0x000003D1u : 0u);
*p1 = (mp_word)p1_full;
*p2 = (mp_word)(p1_full >> 32) + (withModHigher ? 1u : 0u);
}
void mp_mul_mod_word_sub_portable(mp_number * const r, const mp_word w, const bool withModHigher) {
mp_word p0, p1, p2;
mp_mod_word_addend(w, withModHigher, &p0, &p1, &p2);
ulong s = (ulong)r->d[0] + p0;
r->d[0] = (mp_word)s;
mp_word c = (mp_word)(s >> 32);
s = (ulong)r->d[1] + p1 + c;
r->d[1] = (mp_word)s;
c = (mp_word)(s >> 32);
s = (ulong)r->d[2] + p2 + c;
r->d[2] = (mp_word)s;
c = (mp_word)(s >> 32);
for (mp_word i = 3; i < MP_WORDS; ++i) {
s = (ulong)r->d[i] + c;
r->d[i] = (mp_word)s;
c = (mp_word)(s >> 32);
}
}
#ifdef PROFANITY_PTX_MP
/* The same addition as one carry chain rather than eight widening adds.
*
* Less of a win than the multiply above — a 64-bit add already lowers to
* add.cc/addc and the compiler does see through the pattern — but the five
* words past p2 exist only to carry, and written this way they cost one
* instruction each instead of an extension, an add and a shift.
*/
void mp_mul_mod_word_sub_ptx(mp_number * const r, const mp_word w, const bool withModHigher) {
mp_word p0, p1, p2;
mp_mod_word_addend(w, withModHigher, &p0, &p1, &p2);
mp_word r0 = r->d[0], r1 = r->d[1], r2 = r->d[2], r3 = r->d[3];
mp_word r4 = r->d[4], r5 = r->d[5], r6 = r->d[6], r7 = r->d[7];
asm volatile(
"add.cc.u32 %0, %0, %8;\n\t"
"addc.cc.u32 %1, %1, %9;\n\t"
"addc.cc.u32 %2, %2, %10;\n\t"
"addc.cc.u32 %3, %3, 0;\n\t"
"addc.cc.u32 %4, %4, 0;\n\t"
"addc.cc.u32 %5, %5, 0;\n\t"
"addc.cc.u32 %6, %6, 0;\n\t"
"addc.u32 %7, %7, 0;"
: "+r"(r0), "+r"(r1), "+r"(r2), "+r"(r3),
"+r"(r4), "+r"(r5), "+r"(r6), "+r"(r7)
: "r"(p0), "r"(p1), "r"(p2));
r->d[0] = r0; r->d[1] = r1; r->d[2] = r2; r->d[3] = r3;
r->d[4] = r4; r->d[5] = r5; r->d[6] = r6; r->d[7] = r7;
}
#endif /* PROFANITY_PTX_MP */
// Which of the two the rest of this file gets.
static inline void mp_mul_mod_word_sub(mp_number * const r, const mp_word w, const bool withModHigher) {
#ifdef PROFANITY_PTX_MP
mp_mul_mod_word_sub_ptx(r, w, withModHigher);
#else
mp_mul_mod_word_sub_portable(r, w, withModHigher);
#endif
}
// Modular multiplication. Based on Algorithm 3 (and a series of hunches) from this article:
// https://www.esat.kuleuven.be/cosic/publications/article-1191.pdf
// When I first implemented it I never encountered a situation where the additional end steps
// of adding or subtracting the modulo was necessary. Maybe it's not for the particular modulo
// used in secp256k1, maybe the overflow bit can be skipped in to avoid 8 subtractions and
// trade it for the final steps? Maybe the final steps are necessary but seldom needed?
// I have no idea, for the time being I'll leave it like this, also see the comments at the
// beginning of this document under the title "Cutting corners".
void mp_mod_mul(mp_number * const r, const mp_number * const X, const mp_number * const Y) {
mp_number Z = { {0} };
mp_word extraWord;
for (int i = MP_WORDS - 1; i >= 0; --i) {
// Z = Z * 2^32
extraWord = Z.d[7]; Z.d[7] = Z.d[6]; Z.d[6] = Z.d[5]; Z.d[5] = Z.d[4]; Z.d[4] = Z.d[3]; Z.d[3] = Z.d[2]; Z.d[2] = Z.d[1]; Z.d[1] = Z.d[0]; Z.d[0] = 0;
// Z = Z + X * Y_i
bool overflow = mp_mul_word_add_extra(&Z, X, Y->d[i], &extraWord);
// Z = Z - qM
mp_mul_mod_word_sub(&Z, extraWord, overflow);
}
*r = Z;
}
// =====================================================================================
// secp256k1 field prime p = 2^256 - 2^32 - 977, therefore
// 2^256 == 2^32 + 977 == 0x1000003D1 (mod p) [ call this constant c ]
//
// Instead of mp_mod_mul's 8 interleaved reductions we compute the whole product first and
// reduce ONCE:
// STAGE 1 : full 256x256 -> 512-bit product = low limbs L[0..7] + high limbs H[0..7]
// STAGE 2 : fold H back into L, because H*2^256 == H*c (mod p). Each high limb H[k]
// therefore contributes 977*H[k] at limb k and H[k] at limb k+1
// (that is exactly H[k]*(2^32 + 977)). A single carry ripple across the 8 limbs
// leaves a small (< 2^33) overflow which is folded once more through c into limbs
// 0..2, followed by one final tiny carry fold.
//
// The result is congruent mod p and < 2^256; it is NOT canonicalised to [0,p). This is the
// SAME contract stock mp_mod_mul honours (stock is itself only congruent, e.g. (p-1)^2 -> p+1)
// and, for in-domain operands (both < p), the fold lands on the identical representative as
// stock -- verified bit-exact on pocl over millions of random + edge + ambiguous-zone cases.
// =====================================================================================
#define MP_MOD_C_LO 0x000003D1u // low 32 bits of c = 2^32 + 977 (977 == 0x3D1)
// Shared STAGE 2 fold: given a 512-bit value as low limbs L[0..7] and high limbs H[0..7],
// write a congruent value < 2^256 into r. mul_hi + ulong idiom; exact on every device.
static inline void mp_mod_reduce_fold_portable(mp_number * const r, const mp_word * const L, const mp_word * const H) {
// pass 1: t[0..8] = L + H*c ; H[k] -> 977*H[k] at limb k and H[k] at limb k+1
mp_word t[MP_WORDS + 1];
ulong acc = 0;
for (int k = 0; k < MP_WORDS + 1; ++k) {
if (k < MP_WORDS) acc += (ulong)L[k];
if (k < MP_WORDS) acc += (ulong)977u * H[k];
if (k >= 1) acc += (ulong)H[k - 1];
t[k] = (mp_word)acc;
acc >>= 32;
}
const mp_word finalCarry = (mp_word)acc; // 0 or 1
// value above 2^256 is O = t[8] + finalCarry*2^32 (proven < 2^33).
// pass 2: fold O*2^256 == O*c == O*977 + O*2^32 into limbs 0..2.
const ulong add0 = (ulong)977u * t[MP_WORDS]; // 977*t8 -> limbs 0,1
const ulong add1 = (ulong)t[MP_WORDS] + (ulong)977u * finalCarry; // t8 + 977*fc -> limb 1
const ulong add2 = (ulong)finalCarry; // fc -> limb 2
ulong s, c;
s = (ulong)t[0] + (add0 & 0xffffffffu); r->d[0] = (mp_word)s; c = s >> 32;
s = (ulong)t[1] + (add0 >> 32) + (add1 & 0xffffffffu) + c; r->d[1] = (mp_word)s; c = s >> 32;
s = (ulong)t[2] + (add1 >> 32) + (add2 & 0xffffffffu) + c; r->d[2] = (mp_word)s; c = s >> 32;
for (int i = 3; i < MP_WORDS; ++i) { s = (ulong)t[i] + c; r->d[i] = (mp_word)s; c = s >> 32; }
const mp_word carry2 = (mp_word)c; // 0 or 1
// pass 3: fold that last lone 2^256 through c once more; provably no further carry-out.
if (carry2) {
s = (ulong)r->d[0] + MP_MOD_C_LO; r->d[0] = (mp_word)s; c = s >> 32;
s = (ulong)r->d[1] + 1u + c; r->d[1] = (mp_word)s; c = s >> 32;
for (int i = 2; i < MP_WORDS; ++i) { s = (ulong)r->d[i] + c; r->d[i] = (mp_word)s; c = s >> 32; }
}
}
// -------- PATCH #3: one-shot mod-mul (portable, mul_hi + ulong idiom) --------
void mp_mod_mul_oneshot_portable(mp_number * const r, const mp_number * const X, const mp_number * const Y) {
// STAGE 1: full 512-bit schoolbook product into P[0..15]
mp_word P[MP_WORDS * 2];
for (int i = 0; i < MP_WORDS * 2; ++i) P[i] = 0;
for (int i = 0; i < MP_WORDS; ++i) {
ulong carry = 0;
for (int j = 0; j < MP_WORDS; ++j) {
const ulong tt = (ulong)X->d[i] * Y->d[j] + P[i + j] + carry;
P[i + j] = (mp_word)tt;
carry = tt >> 32;
}
P[i + MP_WORDS] = (mp_word)carry;
}
// STAGE 2: fold high half (P[8..15]) into low half (P[0..7])
mp_mod_reduce_fold_portable(r, P, P + MP_WORDS);
}
// -------- PATCH #6: dedicated mod-sqr (portable) -- same STAGE-1/2 skeleton, symmetric STAGE 1 --------
void mp_mod_sqr_portable(mp_number * const r, const mp_number * const X) {
const mp_word * const a = X->d;
// STAGE 1 (symmetric): T = sum_{i<j} a[i]*a[j] (each cross term once, ~28 muls) ...
mp_word T[MP_WORDS * 2];
for (int i = 0; i < MP_WORDS * 2; ++i) T[i] = 0;
for (int i = 0; i < MP_WORDS; ++i) {
ulong carry = 0;
for (int j = i + 1; j < MP_WORDS; ++j) {
const ulong tt = (ulong)a[i] * a[j] + T[i + j] + carry;
T[i + j] = (mp_word)tt;
carry = tt >> 32;
}
for (int k = i + MP_WORDS; k < MP_WORDS * 2 && carry; ++k) {
const ulong tt = (ulong)T[k] + carry; T[k] = (mp_word)tt; carry = tt >> 32;
}
}
// ... then P = 2*T + diagonal(a[i]^2) (8 diagonal muls -> ~36 total)
mp_word P[MP_WORDS * 2];
ulong carry = 0;
for (int k = 0; k < MP_WORDS * 2; ++k) { const ulong tt = (ulong)T[k] * 2 + carry; P[k] = (mp_word)tt; carry = tt >> 32; }
ulong c = 0;
for (int i = 0; i < MP_WORDS; ++i) {
const ulong sq = (ulong)a[i] * a[i];
ulong s = (ulong)P[2 * i] + (sq & 0xffffffffu) + c; P[2 * i] = (mp_word)s; c = s >> 32;
s = (ulong)P[2 * i + 1] + (sq >> 32) + c; P[2 * i + 1] = (mp_word)s; c = s >> 32;
}
// STAGE 2: identical fold as mp_mod_mul_oneshot
mp_mod_reduce_fold_portable(r, P, P + MP_WORDS);
}
#ifdef PROFANITY_PTX_MP
// -------- PATCH #3: one-shot mod-mul (PTX) --------
// STAGE 1 accumulates the 512-bit product with the GPU-proven mad.lo.cc/madc.hi.cc primitive
// mp_mul_word_add_extra_ptx (r[0..7] += a[0..7]*w, extra += high word, returns carry-out).
// The accumulation bookkeeping is validated bit-exact vs the direct product on pocl using the
// portable twin of the primitive. STAGE 2 reuses the exact ulong fold above.
void mp_mod_mul_oneshot_ptx(mp_number * const r, const mp_number * const X, const mp_number * const Y) {
mp_word P[MP_WORDS * 2];
for (int i = 0; i < MP_WORDS * 2; ++i) P[i] = 0;
for (int j = 0; j < MP_WORDS; ++j) {
mp_number win;
for (int k = 0; k < MP_WORDS; ++k) win.d[k] = P[j + k]; // window = P[j .. j+7]
mp_word ex = P[j + MP_WORDS]; // extra = P[j+8]
mp_word ov = mp_mul_word_add_extra_ptx(&win, X, Y->d[j], &ex); // += X * Y[j] (mad chains)
for (int k = 0; k < MP_WORDS; ++k) P[j + k] = win.d[k];
P[j + MP_WORDS] = ex;
for (int k = j + MP_WORDS + 1; k < MP_WORDS * 2 && ov; ++k) { // ripple carry-out (0/1)
const ulong tt = (ulong)P[k] + ov; P[k] = (mp_word)tt; ov = (mp_word)(tt >> 32);
}
}
mp_mod_reduce_fold_portable(r, P, P + MP_WORDS);
}
// -------- PATCH #6: dedicated mod-sqr (PTX) -- symmetric STAGE 1 via the same primitive --------
// For each i, MAC (a with limbs 0..i zeroed) * a[i] at window offset i: this deposits
// a[k]*a[i] at limb i+k for every k>i, i.e. exactly the once-counted cross terms sum_{i<j}.
void mp_mod_sqr_ptx(mp_number * const r, const mp_number * const X) {
const mp_word * const a = X->d;
mp_word T[MP_WORDS * 2];
for (int i = 0; i < MP_WORDS * 2; ++i) T[i] = 0;
for (int i = 0; i < MP_WORDS - 1; ++i) {
mp_number amask;
for (int k = 0; k < MP_WORDS; ++k) amask.d[k] = (k > i) ? a[k] : 0u; // zero limbs 0..i
mp_number win;
for (int k = 0; k < MP_WORDS; ++k) win.d[k] = T[i + k];
mp_word ex = T[i + MP_WORDS];
mp_word ov = mp_mul_word_add_extra_ptx(&win, &amask, a[i], &ex); // T[i+k] += a[k]*a[i], k>i
for (int k = 0; k < MP_WORDS; ++k) T[i + k] = win.d[k];
T[i + MP_WORDS] = ex;
for (int k = i + MP_WORDS + 1; k < MP_WORDS * 2 && ov; ++k) {
const ulong tt = (ulong)T[k] + ov; T[k] = (mp_word)tt; ov = (mp_word)(tt >> 32);
}
}
mp_word P[MP_WORDS * 2];
ulong carry = 0;
for (int k = 0; k < MP_WORDS * 2; ++k) { const ulong tt = (ulong)T[k] * 2 + carry; P[k] = (mp_word)tt; carry = tt >> 32; }
ulong c = 0;
for (int i = 0; i < MP_WORDS; ++i) {
const ulong sq = (ulong)a[i] * a[i];
ulong s = (ulong)P[2 * i] + (sq & 0xffffffffu) + c; P[2 * i] = (mp_word)s; c = s >> 32;
s = (ulong)P[2 * i + 1] + (sq >> 32) + c; P[2 * i + 1] = (mp_word)s; c = s >> 32;
}
mp_mod_reduce_fold_portable(r, P, P + MP_WORDS);
}
#endif
static inline void mp_mod_mul_oneshot(mp_number * const r, const mp_number * const X, const mp_number * const Y) {
#ifdef PROFANITY_PTX_MP
mp_mod_mul_oneshot_ptx(r, X, Y);
#else
mp_mod_mul_oneshot_portable(r, X, Y);
#endif
}
static inline void mp_mod_sqr(mp_number * const r, const mp_number * const X) {
#ifdef PROFANITY_PTX_MP
mp_mod_sqr_ptx(r, X);
#else
mp_mod_sqr_portable(r, X);
#endif
}
// Modular inversion of a number.
void mp_mod_inverse(mp_number * const r) {
mp_number A = { { 1 } };
mp_number C = { { 0 } };
mp_number v = mod;
mp_word extraA = 0;
mp_word extraC = 0;
while (r->d[0] || r->d[1] || r->d[2] || r->d[3] || r->d[4] || r->d[5] || r->d[6] || r->d[7]) {
while (!(r->d[0] & 1)) {
mp_shr(r);
if (A.d[0] & 1) {
extraA += mp_add_mod(&A);
}
mp_shr_extra(&A, &extraA);
}
while (!(v.d[0] & 1)) {
mp_shr(&v);
if (C.d[0] & 1) {
extraC += mp_add_mod(&C);
}
mp_shr_extra(&C, &extraC);
}
if (mp_gte(r, &v)) {
mp_sub(r, r, &v);
mp_add_more(&A, &extraA, &C, &extraC);
}
else {
mp_sub(&v, &v, r);
mp_add_more(&C, &extraC, &A, &extraA);
}
}
while (extraC) {
extraC -= mp_sub_mod(&C);
}
v = mod;
mp_sub(r, &v, &C);
}
/* ------------------------------------------------------------------------ */
/* Elliptic point and addition (with caveats). */
/* ------------------------------------------------------------------------ */
typedef struct {
mp_number x;
mp_number y;
} point;
// Elliptical point addition
// Does not handle points sharing X coordinate, this is a deliberate design choice.
// For more information on this choice see the beginning of this file.
void point_add(point * const r, point * const p, point * const o) {
mp_number tmp;
mp_number newX;
mp_number newY;
mp_mod_sub(&tmp, &o->x, &p->x);
mp_mod_inverse(&tmp);
mp_mod_sub(&newX, &o->y, &p->y);
mp_mod_mul_oneshot(&tmp, &tmp, &newX); // was mp_mod_mul (orig :417)
mp_mod_sqr(&newX, &tmp); // was mp_mod_mul(&newX,&tmp,&tmp) = tmp^2 (orig :419)
mp_mod_sub(&newX, &newX, &p->x);
mp_mod_sub(&newX, &newX, &o->x);
mp_mod_sub(&newY, &p->x, &newX);
mp_mod_mul_oneshot(&newY, &newY, &tmp); // was mp_mod_mul (orig :424)
mp_mod_sub(&newY, &newY, &p->y);
r->x = newX;
r->y = newY;
}
/* ------------------------------------------------------------------------ */
/* Profanity. */
/* ------------------------------------------------------------------------ */
// foundVariant says which of the addresses a point yields this one was, and so
// what has to be done to the private key behind it — see PROFANITY_VARIANTS and
// profanity_iterate. Zero is the point itself, whose key is the scalar the host
// prints unchanged; one is its negation, whose key is that scalar's.
//
// foundRound says which of the launch's PROFANITY_ROUNDS point additions the
// address turned up at, every one of which lands on a different scalar. Without
// it a launch doing more than one would have no way to say which.
typedef struct {
uint found;
uint foundId;
uint foundRound;
uint foundVariant;
uchar foundHash[20];
} result;
void profanity_init_seed(__global const point * const precomp, point * const p, bool * const pIsFirst, const size_t precompOffset, const ulong seed) {
point o;
for (uchar i = 0; i < 8; ++i) {
const uchar shift = i * 8;
const uchar byte = (seed >> shift) & 0xFF;
if (byte) {
o = precomp[precompOffset + i * 255 + byte - 1];
if (*pIsFirst) {
*p = o;
*pIsFirst = false;
}
else {
point_add(p, p, &o);
}
}
}
}
// The seeding, which a CREATE2 search has nothing to seed: it walks over salts
// rather than points, and every scalar multiplication below is code compiled
// into its program for a kernel it will never enqueue. Dispatcher only creates
// these two for a search with a key in it, so this follows the same condition
// the target already decides — see initBegin and initCreate2.
#if !defined(PROFANITY_CREATE2_SCORER)
// The part of every work item's starting point that does not depend on the work
// item. Only the top limb of the seed carries the index, so the other three
// contribute the same point to all of them — three quarters of the scalar
// multiplication below, repeated once per point in the search and thrown away
// each time. Done once here, and read back out of pUniform by every work item.
//
// createSeed keeps those three limbs from all being zero, so there is always a
// point here — the identity is not something the affine point_add can be handed,
// and every work item below relies on starting from a real one.
__kernel void profanity_init_uniform(__global const point * const precomp, __global point * const pUniform, const ulong4 seed) {
point p;
bool bIsFirst = true;
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 0, seed.x);
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 1, seed.y);
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 2, seed.z);
*pUniform = p;
}
__kernel void profanity_init(__global const point * const precomp, __global mp_number * const pDeltaX, __global mp_number * const pPrevLambda, __global result * const pResult, const ulong4 seed, const ulong4 seedX, const ulong4 seedY, __global const point * const pUniform) {
const size_t id = get_global_id(0);
point p = {
.x = {.d = {
seedX.x & 0xFFFFFFFF, seedX.x >> 32,
seedX.y & 0xFFFFFFFF, seedX.y >> 32,
seedX.z & 0xFFFFFFFF, seedX.z >> 32,
seedX.w & 0xFFFFFFFF, seedX.w >> 32,
}},
.y = {.d = {
seedY.x & 0xFFFFFFFF, seedY.x >> 32,
seedY.y & 0xFFFFFFFF, seedY.y >> 32,
seedY.z & 0xFFFFFFFF, seedY.z >> 32,
seedY.w & 0xFFFFFFFF, seedY.w >> 32,
}},
};
mp_number tmp1, tmp2;
point tmp3;
// Calculate k*G where k = seed.wzyx (in other words, find the point indicated
// by the private key represented in seed). The low three limbs of the seed are
// the same for every work item and profanity_init_uniform did them once for
// all of them; only the top one, which carries the work item's index, is left
// to do here. createSeed keeps those three from all being zero, so there is
// always a point to start from.
point p_random = *pUniform;
bool bIsFirst = false;
profanity_init_seed(precomp, &p_random, &bIsFirst, 8 * 255 * 3, seed.w + id);
point_add(&p, &p, &p_random);
// Calculate current lambda in this point
mp_mod_sub_gx(&tmp1, &p.x);
mp_mod_inverse(&tmp1);
mp_mod_sub_gy(&tmp2, &p.y);
mp_mod_mul(&tmp1, &tmp1, &tmp2);
// Jump to next point (precomp[0] is the generator point G)
tmp3 = precomp[0];
point_add(&p, &tmp3, &p);
// pDeltaX should contain the delta (x - G_x)
mp_mod_sub_gx(&p.x, &p.x);
pDeltaX[id] = p.x;
pPrevLambda[id] = tmp1;
// One entry each rather than the whole buffer each: every work item clearing
// all of it meant PROFANITY_MAX_SCORE writes per point to the same handful of
// addresses, which is millions of stores contending over one cache line for
// work that forty of them can do once. The seeding pass starts at zero, so
// these are the first work items to run.
if (id < PROFANITY_MAX_SCORE + 1) {
pResult[id].found = 0;
}
}
#endif /* !PROFANITY_CREATE2_SCORER */
// This kernel calculates several modular inversions at once with just one inverse.
// It's an implementation of Algorithm 2.11 from Modern Computer Arithmetic:
// https://members.loria.fr/PZimmermann/mca/pub226.html
#if PROFANITY_INVERSE_STRIP == 0 && PROFANITY_INVERSE_GROUP == 0
// Single-level (default): invert 1 batch of PROFANITY_INVERSE_SIZE points.
#define PROFANITY_TWO_LEVEL_INVERSE 0
#elif PROFANITY_INVERSE_STRIP > 0 && PROFANITY_INVERSE_GROUP > 0
// Two-level: batch PROFANITY_INVERSE_STRIP points, and share an inverse across
// PROFANITY_INVERSE_GROUP strips. Slower on some GPUs, so is opt-in only.
#define PROFANITY_TWO_LEVEL_INVERSE 1
#else
#error "PROFANITY_INVERSE_STRIP and PROFANITY_INVERSE_GROUP must both be 0 or both be non-zero"
#endif
// Both inversion schemes are fused into the scoring kernel at the end of this
// file rather than run as a kernel of their own. An inverse is consumed by the
// point addition that immediately follows it, so writing every one of them out
// to global memory for a second kernel to read straight back in was 64 bytes a
// point of traffic and a kernel launch, for a value that never needed to leave
// registers. See PROFANITY_ROUND, which is where the fusing happens.
static inline uchar profanity_byte(const uint * const address, const int i) {
return (uchar)(address[i >> 2] >> ((i & 3) << 3));
}
// This kernel performs en elliptical curve point addition. See:
// https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_addition
// I've made one mathematical optimization by never calculating x_r,
// instead I directly calculate the delta (x_q - x_p). It's for this
// delta we calculate the inverse and that's already been done at this
// point. By calculating and storing the next delta we don't have to
// calculate the delta in profanity_inverse_multiple which saves us
// one call to mp_mod_sub per point, but inversely we have to introduce
// an addition (or addition by subtracting a negative number) in
// profanity_end to retrieve the actual x-coordinate instead of the
// delta as that's what used for calculating the public hash.
//
// One optimization is when calculating the next y-coordinate. As
// given in the wiki the next y-coordinate is given by:
// y_r = λ²(x_p - x_r) - y_p
// In our case the other point P is the generator point so x_p = G_x,
// a constant value. x_r is the new point which we never calculate, we
// calculate the new delta (x_q - x_p) instead. Let's denote the delta
// with d and new delta as d' and remove notation for points P and Q and
// instead refeer to x_p as G_x, y_p as G_y and x_q as x, y_q as y.
// Furthermore let's denote new x by x' and new y with y'.
//
// Then we have:
// d = x - G_x <=> x = d + G_x
// x' = λ² - G_x - x <=> x_r = λ² - G_x - d - G_x = λ² - 2G_x - d
//
// d' = x' - G_x = λ² - 2G_x - d - G_x = λ² - 3G_x - d
//
// So we see that the new delta d' can be calculated with the same
// amount of steps as the new x'; 3G_x is still just a single constant.
//
// Now for the next y-coordinate in the new notation:
// y' = λ(G_x - x') - G_y
//
// If we expand the expression (G_x - x') we can see that this
// subtraction can be removed! Saving us one call to mp_mod_sub!
// G_x - x' = -(x' - G_x) = -d'
// It has the same value as the new delta but negated! We can avoid
// having to perform the negation by:
// y' = λ * -d' - G_y = -G_y - (λ * d')
//
// We can just precalculate the constant -G_y and we get rid of one
// subtraction. Woo!
//
// But we aren't done yet! Let's expand the expression for the next
// lambda, λ'. We have:
// λ' = (y' - G_y) / d'