TheAiCollectiveART commited on
Commit
794e95f
·
verified ·
1 Parent(s): 6a379b4

Update/Add WASM_U-Performance_Record/proof.py for WebAssembly 7.10us record

Browse files
Files changed (1) hide show
  1. WASM_U-Performance_Record/proof.py +413 -0
WASM_U-Performance_Record/proof.py ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ # Watermark: ip zymatica.space | astronautshe.com
3
+ # Parity Verification Engine
4
+
5
+ import os
6
+ import sys
7
+ import json
8
+ import random
9
+ import subprocess
10
+ import hashlib
11
+
12
+ class PythonRadicalPredictor:
13
+ def __init__(self, alpha=1, weight=128):
14
+ self.alpha = alpha
15
+ self.weight = weight
16
+ self.trans_rc = {}
17
+ self.trans_rf = {}
18
+ self.trans_ra = {}
19
+ self.prev_rc = 0
20
+ self.prev_rf = 0
21
+ self.prev_ra = 0
22
+
23
+ def observe(self, rc, rf, ra):
24
+ key_rc = self.prev_rc
25
+ if key_rc not in self.trans_rc:
26
+ self.trans_rc[key_rc] = {}
27
+ self.trans_rc[key_rc][rc] = self.trans_rc[key_rc].get(rc, 0) + self.weight
28
+
29
+ key_rf = (rc << 8) | self.prev_rf
30
+ if key_rf not in self.trans_rf:
31
+ self.trans_rf[key_rf] = {}
32
+ self.trans_rf[key_rf][rf] = self.trans_rf[key_rf].get(rf, 0) + self.weight
33
+
34
+ key_ra = (rc << 16) | (rf << 8) | self.prev_ra
35
+ if key_ra not in self.trans_ra:
36
+ self.trans_ra[key_ra] = {}
37
+ self.trans_ra[key_ra][ra] = self.trans_ra[key_ra].get(ra, 0) + self.weight
38
+
39
+ self.prev_rc = rc
40
+ self.prev_rf = rf
41
+ self.prev_ra = ra
42
+
43
+ def get_cum_freqs_rc(self, prev_rc):
44
+ freqs = [self.alpha] * 256
45
+ if prev_rc in self.trans_rc:
46
+ for sym, count in self.trans_rc[prev_rc].items():
47
+ freqs[sym] += count
48
+ cum_freqs = [0] * 257
49
+ for i in range(256):
50
+ cum_freqs[i+1] = cum_freqs[i] + freqs[i]
51
+ return cum_freqs
52
+
53
+ def get_cum_freqs_rf(self, curr_rc, prev_rf):
54
+ freqs = [self.alpha] * 256
55
+ key = (curr_rc << 8) | prev_rf
56
+ if key in self.trans_rf:
57
+ for sym, count in self.trans_rf[key].items():
58
+ freqs[sym] += count
59
+ cum_freqs = [0] * 257
60
+ for i in range(256):
61
+ cum_freqs[i+1] = cum_freqs[i] + freqs[i]
62
+ return cum_freqs
63
+
64
+ def get_cum_freqs_ra(self, curr_rc, curr_rf, prev_ra):
65
+ freqs = [self.alpha] * 256
66
+ key = (curr_rc << 16) | (curr_rf << 8) | prev_ra
67
+ if key in self.trans_ra:
68
+ for sym, count in self.trans_ra[key].items():
69
+ freqs[sym] += count
70
+ cum_freqs = [0] * 257
71
+ for i in range(256):
72
+ cum_freqs[i+1] = cum_freqs[i] + freqs[i]
73
+ return cum_freqs
74
+
75
+ class BitWriter:
76
+ def __init__(self):
77
+ self.buffer = []
78
+ self.current_byte = 0
79
+ self.bit_count = 0
80
+
81
+ def write_bit(self, bit):
82
+ self.current_byte = (self.current_byte << 1) | (bit & 1)
83
+ self.bit_count += 1
84
+ if self.bit_count % 8 == 0:
85
+ self.buffer.append(self.current_byte)
86
+ self.current_byte = 0
87
+
88
+ def write_bit_helper(self, underflow_bits, bit):
89
+ self.write_bit(bit)
90
+ for _ in range(underflow_bits[0]):
91
+ self.write_bit(1 - bit)
92
+ underflow_bits[0] = 0
93
+
94
+ def flush(self):
95
+ if self.bit_count % 8 != 0:
96
+ padding_bits = 8 - (self.bit_count % 8)
97
+ self.current_byte <<= padding_bits
98
+ self.buffer.append(self.current_byte)
99
+ self.current_byte = 0
100
+ self.bit_count += padding_bits
101
+ return bytes(self.buffer)
102
+
103
+ class BitReader:
104
+ def __init__(self, buffer):
105
+ self.buffer = buffer
106
+ self.bit_index = 0
107
+ self.total_bits = len(buffer) * 8
108
+
109
+ def read_bit(self):
110
+ if self.bit_index >= self.total_bits:
111
+ return 0
112
+ byte_pos = self.bit_index // 8
113
+ bit_pos = 7 - (self.bit_index % 8)
114
+ bit = (self.buffer[byte_pos] >> bit_pos) & 1
115
+ self.bit_index += 1
116
+ return bit
117
+
118
+ def python_encode(concepts, alpha=1, weight=128):
119
+ pred = PythonRadicalPredictor(alpha, weight)
120
+ w = BitWriter()
121
+ low = 0
122
+ high = 0xFFFFFFFF
123
+ underflow_bits = [0]
124
+ trace_info = []
125
+
126
+ for c_idx, c in enumerate(concepts):
127
+ rc = (c['domain'] << 4) | c['subdomain']
128
+ rf = (c['operation'] << 4) | c['modality']
129
+ ra = (c['depth'] << 4) | c['polarity']
130
+ symbols = [rc, rf, ra]
131
+ types = ["RC", "RF", "RA"]
132
+
133
+ prev_rc = pred.prev_rc
134
+ prev_rf = pred.prev_rf
135
+ prev_ra = pred.prev_ra
136
+
137
+ for step in range(3):
138
+ if step == 0:
139
+ cum_freqs = pred.get_cum_freqs_rc(prev_rc)
140
+ elif step == 1:
141
+ cum_freqs = pred.get_cum_freqs_rf(symbols[0], prev_rf)
142
+ else:
143
+ cum_freqs = pred.get_cum_freqs_ra(symbols[0], symbols[1], prev_ra)
144
+
145
+ sym = symbols[step]
146
+ total = cum_freqs[256]
147
+ cum_low = cum_freqs[sym]
148
+ cum_high = cum_freqs[sym + 1]
149
+
150
+ range_width = high - low + 1
151
+
152
+ high_before = high
153
+ low_before = low
154
+
155
+ high = (low + (range_width * cum_high) // total - 1) & 0xFFFFFFFF
156
+ low = (low + (range_width * cum_low) // total) & 0xFFFFFFFF
157
+
158
+ bits_written = []
159
+ temp_underflow = [underflow_bits[0]]
160
+
161
+ # Simulate bit writing helper to capture trace outputs
162
+ def write_bit_simulate(bit):
163
+ bits_written.append(str(bit))
164
+ def write_bit_helper_simulate(u_bits, bit):
165
+ write_bit_simulate(bit)
166
+ for _ in range(u_bits[0]):
167
+ write_bit_simulate(1 - bit)
168
+ u_bits[0] = 0
169
+
170
+ while True:
171
+ if high_before < 0x80000000:
172
+ write_bit_helper_simulate(temp_underflow, 0)
173
+ low_before = (low_before << 1) & 0xFFFFFFFF
174
+ high_before = ((high_before << 1) | 1) & 0xFFFFFFFF
175
+ elif low_before >= 0x80000000:
176
+ write_bit_helper_simulate(temp_underflow, 1)
177
+ low_before = ((low_before - 0x80000000) << 1) & 0xFFFFFFFF
178
+ high_before = (((high_before - 0x80000000) << 1) | 1) & 0xFFFFFFFF
179
+ elif low_before >= 0x40000000 and high_before < 0xC0000000:
180
+ temp_underflow[0] += 1
181
+ low_before = ((low_before - 0x40000000) << 1) & 0xFFFFFFFF
182
+ high_before = (((high_before - 0x40000000) << 1) | 1) & 0xFFFFFFFF
183
+ else:
184
+ break
185
+
186
+ # Now write the real bits
187
+ while True:
188
+ if high < 0x80000000:
189
+ w.write_bit_helper(underflow_bits, 0)
190
+ low = (low << 1) & 0xFFFFFFFF
191
+ high = ((high << 1) | 1) & 0xFFFFFFFF
192
+ elif low >= 0x80000000:
193
+ w.write_bit_helper(underflow_bits, 1)
194
+ low = ((low - 0x80000000) << 1) & 0xFFFFFFFF
195
+ high = (((high - 0x80000000) << 1) | 1) & 0xFFFFFFFF
196
+ elif low >= 0x40000000 and high < 0xC0000000:
197
+ underflow_bits[0] += 1
198
+ low = ((low - 0x40000000) << 1) & 0xFFFFFFFF
199
+ high = (((high - 0x40000000) << 1) | 1) & 0xFFFFFFFF
200
+ else:
201
+ break
202
+
203
+ trace_info.append({
204
+ "concept_idx": c_idx,
205
+ "step": step,
206
+ "symbol_type": types[step],
207
+ "symbol_value": sym,
208
+ "low_before": f"0x{low_before:08x}",
209
+ "high_before": f"0x{high_before:08x}",
210
+ "cum_low": cum_low,
211
+ "cum_high": cum_high,
212
+ "total": total,
213
+ "bits_written": "".join(bits_written)
214
+ })
215
+
216
+ pred.observe(rc, rf, ra)
217
+
218
+ underflow_bits[0] += 1
219
+ if low < 0x40000000:
220
+ w.write_bit_helper(underflow_bits, 0)
221
+ else:
222
+ w.write_bit_helper(underflow_bits, 1)
223
+ return w.flush(), w.bit_count, trace_info
224
+
225
+
226
+ def python_decode(encoded_bytes, num_concepts, alpha=1, weight=128):
227
+ pred = PythonRadicalPredictor(alpha, weight)
228
+ r = BitReader(encoded_bytes)
229
+
230
+ value = 0
231
+ for _ in range(32):
232
+ value = (value << 1) | r.read_bit()
233
+
234
+ low = 0
235
+ high = 0xFFFFFFFF
236
+ decoded = []
237
+
238
+ for _ in range(num_concepts):
239
+ prev_rc = pred.prev_rc
240
+ prev_rf = pred.prev_rf
241
+ prev_ra = pred.prev_ra
242
+ symbols = [0, 0, 0]
243
+
244
+ for step in range(3):
245
+ if step == 0:
246
+ cum_freqs = pred.get_cum_freqs_rc(prev_rc)
247
+ elif step == 1:
248
+ cum_freqs = pred.get_cum_freqs_rf(symbols[0], prev_rf)
249
+ else:
250
+ cum_freqs = pred.get_cum_freqs_ra(symbols[0], symbols[1], prev_ra)
251
+
252
+ total = cum_freqs[256]
253
+ range_width = high - low + 1
254
+ scaled_val = ((value - low + 1) * total - 1) // range_width
255
+
256
+ sym = 0
257
+ l = 0
258
+ rr = 255
259
+ while l <= rr:
260
+ mid = (l + rr) // 2
261
+ if cum_freqs[mid] <= scaled_val < cum_freqs[mid+1]:
262
+ sym = mid
263
+ break
264
+ elif scaled_val >= cum_freqs[mid+1]:
265
+ l = mid + 1
266
+ else:
267
+ rr = mid - 1
268
+
269
+ symbols[step] = sym
270
+ cum_low = cum_freqs[sym]
271
+ cum_high = cum_freqs[sym+1]
272
+
273
+ high = (low + (range_width * cum_high) // total - 1) & 0xFFFFFFFF
274
+ low = (low + (range_width * cum_low) // total) & 0xFFFFFFFF
275
+
276
+ while True:
277
+ if high < 0x80000000:
278
+ low = (low << 1) & 0xFFFFFFFF
279
+ high = ((high << 1) | 1) & 0xFFFFFFFF
280
+ value = ((value << 1) | r.read_bit()) & 0xFFFFFFFF
281
+ elif low >= 0x80000000:
282
+ low = ((low - 0x80000000) << 1) & 0xFFFFFFFF
283
+ high = (((high - 0x80000000) << 1) | 1) & 0xFFFFFFFF
284
+ value = (((value - 0x80000000) << 1) | r.read_bit()) & 0xFFFFFFFF
285
+ elif low >= 0x40000000 and high < 0xC0000000:
286
+ low = ((low - 0x40000000) << 1) & 0xFFFFFFFF
287
+ high = (((high - 0x40000000) << 1) | 1) & 0xFFFFFFFF
288
+ value = (((value - 0x40000000) << 1) | r.read_bit()) & 0xFFFFFFFF
289
+ else:
290
+ break
291
+
292
+ rc, rf, ra = symbols
293
+ decoded.append({
294
+ 'domain': rc >> 4,
295
+ 'subdomain': rc & 0x0F,
296
+ 'operation': rf >> 4,
297
+ 'modality': rf & 0x0F,
298
+ 'depth': ra >> 4,
299
+ 'polarity': ra & 0x0F
300
+ })
301
+ pred.observe(rc, rf, ra)
302
+
303
+ return decoded
304
+
305
+ def generate_fuzz_data(count=100):
306
+ concepts = []
307
+ for _ in range(count):
308
+ concepts.append({
309
+ 'domain': random.randint(0, 15),
310
+ 'subdomain': random.randint(0, 15),
311
+ 'operation': random.randint(0, 15),
312
+ 'modality': random.randint(0, 15),
313
+ 'depth': random.randint(0, 15),
314
+ 'polarity': random.randint(0, 15)
315
+ })
316
+ return concepts
317
+
318
+ def run_parity_test():
319
+ print("=" * 80)
320
+ print(" [+] Starting Fuzz Parity Test Engine...")
321
+ print("=" * 80)
322
+
323
+ # 1. Generate 100 random coordinate structures
324
+ test_concepts = generate_fuzz_data(100)
325
+ print(f" - Generated {len(test_concepts)} random 6D coordinates.")
326
+
327
+ # Write them to a JSON file for the Node.js / WASM script to read
328
+ with open('test_input.json', 'w') as f:
329
+ json.dump(test_concepts, f)
330
+
331
+ # 2. Run Python range encoding
332
+ py_bytes, py_bits, trace_data = python_encode(test_concepts)
333
+
334
+ # Save the trace data to parity_trace.json
335
+ with open('parity_trace.json', 'w') as f:
336
+ json.dump(trace_data, f, indent=2)
337
+ print(" [+] Step-by-step state trace outputted to parity_trace.json")
338
+
339
+ py_decoded = python_decode(py_bytes, len(test_concepts))
340
+
341
+ # Check Python self-parity
342
+ for idx, (orig, dec) in enumerate(zip(test_concepts, py_decoded)):
343
+ if orig != dec:
344
+ print(f" [-] ERROR: Python self-parity failed at element {idx}!")
345
+ return False
346
+ print(" [+] Python self-parity checks passed successfully.")
347
+
348
+ # Save python compressed payload
349
+ with open('payload_py.bin', 'wb') as f:
350
+ f.write(py_bytes)
351
+
352
+ # 3. Compile Zig code to WASM if not already done
353
+ print(" - Building Zig WASM target...")
354
+ try:
355
+ subprocess.run([
356
+ "zig", "build-exe", "proof.zig",
357
+ "-target", "wasm32-freestanding",
358
+ "-O", "ReleaseFast",
359
+ "--name", "proof_wasm",
360
+ "--export=wasm_encode", "--export=wasm_get_encoded_bits",
361
+ "--export=wasm_decode", "--export=run_verification"
362
+ ], check=True)
363
+ print(" [+] Compiled proof_wasm.wasm successfully!")
364
+ except Exception as e:
365
+ print(f" [-] Failed to compile proof.zig: {e}")
366
+ print(" [-] Make sure Zig is installed and available in PATH.")
367
+ return False
368
+
369
+ # 4. Invoke Node.js cross-runtime verification tool
370
+ print(" - Running Node.js/WASM encoding task...")
371
+ try:
372
+ subprocess.run(["node", "run_wasm.js"], check=True)
373
+ except Exception as e:
374
+ print(f" [-] Node.js/WASM execution execution error: {e}")
375
+ return False
376
+
377
+ # 5. Assert byte parity between Python and WASM
378
+ if not os.path.exists('payload_wasm.bin'):
379
+ print(" [-] ERROR: Node.js did not produce payload_wasm.bin!")
380
+ return False
381
+
382
+ with open('payload_wasm.bin', 'rb') as f:
383
+ wasm_bytes = f.read()
384
+
385
+ print(f" - Python compressed size: {len(py_bytes)} bytes ({py_bits} bits)")
386
+ print(f" - WASM compressed size: {len(wasm_bytes)} bytes")
387
+
388
+ # Assert exact byte match
389
+ if py_bytes != wasm_bytes:
390
+ print(" [-] ERROR: Bit-Parity Mismatch between Python and WebAssembly!")
391
+ print(f" - Python MD5: {hashlib.md5(py_bytes).hexdigest()}")
392
+ print(f" - WASM MD5: {hashlib.md5(wasm_bytes).hexdigest()}")
393
+ return False
394
+
395
+ print(" [+] SUCCESS: Isomorphic Bit-Parity Verified! Python and WASM produced byte-for-byte identical output.")
396
+
397
+ # 6. Check Decoded Parity from WASM output
398
+ with open('test_output_wasm.json', 'r') as f:
399
+ wasm_decoded = json.load(f)
400
+
401
+ for idx, (orig, dec) in enumerate(zip(test_concepts, wasm_decoded)):
402
+ if orig != dec:
403
+ print(f" [-] ERROR: Decoded value from WASM mismatches original at index {idx}!")
404
+ return False
405
+
406
+ print(" [+] SUCCESS: Reconstructed coordinates from WASM match input identically.")
407
+ return True
408
+
409
+ if __name__ == "__main__":
410
+ if len(sys.argv) > 1 and sys.argv[1] == '--fuzz':
411
+ run_parity_test()
412
+ else:
413
+ run_parity_test()