Read the checking program (131 lines of Python)
"""Check a factorisation of the fixed RSA challenge modulus and prove both factors prime."""
import json
import sys
from math import gcd
MAX_BYTES = 32768
MAX_DIGITS = 309
MODULUS = int(
"135066410865995223349603216278805969938881475605667027524485143851526510"
"604859533833940287150571909441798207282164471551373680419703964191743046"
"496589274256239341020864383202110372958725762358509643110564073501508187"
"510676594629205563685529475213500852879416377328533906109750544334999811"
"150056977236890927563"
)
UINT64 = 1 << 64
MAX_WITNESS_CHECKS = 256
def decimal(token, digits=MAX_DIGITS):
if len(token) > digits or not token.isdigit():
raise ValueError("Use unsigned decimal integers within the digit limit.")
return int(token)
def is_prime64(n):
"""These seven Miller–Rabin bases are exhaustive below 2**64."""
if n < 2 or n >= UINT64:
return False
if n % 2 == 0:
return n == 2
odd = n - 1
twos = (odd & -odd).bit_length() - 1
odd >>= twos
for base in (2, 325, 9375, 28178, 450775, 9780504, 1795265022):
base %= n
if base == 0:
continue
residue = pow(base, odd, n)
if residue in (1, n - 1):
continue
for _ in range(twos - 1):
residue = residue * residue % n
if residue == n - 1:
break
else:
return False
return True
def certified_primes(lines, largest):
"""Check Pocklington proofs in increasing order of prime candidate."""
proven = set()
previous = UINT64 - 1
checks = 0
for line in lines:
tokens = line.split()
if len(tokens) < 3:
raise ValueError("A prime proof needs a candidate, a witness and prime powers.")
checks += len(tokens) - 1
if checks > MAX_WITNESS_CHECKS:
raise ValueError("The submission exceeds the prime proof work limit.")
n, witness = map(decimal, tokens[:2])
if not previous < n <= largest or not 1 < witness < n or n % 2 == 0:
raise ValueError("A prime proof candidate or witness is out of range.")
previous = n
# Peel off complete prime powers of n−1. The remaining cofactor may
# be unknown. Every prime divisor p of n must have F dividing p−1.
remaining = n - 1
distinct = set()
for token in tokens[2:]:
parts = token.split(b"^", 1)
if len(parts) != 2:
raise ValueError("Write each prime power as q^e.")
q, e = decimal(parts[0]), decimal(parts[1], 4)
if q in distinct or not (q in proven or is_prime64(q)):
raise ValueError("Each prime power needs a distinct, already proven prime.")
if e < 1 or (q.bit_length() - 1) * e > n.bit_length():
raise ValueError("A prime power exponent is out of range.")
power = q ** e
remaining, rest = divmod(remaining, power)
if rest or remaining % q == 0:
raise ValueError("Each prime power must divide the candidate minus one exactly.")
distinct.add(q)
factored_part = (n - 1) // remaining
if factored_part * factored_part <= n:
raise ValueError("The proven prime powers must multiply to more than the square root.")
if pow(witness, n - 1, n) != 1 or any(
gcd(pow(witness, (n - 1) // q, n) - 1, n) != 1 for q in distinct
):
raise ValueError("The prime proof witness fails Pocklington's congruences.")
proven.add(n)
return proven
def verify(data, modulus=MODULUS):
lines = data.strip().splitlines()
factors = lines[0].split() if lines else []
if len(factors) != 2:
raise ValueError("The first line must give exactly two prime factors.")
p, q = map(decimal, factors)
if not (1 < p < modulus and 1 < q < modulus):
raise ValueError("Both factors must be strictly between one and the modulus.")
if p * q != modulus:
raise ValueError("The product does not equal the challenge modulus.")
proven = certified_primes(lines[1:], max(p, q))
if any(n not in proven and not is_prime64(n) for n in (p, q)):
raise ValueError("Both factors must be prime and large factors need prime proofs.")
return 0
def main():
data = sys.stdin.buffer.read(MAX_BYTES + 1)
try:
if len(data) > MAX_BYTES:
raise ValueError("The submission exceeds the byte limit.")
if not data.isascii() or any(c < 32 and c not in (9, 10, 13) for c in data):
raise ValueError("Use plain ASCII text.")
score = verify(data)
except ValueError as error:
result = {"valid": False, "score": None, "message": str(error)}
else:
result = {"valid": True, "score": score, "message": "Both proven primes multiply to the challenge modulus."}
print(json.dumps(result))
if __name__ == "__main__":
main()