interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / pipeline / seal-reuse-check.py

42 lines · 2081 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2# External independent SHA256 oracle for the Omega normalized-command memo.
3import ctypes,hashlib,json,random,sys,time
4from pathlib import Path
5l=ctypes.CDLL(sys.argv[1]);fn=l.omega_native_mcreseal
6fn.argtypes=[ctypes.POINTER(ctypes.c_uint64),ctypes.c_void_p,ctypes.c_uint64,ctypes.c_void_p,ctypes.c_uint64,ctypes.c_uint64,ctypes.c_void_p,ctypes.c_uint64,ctypes.c_void_p,ctypes.c_uint64]
7U=ctypes.c_ubyte
8b=(U*1024)();q=(U*1024)();c=(U*2048)();o=(U*32)();r=ctypes.c_uint64()
9rng=random.Random(710)
10for a in (b,q):
11 for i in range(1024):a[i]=rng.randrange(256)
12def digest(n):
13 v=bytearray(q[:384]);v[40:44]=bytes(4);v[48:52]=bytes(4)
14 return hashlib.sha256(bytes(b[:896+n*4])+v+bytes(q[512:896])).digest()
15cases=0
16def check(n):
17 global cases
18 for i in range(32):o[i]=255
19 rc=fn(ctypes.byref(r),b,1024,q,1024,n,c,2048,o,32)
20 assert rc==0,(rc,n)
21 if 1<=n<=32:assert r.value==0 and bytes(o)==digest(n),(n,r.value,bytes(o).hex(),digest(n).hex())
22 else:assert r.value==2**64-1 and bytes(o)==bytes(32)
23 cases+=1
24for n in (0,1,2,10,31,32,33,2**64-1,10):check(n);check(n)
25# Every byte of both inputs: covered mutations must refresh, ignored bytes must retain exact digest.
26for a in (b,q):
27 for i in range(1024):
28 a[i]^=1;check(32);check(32);a[i]^=1;check(32)
29# Same bytes interpreted with each argument count, both directions.
30for n in list(range(1,33))+list(range(32,0,-1)):check(n);check(n)
31# Zero commands have equal overlapping prefixes across argument counts.
32for a in (b,q):
33 for i in range(1024):a[i]=0
34for n in (32,1,32,2,31,10):check(n);check(n)
35check(10);start=time.perf_counter_ns()
36for i in range(256):
37 assert fn(ctypes.byref(r),b,1024,q,1024,10,c,2048,o,32)==0 and r.value==0
38elapsed=time.perf_counter_ns()-start
39# Refresh cold memo as well, proving no prior command is required.
40for i in range(2048):c[i]=0
41check(10)
42print(json.dumps({'pass':True,'oracle_cases':cases,'warm_calls':256,'warm_total_ns':elapsed,'all_input_byte_mutations':2048,'oracle':'Python hashlib.sha256 over original normalized byte contract'}))