interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / om / p256 / p256.om

682 lines · 18073 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note p256.om: ECDSA over NIST P-256 with SHA-256, signing with the RFC 6979 deterministic nonce and
3note verification, written to match the spark-rsi judge signer byte for byte (RustCrypto p256 0.14.0,
4note ecdsa 0.17.0, rfc6979 0.6.0): the signature is over SHA-256(message), r||s 64 bytes big endian,
5note s is NOT normalized to low-S (p256 sets NORMALIZE_S = false) and verification accepts both s
6note and n - s. Library unit: functions and constants only, no main, no globals. It needs the sibling
7note unit om/p256/sha256.om (the omega-mv om/sha256/sha256.om bytes) concatenated before this file.
8note This is a parity-proven library, not a signer deployment: it holds no key and reads no file.
9note
10note Numbers: 256-bit values are 8 limbs of 32 bits, least significant first, one limb per ulong cell
11note of the workspace w (ulong 1024). Arithmetic mod p (field) and mod n (group order) is Montgomery
12note multiplication (CIOS, R = 2^256): a limb product plus two limbs is at most 2^64 - 1, so no ulong
13note ever wraps where a carry is meant. Every add, sub and mul result is fully reduced (below the
14note modulus); inputs must be below the modulus.
15note
16note Side channels (see README): field and scalar add, sub, mul and the final subtraction are branch
17note free and index only by public loop counters; the secret-scalar multiply is double-and-add-always
18note over all 256 bits with a masked select and complete (Renes-Costello-Batina 2015, Algorithm 4)
19note additions, so no secret-dependent branch or index occurs in it. Not constant time: the RFC 6979
20note retry (k >= n, r = 0, s = 0) branches on secret-derived values, as the Rust crate does; the
21note compiler gives no constant-time guarantee (omc instruction selection is not audited for it);
22note the key and nonce stay in caller memory with no zeroing.
23note
24note Workspace layout (cell offsets into w). A modulus block of 48 cells at PMP (p) and PMN (n):
25note +0 the modulus, +8 n0' = -m^-1 mod 2^32, +16 R^2 mod m, +24 R mod m (Montgomery one), +32 m - 2.
26note Points are projective (X : Y : Z), 24 cells, coordinates in the Montgomery domain mod p.
27note Byte buffer b (uchar 4096): b[0..512) HMAC/SHA staging, PBK K, PBV V, PBX x, PBH h1, 1152 inner
28note tag, PBT candidate k (RFC 6979 3.2), 2048..3072 message, 3072..3137 public key (0x04 || X || Y),
29note 3200..3264 signature r||s, 3264..3296 private key d, 3296..3328 SHA-256 of the message.
30
31const PMP ulong 0
32const PMN ulong 48
33const PONE1 ulong 96
34const PBM ulong 104
35const PGX ulong 120
36const PW1 ulong 160
37const PW2 ulong 168
38const PXX ulong 176
39const PYY ulong 184
40const PZZ ulong 192
41const PXY ulong 200
42const PYZ ulong 208
43const PXZ ulong 216
44const PU ulong 224
45const PV ulong 232
46const PYM ulong 240
47const PYP ulong 248
48const PBX3 ulong 256
49const PXX3 ulong 264
50const PZZ3 ulong 272
51const PRX ulong 280
52const PACC ulong 304
53const PTMP ulong 328
54const PBASE ulong 352
55const PZI ulong 376
56const PD ulong 512
57const PK ulong 520
58const PZ ulong 528
59const PR ulong 536
60const PS ulong 544
61const PU1 ulong 552
62const PU2 ulong 560
63const PT1 ulong 576
64const PT2 ulong 584
65const PT3 ulong 592
66const PX ulong 608
67const PY ulong 616
68const PQ ulong 640
69const PP1 ulong 664
70const PP2 ulong 688
71const PBK ulong 1024
72const PBV ulong 1056
73const PBX ulong 1088
74const PBH ulong 1120
75const PBI ulong 1152
76const PBT ulong 1184
77const PBMSG ulong 2048
78const PBPUB ulong 3072
79const PBSIG ulong 3200
80const PBKEY ulong 3264
81const PBDIG ulong 3296
82
83fn p2cpy w edit ulong 1024 o ulong a ulong to void
84for i ulong 8
85put w add o i get w add a i
86end
87end
88
89fn p2pcpy w edit ulong 1024 o ulong a ulong to void
90for i ulong 24
91put w add o i get w add a i
92end
93end
94
95note o = the small number v
96fn p2set w edit ulong 1024 o ulong v ulong to void
97put w o v
98for i ulong 7
99put w add o add i 1 0
100end
101end
102
103note o = t mod m for t = t[0..8) + t[8] 2^256 below 2m: subtract m, keep the difference when t[8] is
104note set or the subtraction did not borrow; branch free (masked select)
105fn p2red w edit ulong 1024 o ulong t view ulong 10 m ulong to void
106u = new ulong 8 0
107bw = ulong 0
108x = ulong 0
109for i ulong 8
110x = sub sub get t i get w add m i bw
111put u i and x 4294967295
112bw = shr x 63
113end
114sel = or get t 8 xor bw 1
115mk = sub 0 sel
116for j ulong 8
117y = and get u j mk
118put w add o j or y and get t j not mk
119end
120end
121
122note t += a * bi over 8 limbs, carry into t[8], t[9] (one CIOS row)
123fn p2mrow t edit ulong 10 w edit ulong 1024 a ulong bi ulong to void
124c = ulong 0
125x = ulong 0
126for j ulong 8
127x = add add get t j mul get w add a j bi c
128put t j and x 4294967295
129c = shr x 32
130end
131x = add get t 8 c
132put t 8 and x 4294967295
133put t 9 shr x 32
134end
135
136note t = (t + q m) / 2^32 with q = t[0] n0' mod 2^32 (one CIOS reduction step)
137fn p2mred t edit ulong 10 w edit ulong 1024 m ulong to void
138q = and mul get t 0 get w add m 8 4294967295
139x = add get t 0 mul q get w m
140c = shr x 32
141j1 = ulong 0
142for k ulong 7
143j1 = add k 1
144x = add add get t j1 mul q get w add m j1 c
145put t k and x 4294967295
146c = shr x 32
147end
148x = add get t 8 c
149put t 7 and x 4294967295
150put t 8 add get t 9 shr x 32
151end
152
153note o = a b R^-1 mod m (Montgomery product); o may be a or b
154fn p2mul w edit ulong 1024 o ulong a ulong b ulong m ulong to void
155t = new ulong 10 0
156for i ulong 8
157p2mrow t w a get w add b i
158p2mred t w m
159end
160p2red w o t m
161end
162
163note o = a + b mod m; o may be a or b
164fn p2add w edit ulong 1024 o ulong a ulong b ulong m ulong to void
165t = new ulong 10 0
166c = ulong 0
167x = ulong 0
168for i ulong 8
169x = add add get w add a i get w add b i c
170put t i and x 4294967295
171c = shr x 32
172end
173put t 8 c
174p2red w o t m
175end
176
177note o = a - b mod m; o may be a or b (adds m under a mask when the subtraction borrows)
178fn p2sub w edit ulong 1024 o ulong a ulong b ulong m ulong to void
179t = new ulong 8 0
180bw = ulong 0
181x = ulong 0
182for i ulong 8
183x = sub sub get w add a i get w add b i bw
184put t i and x 4294967295
185bw = shr x 63
186end
187mk = sub 0 bw
188c = ulong 0
189for j ulong 8
190x = add add get t j and get w add m j mk c
191put w add o j and x 4294967295
192c = shr x 32
193end
194end
195
196note o = a mod m for a below 2m (one masked subtraction)
197fn p2modr w edit ulong 1024 o ulong m ulong to void
198t = new ulong 10 0
199for i ulong 8
200put t i get w add o i
201end
202p2red w o t m
203end
204
205note a < m as 256-bit integers
206fn p2lt w edit ulong 1024 a ulong m ulong to bool
207bw = ulong 0
208x = ulong 0
209for i ulong 8
210x = sub sub get w add a i get w add m i bw
211bw = shr x 63
212end
213ret eq bw 1
214end
215
216fn p2isz w edit ulong 1024 a ulong to bool
217v = ulong 0
218for i ulong 8
219v = or v get w add a i
220end
221ret eq v 0
222end
223
224fn p2eqv w edit ulong 1024 a ulong b ulong to bool
225v = ulong 0
226for i ulong 8
227v = or v xor get w add a i get w add b i
228end
229ret eq v 0
230end
231
232note 1 <= a < n
233fn p2rng w edit ulong 1024 a ulong to bool
234ret and not p2isz w a p2lt w a PMN
235end
236
237note o = a^e in the Montgomery domain mod m; the exponent e (plain limbs) is public: the branch
238note follows its bits. o may be a. Uses PW1, PW2.
239fn p2pow w edit ulong 1024 o ulong a ulong e ulong m ulong to void
240p2cpy w PW2 a
241p2cpy w PW1 add m 24
242i = ulong 0
243bit = ulong 0
244for j ulong 256
245i = sub 255 j
246p2mul w PW1 PW1 PW1 m
247bit = and shr get w add e shr i 5 and i 31 1
248if eq bit 1
249p2mul w PW1 PW1 PW2 m
250end
251end
252p2cpy w o PW1
253end
254
255note o = a^-1 = a^(m-2) (Fermat, m prime; 0 gives 0)
256fn p2inv w edit ulong 1024 o ulong a ulong m ulong to void
257p2pow w o a add m 32 m
258end
259
260note into the Montgomery domain (a R) and out of it (a R^-1)
261fn p2tom w edit ulong 1024 o ulong a ulong m ulong to void
262p2mul w o a add m 16 m
263end
264
265fn p2frm w edit ulong 1024 o ulong a ulong m ulong to void
266p2mul w o a PONE1 m
267end
268
269note a modulus block: n0' by Newton steps (x = m0 is right to 3 bits, five steps give 96),
270note R^2 mod m by 512 modular doublings of 1, R mod m = mont(1, R^2), and m - 2 (m0 >= 2 here)
271fn p2mod w edit ulong 1024 m ulong to void
272m0 = get w m
273x = m0
274for i ulong 5
275x = mul x sub 2 mul m0 x
276end
277put w add m 8 and sub 0 x 4294967295
278p2set w add m 16 1
279for j ulong 512
280p2add w add m 16 add m 16 add m 16 m
281end
282p2mul w add m 24 PONE1 add m 16 m
283p2cpy w add m 32 m
284put w add m 32 sub get w add m 32 2
285end
286
287note curve constants (SEC 2 v2 section 2.4.2, FIPS 186-4 D.1.2.3), 32-bit limbs least significant
288note first: p, n, b, G = (Gx, Gy). b and G are stored in the Montgomery domain, G with Z = R.
289fn p2init w edit ulong 1024 to void
290for i ulong 1024
291put w i 0
292end
293put w 0 4294967295
294put w 1 4294967295
295put w 2 4294967295
296put w 6 1
297put w 7 4294967295
298put w 48 4234356049
299put w 49 4089039554
300put w 50 2803342980
301put w 51 3169254061
302put w 52 4294967295
303put w 53 4294967295
304put w 55 4294967295
305put w PONE1 1
306p2mod w PMP
307p2mod w PMN
308put w PT1 668098635
309put w 577 1003371582
310put w 578 3428036854
311put w 579 1696401072
312put w 580 1989707452
313put w 581 3018571093
314put w 582 2855965671
315put w 583 1522939352
316p2tom w PBM PT1 PMP
317put w PT1 3633889942
318put w 577 4104206661
319put w 578 770388896
320put w 579 1996717441
321put w 580 1671708914
322put w 581 4173129445
323put w 582 3777774151
324put w 583 1796723186
325p2tom w PGX PT1 PMP
326put w PT1 935285237
327put w 577 3417718888
328put w 578 1798397646
329put w 579 734933847
330put w 580 2081398294
331put w 581 2397563722
332put w 582 4263149467
333put w 583 1340293858
334p2tom w add PGX 8 PT1 PMP
335p2cpy w add PGX 16 add PMP 24
336p2set w PT1 0
337end
338
339note o = a + q, complete projective addition for a = -3 (Renes-Costello-Batina 2015, Algorithm 4,
340note in the grouping of RustCrypto primeorder 0.14 EquationAIsMinusThree::add_assign). Valid for
341note a = q and for the identity (0 : 1 : 0). o may be a or q; a and q lie outside PXX..PRX+24.
342fn p2padd w edit ulong 1024 o ulong a ulong q ulong to void
343p2mul w PXX a q PMP
344p2mul w PYY add a 8 add q 8 PMP
345p2mul w PZZ add a 16 add q 16 PMP
346p2add w PU a add a 8 PMP
347p2add w PV q add q 8 PMP
348p2mul w PXY PU PV PMP
349p2add w PU PXX PYY PMP
350p2sub w PXY PXY PU PMP
351p2add w PU add a 8 add a 16 PMP
352p2add w PV add q 8 add q 16 PMP
353p2mul w PYZ PU PV PMP
354p2add w PU PYY PZZ PMP
355p2sub w PYZ PYZ PU PMP
356p2add w PU a add a 16 PMP
357p2add w PV q add q 16 PMP
358p2mul w PXZ PU PV PMP
359p2add w PU PXX PZZ PMP
360p2sub w PXZ PXZ PU PMP
361note bzz3 = 3 (xz - b zz); ym = yy - bzz3; yp = yy + bzz3
362p2mul w PU PBM PZZ PMP
363p2sub w PU PXZ PU PMP
364p2add w PV PU PU PMP
365p2add w PU PV PU PMP
366p2sub w PYM PYY PU PMP
367p2add w PYP PYY PU PMP
368note zz3 = 3 zz; bxz3 = 3 (b xz - (zz3 + xx)); xx3 = 3 xx - zz3
369p2add w PZZ3 PZZ PZZ PMP
370p2add w PZZ3 PZZ3 PZZ PMP
371p2mul w PU PBM PXZ PMP
372p2add w PV PZZ3 PXX PMP
373p2sub w PU PU PV PMP
374p2add w PV PU PU PMP
375p2add w PBX3 PV PU PMP
376p2add w PV PXX PXX PMP
377p2add w PV PV PXX PMP
378p2sub w PXX3 PV PZZ3 PMP
379note X3 = yp xy - yz bxz3; Y3 = yp ym + xx3 bxz3; Z3 = ym yz + xy xx3
380p2mul w PU PYP PXY PMP
381p2mul w PV PYZ PBX3 PMP
382p2sub w PRX PU PV PMP
383p2mul w PU PYP PYM PMP
384p2mul w PV PXX3 PBX3 PMP
385p2add w add PRX 8 PU PV PMP
386p2mul w PU PYM PYZ PMP
387p2mul w PV PXY PXX3 PMP
388p2add w add PRX 16 PU PV PMP
389p2pcpy w o PRX
390end
391
392note PACC = PTMP when bit is 1, else PACC kept: a masked select over all 24 cells, no branch on bit
393fn p2psel w edit ulong 1024 bit ulong to void
394mk = sub 0 bit
395nm = not mk
396for c ulong 24
397y = and get w add PTMP c mk
398put w add PACC c or y and get w add PACC c nm
399end
400end
401
402note o = k p for the plain scalar k (8 limbs, may be secret) and the point p: double-and-add-always
403note over all 256 bits, the sum kept or dropped by a masked select (no branch on k). o may be p.
404fn p2smul w edit ulong 1024 o ulong k ulong p ulong to void
405p2pcpy w PBASE p
406p2set w PACC 0
407p2cpy w add PACC 8 add PMP 24
408p2set w add PACC 16 0
409i = ulong 0
410for j ulong 256
411i = sub 255 j
412p2padd w PACC PACC PACC
413p2padd w PTMP PACC PBASE
414p2psel w and shr get w add k shr i 5 and i 31 1
415end
416p2pcpy w o PACC
417end
418
419note affine x, y (plain) of the projective point p; the identity gives (0, 0)
420fn p2aff w edit ulong 1024 x ulong y ulong p ulong to void
421p2inv w PZI add p 16 PMP
422p2mul w x p PZI PMP
423p2frm w x x PMP
424p2mul w y add p 8 PZI PMP
425p2frm w y y PMP
426end
427
428note 32 big-endian bytes of b at off into the 8 limbs at o, and back
429fn p2ld w edit ulong 1024 o ulong b edit uchar 4096 off ulong to void
430q = ulong 0
431v = ulong 0
432for i ulong 8
433q = add off sub 28 mul i 4
434v = ulong get b q
435v = or shl v 8 ulong get b add q 1
436v = or shl v 8 ulong get b add q 2
437v = or shl v 8 ulong get b add q 3
438put w add o i v
439end
440end
441
442fn p2st w edit ulong 1024 o ulong b edit uchar 4096 off ulong to void
443q = ulong 0
444v = ulong 0
445for i ulong 8
446q = add off sub 28 mul i 4
447v = get w add o i
448put b q uchar and shr v 24 255
449put b add q 1 uchar and shr v 16 255
450put b add q 2 uchar and shr v 8 255
451put b add q 3 uchar and v 255
452end
453end
454
455note the digest om/sha256 final leaves packed in s[0..4) (byte k = byte k mod 8 of s[k / 8]) into b[out..out+32)
456fn p2dig b edit uchar 4096 s edit ulong 64 out ulong to void
457for i ulong 32
458put b add out i uchar and shr get s shr i 3 mul and i 7 8 255
459end
460end
461
462note b[out..out+32) = SHA-256(b[off..off+n)); the bytes are first moved down to b[0..n), so off is 0
463note or off >= n; n <= 1024
464fn p2sha c edit uchar 104 b edit uchar 4096 s edit ulong 64 off ulong n ulong out ulong to void
465if gt n 1024
466trap 3
467end
468for i ulong n
469put b i get b add off i
470end
471init c
472absorb c b n s
473final c s
474p2dig b s out
475end
476
477note HMAC-SHA-256 (RFC 2104) keyed by the 32 bytes at b[kof..kof+32) over the mlen bytes the caller
478note staged at b[64..64+mlen); the tag goes to b[oof..oof+32) (oof may be kof). kof, oof >= 1024
479note and not PBI; mlen <= 448.
480fn p2hmac c edit uchar 104 b edit uchar 4096 s edit ulong 64 kof ulong mlen ulong oof ulong to void
481if gt mlen 448
482trap 3
483end
484for i ulong 32
485put b i xor get b add kof i 54
486put b add i 32 54
487end
488init c
489absorb c b add 64 mlen s
490final c s
491p2dig b s PBI
492for k ulong 32
493put b k xor get b add kof k 92
494put b add k 32 92
495put b add k 64 get b add PBI k
496end
497init c
498absorb c b 96 s
499final c s
500p2dig b s oof
501end
502
503note V = HMAC_K(V)
504fn p2vnext c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void
505for i ulong 32
506put b add 64 i get b add PBV i
507end
508p2hmac c b s PBK 32 PBV
509end
510
511note RFC 6979 3.2 steps d-g: K = HMAC_K(V || sep || x || h1), V = HMAC_K(V)
512fn p2upd c edit uchar 104 b edit uchar 4096 s edit ulong 64 sep uchar to void
513for i ulong 32
514put b add 64 i get b add PBV i
515put b add 97 i get b add PBX i
516put b add 129 i get b add PBH i
517end
518put b 96 sep
519p2hmac c b s PBK 97 PBK
520p2vnext c b s
521end
522
523note RFC 6979 3.2 steps b-g: V = 0x01 * 32, K = 0x00 * 32, then the two updates
524fn p2drbg c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void
525for i ulong 32
526put b add PBV i 1
527put b add PBK i 0
528end
529p2upd c b s 0
530p2upd c b s 1
531end
532
533note RFC 6979 3.2 step h: T = V = HMAC_K(V) into PBT (qlen = hlen = 256: one block), then
534note K = HMAC_K(V || 0x00), V = HMAC_K(V), as rfc6979 0.6 HmacDrbg::fill_bytes does after every draw
535fn p2gen c edit uchar 104 b edit uchar 4096 s edit ulong 64 to void
536p2vnext c b s
537for i ulong 32
538put b add PBT i get b add PBV i
539put b add 64 i get b add PBV i
540end
541put b 96 0
542p2hmac c b s PBK 33 PBK
543p2vnext c b s
544end
545
546note one signing attempt with the candidate k at PK (1 <= k < n): 0 and r||s at b[PBSIG..) on success,
547note 2 when r or s is 0 (the caller draws the next k, as ecdsa 0.17 sign_prehashed_rfc6979 does)
548fn p2try w edit ulong 1024 b edit uchar 4096 to ulong
549p2smul w PP1 PK PGX
550p2aff w PX PY PP1
551p2cpy w PR PX
552p2modr w PR PMN
553if p2isz w PR
554ret 2
555end
556p2tom w PT1 PK PMN
557p2inv w PT1 PT1 PMN
558p2tom w PT2 PR PMN
559p2tom w PT3 PD PMN
560p2mul w PT2 PT2 PT3 PMN
561p2tom w PT3 PZ PMN
562p2add w PT2 PT2 PT3 PMN
563p2mul w PS PT1 PT2 PMN
564p2frm w PS PS PMN
565if p2isz w PS
566ret 2
567end
568p2st w PR b PBSIG
569p2st w PS b add PBSIG 32
570ret 0
571end
572
573note p2sign: ECDSA P-256 signature of SHA-256(b[PBMSG..PBMSG+mlen)), mlen <= 1024, under the key d
574note (32 bytes big endian) at b[PBKEY..). 0: r||s at b[PBSIG..PBSIG+64), the accepted k at w[PK..).
575note 1: d is 0 or >= n (refused, as SigningKey::from_slice refuses it).
576fn p2sign w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 mlen ulong to ulong
577p2ld w PD b PBKEY
578if not p2rng w PD
579ret 1
580end
581p2sha c b s PBMSG mlen PBDIG
582p2ld w PZ b PBDIG
583p2modr w PZ PMN
584p2st w PZ b PBH
585for i ulong 32
586put b add PBX i get b add PBKEY i
587end
588p2drbg c b s
589st = ulong 2
590while eq st 2 bound 64
591p2gen c b s
592p2ld w PK b PBT
593if p2rng w PK
594st = p2try w b
595end
596end
597ret st
598end
599
600note p2pub: the public key of d at b[PBKEY..) as uncompressed SEC1 0x04 || X || Y at
601note b[PBPUB..PBPUB+65). 0 done, 1 d refused (0 or >= n).
602fn p2pub w edit ulong 1024 b edit uchar 4096 to ulong
603p2ld w PD b PBKEY
604if not p2rng w PD
605ret 1
606end
607p2smul w PP1 PD PGX
608p2aff w PX PY PP1
609put b PBPUB 4
610p2st w PX b add PBPUB 1
611p2st w PY b add PBPUB 33
612ret 0
613end
614
615note the affine point (PX, PY) (plain) is on y^2 = x^3 - 3x + b with x, y < p
616fn p2oncv w edit ulong 1024 to bool
617if not p2lt w PX PMP
618ret false
619end
620if not p2lt w PY PMP
621ret false
622end
623p2tom w PT1 PX PMP
624p2tom w PT2 PY PMP
625p2mul w PT3 PT2 PT2 PMP
626p2mul w PT2 PT1 PT1 PMP
627p2mul w PT2 PT2 PT1 PMP
628p2sub w PT2 PT2 PT1 PMP
629p2sub w PT2 PT2 PT1 PMP
630p2sub w PT2 PT2 PT1 PMP
631p2add w PT2 PT2 PBM PMP
632ret p2eqv w PT2 PT3
633end
634
635note p2vrfy: 1 when r||s at b[PBSIG..PBSIG+64) is a valid signature of SHA-256(b[PBMSG..PBMSG+mlen))
636note under the uncompressed SEC1 key at b[PBPUB..PBPUB+65), else 0: a key that is not 0x04 || X || Y
637note with X, Y < p on the curve, r or s outside 1..n-1, u1 G + u2 Q the identity, or x mod n != r.
638note High s is accepted (p256 0.14 does not normalize s). Variable time is fine here: all inputs are public.
639fn p2vrfy w edit ulong 1024 c edit uchar 104 b edit uchar 4096 s edit ulong 64 mlen ulong to ulong
640if ne get b PBPUB 4
641ret 0
642end
643p2ld w PX b add PBPUB 1
644p2ld w PY b add PBPUB 33
645if not p2oncv w
646ret 0
647end
648p2ld w PR b PBSIG
649p2ld w PS b add PBSIG 32
650if not p2rng w PR
651ret 0
652end
653if not p2rng w PS
654ret 0
655end
656p2sha c b s PBMSG mlen PBDIG
657p2ld w PZ b PBDIG
658p2modr w PZ PMN
659p2tom w PT1 PS PMN
660p2inv w PT1 PT1 PMN
661p2tom w PT2 PZ PMN
662p2mul w PU1 PT2 PT1 PMN
663p2frm w PU1 PU1 PMN
664p2tom w PT2 PR PMN
665p2mul w PU2 PT2 PT1 PMN
666p2frm w PU2 PU2 PMN
667p2tom w PQ PX PMP
668p2tom w add PQ 8 PY PMP
669p2cpy w add PQ 16 add PMP 24
670p2smul w PP1 PU1 PGX
671p2smul w PP2 PU2 PQ
672p2padd w PP1 PP1 PP2
673if p2isz w add PP1 16
674ret 0
675end
676p2aff w PX PY PP1
677p2modr w PX PMN
678if p2eqv w PX PR
679ret 1
680end
681ret 0
682end