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 / tests / drv / bn.om

214 lines · 7572 bytes Raw Blame History
1note omtls test driver bn: ot_bn_modexp (src/bn.om) against Wolfram, plus identities and refusals.
2note Usage: bn (no arguments); one line per failed case, then PASS bn cases=N or FAIL bn pass=P fail=F.
3note Case (l, el, s): n, x and e are generated bytes, byte i of seed t = (31 i^2 + 7 i t + 13 t + 5) mod 256;
4note n = gen(l, s) with its top bit and bit 0 set, x = gen(l, s + 1) with its top bit cleared (so x < n),
5note e = gen(el, s + 2); the expected value is SHA-256 of x^e mod n as l big-endian bytes.
6note wolfram (WolframLanguageEvaluator, 2026-11-11): gen[l_, s_] := Table[Mod[i^2*31 + i*s*7 + s*13 + 5, 256],
7note {i, 0, l - 1}], the same bit fixes, y = PowerMod[x, e, n],
8note Hash[ByteArray[IntegerDigits[y, 256, l]], "SHA256", "HexString"]; x < n and OddQ[n] True for every case.
9text ot_bnv_area "bn"
10text ot_bnv_tfailed "FAILED bn-"
11text ot_bnv_tnl "\n"
12text ot_bnv_cases "1 1 1 c4694f2e93d5c4e7d51f9c5deb75e6cc8be5e1114178c6a45b6fc2c566a0aa8c 2 1 2 1d9481e46f7811bca60f775dc441941edd0dd903609def214d525d7a70b6b965 4 3 3 7308abf80de2bf3eb90cca38652dfdcf5c2e0e520fcb5bef8ee92407b61c7947 5 4 4 953875d1c2e2b1cdb840b8674f36584b18bffb87d7cc4e59d50c32d62b59a133 8 8 5 beebfb481dc4922676f0b5df407acecedfbf93917b5d3872a6d81bdeb6015733 9 3 6 b4131a50f671b5ba941bcbbe03e0c99be3d12d4f2b27b06ff4ee1868c1e2f70a 12 12 7 930a69a144748e6c3288eba3169ed5bf30e4c8fd8aa05823035216419846a99b 32 32 8 0ef42e3ce1210d6443420fbdbedb9d3fe35994ec6d543a9e249beaa27d609d24 33 3 9 c7d33fc552bcb5e105241bd22158f4e73312d608282e1afd109bb01cb16a5a59 64 64 10 7ca06c5f7dace537f398817206ebb8c8d78a5eef036d72da690265fcbf873151 128 3 11 c01faf39fd36e28ab66464469ecb80f5ec652a78577bf759122dca535168dac7 129 129 12 9ca60986bf07efe592a1ee1f6329aaf34c7f72ea68bcfc549a63fd06740efb1c 256 256 13 836d7a76e83867b731f3b3c4d7639b00ef7aeb7edbbfa5dfc47629df2573442a 256 3 14 46ab0d194ebe276529381832165d3a47efc2d46ebf0005edab4186a774dc5257 384 3 15 bacd6ea113242a600bceb4c2c9f8425dcc3903d2e033b123dc695342849fb59c 512 3 16 764b1b31bad86c5ea2707a1c7c07e39a0f2b2d9f19d8b318973415d0d0a9a76f 512 512 17 e95106dcdcfeec879a53e7541a2a8206365b793efbd288cd47010531998cda75 1024 3 18 844564ae276737dc58bd5d03e04a24440c59ed280b05253c4fd843781306c8cd 2048 3 19 fe913bbd611a8967d5c9b0fd99af5461e3af66dd5f5aabc5cc96d92755cfc88f 2048 1 20 3f00b1619ef63bb094c2a116897419748f011078f44a18375cc339a73227dfb1"
13fn ot_bnv_gen o edit uchar * s ulong to void
14for i ulong len o bound 9223372036854775807
15a = mul mul i i 31
16a = add a mul mul i s 7
17a = add a add mul s 13 5
18put o i uchar and a 255
19end
20end
21fn ot_bnv_ck r edit ulong 4 ok bool id ulong to void
22if ok
23put r 0 add get r 0 1
24else
25put r 1 add get r 1 1
26ot_sy_put ot_bnv_tfailed
27ot_sy_num id
28ot_sy_put ot_bnv_tnl
29end
30end
31note n, x, e of case (l, el, s) into nb, xb, eb.
32fn ot_bnv_case nb edit uchar 4096 xb edit uchar 4096 eb edit uchar 4096 l ulong el ulong s ulong to void
33for i ulong 4096
34put nb i 0
35put xb i 0
36put eb i 0
37end
38ot_bnv_gen part nb 0 l s
39ot_bnv_gen part xb 0 l add s 1
40ot_bnv_gen part eb 0 el add s 2
41put nb 0 or get nb 0 128
42put nb sub l 1 or get nb sub l 1 1
43put xb 0 and get xb 0 127
44end
45fn ot_bnv_run nb view uchar 4096 eb view uchar 4096 xb edit uchar 4096 c view ulong 4 to bool
46ret ot_bn_modexp view part nb 0 get c 0 view part eb 0 get c 1 part xb 0 get c 0
47end
48note Case c (l, el, s) against the expected hash h.
49fn ot_bnv_one r edit ulong 4 c view ulong 4 h view uchar 64 to void
50nb = new uchar 4096 0
51xb = new uchar 4096 0
52eb = new uchar 4096 0
53o = new uchar 64 0
54ot_bnv_case nb xb eb get c 0 get c 1 get c 2
55ok = ot_bnv_run nb eb xb c
56hl = ot_hash 0 view part xb 0 get c 0 o
57ot_bnv_ck r and ok ot_sy_eq o h 32 get c 2
58end
59fn ot_bnv_zero a view uchar * to bool
60v = uchar 0
61for i ulong len a bound 9223372036854775807
62v = or v get a i
63end
64ret eq v 0
65end
66note y equals the small number v (as l bytes).
67fn ot_bnv_is a view uchar * v uchar to bool
68if ne get a sub len a 1 v
69ret false
70end
71ret ot_bnv_zero view part a 0 sub len a 1
72end
73note Identities on case (256, 3, 14): x = n - 1 with e = 2 and e = 65537, x = 0, x = 1, empty and zero e,
74note and leading zero bytes on n and x.
75fn ot_bnv_ids r edit ulong 4 to void
76nb = new uchar 4096 0
77xb = new uchar 4096 0
78eb = new uchar 4096 0
79h = new uchar 64 0
80o = new uchar 64 0
81ot_bnv_case nb xb eb 256 3 14
82for i ulong 256
83put xb i get nb i
84end
85put xb 255 sub get xb 255 1
86put eb 0 2
87ok = ot_bnv_x nb eb xb 256 1 256
88ot_bnv_ck r and ok ot_bnv_is view part xb 0 256 1 101
89for i ulong 256
90put xb i get nb i
91end
92put xb 255 sub get xb 255 1
93put eb 0 1
94put eb 1 0
95put eb 2 1
96ok = ot_bnv_x nb eb xb 256 3 256
97put xb 255 add get xb 255 1
98ot_bnv_ck r and ok ot_sy_eq xb nb 256 102
99for i ulong 256
100put xb i 0
101end
102ok = ot_bnv_x nb eb xb 256 3 256
103ot_bnv_ck r and ok ot_bnv_zero view part xb 0 256 103
104put xb 255 1
105ok = ot_bnv_x nb eb xb 256 3 256
106ot_bnv_ck r and ok ot_bnv_is view part xb 0 256 1 104
107put xb 7 9
108ok = ot_bnv_x nb eb xb 256 0 256
109ot_bnv_ck r and ok ot_bnv_is view part xb 0 256 1 105
110put xb 7 9
111put eb 0 0
112put eb 1 0
113ok = ot_bnv_x nb eb xb 256 2 256
114ot_bnv_ck r and ok ot_bnv_is view part xb 0 256 1 106
115ot_bnv_case nb xb eb 256 3 14
116ot_bnv_lead nb xb
117ok = ot_bnv_x nb eb xb 258 3 258
118x = ot_hash 0 view part xb 2 256 o
119x = ot_bnv_hex h 14
120ot_bnv_ck r and and ok ot_sy_eq o h 32 ot_bnv_zero view part xb 0 2 107
121end
122note Shift n and x right by two bytes (two leading zeros).
123fn ot_bnv_lead nb edit uchar 4096 xb edit uchar 4096 to void
124for j ulong 256
125i = sub 255 j
126put nb add i 2 get nb i
127put xb add i 2 get xb i
128end
129put nb 0 0
130put nb 1 0
131put xb 0 0
132put xb 1 0
133end
134note The expected hash of case seed s from ot_bnv_cases into h; gives 0.
135fn ot_bnv_hex h edit uchar 64 s ulong to ulong
136st = new ulong 10 0
137put st 4 len ot_bnv_cases
138for c ulong 20
139l = ot_sy_ndec ot_bnv_cases st
140el = ot_sy_ndec ot_bnv_cases st
141t = ot_sy_ndec ot_bnv_cases st
142v = ot_sy_nhex ot_bnv_cases st h
143if eq t s
144ret 0
145end
146end
147ret 1
148end
149fn ot_bnv_x nb view uchar 4096 eb view uchar 4096 xb edit uchar 4096 nl ulong el ulong xl ulong to bool
150ret ot_bn_modexp view part nb 0 nl view part eb 0 el part xb 0 xl
151end
152note Refusal: modexp gives false and leaves xo all zero.
153fn ot_bnv_no nb view uchar 4096 eb view uchar 4096 xb edit uchar 4096 nl ulong el ulong xl ulong to bool
154ok = ot_bnv_x nb eb xb nl el xl
155ret and not ok ot_bnv_zero view part xb 0 xl
156end
157fn ot_bnv_refuse r edit ulong 4 to void
158nb = new uchar 4096 0
159xb = new uchar 4096 0
160eb = new uchar 4096 0
161ot_bnv_case nb xb eb 256 3 14
162put nb 255 and get nb 255 254
163ot_bnv_ck r ot_bnv_no nb eb xb 256 3 256 110
164ot_bnv_case nb xb eb 256 3 14
165put nb 0 1
166put xb 0 1
167ot_bnv_ck r ot_bnv_no nb eb xb 1 3 1 111
168put nb 0 0
169put nb 1 0
170put xb 0 0
171ot_bnv_ck r ot_bnv_no nb eb xb 2 3 2 112
172ot_bnv_ck r ot_bnv_no nb eb xb 0 3 0 113
173ot_bnv_case nb xb eb 256 3 14
174for i ulong 256
175put xb i get nb i
176end
177ot_bnv_ck r ot_bnv_no nb eb xb 256 3 256 114
178for i ulong 256
179put xb i 255
180end
181ot_bnv_ck r ot_bnv_no nb eb xb 256 3 256 115
182put xb 0 1
183ot_bnv_ck r ot_bnv_no nb eb xb 256 3 255 116
184for i ulong 2049
185put nb i 255
186put xb i 0
187end
188put xb 5 1
189ot_bnv_ck r ot_bnv_no nb eb xb 2049 3 2049 117
190ot_bnv_case nb xb eb 256 3 14
191put xb 3 1
192ot_bnv_ck r ot_bnv_no nb eb xb 256 2049 256 118
193end
194fn main to int
195r = new ulong 4 0
196st = new ulong 10 0
197h = new uchar 64 0
198c = new ulong 4 0
199put st 4 len ot_bnv_cases
200for j ulong 20
201put c 0 ot_sy_ndec ot_bnv_cases st
202put c 1 ot_sy_ndec ot_bnv_cases st
203put c 2 ot_sy_ndec ot_bnv_cases st
204v = ot_sy_nhex ot_bnv_cases st h
205if eq v 32
206ot_bnv_one r c h
207else
208ot_bnv_ck r false get c 2
209end
210end
211ot_bnv_ids r
212ot_bnv_refuse r
213ret ot_sy_done ot_bnv_area get r 0 get r 1
214end