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
| from Crypto.Util.number import * import ast from sage.all import xgcd, gcd, matrix, ZZ import gmpy2
def pow(a, b, c): return int(gmpy2.powmod(a, b, c))
with open("output.txt") as f: s, t, n = ast.literal_eval(f.readline()) c = ast.literal_eval(f.readline())
def inverse(x, y): return pow(x, -1, y)
flagct = [] for (c1, c2) in c: for m in range(1, 256): if c1 == c2 == 0: flagct.append(0) break rs = c1 * inverse(m, n) % n rt = c2 * inverse(m, n) % n if pow(rs, t, n) == pow(rt, s, n): flagct.append(m) break else: raise ValueError("???") print(bytes(flagct))
g, x, y = xgcd(s, t) assert g == 1 assert x * s + y * t == 1 rrr = [] for m, (c1, c2) in zip(flagct, c): try: im = inverse(m, n) rs = im * c1 % n rt = im * c2 % n r = pow(rs, x, n) * pow(rt, y, n) % n rrr.append(r) except: break print(len(rrr))
import sys
sys.path.append("./MT19937-Symbolic-Execution-and-Solver/source") from MT19937 import MT19937, MT19937_symbolic from XorSolver import XorSolver import random
from functools import lru_cache
@lru_cache(maxsize=None) def split1024(r): x = [] for _ in range(1024 // 32): x.append(r & 0xFFFFFFFF) r >>= 32 return x
def clone(rng): return MT19937(state=rng.state[:])
def grb1024(rng): ss = 0 for i in range(1024 // 32): ss |= rng() << (i * 32) return ss
def randgen(n, prob=0.3, prob2=0.05): ar = [] for _ in range(n): r = random.random() if r < prob2: ar.append(2) elif r < prob: ar.append(1) else: ar.append(0) return ar
def compare(x, y): cnt = 0 for a, b in zip(f"{x:01024b}", f"{y:01024b}"): cnt += a == b return cnt
def try_xx(xx, n_cmp, return_rng=False): rtmp = [r + x * n for r, x in zip(rr, xx)] data = [] for r in rtmp: data += split1024(r) try: rng_clone = MT19937(state_from_data=(data, 32)) cr = clone(rng_clone) tot = 0 for j in range(n_cmp): lhs = grb1024(cr) % n rhs = rrr[baseidx + j] cond = lhs == rhs cp = compare(lhs, rhs) tot += cp if return_rng: return rng_clone return tot except Exception as ex: pass
def mutate(xx): xx = list(xx) v = random.randint(0, 2) xx[random.randrange(len(xx))] = v return xx
baseidx = 0 rr = rrr[baseidx : baseidx + 20] xx = [0] * 20 tot = 0 n_cmp = 40
while tot < n_cmp * 1024: mut = [mutate(xx) for _ in range(10)] + [randgen(len(xx)) for _ in range(10)] res = [(ret, mx) for mx in mut if (ret := try_xx(mx, n_cmp)) is not None] res.sort(key=lambda x: x[0], reverse=True) if len(res) >= 1: tot, mx = res[0] print(tot, mx) xx = mx
rng_clone = try_xx(xx, n_cmp, return_rng=True)
cr = clone(rng_clone) print(grb1024(cr) % n, rrr[baseidx]) cr = clone(rng_clone) print([cr() for _ in range(1024 // 32)], split1024(rrr[baseidx] + xx[0] * n))
rng = clone(rng_clone) rng.reverse_states(512 * (1024 // 32)) for i in range(512): ss = grb1024(rng) if s == ss: print("good") print(hex(s)) print(hex(ss)) a = prv print(hex(a)) phik = s * a - t * a + t d = inverse(0x10001, phik) cc = bytes_to_long(bytes(flagct)) m = pow(cc, d, n) print(long_to_bytes(m)) prv = ss
|