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 = 0x05177B8A2A0FD6A4FF55CDA06B0924E125F86CAD9B
A = 0x043182D283FCE3880730C9A2FDD3F6016529A166AF
B = 0x020C61E9459E53D8871BCAADC2DFC8AD5225228035
ORDER = 0x05177B8A2A0FD6A4FF55CCA7B8A1E21C88BD53B2C1
BASE = (0x0017E7012277E1B4E43F7BF74657E8BE08BACA175B, 0x00AA03A0A82690704697E8C504CB135B2B6EEF3C83)
TARGET = (0x01DC1E9A482085B3DFA722EB7A541D50505ED31DCA, 0x012D71ECC1578BFBE203D0C2CE238EB6060ADCAA1E)
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()