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