CodeShell.kr - Errata

Challenge

三条 lane 各 128 个点,其中恰好 32 个 y 值被篡改。这是标准 Reed–Solomon 纠错,难度在于要把「恰好 96/128 成立」这条自洽性当成验证依据。

Recover the message

恢复消息。

1
https://codeshell.kr/challenges/errata/

Solution

附件 errata-player.ziprules.mdsamples.csv(384 行 = 3 lane × 128 点)、cipher.bin(30 字节)。

Step 1:规格是 GF(257) 上三条 lane,每条 128 个点、真实数据为 degree ≤ 15 的多项式,其中恰好 32 个 y 值被篡改(errata)。这是标准 Reed–Solomon 纠错问题,用 Berlekamp–Welch 求解(n=128, k=16, e=32,需 128 ≥ 16 + 2·32 = 80,可行)。

Step 2:三条 lane 的拟合曲线恰好在 96/128 点上成立(96 = 128 − 32),与规则里的 "Exactly 32 altered y values per lane" 完全一致,且多项式除法余数为 0:

1
2
3
lane 0: e=32 deg=15 matches=96/128
lane 1: e=32 deg=15 matches=96/128
lane 2: e=32 deg=15 matches=96/128

Step 3:按升序系数、BE16、逐 lane 拼接得 T(96 字节),K = SHA256("ERRATA/DEVIL/V1" || T)S = SHA256(K||BE32(0)) || SHA256(K||BE32(1)) || ...flag = cipher.bin XOR S[:30]

Script

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
#!/usr/bin/env python3
"""errata solver: Berlekamp-Welch over GF(257), deg<=15, exactly 32 errors/lane."""
import csv, hashlib, sys

P = 257

def inv(a):
return pow(a % P, P - 2, P)

def gauss(A, b):
"""Solve A x = b over GF(P). A: list of rows (len m), b: len n. Returns x or None."""
n = len(A); m = len(A[0])
M = [row[:] + [b[i]] for i, row in enumerate(A)]
piv_cols = []
r = 0
for c in range(m):
piv = None
for i in range(r, n):
if M[i][c] % P:
piv = i; break
if piv is None:
continue
M[r], M[piv] = M[piv], M[r]
iv = inv(M[r][c])
M[r] = [(v * iv) % P for v in M[r]]
for i in range(n):
if i != r and M[i][c] % P:
f = M[i][c]
M[i] = [(M[i][j] - f * M[r][j]) % P for j in range(m + 1)]
piv_cols.append(c)
r += 1
if r == n:
break
# check consistency
for i in range(r, n):
if all(M[i][j] % P == 0 for j in range(m)) and M[i][m] % P != 0:
return None
if len(piv_cols) < m:
# free variables -> set them 0
pass
x = [0] * m
for i, c in enumerate(piv_cols):
x[c] = M[i][m] % P
return x

def polymul(a, b):
res = [0] * (len(a) + len(b) - 1)
for i, ai in enumerate(a):
if ai:
for j, bj in enumerate(b):
res[i + j] = (res[i + j] + ai * bj) % P
return res

def polydivmod(a, b):
a = a[:]
db = len(b) - 1
ib = inv(b[-1])
q = [0] * (len(a) - db)
for i in range(len(a) - 1, db - 1, -1):
coef = (a[i] * ib) % P
q[i - db] = coef
if coef:
for j in range(db + 1):
a[i - db + j] = (a[i - db + j] - coef * b[j]) % P
rem = a[:db]
while q and q[-1] == 0:
q.pop()
return q, rem

def polyeval(f, x):
v = 0
for c in reversed(f):
v = (v * x + c) % P
return v

def berlekamp_welch(pts, k, e):
"""pts: list of (x,y). k = number of coeffs (deg+1). e = num errors."""
n = len(pts)
degQ = k - 1 + e
nQ = degQ + 1
nE = e # e coefficients e_0..e_{e-1}, E monic of deg e
m = nQ + nE
A = []; b = []
for (x, y) in pts:
row = [0] * m
xp = 1
for j in range(nQ):
row[j] = xp
xp = (xp * x) % P
# -y * x^j for j in 0..e-1
xp = 1
for j in range(nE):
row[nQ + j] = (-y * xp) % P
xp = (xp * x) % P
# rhs = y * x^e
A.append(row)
b.append((y * xp) % P) # xp == x^e here
sol = gauss(A, b)
if sol is None:
return None
Q = sol[:nQ]
Ecol = sol[nQ:] + [1]
f, rem = polydivmod(Q, Ecol)
if any(r % P for r in rem):
return None
return f

def main():
path = sys.argv[1] if len(sys.argv) > 1 else "samples.csv"
lanes = {0: [], 1: [], 2: []}
with open(path, newline="") as fh:
rd = csv.reader(fh)
header = next(rd)
for row in rd:
if not row or not row[0].strip():
continue
l, x, y = int(row[0]), int(row[1]), int(row[2])
lanes[l].append((x, y))
for l in lanes:
print(f"lane {l}: {len(lanes[l])} points", file=sys.stderr)

T = b""
for l in (0, 1, 2):
pts = lanes[l]
found = None
for e in (32, 31, 33, 30, 34, 28, 36, 24, 40, 20, 48):
f = berlekamp_welch(pts, 16, e)
if f is None:
continue
good = sum(1 for (x, y) in pts if polyeval(f, x) == y)
print(f"lane {l}: e={e} deg={len(f)-1} matches={good}/{len(pts)}", file=sys.stderr)
if good >= len(pts) - e and len(f) - 1 <= 15:
found = f
break
if found is None:
print(f"lane {l}: FAILED", file=sys.stderr)
return
f = found[:]
while len(f) < 16:
f.append(0)
print(f"lane {l} coeffs: {f}", file=sys.stderr)
for c in f:
T += c.to_bytes(2, "big")
print("T =", T.hex(), file=sys.stderr)

K = hashlib.sha256(b"ERRATA/DEVIL/V1" + T).digest()
print("K =", K.hex(), file=sys.stderr)
S = b"".join(hashlib.sha256(K + i.to_bytes(4, "big")).digest() for i in range(8))
with open("cipher.bin", "rb") as fh:
ct = fh.read()
flag = bytes(a ^ b for a, b in zip(ct, S))
print("FLAG:", flag)
print("FLAG hex:", flag.hex())

if __name__ == "__main__":
main()
CodeShell{ERRORS_ARE_EVIDENCE}