interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / bn.om

273 lines · 5630 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Public-data arithmetic. Little-endian radix 2^32 limbs, each held in ulong.
3note 512 limbs is a stack-sized limit of this compiler without runtime allocation, not key policy.
4note Context: limb count, -n[0]^-1 mod 2^32, then 512 modulus limbs.
5note Internal callers keep limbs normalized and operands below the modulus.
6fn ot_bn_copy o edit ulong 512 a view ulong 512 to void
7for i ulong 512
8put o i get a i
9end
10end
11fn ot_bn_import o edit ulong 512 a view uchar * to bool
12for i ulong 512
13put o i 0
14end
15if gt len a 2048
16ret false
17end
18for i ulong len a
19j = shr i 2
20v = shl ulong get a sub sub len a 1 i mul and i 3 8
21put o j or get o j v
22end
23ret true
24end
25fn ot_bn_export a view ulong 512 o edit uchar * to bool
26if gt len o 2048
27ret false
28end
29for i ulong len o
30v = get a shr i 2
31s = mul and i 3 8
32b = uchar and shr v s 255
33j = sub sub len o 1 i
34put o j b
35end
36ret true
37end
38fn ot_bn_cmp a view ulong 512 b view ulong 512 k ulong to int
39for j ulong k
40i = sub sub k 1 j
41if lt get a i get b i
42ret -1
43end
44if gt get a i get b i
45ret 1
46end
47end
48ret 0
49end
50fn ot_bn_zero a view ulong 512 k ulong to bool
51v = ulong 0
52for i ulong k
53v = or v get a i
54end
55ret eq v 0
56end
57fn ot_bn_sub a edit ulong 512 b view ulong 512 k ulong to ulong
58carry = ulong 0
59for i ulong k
60v = sub sub get a i get b i carry
61put a i and v 0xffffffff
62carry = shr v 63
63end
64ret carry
65end
66fn ot_bn_add a edit ulong 512 b view ulong 512 k ulong to ulong
67carry = ulong 0
68for i ulong k
69v = add add get a i get b i carry
70put a i and v 0xffffffff
71carry = shr v 32
72end
73ret carry
74end
75fn ot_bn_ge a view ulong 512 c view ulong 514 to bool
76k = get c 0
77for j ulong k
78i = sub sub k 1 j
79if lt get a i get c add i 2
80ret false
81end
82if gt get a i get c add i 2
83ret true
84end
85end
86ret true
87end
88fn ot_bn_reduce a edit ulong 512 c view ulong 514 to void
89borrow = ulong 0
90for i ulong get c 0
91v = sub sub get a i get c add i 2 borrow
92put a i and v 0xffffffff
93borrow = shr v 63
94end
95end
96fn ot_bn_twice a edit ulong 512 c view ulong 514 to void
97carry = ulong 0
98for i ulong get c 0
99v = add shl get a i 1 carry
100put a i and v 0xffffffff
101carry = shr v 32
102end
103if or ne carry 0 ot_bn_ge a c
104ot_bn_reduce a c
105end
106end
107fn ot_bn_ctx c edit ulong 514 n view ulong 512 to bool
108k = ulong 512
109while gt k 0 bound 512
110if ne get n sub k 1 0
111break
112end
113k = sub k 1
114end
115if eq k 0
116ret false
117end
118if eq and get n 0 1 0
119ret false
120end
121if eq k 1
122if le get n 0 1
123ret false
124end
125end
126put c 0 k
127v = ulong 1
128for i ulong 5
129v = and mul v sub 2 mul get n 0 v 0xffffffff
130end
131put c 1 and neg v 0xffffffff
132for i ulong 512
133put c add i 2 get n i
134end
135ret true
136end
137fn ot_bn_rr o edit ulong 512 c view ulong 514 to void
138for i ulong 512
139put o i 0
140end
141put o 0 1
142for i ulong mul get c 0 64
143ot_bn_twice o c
144end
145end
146note One CIOS product row: max product + limb + carry = 2^64-1.
147fn ot_bn_row t edit ulong 514 a view ulong 512 v ulong k ulong to void
148carry = ulong 0
149for j ulong k
150w = add mul get a j v get t j
151w = add w carry
152put t j and w 0xffffffff
153carry = shr w 32
154end
155w = add get t k carry
156put t k and w 0xffffffff
157put t add k 1 shr w 32
158end
159fn ot_bn_cancel t edit ulong 514 c view ulong 514 to void
160k = get c 0
161v = and mul get t 0 get c 1 0xffffffff
162carry = ulong 0
163for j ulong k
164w = add mul get c add j 2 v get t j
165w = add w carry
166if gt j 0
167put t sub j 1 and w 0xffffffff
168end
169carry = shr w 32
170end
171w = add get t k carry
172put t sub k 1 and w 0xffffffff
173put t k add get t add k 1 shr w 32
174put t add k 1 0
175end
176fn ot_bn_mont o edit ulong 512 a view ulong 512 b view ulong 512 c view ulong 514 to void
177t = new ulong 514 0
178for i ulong get c 0
179ot_bn_row t a get b i get c 0
180ot_bn_cancel t c
181end
182for i ulong 512
183put o i 0
184end
185for i ulong get c 0
186put o i get t i
187end
188if or ne get t get c 0 0 ot_bn_ge o c
189ot_bn_reduce o c
190end
191end
192fn ot_bn_square a edit ulong 512 c view ulong 514 to void
193b = new ulong 512 0
194o = new ulong 512 0
195ot_bn_copy b a
196ot_bn_mont o a b c
197ot_bn_copy a o
198end
199fn ot_bn_mul a edit ulong 512 b view ulong 512 c view ulong 514 to void
200o = new ulong 512 0
201ot_bn_mont o a b c
202ot_bn_copy a o
203end
204fn ot_bn_modadd a edit ulong 512 b view ulong 512 c view ulong 514 to void
205v = ot_bn_add a b get c 0
206if or ne v 0 ot_bn_ge a c
207ot_bn_reduce a c
208end
209end
210fn ot_bn_modsub a edit ulong 512 b view ulong 512 c view ulong 514 to void
211v = ot_bn_sub a b get c 0
212if ne v 0
213carry = ulong 0
214for i ulong get c 0
215w = add add get a i get c add i 2 carry
216put a i and w 0xffffffff
217carry = shr w 32
218end
219end
220end
221fn ot_bn_powbyte o edit ulong 512 base view ulong 512 c view ulong 514 v uchar to void
222for j ulong 8
223ot_bn_square o c
224if ne and shr v uchar sub 7 j 1 0
225ot_bn_mul o base c
226end
227end
228end
229fn ot_bn_pow o edit ulong 512 a view ulong 512 e view uchar * c view ulong 514 rr view ulong 512 to void
230base = new ulong 512 0
231one = new ulong 512 0
232put one 0 1
233ot_bn_mont base a rr c
234ot_bn_mont o one rr c
235for i ulong len e
236ot_bn_powbyte o base c get e i
237end
238ot_bn_mul o one c
239end
240fn ot_bn_modwork n view uchar * e view uchar * xo edit uchar * to bool
241nn = new ulong 512 0
242xx = new ulong 512 0
243rr = new ulong 512 0
244yy = new ulong 512 0
245cc = new ulong 514 0
246if or eq len n 0 gt len n 2048
247ret false
248end
249if or gt len e 2048 ne len xo len n
250ret false
251end
252ok = ot_bn_import nn n
253ok = ot_bn_import xx xo
254if ge ot_bn_cmp xx nn 512 0
255ret false
256end
257if not ot_bn_ctx cc nn
258ret false
259end
260ot_bn_rr rr cc
261ot_bn_pow yy xx e cc rr
262ret ot_bn_export yy xo
263end
264note In-place big-endian modular exponentiation. False clears xo; no private-key timing guarantee.
265fn ot_bn_modexp n view uchar * e view uchar * xo edit uchar * to bool
266if ot_bn_modwork n e xo
267ret true
268end
269for i ulong len xo bound 9223372036854775807
270put xo i 0
271end
272ret false
273end