Mixed-size batched Montgomery modular exponentiation with CRT-style split items
/app/cpu/main.cpp is the source of a multicore CPU program (C++17, OpenMP).
The program takes one input-file path (PROGRAM INPUT_FILE) and writes its result to stdout.
What the program computes
A batch of M independent multi-precision modular exponentiations whose moduli have mixed
sizes (128 to 4096 bits, multiples of 128). Every item's data come from the counter hash in the
CPU source: key_i = mix64(SEED ^ mix64(i + 0x5851F42D4C957F2D)) and
word(key, s, j) = mix64(key + (s << 40) + j) (all arithmetic mod 2^64; mix64 is the
SplitMix64 finaliser). Numbers are little-endian arrays of 64-bit limbs built from word.
For item i:
- Class
k: the smallestkwithword(key,0,0) mod W < wcum_k(cumulative class weights,W= their total). The item hasb = BITS_kbits andL = b/64limbs. - Kind:
t = word(key,0,1) mod 1000; split ift < SPLIT, else near-power ift < SPLIT + NEARPOW, else plain. - Exponent length
E: ifword(key,0,2) mod 1000 < SMALLEXPthenE = word(key,0,3) mod 33(0..32, where 0 means the exponent is 0), otherwiseE = b(plain, near-power) orE = b/2(split). An exponent "of lengthEfrom streams" is the limbsword(key,s,j)truncated toEbits with bitE-1set. - Plain: modulus
N= limbsword(key,1,j),j < L, with the top bit and bit 0 forced to 1. Near-power:N = 2^b - cwithc = (word(key,1,0) mod 2^32) | 1. Basex= limbsword(key,2,j)(bbits, may exceedN), exponent of lengthEfrom stream 3. Resultr = x^e mod N. - Split (CRT-style,
H = L/2limbs, half sizeb/2):p= limbsword(key,1,j),j < H, top bit and bit 0 set. For attempta = 0, 1, 2, ...,q= limbsword(key,8+a,j)with top bit and bit 0 set; the firstqwithq mod p != 0andgcd(q mod p, p) = 1is kept. Basec= limbsword(key,2,j),j < L(bbits). Exponentsdp(stream 3) anddq(stream 4), both of lengthE. Thenm1 = c^dp mod p,m2 = c^dq mod q,h = (q mod p)^(-1) * (m1 - m2) mod p, andr = m2 + h*q(so0 <= r < p*q).pandqare not primes; this is simply the defined recombination.
x^0 = 1 for every modulus. The CPU uses CIOS Montgomery multiplication with 64-bit limbs and
fixed 4-bit exponent windows, but any method producing the same exact integers is fine
(other limb sizes, other windowing, several threads per number, ...).
Input format
SEED M K SPLIT NEARPOW SMALLEXP
BITS_1 WEIGHT_1
...
BITS_K WEIGHT_K
SEED unsigned 64-bit, 0 <= M, 1 <= K <= 32, 128 <= BITS_k <= 4096 with BITS_k mod 128 = 0,
WEIGHT_k >= 1, permille values SPLIT + NEARPOW <= 1000, 0 <= SMALLEXP <= 1000.
Output format
items M classes K
class k bits BITS_k count C split S nearpow P hash H xorlow X (one line per class)
first k item i ebits E r 0x... (first non-split item of class k, if any)
firstsplit k item i ebits E r 0x... (first split item of class k, if any)
total T
r is printed as L zero-padded 16-digit hex limbs, most significant first. With
h_i = fold_j mix64(h ^ r_j ^ (j << 56)) starting from h = mix64(i ^ 0xD1B54A32D192ED03)
(the result_hash function), H is the sum of h_i over the class mod 2^64, X the XOR of the
low limbs r_0, and T = sum_i h_i * (2i+1) mod 2^64. Hex fields are 16 digits. The first
and firstsplit lines come in class order, first before firstsplit.
Hidden inputs
Correctness inputs (about 12): an empty batch, a single 128-bit item, three full-size 4096-bit items, batches of 20 to 300 items over random subsets of sizes 128..4096, all-split batches, all-near-power batches, all-short-exponent batches, and mixed batches. The timing input has 28,000 to 34,000 items over the four classes 1024, 2048, 3072 and 4096 bits (weights 3-5, 3-5, 1-3 and 1-3, in random order), with 20-30% split items, 5-10% near-power moduli and 3-6% short exponents.
Your task
Port this program to CUDA in /app/gpu/main.cu: same command line (PROGRAM INPUT_FILE),
same output on stdout. Then make it as fast as you can on the GPU while keeping the output correct.
The main computation must execute on the GPU. Renaming CPU code to .cu, running a
CPU fallback, or launching a dummy kernel while computing the answer on the CPU does not satisfy this task.
The CPU source is the specification: when this text and the code disagree, the code wins.
This sandbox is for writing the source only: it has g++ and the CPU program, but no CUDA
compiler and no GPU. Compilation, execution and verification happen later on an H100 in a
separate verifier, and a port that does not compile is rejected, so check your logic against
the CPU program before you finish (for example by emulating your kernels on the host).
How your port is verified
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -O3 -arch=sm_90 -std=c++17 -o port /app/gpu/main.cu; no extra libraries are linked. The CUDA runtime and the header-only libraries that ship with the CUDA toolkit (CUB, Thrust, libcu++) are allowed. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: 28,000 to 34,000 exponentiations: moduli of 1024, 2048, 3072 and 4096 bits (the big classes dominate the work), 20-30% CRT-style split items, 5-10% near-power moduli and 3-6% short exponents.
/app/inputs/timing_example.inhas the shape and size of the hidden timing input. Your port is timed against a reference GPU port on it; the faster your port, the higher its score. /app/inputs/holds public input examples for understanding the input format.