| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
from math import floor |
| 5 |
|
| 6 |
Mlen = 761 |
| 7 |
if len(sys.argv) > 1: Mlen = int(sys.argv[1]) |
| 8 |
|
| 9 |
m0 = 4591 |
| 10 |
if len(sys.argv) > 2: m0 = int(sys.argv[2]) |
| 11 |
|
| 12 |
m1 = m0 |
| 13 |
if len(sys.argv) > 3: m1 = int(sys.argv[3]) |
| 14 |
# M is Mlen-1 copies of m0, plus 1 copy of m1 |
| 15 |
|
| 16 |
offset = m0//2 |
| 17 |
if len(sys.argv) > 4: offset = int(sys.argv[4]) |
| 18 |
|
| 19 |
div3 = False |
| 20 |
if len(sys.argv) > 5: div3 = sys.argv[5]=='True' |
| 21 |
|
| 22 |
top = 16384 |
| 23 |
|
| 24 |
print('/* auto-generated; do not edit */') |
| 25 |
print('/* 20250302 djb: port to -Wc++-compat */') |
| 26 |
print('') |
| 27 |
print('#include "crypto_decode.h"') |
| 28 |
print('#include "crypto_int16.h"') |
| 29 |
print('#include "crypto_int32.h"') |
| 30 |
print('#define int16 crypto_int16') |
| 31 |
print('#define int32 crypto_int32') |
| 32 |
|
| 33 |
print(""" |
| 34 |
static int16 mullo(int16 x,int16 y) |
| 35 |
{ |
| 36 |
return x*y; |
| 37 |
} |
| 38 |
|
| 39 |
static int16 mulhi(int16 x,int16 y) |
| 40 |
{ |
| 41 |
return (x*(int32)y)>>16; |
| 42 |
} |
| 43 |
""") |
| 44 |
|
| 45 |
print('void crypto_decode(void *v,const unsigned char *s)') |
| 46 |
print('{') |
| 47 |
print(' int16 *R = (int16 *) v;') |
| 48 |
|
| 49 |
print(' long long i;') |
| 50 |
print(' int16 a0,a1,ri,lo,s0,s1;') |
| 51 |
|
| 52 |
def poke(layer,pos,contents): |
| 53 |
if layer == 1 and div3: |
| 54 |
return 'R[%s] = 3*%s%+d;' % (pos,contents,-offset) |
| 55 |
if layer == 1 and offset != 0: |
| 56 |
return 'R[%s] = %s%+d;' % (pos,contents,-offset) |
| 57 |
return 'R[%s] = %s;' % (pos,contents) |
| 58 |
|
| 59 |
def mulmoddata(c,q): |
| 60 |
y = (c<<16)%q |
| 61 |
if y*2 >= q: y -= q |
| 62 |
|
| 63 |
t = 0 |
| 64 |
u = q |
| 65 |
while not u&1: |
| 66 |
t += 1 |
| 67 |
u >>= 1 |
| 68 |
|
| 69 |
uinv = pow(u,16383,65536) |
| 70 |
if uinv>=32768: uinv -= 65536 |
| 71 |
assert (u*uinv)%65536 == 1 |
| 72 |
|
| 73 |
z = (y-(c<<16))>>t |
| 74 |
z = (z*uinv)%65536 |
| 75 |
if z >= 32768: z -= 65536 |
| 76 |
assert (z*u-((y-(c<<16))>>t))%(2**16) == 0 |
| 77 |
|
| 78 |
return y,t,u,uinv,z |
| 79 |
|
| 80 |
def inner(indent,inpos,m0,m1,bytes,outpos0,outpos1): |
| 81 |
stanza = '' |
| 82 |
|
| 83 |
y,t,u,uinv,z = mulmoddata(256,m0) |
| 84 |
|
| 85 |
stanza += indent + 'ri = R[%s];\n' % (inpos) |
| 86 |
a0lower,a0upper = 0,1<<14 |
| 87 |
|
| 88 |
for loop in reversed(range(bytes)): |
| 89 |
stanza += indent + 's%d = *--s;\n' % loop |
| 90 |
|
| 91 |
for loop in reversed(range(bytes)): |
| 92 |
if y > 0: |
| 93 |
a0lower,a0upper = a0lower*y,a0upper*y |
| 94 |
else: |
| 95 |
a0lower,a0upper = a0upper*y,a0lower*y |
| 96 |
a0lower,a0upper = a0lower-(m0<<15),a0upper+(m0<<15) |
| 97 |
a0lower,a0upper = a0lower>>16,a0upper>>16 |
| 98 |
if loop == bytes-1: |
| 99 |
stanza += indent + 'lo = mullo(ri,%d);\n' % z |
| 100 |
stanza += indent + 'a0 = mulhi(ri,%d)-mulhi(lo,%d); /* %d...%d */\n' % (y,m0,a0lower,a0upper) |
| 101 |
else: |
| 102 |
stanza += indent + 'lo = mullo(a0,%d);\n' % z |
| 103 |
stanza += indent + 'a0 = mulhi(a0,%d)-mulhi(lo,%d); /* %d...%d */\n' % (y,m0,a0lower,a0upper) |
| 104 |
|
| 105 |
a0upper += 255 |
| 106 |
stanza += indent + 'a0 += s%d; /* %d...%d */\n' % (loop,a0lower,a0upper) |
| 107 |
|
| 108 |
if bytes == 0: |
| 109 |
stanza += indent + 'a0 = ri;\n' |
| 110 |
|
| 111 |
if a0upper >= 2*m0: |
| 112 |
y1,t1,u1,uinv1,z1 = mulmoddata(1,m0) |
| 113 |
if y1 > 0: |
| 114 |
a0lower,a0upper = a0lower*y1,a0upper*y1 |
| 115 |
else: |
| 116 |
a0lower,a0upper = a0upper*y1,a0lower*y1 |
| 117 |
a0lower,a0upper = a0lower-(m0<<15),a0upper+(m0<<15) |
| 118 |
a0lower,a0upper = a0lower>>16,a0upper>>16 |
| 119 |
if bytes == 0: |
| 120 |
stanza += indent + 'lo = mullo(a0,%d);\n' % z1 |
| 121 |
else: |
| 122 |
stanza += indent + 'lo = mullo(a0,%d);\n' % z1 |
| 123 |
stanza += indent + 'a0 = mulhi(a0,%d)-mulhi(lo,%d); /* %d...%d */\n' % (y1,m0,a0lower,a0upper) |
| 124 |
|
| 125 |
while a0upper >= m0: |
| 126 |
a0lower,a0upper = a0lower-m0,a0upper-m0 |
| 127 |
stanza += indent + 'a0 -= %d; /* %d..>%d */\n' % (m0,a0lower,a0upper) |
| 128 |
|
| 129 |
while a0lower < 0: |
| 130 |
a0lower,a0upper = min(0,a0lower+m0),max(m0-1,a0upper) |
| 131 |
stanza += indent + 'a0 += crypto_int16_negative_mask(a0)&%d; /* %d...%d */\n' % (m0,a0lower,a0upper) |
| 132 |
|
| 133 |
if bytes == 0: |
| 134 |
stanza += indent + 'a1 = (ri-a0)>>%d;\n' % t |
| 135 |
|
| 136 |
elif bytes == 1: |
| 137 |
if t == 0: |
| 138 |
stanza += indent + 'a1 = (ri<<8)+s0-a0;\n' |
| 139 |
elif t == 8: |
| 140 |
stanza += indent + 'a1 = ri+((s0-a0)>>8);\n' |
| 141 |
elif t < 8: |
| 142 |
stanza += indent + 'a1 = (ri<<%d)+((s0-a0)>>%d);\n' % (8-t,t) |
| 143 |
else: |
| 144 |
stanza += indent + 'a1 = (ri+((s0-a0)>>8))>>%d;\n' % t-8 |
| 145 |
|
| 146 |
else: |
| 147 |
assert bytes == 2 |
| 148 |
if t == 0: |
| 149 |
stanza += indent + 'a1 = (s1<<8)+s0-a0;\n' |
| 150 |
elif t == 8: |
| 151 |
stanza += indent + 'a1 = (ri<<8)+s1+((s0-a0)>>8);\n' |
| 152 |
elif t < 8: |
| 153 |
stanza += indent + 'a1 = (ri<<%d)+(s1<<%d)+((s0-a0)>>%d);\n' % (16-t,8-t,t) |
| 154 |
else: |
| 155 |
stanza += indent + 'a1 = ((((int32)ri)<<16)+(s1<<8)+s0-a0)>>%d;\n' % t |
| 156 |
|
| 157 |
stanza += indent + 'a1 = mullo(a1,%d);\n' % uinv |
| 158 |
stanza += '\n' |
| 159 |
stanza += indent + '/* invalid inputs might need reduction mod %d */\n' % m1 |
| 160 |
stanza += indent + 'a1 -= %d;\n' % m1 |
| 161 |
stanza += indent + 'a1 += crypto_int16_negative_mask(a1)&%d;\n' % m1 |
| 162 |
stanza += '\n' |
| 163 |
stanza += indent + '%s\n' % poke(layer,outpos0,'a0') |
| 164 |
stanza += indent + '%s\n' % poke(layer,outpos1,'a1') |
| 165 |
|
| 166 |
return stanza |
| 167 |
|
| 168 |
def stanzaloop(looplen,layer,m0,bytes): |
| 169 |
stanza = '' |
| 170 |
if looplen >= 1: |
| 171 |
stanza += ' for (i = %d;i >= 0;--i) {\n' % (looplen-1) |
| 172 |
stanza += inner(' ','i',m0,m0,bytes,'2*i','2*i+1') |
| 173 |
stanza += ' }\n' |
| 174 |
return stanza |
| 175 |
|
| 176 |
stanzas = [] |
| 177 |
|
| 178 |
layer = 1 |
| 179 |
while Mlen > 1: |
| 180 |
n0 = m0*m0 |
| 181 |
bytes0 = 0 |
| 182 |
while n0 >= top: |
| 183 |
bytes0 += 1 |
| 184 |
n0 = (n0+255)>>8 |
| 185 |
|
| 186 |
if Mlen&1: |
| 187 |
looplen = Mlen//2 |
| 188 |
r0 = 'R[%s]' % (looplen) |
| 189 |
stanza = ' %s\n' % poke(layer,2*looplen,r0) |
| 190 |
stanza += stanzaloop(looplen,layer,m0,bytes0) |
| 191 |
n1 = m1 |
| 192 |
stanzas += [stanza] |
| 193 |
|
| 194 |
else: |
| 195 |
n1 = m0*m1 |
| 196 |
bytes1 = 0 |
| 197 |
while n1 >= top: |
| 198 |
bytes1 += 1 |
| 199 |
n1 = (n1+255)>>8 |
| 200 |
|
| 201 |
if m1 == m0: |
| 202 |
looplen = (Mlen+1)//2 |
| 203 |
stanza = stanzaloop(looplen,layer,m0,bytes0) |
| 204 |
else: |
| 205 |
looplen = (Mlen-1)//2 |
| 206 |
stanza = inner(' ',looplen,m0,m1,bytes1,2*looplen,2*looplen+1) |
| 207 |
stanza += stanzaloop(looplen,layer,m0,bytes0) |
| 208 |
|
| 209 |
stanzas += [stanza] |
| 210 |
|
| 211 |
if m0 == m1: |
| 212 |
stanzas += [' /* reconstruct mod %d*[%d] */\n' % (Mlen,m0)] |
| 213 |
else: |
| 214 |
stanzas += [' /* reconstruct mod %d*[%d]+[%d] */\n' % (Mlen-1,m0,m1)] |
| 215 |
|
| 216 |
m0,m1,Mlen = n0,n1,(Mlen+1)//2 |
| 217 |
layer += 1 |
| 218 |
|
| 219 |
stanza = '' |
| 220 |
stanza += ' s += crypto_decode_STRBYTES;\n' |
| 221 |
|
| 222 |
stanza += ' a1 = 0;\n' |
| 223 |
q = m1 |
| 224 |
y,t,u,uinv,z = mulmoddata(256,m1) |
| 225 |
a1lower = 0 |
| 226 |
a1upper = 0 |
| 227 |
|
| 228 |
while m1 > 1: |
| 229 |
if m1 != q: |
| 230 |
stanza += ' lo = mullo(a1,%d);\n' % z |
| 231 |
stanza += ' a1 = mulhi(a1,%d)-mulhi(lo,%d);\n' % (y,q) |
| 232 |
if y > 0: |
| 233 |
a1lower,a1upper = a1lower*y,a1upper*y |
| 234 |
a1lower,a1upper = a1lower-(q<<15),a1upper+(q<<15)-1 |
| 235 |
else: |
| 236 |
a1lower,a1upper = a1upper*y,a1lower*y |
| 237 |
a1lower,a1upper = a1upper-(q<<15),a1lower+(q<<15)-1 |
| 238 |
a1lower >>= 16 |
| 239 |
a1upper >>= 16 |
| 240 |
|
| 241 |
a1upper += 255 |
| 242 |
stanza += ' a1 += *--s; /* %d...%d */\n' % (a1lower,a1upper) |
| 243 |
|
| 244 |
m1 = (m1+255)>>8 |
| 245 |
|
| 246 |
while a1upper >= q: |
| 247 |
a1lower,a1upper = a1lower-q,a1upper-q |
| 248 |
stanza += ' a1 -= %d; /* %d...%d */\n' % (q,a1lower,a1upper) |
| 249 |
|
| 250 |
while a1lower < 0: |
| 251 |
a1lower,a1upper = min(0,a1lower+q),max(a1upper,q-1) |
| 252 |
stanza += ' a1 += crypto_int16_negative_mask(a1)&%d; /* %d...%d */\n' % (q,a1lower,a1upper) |
| 253 |
|
| 254 |
stanza += ' %s\n' % poke(layer,0,'a1') |
| 255 |
stanzas += [stanza] |
| 256 |
|
| 257 |
for stanza in reversed(stanzas): |
| 258 |
print(' ') |
| 259 |
sys.stdout.write(stanza) |
| 260 |
|
| 261 |
print('}') |