summaryrefslogtreecommitdiff
path: root/bip-0324/secp256k1_test_vectors.py
blob: 57ae801cc7efe884aa9c109e3c06bc9b9555b365 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Convert the BIP-324 test vectors to secp256k1 code."""

import csv
import reference
import os
import sys

FILENAME_XSWIFTEC_INV_TEST = os.path.join(sys.path[0], 'xswiftec_inv_test_vectors.csv')
FILENAME_ELLSWIFT_DECODE_TEST = os.path.join(sys.path[0], 'ellswift_decode_test_vectors.csv')

def format_int(v):
    """Format 0 as "0", but other integers as 0x%08x."""
    if v == 0:
        return "0"
    return f"0x{v:08x}"

def format_fe(fe):
    """Format a field element constant as SECP256K1_FE_CONST code."""
    vals = [(int(fe) >> (32 * (7 - i))) & 0xffffffff for i in range(8)]
    strs = ", ".join(format_int(v) for v in vals)
    return f"SECP256K1_FE_CONST({strs})"

def output_xswiftec_inv_cases():
    """Generate lines corresponding to the xswiftec_inv test cases."""
    with open(FILENAME_XSWIFTEC_INV_TEST, newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        print("xswiftec_inv cases:")
        for row in reader:
            u = int.from_bytes(bytes.fromhex(row['u']), 'big')
            x = int.from_bytes(bytes.fromhex(row['x']), 'big')
            pat = sum(1<<c for c in range(8) if row[f"case{c}_t"])
            tstrs = []
            for c in range(8):
                tstrs.append(format_fe(int.from_bytes(bytes.fromhex(row[f"case{c}_t"]), 'big')))
            print(f"    {{0x{pat:02x}, {format_fe(u)}, {format_fe(x)}, {{{', '.join(tstrs)}}}}},")
    print()

def output_ellswift_decode_cases():
    """Generate lines corresponding to the ellswift_decode test cases."""
    with open(FILENAME_ELLSWIFT_DECODE_TEST, newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        print("ellswift_decode cases:")
        for row in reader:
            enc = bytes.fromhex(row['ellswift'])
            tval = int.from_bytes(enc[32:], 'big') % reference.FE.SIZE
            x = int.from_bytes(bytes.fromhex(row['x']), 'big')
            encstr = ", ".join(f"0x{b:02x}" for b in enc)
            print(f"    {{{{{encstr}}}, {format_fe(x)}, {tval & 1}}},")
    print()

output_xswiftec_inv_cases()
output_ellswift_decode_cases()