Read the checking program (74 lines of Python)
"""Check a submitted scalar against the fixed public Certicom challenge points."""
import json
import sys
MAX_BYTES = 128
FIELD = 0x048E1D43F293469E33194C43186B3ABC0B
A = 0x041CB121CE2B31F608A76FC8F23D73CB66
B = 0x02F74F717E8DEC90991E5EA9B2FF03DA58
ORDER = 0x048E1D43F293469E317F7ED728F6B8E6F1
BASE = (0x03DF84A96B5688EF574FA91A32E197198A, 0x014721161917A44FB7B4626F36F0942E71)
TARGET = (0x03AA6F004FC62E2DA1ED0BFB62C3FFB568, 0x009C21C284BA8A445BB2701BF55E3A67ED)
def add(left, right, prime, a):
"""Use affine point addition over the prime field. None denotes infinity."""
if left is None:
return right
if right is None:
return left
x, y = left
u, v = right
if x == u:
if (y + v) % prime == 0:
return None
numerator, denominator = 3 * x * x + a, 2 * y
else:
numerator, denominator = v - y, u - x
slope = numerator * pow(denominator, -1, prime) % prime
x_out = (slope * slope - x - u) % prime
return x_out, (slope * (x - x_out) - y) % prime
def multiply(scalar, point, prime, a):
"""Evaluate a specified multiple using double-and-add."""
result = None
for bit in bin(scalar)[2:]:
result = add(result, result, prime, a)
if bit == "1":
result = add(result, point, prime, a)
return result
def verify(data, curve=(FIELD, A, ORDER, BASE, TARGET)):
prime, a, order, base, target = curve
token = data.strip()
if not token.isdigit() or len(token) > len(str(order)):
raise ValueError("Give one decimal scalar within the digit limit.")
scalar = int(token)
if not 1 <= scalar < order:
raise ValueError("The scalar is outside the allowed range.")
if multiply(scalar, base, prime, a) != target:
raise ValueError("The scalar does not map the base point to the target point.")
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": "The scalar maps the fixed base point to the target point."}
print(json.dumps(result))
if __name__ == "__main__":
main()