| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
Mlen = 761 |
| 6 |
if len(sys.argv) > 1: Mlen = int(sys.argv[1]) |
| 7 |
|
| 8 |
m0 = 4591 |
| 9 |
if len(sys.argv) > 2: m0 = int(sys.argv[2]) |
| 10 |
|
| 11 |
m1 = m0 |
| 12 |
if len(sys.argv) > 3: m1 = int(sys.argv[3]) |
| 13 |
# M is Mlen-1 copies of m0, plus 1 copy of m1 |
| 14 |
|
| 15 |
offset = m0//2 |
| 16 |
if len(sys.argv) > 4: offset = int(sys.argv[4]) |
| 17 |
|
| 18 |
div3 = False |
| 19 |
if len(sys.argv) > 5: div3 = sys.argv[5]=='True' |
| 20 |
|
| 21 |
reading = 'R0' |
| 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_encode.h"') |
| 28 |
print('#include "crypto_int16.h"') |
| 29 |
print('#include "crypto_uint16.h"') |
| 30 |
print('#include "crypto_uint32.h"') |
| 31 |
print('#define int16 crypto_int16') |
| 32 |
print('#define uint16 crypto_uint16') |
| 33 |
print('#define uint32 crypto_uint32') |
| 34 |
print('') |
| 35 |
|
| 36 |
print('void crypto_encode(unsigned char *out,const void *v)') |
| 37 |
print('{') |
| 38 |
print(' const int16 *%s = (const int16 *) v;' % reading) |
| 39 |
|
| 40 |
if Mlen > 1: |
| 41 |
print(' /* XXX: caller could overlap R with input */') |
| 42 |
print(' uint16 R[%d];' % ((Mlen+1)//2)) |
| 43 |
|
| 44 |
if Mlen > 2**29: |
| 45 |
print(' long long i;') |
| 46 |
else: |
| 47 |
print(' long i;') |
| 48 |
|
| 49 |
print(' uint16 r0,r1;') |
| 50 |
print(' uint32 r2;') |
| 51 |
|
| 52 |
def access(reading,pos): |
| 53 |
if reading == 'R0' and div3: |
| 54 |
return '(((%s[%s]%+d)&16383)*10923)>>15' % (reading,pos,offset) |
| 55 |
if reading == 'R0': |
| 56 |
return '(%s[%s]%+d)&16383' % (reading,pos,offset) |
| 57 |
return '%s[%s]' % (reading,pos) |
| 58 |
|
| 59 |
def printloop(looplen,reading,todo,m0,bytes): |
| 60 |
if looplen <= 0: return |
| 61 |
if looplen == 1: |
| 62 |
print(' r0 = %s;' % access(reading,0)) |
| 63 |
print(' r1 = %s;' % access(reading,1)) |
| 64 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 65 |
for j in range(bytes): |
| 66 |
print(' *out++ = r2; r2 >>= 8;') |
| 67 |
print(' %s[0] = r2;' % todo) |
| 68 |
return |
| 69 |
print(' for (i = 0;i < %d;++i) {' % looplen) |
| 70 |
print(' r0 = %s;' % access(reading,'2*i')) |
| 71 |
print(' r1 = %s;' % access(reading,'2*i+1')) |
| 72 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 73 |
for j in range(bytes): |
| 74 |
print(' *out++ = r2; r2 >>= 8;') |
| 75 |
print(' %s[i] = r2;' % todo) |
| 76 |
print(' }') |
| 77 |
|
| 78 |
todo = 'R' |
| 79 |
while Mlen > 1: |
| 80 |
print(' ') |
| 81 |
|
| 82 |
n0 = m0*m0 |
| 83 |
bytes0 = 0 |
| 84 |
while n0 >= top: |
| 85 |
bytes0 += 1 |
| 86 |
n0 = (n0+255)>>8 |
| 87 |
|
| 88 |
if Mlen&1: |
| 89 |
looplen = Mlen//2 |
| 90 |
printloop(looplen,reading,todo,m0,bytes0) |
| 91 |
r0 = access(reading,2*looplen) |
| 92 |
print(' %s[%d] = %s;' % (todo,looplen,r0)) |
| 93 |
n1 = m1 |
| 94 |
|
| 95 |
else: |
| 96 |
n1 = m0*m1 |
| 97 |
bytes1 = 0 |
| 98 |
while n1 >= top: |
| 99 |
bytes1 += 1 |
| 100 |
n1 = (n1+255)>>8 |
| 101 |
|
| 102 |
if bytes1 == bytes0: |
| 103 |
looplen = (Mlen+1)//2 |
| 104 |
printloop(looplen,reading,todo,m0,bytes0) |
| 105 |
else: |
| 106 |
looplen = (Mlen-1)//2 |
| 107 |
printloop(looplen,reading,todo,m0,bytes0) |
| 108 |
print(' r0 = %s;' % access(reading,2*looplen)) |
| 109 |
print(' r1 = %s;' % access(reading,2*looplen+1)) |
| 110 |
print(' r2 = r0+r1*(uint32)%d;' % m0) |
| 111 |
for j in range(bytes1): |
| 112 |
print(' *out++ = r2; r2 >>= 8;') |
| 113 |
print(' %s[%d] = r2;' % (todo,looplen)) |
| 114 |
|
| 115 |
m0,m1,Mlen = n0,n1,(Mlen+1)//2 |
| 116 |
reading = todo |
| 117 |
|
| 118 |
print(' ') |
| 119 |
print(' r0 = %s;' % access(reading,0)) |
| 120 |
while m1 > 1: |
| 121 |
print(' *out++ = r0; r0 >>= 8;') |
| 122 |
m1 = (m1+255)>>8 |
| 123 |
|
| 124 |
print('}') |