interplane.Get Omega
Read in your language

open-humanity

Privacy-first peer assistance network for sovereign autonomous agents

Brought by drakestapleton

branch main Branches Tags History

open-humanity / om / ed25519.om

637 lines · 14332 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note ed25519.om: Ed25519 signature verification (RFC 8032 section 5.1.7), verification only.
3note Two rule sets, chosen by mode: 0 strict = Go crypto/ed25519.Verify, 1 consensus = ZIP 215 as in
4note github.com/hdevalence/ed25519consensus. Needs the sibling unit om/sha512/sha512.om (s512pre)
5note concatenated before this file. Library unit: functions only, no main, no globals.
6note Variable time: every input is public (key, message, signature). NOT constant-time-audited.
7note
8note Field GF(2^255-19): 16 limbs of radix 2^16 held in ulong cells (TweetNaCl layout, but unsigned).
9note Every field element lives in one workspace w (ulong 1024) at a slot offset; functions take
10note offsets so one array is passed and no two names alias. Limb bounds (see README):
11note mul output: limbs <= 65535 except limb 0 <= 65573;
12note sub adds 8p limbwise before subtracting, so it never wraps while the subtrahend is a mul output,
13note a canonical value, or a sum of two of those; edfsub traps 9 if a subtrahend limb exceeds the
14note 8p limb; mul inputs must be below 2^21 per limb (edfmul traps 10 otherwise), so a column sum
15note is below 16 * 2^42 and the folded sum (times 39) below 2^52: no ulong ever wraps.
16note Signed arithmetic (scalar reduction mod L, edmodl) traps on overflow instead of wrapping.
17
18note workspace slots (ulong offsets into w)
19const edzero ulong 0
20const edone ulong 16
21const edd ulong 32
22const edd2 ulong 48
23const edsqm1 ulong 64
24const edbp ulong 80
25const edna ulong 144
26const edba ulong 208
27const edq ulong 272
28const edrr ulong 336
29const edta ulong 400
30const edtb ulong 416
31const edtc ulong 432
32const edtd ulong 448
33const edte ulong 464
34const edtf ulong 480
35const edtg ulong 496
36const edth ulong 512
37const edtt ulong 528
38const edp0 ulong 544
39const edp1 ulong 560
40const edp2 ulong 576
41const edp3 ulong 592
42const edp4 ulong 608
43const edu ulong 624
44const edv ulong 640
45const edv3 ulong 656
46const edr ulong 672
47const edck ulong 688
48const edx ulong 704
49const edlimb ulong 2097152
50
51note constants: 1, d, 2d, sqrt(-1) (RFC 8032 section 5.1), base point B (RFC 8032 section 5.1) as
52note extended coordinates X Y Z=1 T=XY; decimal radix-2^16 limbs, least significant first
53fn edinit w edit ulong 1024 to void
54put w 16 1
55put w 32 30883
56put w 33 4953
57put w 34 19914
58put w 35 30187
59put w 36 55467
60put w 37 16705
61put w 38 2637
62put w 39 112
63put w 40 59544
64put w 41 30585
65put w 42 16505
66put w 43 36039
67put w 44 65139
68put w 45 11119
69put w 46 27886
70put w 47 20995
71put w 48 61785
72put w 49 9906
73put w 50 39828
74put w 51 60374
75put w 52 45398
76put w 53 33411
77put w 54 5274
78put w 55 224
79put w 56 53552
80put w 57 61171
81put w 58 33010
82put w 59 6542
83put w 60 64743
84put w 61 22239
85put w 62 55772
86put w 63 9222
87put w 64 41136
88put w 65 18958
89put w 66 6951
90put w 67 50414
91put w 68 58488
92put w 69 44335
93put w 70 6150
94put w 71 12099
95put w 72 55207
96put w 73 15867
97put w 74 153
98put w 75 11085
99put w 76 57099
100put w 77 20417
101put w 78 9344
102put w 79 11139
103put w 80 54554
104put w 81 36645
105put w 82 11616
106put w 83 51542
107put w 84 42930
108put w 85 38181
109put w 86 51040
110put w 87 26924
111put w 88 56412
112put w 89 64982
113put w 90 57905
114put w 91 49316
115put w 92 21502
116put w 93 52590
117put w 94 14035
118put w 95 8553
119put w 96 26200
120put w 97 26214
121put w 98 26214
122put w 99 26214
123put w 100 26214
124put w 101 26214
125put w 102 26214
126put w 103 26214
127put w 104 26214
128put w 105 26214
129put w 106 26214
130put w 107 26214
131put w 108 26214
132put w 109 26214
133put w 110 26214
134put w 111 26214
135put w 128 56739
136put w 129 42423
137put w 130 35507
138put w 131 28126
139put w 132 21237
140put w 133 30545
141put w 134 40832
142put w 135 8432
143put w 136 58237
144put w 137 25771
145put w 138 20110
146put w 139 26346
147put w 140 30309
148put w 141 55179
149put w 142 24335
150put w 143 26503
151put w 112 1
152end
153
154note copy n cells from offset a to offset o
155fn edfcpy w edit ulong 1024 o ulong a ulong n ulong to void
156for i ulong n
157put w add o i get w add a i
158end
159end
160
161note o = a + b limbwise, no carry
162fn edfadd w edit ulong 1024 o ulong a ulong b ulong to void
163for i ulong 16
164put w add o i add get w add a i get w add b i
165end
166end
167
168note limb i of 8p = 8 * (2^255 - 19) in radix 2^16: 8*65517, 8*65535 (x14), 8*32767
169fn ed8p i ulong to ulong
170if eq i 0
171ret 524136
172end
173if eq i 15
174ret 262136
175end
176ret 524280
177end
178
179note o = a + 8p - b limbwise; trap 9 if a subtrahend limb is above the 8p limb (cannot happen for
180note the call sites below; the check makes a broken bound stop instead of wrapping)
181fn edfsub w edit ulong 1024 o ulong a ulong b ulong to void
182for i ulong 16
183k = ed8p i
184y = get w add b i
185if gt y k
186trap 9
187end
188put w add o i sub add get w add a i k y
189end
190end
191
192note one carry pass over 16 limbs; the carry out of limb 15 (weight 2^256 = 38 mod p) folds into limb 0
193fn edfcar t edit ulong 16 to void
194for i ulong 15
195put t add i 1 add get t add i 1 shr get t i 16
196put t i and get t i 65535
197end
198c = shr get t 15 16
199put t 15 and get t 15 65535
200put t 0 add get t 0 mul c 38
201end
202
203note o = a * b mod p (o may equal a or b); trap 10 if an input limb is 2^21 or more
204fn edfmul w edit ulong 1024 o ulong a ulong b ulong to void
205t = new ulong 32 0
206c = new ulong 16 0
207for j ulong 16
208y = get w add b j
209if ge y edlimb
210trap 10
211end
212put c j y
213end
214for i ulong 16
215x = get w add a i
216if ge x edlimb
217trap 10
218end
219for j ulong 16
220q = add i j
221put t q add get t q mul x get c j
222end
223end
224r = new ulong 16 0
225for i ulong 15
226put r i add get t i mul 38 get t add i 16
227end
228put r 15 get t 15
229edfcar r
230edfcar r
231for i ulong 16
232put w add o i get r i
233end
234end
235
236note o = a^(2^n), n >= 1
237fn edfsqn w edit ulong 1024 o ulong a ulong n ulong to void
238edfmul w o a a
239for i ulong sub n 1
240edfmul w o o o
241end
242end
243
244note canonical limbs of slot a into t: full carries, then subtract p while the value is >= p
245fn edfcan w edit ulong 1024 a ulong t edit ulong 16 to void
246for i ulong 16
247put t i get w add a i
248end
249edfcar t
250edfcar t
251while gt get t 0 65535 bound 8
252edfcar t
253end
254m = new ulong 16 0
255for r ulong 2
256bw = ulong 0
257for i ulong 16
258pl = div ed8p i 8
259v = sub sub add get t i 65536 pl bw
260put m i and v 65535
261bw = sub 1 shr v 16
262end
263if eq bw 0
264for i ulong 16
265put t i get m i
266end
267end
268end
269end
270
271note o = canonical form of a
272fn edfnorm w edit ulong 1024 o ulong a ulong to void
273t = new ulong 16 0
274edfcan w a t
275for i ulong 16
276put w add o i get t i
277end
278end
279
280note 1 when a = b mod p
281fn edfeq w edit ulong 1024 a ulong b ulong to ulong
282s = new ulong 16 0
283t = new ulong 16 0
284edfcan w a s
285edfcan w b t
286for i ulong 16
287if ne get s i get t i
288ret 0
289end
290end
291ret 1
292end
293
294note o = z^(2^250 - 1), e = z^11 (addition chain of ref10 fe_pow22523 / fe_invert); uses edp0..edp2
295fn edf250 w edit ulong 1024 o ulong e ulong z ulong to void
296edfmul w edp0 z z
297edfmul w edp1 edp0 edp0
298edfmul w edp1 edp1 edp1
299edfmul w edp1 z edp1
300edfmul w e edp0 edp1
301edfmul w edp0 e e
302edfmul w edp0 edp1 edp0
303edfsqn w edp1 edp0 5
304edfmul w edp0 edp1 edp0
305edfsqn w edp1 edp0 10
306edfmul w edp1 edp1 edp0
307edfsqn w edp2 edp1 20
308edfmul w edp1 edp2 edp1
309edfsqn w edp1 edp1 10
310edfmul w edp0 edp1 edp0
311edfsqn w edp1 edp0 50
312edfmul w edp1 edp1 edp0
313edfsqn w edp2 edp1 100
314edfmul w edp1 edp2 edp1
315edfsqn w edp1 edp1 50
316edfmul w o edp1 edp0
317end
318
319note o = a^(p-2) = 1/a (0 for a = 0)
320fn edfinv w edit ulong 1024 o ulong a ulong to void
321edf250 w edp4 edp3 a
322edfsqn w edp4 edp4 5
323edfmul w o edp4 edp3
324end
325
326note o = a^((p-5)/8) = a^(2^252 - 3)
327fn edfp58 w edit ulong 1024 o ulong a ulong to void
328edf250 w edp4 edp3 a
329edfsqn w edp4 edp4 2
330edfmul w o edp4 a
331end
332
333note slot o = the 32 little-endian bytes of b with the top bit cleared; values p..2^255-1 are kept
334note (they are reduced mod p by later arithmetic), as Go field.Element.SetBytes does
335fn edfbytes w edit ulong 1024 o ulong b view uchar 32 to void
336for i ulong 16
337lo = ulong get b mul i 2
338hi = ulong get b add mul i 2 1
339put w add o i or lo shl hi 8
340end
341put w add o 15 and get w add o 15 32767
342end
343
344note decode 32 bytes into the extended point at slot pt; 1 ok, 0 not a point. Go edwards25519
345note Point.SetBytes: y = bytes mod p (non-canonical y accepted), x = +sqrt((y^2-1)/(d y^2+1)) via
346note field.Element.SqrtRatio, negated when bit 255 is set (x = 0 with bit 255 set is accepted).
347fn edpdec w edit ulong 1024 pt ulong b view uchar 32 to ulong
348y = add pt 16
349edfbytes w y b
350edfmul w edu y y
351edfmul w edv edu edd
352edfadd w edv edv edone
353edfsub w edu edu edone
354edfnorm w edu edu
355edfmul w edv3 edv edv
356edfmul w edv3 edv3 edv
357edfmul w edr edv3 edv3
358edfmul w edr edr edv
359edfmul w edr edr edu
360edfp58 w edr edr
361edfmul w edr edr edv3
362edfmul w edr edr edu
363edfmul w edck edr edr
364edfmul w edck edck edv
365if eq edfeq w edck edu 0
366edfsub w edx edzero edu
367if eq edfeq w edck edx 0
368ret 0
369end
370edfmul w edr edr edsqm1
371end
372edfnorm w edx edr
373sign = shr ulong get b 31 7
374if ne and get w edx 1 sign
375edfsub w edx edzero edx
376edfnorm w edx edx
377end
378edfcpy w pt edx 16
379edfcpy w add pt 32 edone 16
380edfmul w add pt 48 edx y
381ret 1
382end
383
384note p = -p (negate X and T); the results are brought back to canonical limbs so that every stored
385note coordinate stays a valid subtrahend for edfsub
386fn edpneg w edit ulong 1024 p ulong to void
387edfsub w p edzero p
388edfnorm w p p
389t = add p 48
390edfsub w t edzero t
391edfnorm w t t
392end
393
394note p = p + q, extended coordinates, add-2008-hwcd-3 (TweetNaCl add), complete for Ed25519
395fn edpadd w edit ulong 1024 p ulong q ulong to void
396py = add p 16
397qy = add q 16
398edfsub w edta py p
399edfsub w edtt qy q
400edfmul w edta edta edtt
401edfadd w edtb p py
402edfadd w edtt q qy
403edfmul w edtb edtb edtt
404edfmul w edtc add p 48 add q 48
405edfmul w edtc edtc edd2
406edfmul w edtd add p 32 add q 32
407edfadd w edtd edtd edtd
408edfsub w edte edtb edta
409edfsub w edtf edtd edtc
410edfadd w edtg edtd edtc
411edfadd w edth edtb edta
412edfmul w p edte edtf
413edfmul w py edth edtg
414edfmul w add p 32 edtg edtf
415edfmul w add p 48 edte edth
416end
417
418note p = 2p, dbl-2008-hwcd with a = -1, every output coordinate negated (same projective point):
419note A = X^2, B = Y^2, C = 2Z^2, H = A + B, E = (X+Y)^2 - H, G = B - A, F = C + A - B,
420note X = E F, Y = G H, T = E H, Z = F G
421fn edpdbl w edit ulong 1024 p ulong to void
422py = add p 16
423edfmul w edta p p
424edfmul w edtb py py
425edfmul w edtc add p 32 add p 32
426edfadd w edtc edtc edtc
427edfadd w edth edta edtb
428edfadd w edte p py
429edfmul w edte edte edte
430edfsub w edte edte edth
431edfsub w edtg edtb edta
432edfadd w edtf edtc edta
433edfsub w edtf edtf edtb
434edfmul w p edte edtf
435edfmul w py edtg edth
436edfmul w add p 48 edte edth
437edfmul w add p 32 edtf edtg
438end
439
440note canonical encoding of point p (RFC 8032 section 5.1.2): y, with bit 255 = x mod 2
441fn edpenc w edit ulong 1024 p ulong o edit uchar 32 to void
442edfinv w edu add p 32
443edfmul w edv p edu
444edfmul w edv3 add p 16 edu
445edfnorm w edv edv
446edfnorm w edv3 edv3
447for i ulong 16
448v = get w add edv3 i
449put o mul i 2 uchar and v 255
450put o add mul i 2 1 uchar shr v 8
451end
452put o 31 or get o 31 uchar shl and get w edv 1 7
453end
454
455note 1 when p is the identity (X = 0 and Y = Z), as Go Point.Equal(NewIdentityPoint())
456fn edpid w edit ulong 1024 p ulong to ulong
457if eq edfeq w p edzero 0
458ret 0
459end
460ret edfeq w add p 16 add p 32
461end
462
463note bit i of the 32-byte little-endian scalar b
464fn edbit b view uchar 32 i ulong to ulong
465ret and shr ulong get b shr i 3 and i 7 1
466end
467
468note q = [s]B + [k]P where P is the point at edna; joint double-and-add from bit 252
469note (s < L and k < L are below 2^253), with B + P precomputed at edba
470fn edsmul w edit ulong 1024 s view uchar 32 k view uchar 32 to void
471edfcpy w edba edbp 64
472edpadd w edba edna
473edfcpy w edq edzero 64
474edfcpy w add edq 16 edone 16
475edfcpy w add edq 32 edone 16
476for j ulong 253
477i = sub 252 j
478edpdbl w edq
479sb = edbit s i
480kb = edbit k i
481if eq sb 1
482if eq kb 1
483edpadd w edq edba
484else
485edpadd w edq edbp
486end
487else
488if eq kb 1
489edpadd w edq edna
490end
491end
492end
493end
494
495note the group order L = 2^252 + 27742317777372353535851937790883648493 as 32 little-endian bytes
496fn edlset l edit long 32 to void
497put l 0 237
498put l 1 211
499put l 2 245
500put l 3 92
501put l 4 26
502put l 5 99
503put l 6 18
504put l 7 88
505put l 8 214
506put l 9 156
507put l 10 247
508put l 11 162
509put l 12 222
510put l 13 249
511put l 14 222
512put l 15 20
513put l 31 16
514end
515
516note 1 when the 32-byte little-endian scalar s is below L (Go Scalar.SetCanonicalBytes / isReduced)
517fn edsclt s view uchar 32 to ulong
518l = new long 32 0
519edlset l
520i = ulong 32
521while gt i 0 bound 33
522i = sub i 1
523a = long get s i
524b = get l i
525if lt a b
526ret 1
527end
528if gt a b
529ret 0
530end
531end
532ret 0
533end
534
535note x[0..32) = x mod L as bytes, for the 64 bytes x[0..64) (TweetNaCl modL, signed limbs; any
536note overflow traps)
537fn edmodl x edit long 64 to void
538l = new long 32 0
539edlset l
540i = long 63
541while ge i 32 bound 33
542carry = long 0
543b = sub i 32
544q = mul 16 get x i
545j = b
546while lt j sub i 12 bound 21
547lj = get l sub j b
548v = sub add get x j carry mul q lj
549carry = shr add v 128 8
550put x j sub v mul carry 256
551j = add j 1
552end
553put x j add get x j carry
554put x i 0
555i = sub i 1
556end
557carry = long 0
558for j ulong 32
559v = add get x j sub carry mul shr get x 31 4 get l j
560carry = shr v 8
561put x j and v 255
562end
563for j ulong 32
564put x j sub get x j mul carry get l j
565end
566for j ulong 32
567put x add j 1 add get x add j 1 shr get x j 8
568put x j and get x j 255
569end
570end
571
572note k = SHA-512(R || A || M) mod L (RFC 8032 section 5.1.7 step 2)
573fn edhram sg view uchar 64 pk view uchar 32 m view uchar 65536 n ulong k edit uchar 32 to void
574pre = new uchar 64 0
575for i ulong 32
576put pre i get sg i
577put pre add i 32 get pk i
578end
579hd = new uchar 64 0
580s512pre pre m n hd
581x = new long 64 0
582for i ulong 64
583put x i long get hd i
584end
585edmodl x
586for i ulong 32
587put k i uchar get x i
588end
589end
590
591note Ed25519 verification. pk: 32-byte public key A; m[0..n): message, n <= 65536 (else trap 3 in
592note s512pre); sg: 64-byte signature R || S; mode 0 strict (Go crypto/ed25519.Verify), mode 1
593note consensus (ZIP 215, ed25519consensus.Verify); any other mode traps 11. Returns 1 valid, 0 invalid.
594fn edverify pk view uchar 32 m view uchar 65536 n ulong sg view uchar 64 mode ulong to ulong
595if gt mode 1
596trap 11
597end
598s = new uchar 32 0
599encoded_r = new uchar 32 0
600k = new uchar 32 0
601e = new uchar 32 0
602w = new ulong 1024 0
603for i ulong 32
604put encoded_r i get sg i
605put s i get sg add i 32
606end
607if eq edsclt s 0
608ret 0
609end
610edinit w
611if eq edpdec w edna pk 0
612ret 0
613end
614edpneg w edna
615if eq mode 1
616if eq edpdec w edrr encoded_r 0
617ret 0
618end
619end
620edhram sg pk m n k
621edsmul w s k
622if eq mode 0
623edpenc w edq e
624for i ulong 32
625if ne get e i get encoded_r i
626ret 0
627end
628end
629ret 1
630end
631edpneg w edrr
632edpadd w edq edrr
633edpdbl w edq
634edpdbl w edq
635edpdbl w edq
636ret edpid w edq
637end