1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
| DEBUG = False pc = 32
if not DEBUG: _f = open('output.txt', 'w')
_caches = {}
def _const(value): global pc if value in _caches: return _caches[value] result = pc pc += 1 if not DEBUG: print(f'{value:.20f}', file=_f) _caches[value] = result return result
def _sub2(a, b): global pc result = pc pc += 1 if not DEBUG: print(a, b, file=_f) return result
class Val: def __repr__(self): return str(self)
def back(self): return self - Const(0.0)
def __neg__(self): return Const(0.0) - self
def f(self, k): return self - _fs[k] - _fss[k]
def g(self, k): return self - _gss[k] - _gs[k]
def bits(self, p=8): if isinstance(self, Const): t = int(self.v) assert 0 <= t < 2**p result = [] for _ in range(p): result.append(Const(t & 1)) t >>= 1 return BitVec(result)
t = self result = [] for i in range(p - 1, -1, -1): bit = t.f(i).g(i + 1) t = t - bit for j in range(i, 0, -1): bit = bit.g(j) result.append(bit)
return BitVec(result[::-1])
class Const(Val): def __init__(self, v): self.v = float(v)
@property def pos(self): if hasattr(self, '_pos'): return self._pos self._pos = _const(self.v) return self._pos
def __sub__(self, other): if isinstance(other, Var): return Var(_sub2(self.pos, other.pos)) assert isinstance(other, Const) return Const(self.v - other.v)
def __str__(self): return f'Const({self.v})'
_fs = [ 9024808620654594.00000000000000000000, -36028796876357636.00000000000000000000, 55244188715091032.00000000000000000000, -144115188075855856.00000000000000000000, -246116941333034592.00000000000000000000, -576460752303423424.00000000000000000000, -610435908294422144.00000000000000000000, -2305843009213693696.00000000000000000000, ] _fss = [Const(float(2**i - 1) - _fs[i]) for i in range(8)] _fs = [Const(t) for t in _fs] _gs = [ 4503599627370495.50000000000000000000, 9007199254740991.00000000000000000000, 18014398509481982.00000000000000000000, 36028797018963964.00000000000000000000, 72057594037927928.00000000000000000000, 144115188075855856.00000000000000000000, 288230376151711712.00000000000000000000, 576460752303423424.00000000000000000000, 1152921504606846848.00000000000000000000, ] _gss = [Const(-g) for g in _gs] _gs = [Const(g) for g in _gs]
class Var(Val): def __init__(self, pos): self.pos = pos
def __str__(self): return f'Var({self.pos})'
def __sub__(self, other): return Var(_sub2(self.pos, other.pos))
def add3(a, b, c): return a - ((Const(0.0) - b) - c)
def concat(*args): result = [] for arg in args: assert isinstance(arg, BitVec) result.extend(arg.bits) return BitVec(result)
def bit_op(f): def g(a, b): if isinstance(a, Const) and isinstance(b, Var): return f(b, a) return f(a, b)
return g
@bit_op def bit_xor(a, b): if isinstance(b, Const): if int(b.v) == 0: return a else: return Const(1.0) - a
s = a - (-b) return s - s.f(1).g(2)
@bit_op def bit_and(a, b): if isinstance(b, Const): if int(b.v) == 0: return Const(0.0) else: return a
return (a - (-b)).f(1).g(2).g(1)
class BitVec: def __init__(self, bits): self.bits = bits
def __str__(self): return f'BitVec{self.bits}'
def __repr__(self): return str(self)
def __len__(self): return len(self.bits)
def __add__(self, other): assert len(self) == len(other) carry = Const(0.0) result = [] for i in range(len(self)): add = add3(self.bits[i], other.bits[i], carry) tmp = add.f(1).g(2) result.append(add - tmp) if i != len(self) - 1: carry = tmp.g(1) return BitVec(result)
def __int__(self): result = 0 for b in reversed(self.bits): assert isinstance(b, Const) result = result * 2 + int(b.v) return result
def __xor__(self, other): assert len(self) == len(other) return BitVec([bit_xor(a, b) for a, b in zip(self.bits, other.bits)])
def __and__(self, other): assert len(self) == len(other) return BitVec([bit_and(a, b) for a, b in zip(self.bits, other.bits)])
def __invert__(self): return BitVec([Const(1.0) - b for b in self.bits])
def __rshift__(self, n): return BitVec(self.bits[n:] + [Const(0.0)] * n)
def rotr(self, n): return BitVec(self.bits[n:] + self.bits[:n])
def scalar(self): t = Const(0.0) for b in reversed(self.bits): t = add3(t, t, b) return t
if 1 == 1: data = [Var(i) for i in range(32)] else: data = [Const(0.0) for i in range(32)] data[5] = Const(0x23)
for i in range(32): data.append(Const(0.0)) data[32] = Const(128.0) data[62] = Const(1.0)
data = [i.bits() for i in data]
st = [ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19, ] st = [Const(i).bits(32) for i in st] a, b, c, d, e, f, g, h = st
K = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
K = [Const(i).bits(32) for i in K]
w = [ concat(data[i + 3], data[i + 2], data[i + 1], data[i]) for i in range(0, 64, 4) ]
for i in range(16, 64): s0 = w[i - 15].rotr(7) ^ w[i - 15].rotr(18) ^ (w[i - 15] >> 3) s1 = w[i - 2].rotr(17) ^ w[i - 2].rotr(19) ^ (w[i - 2] >> 10) w.append(w[i - 16] + s0 + w[i - 7] + s1)
for i in range(64): s1 = e.rotr(6) ^ e.rotr(11) ^ e.rotr(25) ch = (e & f) ^ (~e & g) temp1 = h + s1 + ch + K[i] + w[i] s0 = a.rotr(2) ^ a.rotr(13) ^ a.rotr(22) maj = (a & b) ^ (a & c) ^ (b & c) temp2 = s0 + maj
h = g g = f f = e e = d + temp1 d = c c = b b = a a = temp1 + temp2
st[0] = st[0] + a st[1] = st[1] + b st[2] = st[2] + c st[3] = st[3] + d st[4] = st[4] + e st[5] = st[5] + f st[6] = st[6] + g st[7] = st[7] + h
result = [] for i in range(8): result += [ st[i].bits[24:], st[i].bits[16:24], st[i].bits[8:16], st[i].bits[:8], ]
result = [BitVec(i).scalar() for i in result] for i in result: i.back()
|