"""Download original published compiled artifacts privately; do not execute them.""" import argparse from concurrent.futures import ThreadPoolExecutor, as_completed import hashlib import json from pathlib import Path import subprocess REVISION = '9036e64326acf4acfae159b96664e9708445c1d1' BASE = f'https://huggingface.co/datasets/JackMcCarthy/riemann-hypothesis-build/resolve/{REVISION}' FILES = { 'olean-manifest.sha256': (64388496, 'fb3609ad73a17102e6e3e2346c5358a9c0957bcf5945442a7e5ffaa00c441ff5'), 'oleans.tar.zst': (28638044751, '953bab5386a7849527a11960f80a2fab427c49c637d6e94cc75788dd8f40196b'), } MAIN_SHA256 = '9146dfb4148e24d2252e04c50742f3e49dd126dfcc5006d10bf5f873ddf796ad' def digest(path): with path.open('rb') as stream: return hashlib.file_digest(stream,'sha256').hexdigest() def fetch(root, name, workers): size, expected = FILES[name] target = root / name if target.exists() and target.stat().st_size == size and digest(target) == expected: return target chunk_size = 2 * 1024 * 1024 if name.endswith('.sha256') else 64 * 1024 * 1024 pairs = [(start,min(start + chunk_size,size)-1) for start in range(0,size,chunk_size)] directory = root / (name + '.chunks'); directory.mkdir(parents=True,exist_ok=True) def one(pair): start,end = pair p = directory / f'{start:012d}-{end:012d}.bin' if p.exists() and p.stat().st_size == end-start+1: return partial = p.with_suffix('.partial'); headers = p.with_suffix('.headers') subprocess.run(['curl','-fsSL','--max-time','600','--retry','4','--retry-all-errors', '--retry-delay','3','-r',f'{start}-{end}','-D',str(headers),'-o',str(partial), BASE + '/' + name + f'?download=true&source-range={start}-{end}'],check=True) if (f'content-range: bytes {start}-{end}/{size}' not in headers.read_text().lower() or partial.stat().st_size != end-start+1): raise ValueError('incorrect range response for original published cache') partial.replace(p) errors=[] with ThreadPoolExecutor(max_workers=workers) as pool: jobs=[pool.submit(one,pair) for pair in pairs] for count,job in enumerate(as_completed(jobs),1): try: job.result() except Exception as error: errors.append(str(error)); print('Range failed; completed chunks retained for resume.',flush=True) print(f'progress: {count}/{len(jobs)} {name} range tasks finished',flush=True) if errors: raise RuntimeError(f'{len(errors)} ranges failed; rerun to reuse completed chunks') partial = target.with_suffix(target.suffix+'.verified-partial') h=hashlib.sha256() with partial.open('wb') as output: for start,end in pairs: with (directory/f'{start:012d}-{end:012d}.bin').open('rb') as stream: while data:=stream.read(4*1024*1024): output.write(data); h.update(data) if partial.stat().st_size != size or h.hexdigest()!=expected: raise ValueError('original cache archive digest mismatch; no extraction permitted') partial.replace(target) print(f'{name}: published size and SHA-256 matched',flush=True) return target def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--private-root',required=True,type=Path) parser.add_argument('--report',required=True,type=Path) parser.add_argument('--workers',type=int,default=32) args=parser.parse_args() if not 1<=args.workers<=32: parser.error('workers must be in 1..32') args.private_root.mkdir(parents=True,exist_ok=True) manifest=fetch(args.private_root,'olean-manifest.sha256',args.workers) entries={} for line in manifest.read_text().splitlines(): sha,path=line.split(maxsplit=1); path=path.lstrip('*').removeprefix('./') if path in entries: raise ValueError('duplicate compiled-file manifest path') entries[path]=sha if entries.get('.lake/build/lib/lean/Main.olean')!=MAIN_SHA256: raise ValueError('published Main.olean hash differs from the independent verification page') archive=fetch(args.private_root,'oleans.tar.zst',args.workers) report={'producer_revision':REVISION,'base_url':BASE,'files':{n:{'bytes':s,'sha256':h} for n,(s,h) in FILES.items()}, 'manifest_entries':len(entries),'main_olean_sha256':MAIN_SHA256, 'scope':'Published compiled-cache bytes only; not extracted, executed, independently rebuilt or used to produce an atlas.'} args.report.write_text(json.dumps(report,indent=2)+'\n') print('Original cache download verified. No independent rebuild or declaration extraction performed.',flush=True) if __name__=='__main__':main()