generate_public_window8.py

cdk-rsx/crates/crypto/secp256k1/tools/generate_public_window8.py

generate_public_window8.py89 lines← SDK documentation
1#!/usr/bin/env python32# SPDX-License-Identifier: Apache-2.0 OR MIT3"""Reproduce public width-eight G constants using independent integer arithmetic."""4from pathlib import Path5import argparse67P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F8N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD03641419BETA = 0x7AE96A2B657C07106E64479EAC3434E99CF0497512F58995C1396C28719501EE10LAMBDA = 0x5363AD4CC05C30E0A5261C028812645A122E22EA20816678DF02967C1B23BD7211G = (12    0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,13    0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,14)151617def add(a, b):18    if a is None:19        return b20    if b is None:21        return a22    x, y = a23    u, v = b24    if x == u:25        if (y + v) % P == 0:26            return None27        slope = 3 * x * x * pow(2 * y, -1, P) % P28    else:29        slope = (v - y) * pow((u - x) % P, -1, P) % P30    xx = (slope * slope - x - u) % P31    return xx, (slope * (x - xx) - y) % P323334def multiply(k, point):35    result = None36    while k:37        if k & 1:38            result = add(result, point)39        point = add(point, point)40        k >>= 141    return result424344def main():45    parser = argparse.ArgumentParser(description=__doc__)46    parser.add_argument("--check", action="store_true", help="verify the frozen file without writing")47    args = parser.parse_args()48    assert multiply(N, G) is None49    assert pow(BETA, 3, P) == 1 and BETA != 150    assert multiply(LAMBDA, G) == (G[0] * BETA % P, G[1])51    odd = []52    point, step = G, add(G, G)53    for i in range(64):54        assert point == multiply(2 * i + 1, G)55        odd.append(point)56        point = add(point, step)57    points = odd + [(x * BETA % P, y) for x, y in odd]58    assert len(set(points)) == 12859    lines = [60        "// SPDX-License-Identifier: Apache-2.0 OR MIT\n",61        "// Generated by tools/generate_public_window8.py; do not edit.\n",62        "// [1,3,...,127]*G followed by their existing beta endomorphism.\n",63        "use super::{AffinePoint, FieldElement};\n",64        "pub(super) static GENERATOR: [AffinePoint; 128] = [\n",65    ]66    for x, y in points:67        assert 0 <= x < P and 0 <= y < P and y * y % P == (x * x * x + 7) % P68        lines.append("    AffinePoint::new(\n")69        for value in (x, y):70            lines.append("        FieldElement::from_bytes_unchecked(&[\n")71            encoded = value.to_bytes(32, "big")72            for start in range(0, 32, 14):73                row = ", ".join(f"0x{b:02x}" for b in encoded[start:start + 14])74                lines.append(f"            {row},\n")75            lines.append("        ]),\n")76        lines.append("    ),\n")77    lines.append("];\n")78    target = Path(__file__).resolve().parents[2] / "vendor/k256/src/arithmetic/prepared/window8_table.rs"79    text = "".join(lines)80    if args.check:81        assert target.read_text() == text, f"frozen generator table differs: {target}"82        print(f"Verified {len(points)} public affine entries and exact frozen bytes: {target}")83    else:84        target.write_text(text)85        print(f"Verified and wrote {len(points)} public affine entries: {target}")868788if __name__ == "__main__":89    main()