From d8c095ae09eba57494f5b7f261441a1d5e3c9262 Mon Sep 17 00:00:00 2001 From: Tekalign <152792726+tekuuu@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:29:33 +0300 Subject: [PATCH 1/5] Implement encoded CRT address conversion for compact physical tensors --- research/crt-address-streams/EXPECTED.json | 154 ++++++++ .../crt-address-streams/NETWORK-SOURCES.json | 56 +++ research/crt-address-streams/PROOF.md | 175 +++++++++ research/crt-address-streams/README.md | 88 +++++ research/crt-address-streams/SOURCES.json | 24 ++ research/crt-address-streams/TASK.json | 75 ++++ research/crt-address-streams/VERIFY.json | 21 ++ .../crt-address-streams/check_crt_cost.py | 12 + .../crt-address-streams/check_crt_runtime.py | 331 ++++++++++++++++++ research/crt-address-streams/crt_converter.py | 266 ++++++++++++++ .../crt-address-streams/crt_descriptor.py | 112 ++++++ research/crt-address-streams/crt_offsets.py | 73 ++++ research/crt-address-streams/crt_padding.py | 111 ++++++ research/crt-address-streams/crt_shape.py | 210 +++++++++++ research/crt-address-streams/crt_shear.py | 70 ++++ research/crt-address-streams/crt_sources.py | 105 ++++++ research/crt-address-streams/crt_streams.py | 42 +++ research/crt-address-streams/encoded_crt.py | 126 +++++++ research/crt-address-streams/verify.py | 87 +++++ 19 files changed, 2138 insertions(+) create mode 100644 research/crt-address-streams/EXPECTED.json create mode 100644 research/crt-address-streams/NETWORK-SOURCES.json create mode 100644 research/crt-address-streams/PROOF.md create mode 100644 research/crt-address-streams/README.md create mode 100644 research/crt-address-streams/SOURCES.json create mode 100644 research/crt-address-streams/TASK.json create mode 100644 research/crt-address-streams/VERIFY.json create mode 100644 research/crt-address-streams/check_crt_cost.py create mode 100644 research/crt-address-streams/check_crt_runtime.py create mode 100644 research/crt-address-streams/crt_converter.py create mode 100644 research/crt-address-streams/crt_descriptor.py create mode 100644 research/crt-address-streams/crt_offsets.py create mode 100644 research/crt-address-streams/crt_padding.py create mode 100644 research/crt-address-streams/crt_shape.py create mode 100644 research/crt-address-streams/crt_shear.py create mode 100644 research/crt-address-streams/crt_sources.py create mode 100644 research/crt-address-streams/crt_streams.py create mode 100644 research/crt-address-streams/encoded_crt.py create mode 100644 research/crt-address-streams/verify.py diff --git a/research/crt-address-streams/EXPECTED.json b/research/crt-address-streams/EXPECTED.json new file mode 100644 index 0000000000..02b5a7c63e --- /dev/null +++ b/research/crt-address-streams/EXPECTED.json @@ -0,0 +1,154 @@ +{ + "base": "20ecf2891d80b5acf18d980b2164fb44ea7faa90", + "complete_columns": [ + [ + { + "axes": [ + [ + 3, + 4 + ] + ], + "columns": 3, + "directions": 2, + "rows": 2 + }, + { + "axes": [ + [ + 3, + 4 + ], + [ + 5, + 8 + ] + ], + "columns": 15, + "directions": 2, + "rows": 2 + }, + { + "axes": [ + [ + 5, + 8 + ], + [ + 3, + 4 + ] + ], + "columns": 15, + "directions": 2, + "rows": 1 + }, + { + "axes": [ + [ + 3, + 4 + ], + [ + 5, + 8 + ], + [ + 7, + 8 + ] + ], + "columns": 105, + "directions": 2, + "rows": 1 + } + ], + 39 + ], + "cost": { + "Phi": "99827619697/100000000000", + "beta": "41544341/41616125", + "gaps": { + "Phi_minus_beta": "36798102513/33292900000000000", + "beta_minus_D": "84903/66585800" + }, + "multiplication_conversions": 3, + "padding_passes": 6, + "tau": "332353/332929" + }, + "descriptor_and_prefix": [ + { + "axes": [ + [ + 3, + 4 + ], + [ + 5, + 8 + ] + ], + "peak_roles": 30 + }, + { + "axes": [ + [ + 5, + 8 + ], + [ + 3, + 4 + ] + ], + "peak_roles": 30 + }, + { + "axes": [ + [ + 3, + 4 + ], + [ + 5, + 8 + ], + [ + 7, + 8 + ] + ], + "peak_roles": 30 + } + ], + "inverse_units": { + "cases": 2176, + "peak_roles": 9 + }, + "rejection_controls": { + "controls": 16, + "peak_roles": 39 + }, + "runtime_files": 8, + "schema": 1, + "shear_geometry": [ + { + "inverse": false, + "peak_roles": 10 + }, + { + "inverse": true, + "peak_roles": 10 + } + ], + "source_controls": 4, + "source_head": "240597ed313fc2854c0462b23c89dc3bae72aaf5", + "typed_plan": { + "address_bits": 5, + "padded_records": 32, + "peak_roles": 39, + "production": false, + "record_bits": 36, + "source_records": 15 + } +} diff --git a/research/crt-address-streams/NETWORK-SOURCES.json b/research/crt-address-streams/NETWORK-SOURCES.json new file mode 100644 index 0000000000..efd965ec0f --- /dev/null +++ b/research/crt-address-streams/NETWORK-SOURCES.json @@ -0,0 +1,56 @@ +{ + "head": "240597ed313fc2854c0462b23c89dc3bae72aaf5", + "files": [ + { + "path": "research/physical-gaussian-tensor/tensor_sources.py", + "sha256": "cba69b2bc93b7dabbffdbdf07fb41130fde5d65a8f4d344ade52d3a326e30ac0", + "size": 9250, + "git_blob": "00edf5a03fad4ed69891dd0ca4a6909bd68ef4cf" + }, + { + "path": "research/physical-gaussian-tensor/tensor_router.py", + "sha256": "cf6d7c58396517d15a28f836676d1da679b68796b48b0f780f6131c8a1d2b2fa", + "size": 7709, + "git_blob": "6f5177cfa1bd03f145527c6ad5ca1ddef74716fd" + }, + { + "path": "research/physical-gaussian-tensor/tensor_fixture.py", + "sha256": "f699d1c52334f42fe548b71c199a79381a980fb351c77515acd107f848cf100b", + "size": 3429, + "git_blob": "83a40a9ada87ae94cadc1ed2ddaa11c482c1d318" + }, + { + "path": "research/physical-gaussian-tensor/inputs/chirp-export.tar.gz", + "sha256": "931ed8c764ea6cc592cb59ae93f8284184139a4d174752fb80f7a42762b2c0ad", + "size": 19129822, + "git_blob": "6087d9a1b3d7dd6c74fe2f9d37ca8aa52ae30fed" + }, + { + "path": "research/physical-gaussian-tensor/inputs/expansion-export.tar.gz", + "sha256": "05146ca4feabe3dde0ab57aaea3eff9f218933562ba5b5c80bb256a247c144c7", + "size": 19199290, + "git_blob": "cde9ea4f1103de30e035eaaad4402f69769ae76d" + }, + { + "path": "research/physical-gaussian-tensor/inputs/headroom-source.tar", + "sha256": "49fd9c1d28809795d109003b4af8d667e22bbc950a75820cf7fb9986d8ac74c6", + "size": 153600, + "git_blob": "c6feb0678519269b7e3156ba8ea48a40840017f2" + }, + { + "path": "research/physical-gaussian-tensor/tensor_plan.py", + "sha256": "474730f42fcd2bd24720af4e08c71fb51cc374c3e16b8b6d7e565900f1a9e7c3", + "size": 12079, + "git_blob": "239caba2d2ccf2640c36d377949df3fb6a920cf0" + }, + { + "path": "research/physical-gaussian-tensor/PROOF.md", + "size": 13933, + "sha256": "1e3450344552e744e0187cf1115c7b470bbe1babc9a2414586ff8ffc8f84476e", + "git_blob": "0f59178830599efa2c689627664245f0ac04f959" + } + ], + "repository": "CrocSwap/integer-mult-bounds", + "source_pr": 3935, + "policy": "Exact frozen source bytes; download outside the checkout or supply the same authenticated offline source tree. Never regenerate or modify these predecessors." +} diff --git a/research/crt-address-streams/PROOF.md b/research/crt-address-streams/PROOF.md new file mode 100644 index 0000000000..4c47a578aa --- /dev/null +++ b/research/crt-address-streams/PROOF.md @@ -0,0 +1,175 @@ +# Encoded coefficient-address conversion + +Tekalign (tekuuu), with substantial OpenAI Codex assistance. Apache-2.0. +Base: `20ecf2891d80b5acf18d980b2164fb44ea7faa90`. + +## Statement and exact interface + +Let D≥1, pairwise coprime odd integers s_i≥3, and powers of two t_i>s_i +be given on canonical little-endian Count tapes. Put S=∏s_i, T=∏t_i, +n=log₂T, P_i=∏_{ji vanish modulo s_i; μ_iP_i=1. Forward updates run in descending +i, so lower coordinates are still original. Inverse updates run in ascending +i and subtract the same expression from already recovered lower coordinates. + +On the full padded box, use offset zero whenever a lower control is invalid. +For a valid lower prefix, rotate only [0,s_i), fixing [s_i,t_i). Each step is +a bijection preserving the valid box and its complement, including arbitrary +nonzero payloads on invalid prefixes and target tails. Consequently initially +zero padding stays zero. Cropping checks every discarded bit is zero; it does +not silently throw away nonzero information. + +The offset producer enumerates physical lower prefixes in natural axis order +(the last lower axis varies fastest), using a single packed address tape. +Its integer arithmetic forms Σa_jP_j and multiplies by μ_i before taking its +residue. No metadata cardinality is decoded into a host integer. Modular inverse +uses the binary extended-gcd invariants u≡ax, v≡ay mod m, 0≤x,y>bit)&1 for record in range(B*t) for bit in range(R)] + receipts=[] + for reverse in (False,True): + pool=ScratchPool(64);source=tape(raw);offset=tape() + for flag,amount in zip((1,0,1,0),amounts): + offset.append(flag) + with counts(amount) as (v,):write_count(v,offset) + offset.rewind() + with counts(B,s,t,R) as params: + with sealed_constructors(),OuterScope(pool).installed(): + result=shifted(source,offset,*params,pool.acquire,inverse=reverse) + actual=read(result);expected=[0]*len(raw) + for prefix in range(B): + for target in range(t): + new=((target+(-1 if reverse else 1)*amounts[prefix])%s) if target>j)&1 for k in range(15) for j in range(width)] + payload=tape(raw) + ports=[plan.sources,plan.flags,plan.axis_rows,*(v.tape for v in plan.fields.values()),payload] + before=[image(t) for t in ports] + engine=Converter(FullRouter(literal_native())) + with counts(1) as (rows,): + output=engine.run_plan(payload,plan,rows) + expected=mapped(raw,oracle_map(((3,4),(5,8))),width,1) + need(read(output)==expected,'actual typed tensor bridge') + need([image(t) for t in ports]==before,'all actual plan ports restored') + consume(output);blank_engine(engine) + payload.clear() + return dict(source_records=15,padded_records=32,address_bits=5,record_bits=width, + peak_roles=engine.pool.peak,production=False) + finally: + plan.close();source.clear();flags.clear() + + +def rejection_controls(): + engine=Converter(FullRouter(literal_native()));total=0 + with counts(2,16,1,1) as params: + good_shape=shape_tape(((3,4),(5,8)));good=tape([int(i==1) for i in range(15)]) + def rejected(source,shape,phrase): + nonlocal total + expect_failure(lambda:engine.run(source,shape,*params),phrase) + blank_engine(engine);total+=1 + for axes,phrase in ((((3,4),(3,4)),'coprime'),(((2,4),(5,8)),'odd CRT'), + (((3,6),(5,8)),'power-of-two'),(((5,4),(3,4)),'strict padded')): + shape=shape_tape(axes);rejected(good,shape,phrase);shape.clear() + for raw,phrase in (([0]*14,'complete Boolean'),([0]*16,'complete CRT padding source'), + ([0]*7+[RIGHT]+[0]*7,'complete Boolean')): + payload=tape(raw);rejected(payload,good_shape,phrase);payload.clear() + malformed=tape([0,0,RIGHT]);rejected(good,malformed,'canonical');malformed.clear() + extended=shape_tape(((3,4),(5,8)));extended.clear() + for bit in read(good_shape)+[1]:extended.append(bit) + extended.rewind();rejected(good,extended,'complete CRT source shape');extended.clear() + expect_failure(lambda:engine.run(good,good,*params),'external complete CRT ports');total+=1 + expect_failure(lambda:engine.run(engine.output,good_shape,*params),'external complete CRT ports');total+=1 + # Wrong algebra and wrong axis order are caught by a direct coefficient oracle. + from crt_converter import Converter as Actual + original=Actual.triangular + from crt_offsets import residue as actual_residue + def check_wrong(): + out=None + try: + out=engine.run(good,good_shape,*params) + need(read(out)==mapped(read(good),oracle_map(((3,4),(5,8))),1,1), + 'direct mutant coefficient mismatch') + finally: + if out is not None:consume(out) + with patch('crt_offsets.residue',lambda a,m:Count.literal(0)): + expect_failure(check_wrong,'direct mutant');total+=1 + with patch.object(Actual,'triangular',lambda self,shape,rows,R,inv:original(self,shape,rows,R,not inv)): + expect_failure(check_wrong,'direct mutant');total+=1 + from crt_converter import compile_descriptor as actual_descriptor + with patch('crt_converter.compile_descriptor',lambda s,f,inv:actual_descriptor(s,f,not inv)): + expect_failure(check_wrong);total+=1 + blank_engine(engine) + good_shape.clear();good.clear() + # Nonzero padding is a rejection, not silently discarded information. + pool=ScratchPool(64);shape_source=shape_tape(((3,4),(5,8))) + from crt_padding import padded + with counts(2,1,1) as (D,C,R): + payload=tape([1]*32) + with sealed_constructors(),OuterScope(pool).installed(): + shape=Shape(shape_source,D,pool.acquire) + try: + expect_failure(lambda:padded(payload,shape,C,R,pool.acquire,remove=True),'zero CRT address padding') + total+=1 + finally:shape.close() + pool.empty();payload.clear() + shape_source.clear() + # Dirty prior work is not accepted as an empty workspace. + dirty=Converter(FullRouter(literal_native())) + dirty.pool.tapes[0].write(1) + with counts(2,16,1,1) as params: + source=tape([0]*15);shape=shape_tape(((3,4),(5,8))) + expect_failure(lambda:dirty.run(source,shape,*params),'empty body');total+=1 + source.clear();shape.clear() + return dict(controls=total,peak_roles=engine.pool.peak) diff --git a/research/crt-address-streams/crt_converter.py b/research/crt-address-streams/crt_converter.py new file mode 100644 index 0000000000..eec21ef3e6 --- /dev/null +++ b/research/crt-address-streams/crt_converter.py @@ -0,0 +1,266 @@ +"""Complete CRT coefficient order <-> compact actual tensor order. + +All numerical metadata stays encoded. The full-support field permutation is +executed by the authenticated, unchanged PR3935 FullRouter, never a free oracle. +""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count, positive +from encoded import compare, subtract +from records import Tape +from scratch_pool import ScratchPool +from session import (OuterScope, borrowed_count, borrowed_tape, erase_marker, + initialize_marker, sealed_constructors) +from stream import Counter +from tensor_router import FullRouter +from words import BLANK, RIGHT, need +from crt_descriptor import compile_descriptor +from crt_offsets import compile_offsets +from crt_padding import padded +from crt_shape import Shape, power_two +from crt_shear import shifted +from crt_streams import copy_rows +from encoded_crt import write_count + + +def shape_from_plan(plan, factory): + from tensor_plan import TensorPlan + need(isinstance(plan, TensorPlan), 'actual PR3935 tensor plan') + output = index = remaining = None + try: + output = factory() + index = Count.literal(0) + remaining = Counter(plan.dimensions) + while not remaining.finished: + row = plan.axis(index) + try: + write_count(row.s, output) + write_count(row.t, output) + finally: + row.close() + index.increment() + remaining.decrement() + output.rewind() + result, output = output, None + return result + finally: + if output is not None: + output.clear() + if remaining is not None: + remaining.close() + if index is not None: + index.close() + + +def repeated_offsets(source, rows, factory): + output = counter = None + source.rewind() + try: + output = factory() + counter = Counter(rows) + while not counter.finished: + while source.read() != BLANK: + symbol = source.read() + need(symbol in (0, 1, RIGHT), 'complete serialized CRT offset metadata') + output.append(symbol) + source.step(1) + source.rewind() + counter.decrement() + output.rewind() + result, output = output, None + return result + finally: + source.rewind() + if counter is not None: + counter.close() + if output is not None: + output.clear() + + +class Converter: + def __init__(self, router): + need(isinstance(router, FullRouter), 'actual complete full-support CRT router') + self.router = router + self.pool = ScratchPool(192) + self.scope = OuterScope(self.pool) + self.output = Tape() + self.closed = True + self.current = None + self.last_counts = None + for tape in (*self.pool.tapes, self.output): + erase_marker(tape) + + def idle(self): + self.router.idle() + need(self.router.closed and not self.router.pool.live and self.router.output.read() == BLANK, + 'consumed CRT full-router output') + + def replace(self, output): + need(output is not self.current, 'distinct owned CRT stage output') + self.current.clear() + self.current = output + self.current.rewind() + + def route(self, shape, p, R, rows, inverse): + descriptor = width = returned = following = records = None + try: + descriptor, width = compile_descriptor(shape, self.pool.acquire, inverse) + with self.scope.suspended(): + returned = self.router.run(borrowed_tape(self.current), borrowed_tape(descriptor), + *(borrowed_count(v) for v in (shape.bits, p, shape.bits, width, R, rows))) + following = self.pool.acquire() + records = Count.product(rows, shape.padded_total) + copy_rows(returned, following, records, R) + need(returned.read() == BLANK, 'complete CRT full-router return') + following.rewind() + self.replace(following) + following = None + self.last_counts['global_routes'] += 1 + self.last_counts['router'] = dict(self.router.last_counts) + finally: + for count in (width, records): + if count is not None: + count.close() + for tape in (descriptor, following): + if tape is not None: + tape.clear() + if returned is not None: + returned.clear() + erase_marker(returned) + self.idle() + + def triangular(self, shape, rows, R, inverse): + index = remaining = one = None + try: + one = Count.literal(1) + index = Count.literal(0) if inverse else subtract(shape.dimensions, one) + remaining = Counter(shape.dimensions) + while not remaining.finished: + row = shape.axis(index) + s, t, width, _P, _mu, prior = row + prefix = offsets = all_offsets = before = suffix_bits = tail_bits = following = None + try: + prefix, offsets = compile_offsets(shape, index, self.pool.acquire) + all_offsets = repeated_offsets(offsets, rows, self.pool.acquire) + before = Count.product(rows, prefix) + after_prefix = subtract(shape.bits, prior) + try: + tail_bits = subtract(after_prefix, width) + finally: + after_prefix.close() + tail = power_two(tail_bits) + try: + suffix_bits = Count.product(tail, R) + finally: + tail.close() + following = shifted(self.current, all_offsets, before, s, t, suffix_bits, + self.pool.acquire, inverse) + self.replace(following) + following = None + self.last_counts['triangular_shifts'] += 1 + remaining.decrement() + if not remaining.finished: + if inverse: + index.increment() + else: + next_index = subtract(index, one) + index.close() + index = next_index + finally: + for count in (*row, prefix, before, suffix_bits, tail_bits): + if count is not None: + count.close() + for tape in (offsets, all_offsets, following): + if tape is not None: + tape.clear() + finally: + if remaining is not None: + remaining.close() + for count in (index, one): + if count is not None: + count.close() + + def run(self, payload, source_shape, dimensions, precision, record_bits, rows, + inverse=False, tensor_plan=None): + need(type(inverse) is bool, 'literal complete CRT direction') + source = borrowed_tape(payload) + shape_source = None if source_shape is None else borrowed_tape(source_shape) + parameters = tuple(borrowed_count(v) for v in (dimensions, precision, record_bits, rows)) + dimensions, p, R, C = parameters + forbidden = (*self.pool.tapes, self.output, *self.router.pool.tapes, self.router.output, + *self.router.router.pool.tapes, self.router.router.output) + need(source not in forbidden and (shape_source is None or shape_source not in forbidden) + and source is not shape_source and all(v.tape not in forbidden + and v.tape is not source and v.tape is not shape_source for v in parameters), + 'external complete CRT ports') + need((shape_source is None) == (tensor_plan is not None), 'one CRT shape source contract') + for count in parameters: + positive(count) + need(self.closed and not self.pool.live and self.output.read() == BLANK, 'consume prior CRT output') + self.idle() + for tape in (*self.pool.tapes, self.output): + initialize_marker(tape) + self.closed = False + self.last_counts = dict(global_routes=0, triangular_shifts=0, padding_passes=0) + shape = owned_shape_source = records = None + succeeded = False + try: + with sealed_constructors(), self.scope.installed(): + try: + if tensor_plan is not None: + owned_shape_source = shape_from_plan(tensor_plan, self.pool.acquire) + shape_source = owned_shape_source + shape = Shape(shape_source, dimensions, self.pool.acquire) + if tensor_plan is not None: + need(compare(shape.bits, tensor_plan.address_bits) == 0 + and compare(shape.source_total, tensor_plan.source_product) == 0 + and compare(shape.padded_total, tensor_plan.target_product) == 0, + 'complete actual tensor shape binding') + owned_shape_source.clear() + owned_shape_source = None + self.current = padded(source, shape, C, R, self.pool.acquire, natural=inverse) + self.last_counts['padding_passes'] += 1 + if inverse: + self.triangular(shape, C, R, True) + self.route(shape, p, R, C, True) + else: + self.route(shape, p, R, C, False) + self.triangular(shape, C, R, False) + following = padded(self.current, shape, C, R, self.pool.acquire, remove=True, natural=not inverse) + self.replace(following) + self.last_counts['padding_passes'] += 1 + records = Count.product(C, shape.source_total) + copy_rows(self.current, self.output, records, R) + need(self.current.read() == BLANK, 'complete compact CRT output') + self.idle() + succeeded = True + finally: + if records is not None: + records.close() + if self.current is not None: + self.current.clear() + self.current = None + if owned_shape_source is not None: + owned_shape_source.clear() + if shape is not None: + shape.close() + source.rewind() + if shape_source is not None and owned_shape_source is None and tensor_plan is None: + shape_source.rewind() + for count in parameters: + count.rewind() + self.pool.empty() + finally: + for tape in self.pool.tapes: + erase_marker(tape) + self.output.rewind() + if not succeeded: + self.output.clear() + erase_marker(self.output) + self.closed = True + return self.output + + def run_plan(self, payload, plan, rows, inverse=False, record_bits=None): + return self.run(payload, None, plan.dimensions, plan.p, + plan.record_bits if record_bits is None else record_bits, rows, + inverse=inverse, tensor_plan=plan) diff --git a/research/crt-address-streams/crt_descriptor.py b/research/crt-address-streams/crt_descriptor.py new file mode 100644 index 0000000000..867505afc5 --- /dev/null +++ b/research/crt-address-streams/crt_descriptor.py @@ -0,0 +1,112 @@ +"""Exact fixed-width old-bit/new-bit descriptors for the FullRouter port.""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count, positive +from encoded import subtract +from stream import Counter +from words import RIGHT, need + + +def position_width(value): + positive(value) + one = Count.literal(1) + current = result = None + try: + current = subtract(value, one) + result = Count.literal(0) + while not current.zero(): + current.divide_small(2) + result.increment() + if result.zero(): + result.increment() + answer, result = result, None + return answer + finally: + for count in (one, current, result): + if count is not None: + count.close() + + +def write_fixed(value, output, width): + positive(width) + counter = Counter(width) + value.rewind() + try: + while not counter.finished: + symbol = value.read() + need(symbol in (0, 1, RIGHT), 'bounded router descriptor integer') + output.append(0 if symbol == RIGHT else symbol) + if symbol != RIGHT: + value.step(1) + counter.decrement() + need(value.read() == RIGHT, 'router descriptor position fits') + finally: + value.rewind() + counter.close() + + +def compile_descriptor(shape, factory, inverse=False): + """Colex axes -> natural axes, or its separately sorted inverse map. + + Each axis retains its internal bit order. Unequal-width field reversal + need not be an involution as a permutation of the fixed bit positions. + """ + need(type(inverse) is bool, 'literal CRT descriptor direction') + output = width = one = index = remaining = None + try: + output = factory() + width = position_width(shape.bits) + one = Count.literal(1) + index = subtract(shape.dimensions, one) if inverse else Count.literal(0) + remaining = Counter(shape.dimensions) + while not remaining.finished: + row = shape.axis(index) + _s, _t, bits, _P, _mu, old_base = row + new_base = leading = bit = loop = None + try: + leading = subtract(shape.bits, old_base) + new_base = subtract(leading, bits) + first_base, second_base = (new_base, old_base) if inverse else (old_base, new_base) + bit = Count.literal(0) + loop = Counter(bits) + while not loop.finished: + first = second = None + try: + first = Count(first_base) + second = Count(second_base) + first.add(bit) + second.add(bit) + write_fixed(first, output, width) + write_fixed(second, output, width) + finally: + for count in (first, second): + if count is not None: + count.close() + bit.increment() + loop.decrement() + remaining.decrement() + if not remaining.finished: + if inverse: + following = subtract(index, one) + index.close() + index = following + else: + index.increment() + finally: + if loop is not None: + loop.close() + for count in (*row, new_base, leading, bit): + if count is not None: + count.close() + output.rewind() + result = output, width + output = width = None + return result + finally: + if output is not None: + output.clear() + if remaining is not None: + remaining.close() + for count in (width, one, index): + if count is not None: + count.close() diff --git a/research/crt-address-streams/crt_offsets.py b/research/crt-address-streams/crt_offsets.py new file mode 100644 index 0000000000..efb054733b --- /dev/null +++ b/research/crt-address-streams/crt_offsets.py @@ -0,0 +1,73 @@ +"""Actual encoded original-lower-coordinate offsets for one triangular CRT step.""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count +from encoded import compare +from stream import Counter +from encoded_crt import residue, write_count +from crt_shape import Address, power_two + + +def compile_offsets(shape, index, factory): + """Return B,metadata for all padded prefixes in physical lower-axis order.""" + row = shape.axis(index) + s, _t, _width, _prefix, mu, prior_bits = row + before = address = output = None + try: + before = power_two(prior_bits) + address = Address(prior_bits, factory) + output = factory() + while not address.finished: + value = position = remaining = amount = None + valid = True + try: + value = Count.literal(0) + position = Count.literal(0) + remaining = Counter(index) + address.msb() + while not remaining.finished: + lower = shape.axis(position) + digit = term = None + try: + source_length, _padded, width, prefix, _inverse, _bits = lower + digit = address.field(width, reverse=True) + valid = valid and compare(digit, source_length) < 0 + term = Count.product(digit, prefix) + value.add(term) + finally: + for count in (digit, term, *lower): + if count is not None: + count.close() + position.increment() + remaining.decrement() + address.reset() + if valid: + product = Count.product(value, mu) + try: + amount = residue(product, s) + finally: + product.close() + else: + amount = Count.literal(0) + output.append(int(valid)) + write_count(amount, output) + address.increment() + finally: + if remaining is not None: + remaining.close() + for count in (value, position, amount): + if count is not None: + count.close() + output.rewind() + result = before, output + before = output = None + return result + finally: + for count in row: + count.close() + if before is not None: + before.close() + if address is not None: + address.close() + if output is not None: + output.clear() diff --git a/research/crt-address-streams/crt_padding.py b/research/crt-address-streams/crt_padding.py new file mode 100644 index 0000000000..3bffe14156 --- /dev/null +++ b/research/crt-address-streams/crt_padding.py @@ -0,0 +1,111 @@ +"""Actual source08 colex insertion/deletion with packed-address validity scans.""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count, positive +from encoded import compare, subtract +from stream import Counter +from words import BLANK, need +from crt_streams import copy_record, zero_record +from encoded_crt import read_count, write_count +from crt_shape import Address + + +def reversed_bounds(shape, factory): + output = one = index = remaining = None + try: + output = factory() + one = Count.literal(1) + index = subtract(shape.dimensions, one) + remaining = Counter(shape.dimensions) + while not remaining.finished: + row = shape.axis(index) + try: + write_count(row[0], output) + write_count(row[2], output) + finally: + for count in row: + count.close() + remaining.decrement() + if not remaining.finished: + following = subtract(index, one) + index.close() + index = following + output.rewind() + result, output = output, None + return result + finally: + if output is not None: + output.clear() + if remaining is not None: + remaining.close() + for count in (one, index): + if count is not None: + count.close() + + +def valid_address(shape, address, bounds): + """One O(total address bits) bound scan, including its restoration.""" + address.reset() + bounds.rewind() + dimensions = Counter(shape.dimensions) + valid = True + try: + while not dimensions.finished: + source_length = width = digit = None + try: + source_length = read_count(bounds) + width = read_count(bounds) + digit = address.field(width) + valid = valid and compare(digit, source_length) < 0 + dimensions.decrement() + finally: + for count in (source_length, width, digit): + if count is not None: + count.close() + need(bounds.read() == BLANK, 'complete compact CRT bounds') + return valid + finally: + dimensions.close() + bounds.rewind() + address.reset() + + +def padded(source, shape, rows, record_bits, factory, remove=False, natural=False): + """Compact records <-> padded records in colex or natural axis order.""" + need(type(remove) is bool, 'literal CRT padding direction') + need(type(natural) is bool, 'literal CRT padding axis order') + positive(rows) + positive(record_bits) + output = address = outer = private_bounds = None + source.rewind() + try: + output = factory() + if natural: + private_bounds = reversed_bounds(shape, factory) + bounds = private_bounds if natural else shape.bounds + outer = Counter(rows) + while not outer.finished: + address = Address(shape.bits, factory) + while not address.finished: + if valid_address(shape, address, bounds): + copy_record(source, output, record_bits) + else: + zero_record(source, output, record_bits, remove) + address.increment() + address.close() + address = None + outer.decrement() + need(source.read() == BLANK, 'complete CRT padding source extent') + output.rewind() + result, output = output, None + return result + finally: + source.rewind() + if address is not None: + address.close() + if outer is not None: + outer.close() + if output is not None: + output.clear() + if private_bounds is not None: + private_bounds.clear() diff --git a/research/crt-address-streams/crt_shape.py b/research/crt-address-streams/crt_shape.py new file mode 100644 index 0000000000..23b6cd3fec --- /dev/null +++ b/research/crt-address-streams/crt_shape.py @@ -0,0 +1,210 @@ +"""Packed encoded CRT shape, checked units and fixed-width address control.""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count, positive +from encoded import compare +from stream import Counter +from session import borrowed_tape +from words import BLANK, LEFT, RIGHT, need +from encoded_crt import inverse, read_count, write_count + + +def power_two(exponent): + result = Count.literal(1) + counter = Counter(exponent) + try: + while not counter.finished: + result.multiply_small(2) + counter.decrement() + answer, result = result, None + return answer + finally: + counter.close() + if result is not None: + result.close() + + +def binary_exponent(value): + positive(value) + result = Count.literal(0) + try: + value.msb() + need(value.read() == 1, 'positive power-of-two CRT field') + value.step(-1) + while value.read() != LEFT: + need(value.read() == 0, 'power-of-two CRT field') + result.increment() + value.step(-1) + value.step(1) + value.rewind() + answer, result = result, None + return answer + finally: + if value.read() == LEFT: + value.step(1) + value.rewind() + if result is not None: + result.close() + + +class Shape: + """Six Count fields per axis on one tape; no growing host list of roles.""" + def __init__(self, source, dimensions, factory): + self.dimensions = self.bits = self.source_total = self.padded_total = self.page = self.bounds = None + positive(dimensions) + outer = None + source.rewind() + try: + self.dimensions = Count(dimensions) + self.bits = Count.literal(0) + self.source_total = Count.literal(1) + self.padded_total = Count.literal(1) + self.page = factory() + self.bounds = factory() + outer = Counter(dimensions) + while not outer.finished: + s = t = width = mu = one = None + try: + s = read_count(source) + t = read_count(source) + one = Count.literal(1) + need(s.read() == 1 and compare(s, one) > 0, 'odd CRT source length at least three') + need(compare(s, t) < 0, 'CRT source fits strict padded field') + width = binary_exponent(t) + mu = inverse(self.source_total, s) + for count in (s, t, width, self.source_total, mu, self.bits): + write_count(count, self.page) + write_count(s, self.bounds) + write_count(width, self.bounds) + following = Count.product(self.source_total, s) + self.source_total.close() + self.source_total = following + following = Count.product(self.padded_total, t) + self.padded_total.close() + self.padded_total = following + self.bits.add(width) + outer.decrement() + finally: + for count in (s, t, width, mu, one): + if count is not None: + count.close() + need(source.read() == BLANK, 'complete CRT source shape') + self.page.rewind() + self.bounds.rewind() + except BaseException: + self.close() + raise + finally: + if outer is not None: + outer.close() + source.rewind() + + def axis(self, index): + need(compare(index, self.dimensions) < 0, 'CRT axis index in range') + self.page.rewind() + before = Counter(index) + values = [] + try: + while not before.finished: + for _ in range(6): + read_count(self.page).close() + before.decrement() + for _ in range(6): + values.append(read_count(self.page)) + result, values = tuple(values), [] + return result + finally: + before.close() + for value in values: + value.close() + self.page.rewind() + + def close(self): + if self.page is not None: + self.page.clear() + self.page = None + if self.bounds is not None: + self.bounds.clear() + self.bounds = None + for name in ('dimensions', 'bits', 'source_total', 'padded_total'): + value = getattr(self, name) + if value is not None: + value.close() + setattr(self, name, None) + + +class Address: + """Fixed-width little-endian binary counter; only a carry enters control.""" + def __init__(self, width, factory): + self.tape = None + self.finished = False + counter = Counter(width) + try: + self.tape = factory() + while not counter.finished: + self.tape.append(0) + counter.decrement() + self.tape.append(RIGHT) + self.tape.rewind() + except BaseException: + self.close() + raise + finally: + counter.close() + + def reset(self): + if self.tape.read() == LEFT: + self.tape.step(1) + self.tape.rewind() + + def msb(self): + self.reset() + while self.tape.read() != RIGHT: + self.tape.step(1) + self.tape.step(-1) + + def increment(self): + need(not self.finished, 'live CRT packed address') + self.reset() + while self.tape.read() == 1: + self.tape.write(0) + self.tape.step(1) + if self.tape.read() == RIGHT: + self.finished = True + else: + need(self.tape.read() == 0, 'binary CRT packed address') + self.tape.write(1) + self.reset() + + def field(self, width, reverse=False): + result = Count.literal(0) + counter = Counter(width) + try: + if not reverse: + borrowed_tape(result.tape).clear() + while not counter.finished: + bit = self.tape.read() + need(bit in (0, 1), 'complete CRT address field') + if reverse: + result.multiply_small(2) + if bit: + result.increment() + else: + result.tape.append(bit) + self.tape.step(-1 if reverse else 1) + counter.decrement() + if not reverse: + result.tape.append(RIGHT) + result.normalize() + answer, result = result, None + return answer + finally: + counter.close() + if result is not None: + result.close() + + def close(self): + if self.tape is not None: + self.reset() + self.tape.clear() + self.tape = None diff --git a/research/crt-address-streams/crt_shear.py b/research/crt-address-streams/crt_shear.py new file mode 100644 index 0000000000..229e3a42de --- /dev/null +++ b/research/crt-address-streams/crt_shear.py @@ -0,0 +1,70 @@ +"""Complete variable-offset CRT field rotations with protected padding tails.""" +import crt_sources +crt_sources.activate() +from cardinal_counts import positive +from encoded import compare, subtract +from crt_streams import copy_rows +from stream import Counter +from words import BLANK, need +from encoded_crt import read_offset + + +def shifted(source, offsets, before, modulus, padded, suffix, factory, inverse=False): + """Borrow B*t*S bits and B encoded offsets; return a distinct owned image.""" + need(type(inverse) is bool, 'literal CRT shear direction') + need(source is not offsets, 'separate CRT payload and offsets') + for count in (before, modulus, padded, suffix): + positive(count) + need(compare(modulus, padded) <= 0, 'CRT valid field fits padded field') + tail = left = right = invalid = output = None + try: + tail = subtract(padded, modulus) + left = factory() + right = factory() + invalid = factory() + output = factory() + source.rewind() + offsets.rewind() + for merging in (False, True): + outer = Counter(before) + try: + while not outer.finished: + amount = complement = None + try: + amount = read_offset(offsets, modulus) + complement = subtract(modulus, amount) + first, second = (amount, complement) if inverse else (complement, amount) + if merging: + copy_rows(right, output, second, suffix) + copy_rows(left, output, first, suffix) + copy_rows(invalid, output, tail, suffix) + else: + copy_rows(source, left, first, suffix) + copy_rows(source, right, second, suffix) + copy_rows(source, invalid, tail, suffix) + outer.decrement() + finally: + for count in (amount, complement): + if count is not None: + count.close() + need(offsets.read() == BLANK, 'complete CRT prefix offset stream') + offsets.rewind() + finally: + outer.close() + if not merging: + need(source.read() == BLANK, 'complete padded CRT shear source') + source.rewind() + for tape in (left, right, invalid): + tape.rewind() + need(left.read() == right.read() == invalid.read() == BLANK, 'complete CRT shear buckets') + output.rewind() + result, output = output, None + return result + finally: + source.rewind() + offsets.rewind() + for tape in (left, right, invalid, output): + if tape is not None: + tape.clear() + if tail is not None: + tail.close() diff --git a/research/crt-address-streams/crt_sources.py b/research/crt-address-streams/crt_sources.py new file mode 100644 index 0000000000..d9af49b64d --- /dev/null +++ b/research/crt-address-streams/crt_sources.py @@ -0,0 +1,105 @@ +"""Authenticate and open the actual PR3935 physical tensor/router cohort.""" +from contextlib import contextmanager +from hashlib import sha1, sha256 +import importlib.util +import json +from pathlib import Path +import sys +import tempfile +import urllib.request +HERE = Path(__file__).resolve().parent +ROOT = HERE.parents[1] +_active = None + + +def authenticate(): + for row in json.loads((HERE/'SOURCES.json').read_text())['files']: + if row['usage'] == 'input' and sha256((ROOT/row['path']).read_bytes()).hexdigest() != row['sha256']: + raise ValueError('source changed: '+row['path']) + + +def activate(): + authenticate() + if _active is None: + raise ValueError('authenticated CRT tensor cohort required') + return _active + + +def source_manifest(): + manifest = json.loads((HERE/'NETWORK-SOURCES.json').read_text()) + if manifest['repository'] != 'CrocSwap/integer-mult-bounds' or manifest['head'] != '240597ed313fc2854c0462b23c89dc3bae72aaf5': + raise ValueError('fixed CRT tensor source identity') + expected = {'tensor_sources.py', 'tensor_router.py', 'tensor_fixture.py', 'tensor_plan.py', + 'inputs/chirp-export.tar.gz', 'inputs/expansion-export.tar.gz', 'inputs/headroom-source.tar', 'PROOF.md'} + paths = {row['path'] for row in manifest['files']} + if paths != {'research/physical-gaussian-tensor/'+name for name in expected} or len(paths) != len(manifest['files']): + raise ValueError('exact CRT tensor dependency inventory') + return manifest + + +def check_tree(root, manifest): + root = Path(root) + for row in manifest['files']: + path = root/row['path'] + if not path.is_file() or path.is_symlink() or path.stat().st_size != row['size']: + raise ValueError('complete frozen tensor source: '+row['path']) + body = path.read_bytes() + blob = sha1(b'blob '+str(len(body)).encode()+b'\0'+body).hexdigest() + if sha256(body).hexdigest() != row['sha256'] or blob != row['git_blob']: + raise ValueError('frozen tensor source changed: '+row['path']) + + +def fetch_tree(root, manifest): + for row in manifest['files']: + path = Path(root)/row['path'] + path.parent.mkdir(parents=True, exist_ok=True) + url = 'https://raw.githubusercontent.com/'+manifest['repository']+'/'+manifest['head']+'/'+row['path'] + request = urllib.request.Request(url, headers={'User-Agent': 'integer-mult-crt-source-replay'}) + with urllib.request.urlopen(request, timeout=45) as response: + body = response.read(row['size']+1) + if len(body) != row['size'] or sha256(body).hexdigest() != row['sha256']: + raise ValueError('downloaded tensor source differs: '+row['path']) + path.write_bytes(body) + check_tree(root, manifest) + + +@contextmanager +def opened(source_root=None, fetch=False): + global _active + authenticate() + if _active is not None or 'tensor_sources' in sys.modules or 'cardinal_counts' in sys.modules: + raise ValueError('cold CRT tensor namespace') + if source_root is not None and fetch: + raise ValueError('choose an offline source root or a fresh fetched tree') + manifest = source_manifest() + temporary = None + if source_root is None: + if not fetch: + raise ValueError('supply --source-root or explicitly permit --fetch') + temporary = tempfile.TemporaryDirectory(prefix='crt-tensor-dependency-') + source_root = Path(temporary.name) + source_root = Path(source_root) + old_path = sys.path[:] + module = None + try: + if fetch: + fetch_tree(source_root, manifest) + check_tree(source_root, manifest) + package = source_root/'research/physical-gaussian-tensor' + spec = importlib.util.spec_from_file_location('tensor_sources', package/'tensor_sources.py') + module = importlib.util.module_from_spec(spec) + sys.modules['tensor_sources'] = module + spec.loader.exec_module(module) + with module.opened() as state: + _active = state + sys.path.insert(0, str(package)) + try: + yield state + finally: + _active = None + finally: + sys.path[:] = old_path + if sys.modules.get('tensor_sources') is module: + del sys.modules['tensor_sources'] + if temporary is not None: + temporary.cleanup() diff --git a/research/crt-address-streams/crt_streams.py b/research/crt-address-streams/crt_streams.py new file mode 100644 index 0000000000..61d10d7e99 --- /dev/null +++ b/research/crt-address-streams/crt_streams.py @@ -0,0 +1,42 @@ +"""Owned countdowns on complete opaque Boolean records, including failure exit.""" +import crt_sources +crt_sources.activate() +from stream import Counter +from words import need + + +def copy_record(source, output, width): + counter = Counter(width) + try: + while not counter.finished: + bit = source.read() + need(bit in (0, 1), 'complete Boolean CRT payload record') + output.append(bit) + source.step(1) + counter.decrement() + finally: + counter.close() + + +def copy_rows(source, output, rows, width): + counter = Counter(rows) + try: + while not counter.finished: + copy_record(source, output, width) + counter.decrement() + finally: + counter.close() + + +def zero_record(source, output, width, remove=False): + counter = Counter(width) + try: + while not counter.finished: + if remove: + need(source.read() == 0, 'zero CRT address padding') + source.step(1) + else: + output.append(0) + counter.decrement() + finally: + counter.close() diff --git a/research/crt-address-streams/encoded_crt.py b/research/crt-address-streams/encoded_crt.py new file mode 100644 index 0000000000..054270cd82 --- /dev/null +++ b/research/crt-address-streams/encoded_crt.py @@ -0,0 +1,126 @@ +"""Fixed-role encoded metadata and odd-modulus inverse for the CRT layout. + +Tekalign, with substantial OpenAI Codex assistance. Apache-2.0. +Uses the retained Count algorithms; no unbounded cardinality is decoded. +""" +import crt_sources +crt_sources.activate() +from cardinal_counts import Count, positive +from encoded import compare, divide, subtract +from session import borrowed_tape +from words import BLANK, RIGHT, need + + +def write_count(value, target): + value.rewind() + try: + while value.read() != RIGHT: + bit = value.read() + need(bit in (0, 1), 'binary CRT metadata count') + target.append(bit) + value.step(1) + target.append(RIGHT) + finally: + value.rewind() + + +def read_count(source): + result = Count.literal(0) + first, multiple, last = True, False, 0 + try: + borrowed_tape(result.tape).clear() + while source.read() in (0, 1): + last = source.read() + multiple = not first + first = False + result.tape.append(last) + source.step(1) + need(not first and (not multiple or last == 1) and source.read() == RIGHT, + 'canonical complete CRT metadata count') + source.step(1) + result.tape.append(RIGHT) + result.rewind() + answer, result = result, None + return answer + finally: + if result is not None: + result.close() + + +def residue(value, modulus): + quotient, remainder = divide(value, modulus) + quotient.close() + return remainder + + +def difference(first, second, modulus): + if compare(first, second) >= 0: + return subtract(first, second) + delta = subtract(second, first) + try: + return subtract(modulus, delta) + finally: + delta.close() + + +def inverse(value, modulus): + """Return a^(-1) mod m for odd m>=3, rejecting nonunits.""" + positive(modulus) + need(modulus.read() == 1, 'odd CRT modulus') + u = v = x = y = one = None + try: + one = Count.literal(1) + need(compare(modulus, one) > 0, 'CRT modulus at least three') + u = residue(value, modulus) + v = Count(modulus) + need(not u.zero(), 'coprime CRT prefix') + x = Count.literal(1) + y = Count.literal(0) + while compare(u, one) != 0 and compare(v, one) != 0: + need(compare(u, v) != 0, 'coprime CRT prefix') + while u.read() == 0: + u.divide_small(2) + if x.read() == 1: + x.add(modulus) + need(x.divide_small(2) == 0, 'exact CRT inverse halving') + while v.read() == 0: + v.divide_small(2) + if y.read() == 1: + y.add(modulus) + need(y.divide_small(2) == 0, 'exact CRT inverse halving') + if compare(u, one) == 0 or compare(v, one) == 0: + break + need(compare(u, v) != 0, 'coprime CRT prefix') + if compare(u, v) > 0: + following = subtract(u, v) + u.close() + u = following + following = difference(x, y, modulus) + x.close() + x = following + else: + following = subtract(v, u) + v.close() + v = following + following = difference(y, x, modulus) + y.close() + y = following + return Count(x if compare(u, one) == 0 else y) + finally: + for count in (u, v, x, y, one): + if count is not None: + count.close() + + +def read_offset(source, modulus): + flag = source.read() + need(flag in (0, 1), 'binary CRT prefix validity flag') + source.step(1) + value = read_count(source) + try: + need(compare(value, modulus) < 0 and (flag or value.zero()), 'bounded CRT prefix offset') + result, value = value, None + return result + finally: + if value is not None: + value.close() diff --git a/research/crt-address-streams/verify.py b/research/crt-address-streams/verify.py new file mode 100644 index 0000000000..e69ebb71d4 --- /dev/null +++ b/research/crt-address-streams/verify.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""Cold, source-authenticated finite CRT replay. No Git or working-tree writes.""" +import argparse +import ast +from hashlib import sha256 +import json +from pathlib import Path +import crt_sources + +HERE=Path(__file__).resolve().parent + + +def need(test,message): + if not test: + raise ValueError(message) + + +def static_contract(): + files=sorted(HERE.glob('crt_*.py'))+[HERE/'encoded_crt.py'] + runtime=[p for p in files if p.name!='crt_sources.py'] + for p in runtime: + tree=ast.parse(p.read_text()) + for node in ast.walk(tree): + if isinstance(node,ast.Attribute): + need(node.attr not in ('cells','head','inspect','__dict__'), 'no runtime host state decoding') + if isinstance(node,ast.Call) and isinstance(node.func,ast.Name) and node.func.id=='range': + need(len(node.args)==1 and isinstance(node.args[0],ast.Constant) and node.args[0].value==6, + 'only fixed six-field runtime loop') + return len(runtime) + + +def source_controls(): + import copy + import sys + import tempfile + manifest=crt_sources.source_manifest() + row=next(r for r in manifest['files'] if r['path'].endswith('/tensor_sources.py')) + with tempfile.TemporaryDirectory(prefix='crt-source-controls-') as folder: + target=Path(folder)/row['path'] + target.parent.mkdir(parents=True) + raw=Path(sys.modules['tensor_sources'].__file__).read_bytes() + target.write_bytes(raw) + single={'files':[row]} + crt_sources.check_tree(folder,single) + def rejects(m): + try:crt_sources.check_tree(folder,m) + except ValueError:return + raise ValueError('source-integrity negative control accepted') + target.write_bytes(raw[:-1]);rejects(single) + target.write_bytes(raw[:-1]+bytes([raw[-1]^1]));rejects(single) + target.write_bytes(raw) + wrong=copy.deepcopy(single);wrong['files'][0]['git_blob']='0'*40;rejects(wrong) + target.unlink();rejects(single) + return 4 + + +def main(): + parser=argparse.ArgumentParser(description=__doc__) + source=parser.add_mutually_exclusive_group(required=True) + source.add_argument('--source-root',type=Path) + source.add_argument('--fetch',action='store_true') + parser.add_argument('--write-receipt',type=Path) + args=parser.parse_args() + from check_crt_cost import check + result={'schema':1,'base':'20ecf2891d80b5acf18d980b2164fb44ea7faa90', + 'source_head':'240597ed313fc2854c0462b23c89dc3bae72aaf5', + 'runtime_files':static_contract(),'cost':check()} + with crt_sources.opened(args.source_root,fetch=args.fetch): + import check_crt_runtime as checks + result['source_controls']=source_controls() + for name in ('complete_columns','descriptor_and_prefix','shear_geometry', + 'inverse_units','typed_plan','rejection_controls'): + result[name]=getattr(checks,name)() + # Check the exact offline dependency bytes again after execution. + if args.source_root is not None: + crt_sources.check_tree(args.source_root,crt_sources.source_manifest()) + normalized=json.loads(json.dumps(result)) + if args.write_receipt is not None: + args.write_receipt.write_text(json.dumps(normalized,indent=2,sort_keys=True)+'\n') + else: + need(normalized==json.loads((HERE/'EXPECTED.json').read_text()),'complete deterministic CRT receipt') + print('PASS_CRT_ADDRESS_STREAMS '+json.dumps({'columns':138,'inverse_cases':2176, + 'controls':normalized['rejection_controls']['controls']+normalized['source_controls'],'wrapper_peak':39},sort_keys=True)) + + +if __name__=='__main__': + main() From ed829156dc9735c717840fbfb9c458370e3aaf5f Mon Sep 17 00:00:00 2001 From: Tekalign <152792726+tekuuu@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:38:06 +0300 Subject: [PATCH 2/5] Isolate pinned imports and fetch large sources by checked ranges --- research/crt-address-streams/README.md | 7 +- research/crt-address-streams/crt_sources.py | 72 ++++++++++++++++----- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/research/crt-address-streams/README.md b/research/crt-address-streams/README.md index dfc952ac0c..e686c57bb9 100644 --- a/research/crt-address-streams/README.md +++ b/research/crt-address-streams/README.md @@ -35,7 +35,9 @@ predecessor sources** into temporary storage. Every size, SHA256 and Git blob is checked; no GitHub credentials are needed. Network availability is required for that command. The exact files and source commit are in [NETWORK-SOURCES.json](NETWORK-SOURCES.json). This avoids duplicating large -frozen predecessor archives in the PR. The predecessor loader independently +frozen predecessor archives in the PR. Only declared source files enter a private import tree, with a fresh bytecode +namespace; unpinned siblings and cached bytecode in an offline tree are ignored. +The predecessor loader independently checks the complete embedded exports, receipts and source-specific notices. For an already downloaded, authenticated tree with paths matching that manifest: @@ -82,7 +84,8 @@ Prepared by Tekalign (tekuuu), with substantial OpenAI Codex assistance in desig code, proof and verification. New work is Apache-2.0. CRT algebra follows Agarwal and Cooley and the retained source08 construction; the full router and physical tensor interface are Jose Manuel (`jmbarrancoml`)'s PR3935, building on -PR3619 and the predecessors pinned within its complete exports. Those sources, +PR3619 and the predecessors pinned within its complete exports, including the original +OpenAI manuscript, RaD/hipotures reports and Douglas Colkitt's retained work. Those sources, licenses and notices are unchanged. This contribution adds the actual encoded layout composition and full conditional cost/backing argument; it does not claim the predecessor's algebra or routing theorem as new work. diff --git a/research/crt-address-streams/crt_sources.py b/research/crt-address-streams/crt_sources.py index d9af49b64d..5fd090054f 100644 --- a/research/crt-address-streams/crt_sources.py +++ b/research/crt-address-streams/crt_sources.py @@ -1,12 +1,16 @@ """Authenticate and open the actual PR3935 physical tensor/router cohort.""" from contextlib import contextmanager +import base64 from hashlib import sha1, sha256 import importlib.util +import http.client import json +import shutil from pathlib import Path import sys import tempfile import urllib.request +import urllib.error HERE = Path(__file__).resolve().parent ROOT = HERE.parents[1] _active = None @@ -53,10 +57,37 @@ def fetch_tree(root, manifest): for row in manifest['files']: path = Path(root)/row['path'] path.parent.mkdir(parents=True, exist_ok=True) - url = 'https://raw.githubusercontent.com/'+manifest['repository']+'/'+manifest['head']+'/'+row['path'] - request = urllib.request.Request(url, headers={'User-Agent': 'integer-mult-crt-source-replay'}) - with urllib.request.urlopen(request, timeout=45) as response: - body = response.read(row['size']+1) + headers={'User-Agent':'integer-mult-crt-source-replay'} + if row['size'] > 1048576: + # Bounded public byte ranges avoid partially delivered large raw + # or base64 responses. Every range and the complete blob are checked. + pieces=[] + for start in range(0,row['size'],1048576): + end=min(row['size'],start+1048576)-1 + url=('https://raw.githubusercontent.com/'+manifest['repository']+'/'+manifest['head']+'/'+row['path'] + +'?crt-bytes='+str(start)+'-'+str(end)) + request=urllib.request.Request(url,headers={**headers,'Range':'bytes='+str(start)+'-'+str(end)}) + for attempt in range(2): + try: + with urllib.request.urlopen(request,timeout=20) as response: + if response.status!=206 or response.headers.get('Content-Range')!='bytes '+str(start)+'-'+str(end)+'/'+str(row['size']): + raise ValueError('exact frozen tensor byte range') + part=response.read(end-start+2) + if len(part)!=end-start+1: + raise ValueError('complete frozen tensor byte range') + pieces.append(part) + break + except (urllib.error.URLError,TimeoutError,http.client.IncompleteRead): + if attempt:raise + body=b''.join(pieces) + else: + blob_url='https://api.github.com/repos/'+manifest['repository']+'/git/blobs/'+row['git_blob'] + with urllib.request.urlopen(urllib.request.Request(blob_url,headers=headers),timeout=30) as response: + encoded=response.read(2*row['size']+16384) + item=json.loads(encoded) + if item.get('sha')!=row['git_blob'] or item.get('size')!=row['size'] or item.get('encoding')!='base64': + raise ValueError('exact public tensor blob metadata') + body=base64.b64decode(''.join(item['content'].split()),validate=True) if len(body) != row['size'] or sha256(body).hexdigest() != row['sha256']: raise ValueError('downloaded tensor source differs: '+row['path']) path.write_bytes(body) @@ -72,34 +103,43 @@ def opened(source_root=None, fetch=False): if source_root is not None and fetch: raise ValueError('choose an offline source root or a fresh fetched tree') manifest = source_manifest() - temporary = None - if source_root is None: - if not fetch: - raise ValueError('supply --source-root or explicitly permit --fetch') - temporary = tempfile.TemporaryDirectory(prefix='crt-tensor-dependency-') - source_root = Path(temporary.name) - source_root = Path(source_root) + if source_root is None and not fetch: + raise ValueError('supply --source-root or explicitly permit --fetch') + temporary = tempfile.TemporaryDirectory(prefix='crt-tensor-dependency-') + private_root = Path(temporary.name)/'sources' old_path = sys.path[:] + old_cache, old_write = sys.pycache_prefix, sys.dont_write_bytecode module = None try: if fetch: - fetch_tree(source_root, manifest) - check_tree(source_root, manifest) - package = source_root/'research/physical-gaussian-tensor' + fetch_tree(private_root, manifest) + else: + check_tree(source_root, manifest) + # Only declared source bytes enter sys.path. Ignore unpinned sibling + # modules/caches in an offline cache; never modify that cache. + for row in manifest['files']: + target=private_root/row['path'] + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(Path(source_root)/row['path'], target) + check_tree(private_root, manifest) + sys.pycache_prefix = str(Path(temporary.name)/'empty-bytecode') + sys.dont_write_bytecode = True + package = private_root/'research/physical-gaussian-tensor' spec = importlib.util.spec_from_file_location('tensor_sources', package/'tensor_sources.py') module = importlib.util.module_from_spec(spec) sys.modules['tensor_sources'] = module spec.loader.exec_module(module) with module.opened() as state: _active = state - sys.path.insert(0, str(package)) + sys.path[:0] = [str(HERE), str(package)] try: yield state finally: _active = None finally: sys.path[:] = old_path - if sys.modules.get('tensor_sources') is module: + sys.pycache_prefix, sys.dont_write_bytecode = old_cache, old_write + if module is not None and sys.modules.get('tensor_sources') is module: del sys.modules['tensor_sources'] if temporary is not None: temporary.cleanup() From bfa957212107a9c772ce9ca3f96dade8efdbe988 Mon Sep 17 00:00:00 2001 From: Tekalign <152792726+tekuuu@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:45:26 +0300 Subject: [PATCH 3/5] Bound source download latency with four checked range streams --- research/crt-address-streams/crt_sources.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/research/crt-address-streams/crt_sources.py b/research/crt-address-streams/crt_sources.py index 5fd090054f..fa0effe0bc 100644 --- a/research/crt-address-streams/crt_sources.py +++ b/research/crt-address-streams/crt_sources.py @@ -1,5 +1,6 @@ """Authenticate and open the actual PR3935 physical tensor/router cohort.""" from contextlib import contextmanager +from concurrent.futures import ThreadPoolExecutor import base64 from hashlib import sha1, sha256 import importlib.util @@ -57,12 +58,12 @@ def fetch_tree(root, manifest): for row in manifest['files']: path = Path(root)/row['path'] path.parent.mkdir(parents=True, exist_ok=True) + print('FETCH_PINNED_TENSOR '+row['path'],flush=True) headers={'User-Agent':'integer-mult-crt-source-replay'} if row['size'] > 1048576: # Bounded public byte ranges avoid partially delivered large raw # or base64 responses. Every range and the complete blob are checked. - pieces=[] - for start in range(0,row['size'],1048576): + def part_at(start): end=min(row['size'],start+1048576)-1 url=('https://raw.githubusercontent.com/'+manifest['repository']+'/'+manifest['head']+'/'+row['path'] +'?crt-bytes='+str(start)+'-'+str(end)) @@ -72,14 +73,16 @@ def fetch_tree(root, manifest): with urllib.request.urlopen(request,timeout=20) as response: if response.status!=206 or response.headers.get('Content-Range')!='bytes '+str(start)+'-'+str(end)+'/'+str(row['size']): raise ValueError('exact frozen tensor byte range') - part=response.read(end-start+2) + part=response.read(end-start+1) if len(part)!=end-start+1: raise ValueError('complete frozen tensor byte range') - pieces.append(part) - break + return part except (urllib.error.URLError,TimeoutError,http.client.IncompleteRead): if attempt:raise - body=b''.join(pieces) + # Four bounded, independent ranges preserve order and exact pins. + # Await every worker before publishing any source file. + with ThreadPoolExecutor(max_workers=4) as executor: + body=b''.join(executor.map(part_at,range(0,row['size'],1048576))) else: blob_url='https://api.github.com/repos/'+manifest['repository']+'/git/blobs/'+row['git_blob'] with urllib.request.urlopen(urllib.request.Request(blob_url,headers=headers),timeout=30) as response: From a6e8c7c4a0e9cba6c82508bdea9f666f51f28c3a Mon Sep 17 00:00:00 2001 From: Tekalign <152792726+tekuuu@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:48:23 +0300 Subject: [PATCH 4/5] Record scoped CRT replay, source setup bill and transport limitation --- research/crt-address-streams/PROOF.md | 9 ++++-- research/crt-address-streams/VALIDATION.md | 37 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 research/crt-address-streams/VALIDATION.md diff --git a/research/crt-address-streams/PROOF.md b/research/crt-address-streams/PROOF.md index 4c47a578aa..479a2b727b 100644 --- a/research/crt-address-streams/PROOF.md +++ b/research/crt-address-streams/PROOF.md @@ -94,11 +94,14 @@ Write V=CTR and r_min=min t_i. With n=Θ(p), R=Θ(p), the complete conditional invoice per conversion is O(V p^β log^(1+τ)p + D V + CT(n+R) - + C D T poly(n)/r_min + poly(p)) + fixed native compilation F_q, + + C D T poly(n)/r_min + poly(p)) + fixed native compilation F_q + source setup F_src, where τ=332353/332929 and β=41544341/41616125. The polynomial degrees are fixed, independent of D,C and the input values. F_q is charged once to a fixed compiled -native instance; it is not silently reconstructed at each conversion. +native instance; it is not silently reconstructed at each conversion. F_src +pays authentication, copying and extraction of the fixed pinned local source +artifacts once per cold cohort, with their fixed storage retained separately. +Network transport is a replay prerequisite, not a numerical tape operation. The first term is the full-support PR3935 router theorem at the exact source pin above (PROOF.md, “four canonical gathers”). It includes factorization into @@ -156,7 +159,7 @@ The child has its own fixed inventories, retained from its authenticated source. sequence of calls, each fixed wrapper role therefore retains its largest past extent. Wrapper *backing* is O(max_history(V+n²)), not merely current live space. Add the inherited child high-water backing (including polynomial planning) and -fixed native storage F_q separately. This is not a quantified production memory +fixed native storage and fixed source/extraction storage separately. This is not a quantified production memory coefficient or a claim that all retained native backing is O(current V). ## Verification scope and unresolved work diff --git a/research/crt-address-streams/VALIDATION.md b/research/crt-address-streams/VALIDATION.md new file mode 100644 index 0000000000..91f0f31c0d --- /dev/null +++ b/research/crt-address-streams/VALIDATION.md @@ -0,0 +1,37 @@ +# Author replay receipts + +Base: `20ecf2891d80b5acf18d980b2164fb44ea7faa90`. +Scientific code freeze: `bfa957212107a9c772ce9ca3f96dade8efdbe988`. +External cohort: PR3935, `240597ed313fc2854c0462b23c89dc3bae72aaf5`; +all eight files have size, SHA256 and Git blob pins in NETWORK-SOURCES.json. +Python: CPython 3.12.14, Linux. No scientific input or predecessor was changed. + +Passed the direct cold replay using the authenticated offline source tree: + +```sh +python3.12 -B research/crt-address-streams/verify.py --source-root /path/to/dependencies +``` + +Result: `PASS_CRT_ADDRESS_STREAMS {"columns": 138, "controls": 20, +"inverse_cases": 2176, "wrapper_peak": 39}`. EXPECTED.json is the complete +replay receipt, including the actual typed TensorPlan and finite domains. + +The committed scientific export passed the repository's export integrity check. +The same replay passed with `-O -B`, an empty executable PATH, no Git repository, +a read-only package/dependency tree, and a deliberate unpinned `crt_shape.py` +import-shadow trap in the dependency tree. All 33 frozen files, including the +trap and unrelated cached predecessor bytecode, were unchanged afterward. +This checks the source isolation as well as the finite numerical obligations. + +The ready registered command uses `--fetch` and independently authenticates the +same source bytes. Fresh local network attempts encountered a truncated large +response and TLS handshake timeouts before completing the source download. +They did **not** reach or fail a mathematical check. The bounded range downloader +retains exact per-range checks, complete hashes and failure propagation. Hosted +CI transport/replay status must be read separately; it is not asserted here. +An authenticated offline source-tree replay is supported for this reason. + +The metadata/pin check passed. No repository-wide historical suite was needed: +this isolated package changes neither shared runtime nor selected construction. +Finite certificates and the scoped argument do not prove the inherited universal +router/native premises or the complete integer multiplication theorem. From e99297be6749518a1b5b85cd129ecaf204d26c63 Mon Sep 17 00:00:00 2001 From: Tekalign <152792726+tekuuu@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:49:53 +0300 Subject: [PATCH 5/5] Credit concurrent tensor interface clarification --- research/crt-address-streams/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/research/crt-address-streams/README.md b/research/crt-address-streams/README.md index e686c57bb9..63648c961b 100644 --- a/research/crt-address-streams/README.md +++ b/research/crt-address-streams/README.md @@ -89,3 +89,8 @@ OpenAI manuscript, RaD/hipotures reports and Douglas Colkitt's retained work. Th licenses and notices are unchanged. This contribution adds the actual encoded layout composition and full conditional cost/backing argument; it does not claim the predecessor's algebra or routing theorem as new work. + +Jose Manuel's concurrent [interface clarification](https://github.com/CrocSwap/integer-mult-bounds/discussions/3664#discussioncomment-19036660) +confirms the direction convention, actual scalar-axis notation and the two final +S factors for the eventual numerical sandwich. Its prospective recovery work is +separate; this package pins the published tensor head above.