"""Certify injectivity on one GL3 highest-weight space of the diagonal decic map.""" import argparse import importlib from math import comb from pathlib import Path import sys import tempfile import numpy as np import torch import triton import triton.language as tl sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from ternary_rank_gpu import PRIME, PANEL, centered, certify_samples binary = importlib.import_module('ternary-primitives') DEGREE = 10 SCENARIOS = 13 # A split evaluation and twelve evaluations for an eleventh difference. def gpu(array): return torch.from_numpy(np.ascontiguousarray(array)).cuda() def pad(n): return (n+31)//32*32 def highest_dimension(defect, third): counts = np.zeros((DEGREE+1,defect+1,third+1),dtype=np.int64) counts[0,0,0] = 1 for d in [0,*range(2,min(DEGREE,defect)+1)]: for j in range(min(d,third)+1): for a in range(1,DEGREE+1): counts[a,d:,j:] += counts[a-1,:defect+1-d,:third+1-j] weights = counts[DEGREE,defect] return int(weights[third] - (weights[third-1] if third else 0)) def highest_blocks(defect, third): terms = [(d,j) for d in range(DEGREE+1) for j in range(d+1)] positions = {term: i for i, term in enumerate(terms)} blocks = [] dimension = 0 for degrees in binary.partitions(defect, DEGREE, DEGREE): if 1 in degrees: continue monomials = binary.profiles(degrees, third) if not monomials: continue basis = binary.primitive_basis(degrees, third, monomials) rows, cols = basis.shape if not cols: continue ids = np.array([[positions[d,j] for d,j in zip(degrees, mon)] for mon in monomials], dtype=np.int32).T matrix = np.zeros((pad(rows),pad(cols)), dtype=np.int8) matrix[:rows,:cols] = basis if pad(rows)*(PRIME//2)**2 >= 2**31: raise ArithmeticError('primitive block exceeds the int32 dot-product bound') blocks.append((gpu(ids), gpu(matrix.T).T, rows, cols, dimension)) dimension += cols if dimension != highest_dimension(defect,third): raise ArithmeticError('highest-space dimension disagrees with the weight character') return terms, blocks, dimension def split_coefficients(terms, rows, rng): factors = rng.integers(PRIME, size=(DEGREE,2,rows,1,1), dtype=np.int32) factors[-1] = -factors[:-1].sum(axis=0) % PRIME poly = np.zeros((rows,DEGREE+1,DEGREE+1), dtype=np.int32) poly[:,0,0] = 1 for u,v in factors: old = poly.copy() poly[:,1:,:] += u*old[:,:-1,:] poly[:,:,1:] += v*old[:,:,:-1] poly %= PRIME return np.stack([poly[:,d-j,j] for d,j in terms], axis=1) def sample_coefficients(terms, rows, rng): coeff = np.zeros((rows,len(terms),16), dtype=np.uint8) coeff[:,:,0] = split_coefficients(terms,rows,rng) form = rng.integers(PRIME,size=(rows,len(terms)),dtype=np.int32) start = rng.integers(PRIME,size=rows,dtype=np.int32) for step in range(DEGREE+2): s = (start+step) % PRIME z = -pow(DEGREE,-1,PRIME)*s % PRIME powers = [np.ones(rows,dtype=np.int32)] for _ in range(DEGREE): powers.append(powers[-1]*z % PRIME) coeff[:,0,step+1] = 1 for ix,(d,j) in enumerate(terms): if d < 2: continue value = np.zeros(rows,dtype=np.int32) for i in range(max(2,j),d+1): value += comb(DEGREE-i,d-i)*powers[d-i]*form[:,i*(i+1)//2+j] value = s*(value % PRIME) if j == 0: value += comb(DEGREE,d)*powers[d] + comb(DEGREE-1,d-1)*s*powers[d-1] coeff[:,ix,step+1] = value % PRIME return coeff @triton.jit def evaluate_differences(out, ids, coeff, weights, n, stride, nterms: tl.constexpr, degree: tl.constexpr, scenarios: tl.constexpr, prime: tl.constexpr, tile: tl.constexpr): row = tl.program_id(1).to(tl.int64) col = tl.program_id(0)*tile + tl.arange(0,tile) scenario = tl.arange(0,16) value = tl.full((tile,16),1,tl.int32) for i in tl.static_range(degree): key = tl.load(ids+i*n+col, mask=colprime//2, residue-prime, residue), mask=col