Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/tls/test/ec/gen_p256.py # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / tls / test / ec / gen_p256.py` 103 lines ยท 4120 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # SPDX-License-Identifier: Apache-2.0 3 # Writes the vectors for the P-256 p2_* section of om/tls/ec.om (p256_driver.om format) to stdout. 4 # Test oracle only: Python integers and textbook affine arithmetic, no code shared with ec.om. 5 # Usage: gen_vectors.py [N_RANDOM] [SEED] 6 import random 7 import sys 8 9 P = 2**256 - 2**224 + 2**192 + 2**96 - 1 10 N = 0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551 11 B = 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B 12 G = (0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296, 13 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5) 14 15 16 def h(v): 17 return format(v, "064x") 18 19 20 def add(p1, p2): 21 if p1 is None: 22 return p2 23 if p2 is None: 24 return p1 25 (x1, y1), (x2, y2) = p1, p2 26 if x1 == x2 and (y1 + y2) % P == 0: 27 return None 28 if p1 == p2: 29 l = (3 * x1 * x1 - 3) * pow(2 * y1, -1, P) % P 30 else: 31 l = (y2 - y1) * pow(x2 - x1, -1, P) % P 32 x3 = (l * l - x1 - x2) % P 33 return (x3, (l * (x1 - x3) - y1) % P) 34 35 36 def mul(k, pt): 37 r = None 38 for bit in bin(k)[2:] if k else "": 39 r = add(r, r) 40 if bit == "1": 41 r = add(r, pt) 42 return r 43 44 45 def pt_line(r): 46 return (h(0), h(0), 0) if r is None else (h(r[0]), h(r[1]), 1) 47 48 49 def main(): 50 n = int(sys.argv[1]) if len(sys.argv) > 1 else 200 51 rnd = random.Random(int(sys.argv[2]) if len(sys.argv) > 2 else 1) 52 edge = [0, 1, 2, 3, P - 1, P - 2, P - 3, 2**32 - 1, 2**32, 2**224, 2**192, 2**96, 2**96 - 1, 53 2**255, P - 2**224, P - 2**96, (P - 1) // 2, 2**256 - 2**224 - 1] 54 edge = [e % P for e in edge] 55 # words that exercise every Solinas column at its extremes: all-ones / all-zero 32-bit words 56 for m in range(16): 57 e = sum((0xFFFFFFFF if (m >> (i % 4)) & 1 else 0) << (32 * i) for i in range(8)) 58 edge.append(e % P) 59 vals = edge + [rnd.randrange(P) for _ in range(n)] 60 out = [] 61 for a in edge: 62 for b in edge: 63 out.append(f"M {h(a)} {h(b)} {h(a * b % P)}") 64 out.append(f"A {h(a)} {h(b)} {h((a + b) % P)}") 65 out.append(f"U {h(a)} {h(b)} {h((a - b) % P)}") 66 # multiplication accepts any inputs below 2^256 (used only with reduced inputs by the library) 67 for a in [2**256 - 1, 2**256 - 2, P, P + 1] + vals[:20]: 68 for b in [2**256 - 1, P, P + 5, 1, 0]: 69 out.append(f"M {h(a)} {h(b)} {h(a * b % P)}") 70 for _ in range(n): 71 a, b = rnd.randrange(P), rnd.randrange(P) 72 out.append(f"M {h(a)} {h(b)} {h(a * b % P)}") 73 out.append(f"A {h(a)} {h(b)} {h((a + b) % P)}") 74 out.append(f"U {h(a)} {h(b)} {h((a - b) % P)}") 75 for a in vals: 76 out.append(f"S {h(a)} {h(a * a % P)}") 77 out.append(f"I {h(a)} {h(pow(a, P - 2, P))}") 78 # scalar multiplication: edge scalars on G and on random points, random scalars 79 pts = [G] + [mul(rnd.randrange(1, N), G) for _ in range(3)] 80 scal = [0, 1, 2, 3, 15, 16, 17, N - 1, N - 2, N, N + 1, (N - 1) // 2, 2**255, 2**256 - 1, 81 0x10, 0xF0, 0x8000000000000000000000000000000000000000000000000000000000000000] 82 for q in pts: 83 for k in scal: 84 rx, ry, f = pt_line(mul(k, q)) 85 out.append(f"K {h(k)} {h(q[0])} {h(q[1])} {rx} {ry} {f}") 86 for _ in range(max(n // 10, 4)): 87 q = pts[rnd.randrange(len(pts))] 88 k = rnd.randrange(2**256) 89 rx, ry, f = pt_line(mul(k, q)) 90 out.append(f"K {h(k)} {h(q[0])} {h(q[1])} {rx} {ry} {f}") 91 # double-scalar: random, infinity (u G + v Q with Q = G and u + v = n), doubling (u = v, Q = G) 92 for _ in range(max(n // 10, 4)): 93 q = pts[rnd.randrange(len(pts))] 94 u, v = rnd.randrange(N), rnd.randrange(N) 95 rx, ry, f = pt_line(add(mul(u, G), mul(v, q))) 96 out.append(f"D {h(u)} {h(v)} {h(q[0])} {h(q[1])} {rx} {ry} {f}") 97 for u, v in [(5, N - 5), (7, 7), (0, 0), (0, 1), (1, 0), (N - 1, N - 1)]: 98 rx, ry, f = pt_line(add(mul(u, G), mul(v, G))) 99 out.append(f"D {h(u)} {h(v)} {h(G[0])} {h(G[1])} {rx} {ry} {f}") 100 print("\n".join(out)) 101 102 103 main()