SoulFireMage commited on
Commit
ec4db45
·
verified ·
1 Parent(s): 37446b3

Add blinding script for the key-holder

Browse files
Files changed (1) hide show
  1. blind.py +109 -0
blind.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """blind.py -- run by the KEY-HOLDER (Richard), never by Claude.
2
+
3
+ What it does:
4
+ 1. Downloads the 14 raw networks (12 trained + 2 random-init) from the private repo.
5
+ 2. Picks 2 trained networks at random and makes shuffled copies (entries permuted
6
+ within each tensor), giving 16 networks in total.
7
+ 3. Writes each network as plain text with a random ID, in a random order.
8
+ 4. Writes key.csv (KEEP THIS PRIVATE) with a random salt line, so its hash can't be
9
+ brute-forced.
10
+ 5. Uploads the 16 blinded text files plus KEY_HASH.txt to the PUBLIC repo.
11
+
12
+ Randomness comes from your operating system, so Claude cannot predict it.
13
+
14
+ Setup (once): pip install numpy huggingface_hub
15
+ hf auth login (or: huggingface-cli login)
16
+ Run: python blind.py
17
+ """
18
+ import csv, hashlib, io, os, secrets, sys
19
+ import numpy as np
20
+ from huggingface_hub import HfApi, hf_hub_download
21
+
22
+ RAW = "SoulFireMage/weight-reading-probe-raw"
23
+ PUB = "SoulFireMage/weight-reading-probe"
24
+ TRAINED = [f"{g}_s{s}" for g in ["breakout", "pong", "pacman", "freeway"] for s in (0, 1, 2)]
25
+ RANDOM = ["random_init_0", "random_init_1"]
26
+ ORDER = ["fc1.weight", "fc1.bias", "fc2.weight", "fc2.bias", "out.weight", "out.bias"]
27
+ SHAPES = {"fc1.weight": (32, 500), "fc1.bias": (32,), "fc2.weight": (32, 32),
28
+ "fc2.bias": (32,), "out.weight": (5, 32), "out.bias": (5,)}
29
+ CH = ["agent", "moving", "targets", "obstacles", "prev_frame"]
30
+
31
+
32
+ def fmt(x):
33
+ return " ".join(f"{v:.4g}" for v in x)
34
+
35
+
36
+ def serialise(w):
37
+ out = io.StringIO()
38
+ out.write("# 17253 parameters. Values to 4 significant figures.\n")
39
+ W1 = w["fc1.weight"]
40
+ out.write("\n## fc1.weight shape [32 x 500] (hidden1 unit j <- input i)\n")
41
+ out.write("## each unit's 500 inputs are laid out as 5 channels x 10 rows x 10 columns\n")
42
+ for j in range(32):
43
+ out.write(f"\n### fc1 unit {j}\n")
44
+ g = W1[j].reshape(5, 10, 10)
45
+ for c in range(5):
46
+ out.write(f"channel {c} ({CH[c]}):\n")
47
+ for r in range(10):
48
+ out.write(f" r{r}: {fmt(g[c, r])}\n")
49
+ out.write("\n## fc1.bias shape [32]\n" + fmt(w["fc1.bias"]) + "\n")
50
+ out.write("\n## fc2.weight shape [32 x 32] (row = hidden2 unit, column = hidden1 unit)\n")
51
+ for j in range(32):
52
+ out.write(f"u{j}: {fmt(w['fc2.weight'][j])}\n")
53
+ out.write("\n## fc2.bias shape [32]\n" + fmt(w["fc2.bias"]) + "\n")
54
+ out.write("\n## out.weight shape [5 x 32] (row = action, column = hidden2 unit)\n")
55
+ for a, name in enumerate(["noop", "up", "down", "left", "right"]):
56
+ out.write(f"a{a} ({name}): {fmt(w['out.weight'][a])}\n")
57
+ out.write("\n## out.bias shape [5]\n" + fmt(w["out.bias"]) + "\n")
58
+ return out.getvalue()
59
+
60
+
61
+ def main():
62
+ rng = np.random.default_rng(secrets.randbits(128))
63
+ nets = {}
64
+ for name in TRAINED + RANDOM:
65
+ p = hf_hub_download(RAW, f"raw/{name}.npz", repo_type="dataset")
66
+ z = np.load(p)
67
+ w = {k: z[k].astype(np.float64) for k in ORDER}
68
+ for k in ORDER:
69
+ assert w[k].shape == SHAPES[k], (name, k, w[k].shape)
70
+ nets[name] = ("random_init" if name in RANDOM else "trained", name, w)
71
+ for src in rng.choice(TRAINED, 2, replace=False):
72
+ w = nets[src][2]
73
+ sh = {k: rng.permutation(w[k].ravel()).reshape(w[k].shape) for k in ORDER}
74
+ nets[f"shuffled_{src}"] = ("shuffled", src, sh)
75
+ ids = set()
76
+ while len(ids) < len(nets):
77
+ ids.add("net-" + secrets.token_hex(3))
78
+ ids = list(ids)
79
+ keys = list(nets)
80
+ rng.shuffle(keys)
81
+ os.makedirs("blinded", exist_ok=True)
82
+ rows = []
83
+ for nid, k in zip(ids, keys):
84
+ cond, src, w = nets[k]
85
+ with open(f"blinded/{nid}.txt", "w", newline="\n") as f:
86
+ f.write(serialise(w))
87
+ game = src.split("_s")[0] if cond != "random_init" else ""
88
+ rows.append([nid, cond, game, src])
89
+ rows.sort()
90
+ with open("key.csv", "w", newline="") as f:
91
+ cw = csv.writer(f)
92
+ cw.writerow(["salt", secrets.token_hex(16), "", ""])
93
+ cw.writerow(["id", "condition", "game", "source"])
94
+ cw.writerows(rows)
95
+ h = hashlib.sha256(open("key.csv", "rb").read()).hexdigest()
96
+ with open("blinded/KEY_HASH.txt", "w") as f:
97
+ f.write(f"SHA-256 of key.csv (held privately by the key-holder): {h}\n")
98
+ print("key.csv SHA-256:", h)
99
+ ans = input(f"Upload {len(ids)} blinded files + KEY_HASH.txt to {PUB}? [y/N] ")
100
+ if ans.strip().lower() == "y":
101
+ HfApi().upload_folder(folder_path="blinded", path_in_repo="blinded", repo_id=PUB,
102
+ repo_type="dataset", commit_message="Blinded networks + key hash")
103
+ print("Uploaded. KEEP key.csv PRIVATE and do not send it to Claude until unblinding.")
104
+ else:
105
+ print("Not uploaded. Files are in ./blinded")
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()