~/bend-docscommunity

ecp256.bend source

ecp256.bend on the hub · documented module

import Baseimport ./fep256.bend as Fimport ./fen256.bend as N# Points of NIST P-256 (FIPS 186-5, SEC 2 secp256r1), y^2 = x^3 - 3x + b, over the# Montgomery field in ./fep256.bend, and ECDSA's scalars mod the order n in# ./fen256.bend. Generated by Element's field generator from one template# for every curve; do not edit.## Jacobian coordinates (X, Y, Z), x = X/Z^2, y = Y/Z^3, the point at# infinity as Z = 0: doubling by dbl-2001-b, addition by add-2007-bl# (Explicit-Formulas Database). Verification holds no secret. ECDH's scalar# is secret: there the field is straight-line code and the ladder picks by a# mask, but add branches on infinity and on equal points, so the scalar# multiplication is not constant time (accepted for an ephemeral key only).type Pt is Data:  Pt{x: F.Fe, y: F.Fe, z: F.Fe}# b, G in Montgomery form.def b() -> F.Fe:  F.Fe{7677, 606, 2215, 7148, 6300, 1158, 268, 6481, 92, 5752, 7002, 1061, 3063, 4357, 1384, 4200, 2164, 3818, 24, 390}def base() -> Pt:  Pt{F.Fe{961, 5258, 4194, 1562, 6631, 4107, 7021, 6303, 2399, 6877, 5185, 3140, 2935, 7065, 6782, 2956, 4981, 7002, 381, 274}, F.Fe{168, 2731, 7994, 2666, 7666, 742, 1657, 2164, 2958, 1445, 3222, 1086, 2269, 1076, 5306, 6922, 6277, 3154, 2044, 174}, F.one()}def infinity() -> Pt:  Pt{F.one(), F.one(), F.zero()}def is_inf(p: Pt) -> Bool:  match p:    case Pt{x, y, +z}:      F.is_zero(z)# dbl-2001-b (a = -3). Infinity (Z = 0) and Y = 0 both give Z3 = 0.def dbl(p: Pt) -> Pt:  match p:    case Pt{+x, +y, +z}:      +delta = F.sq(z)      +gamma = F.sq(y)      +beta = F.mul(x, gamma)      +t = F.sub(x, delta)      +alpha = F.mul(F.add(F.add(t, t), t), F.add(x, delta))      +beta2 = F.add(beta, beta)      +beta4 = F.add(beta2, beta2)      +x3 = F.sub(F.sq(alpha), F.add(beta4, beta4))      +yz = F.add(y, z)      +z3 = F.sub(F.sub(F.sq(yz), gamma), delta)      +g2 = F.sq(gamma)      +g4 = F.add(g2, g2)      +g8 = F.add(g4, g4)      +y3 = F.sub(F.mul(alpha, F.sub(beta4, x3)), F.add(g8, g8))      Pt{x3, y3, z3}# add-2007-bl, general case (P != +-Q, neither at infinity).def add_general(p: Pt, q: Pt, +u1: F.Fe, +s1: F.Fe, +h: F.Fe, +r0: F.Fe, +z1z1: F.Fe, +z2z2: F.Fe) -> Pt:  match p q:    case Pt{x1, y1, +z1} Pt{x2, y2, +z2}:      +h2 = F.add(h, h)      +i = F.sq(h2)      +j = F.mul(h, i)      +r = F.add(r0, r0)      +v = F.mul(u1, i)      +x3 = F.sub(F.sub(F.sub(F.sq(r), j), v), v)      +sj = F.mul(s1, j)      +y3 = F.sub(F.mul(r, F.sub(v, x3)), F.add(sj, sj))      +zz = F.add(z1, z2)      +z3 = F.mul(F.sub(F.sub(F.sq(zz), z1z1), z2z2), h)      Pt{x3, y3, z3}def add_case(inf1: Bool, inf2: Bool, hz: Bool, rz: Bool, +p: Pt, +q: Pt, +u1: F.Fe, +s1: F.Fe, +h: F.Fe, +r0: F.Fe, +z1z1: F.Fe, +z2z2: F.Fe) -> Pt:  match inf1:    case True{}:      q    case False{}:      match inf2:        case True{}:          p        case False{}:          match hz:            case False{}:              add_general(p, q, u1, s1, h, r0, z1z1, z2z2)            case True{}:              match rz:                case True{}:                  dbl(p)                case False{}:                  infinity()def add(p: Pt, q: Pt) -> Pt:  match p q:    case Pt{+x1, +y1, +z1} Pt{+x2, +y2, +z2}:      +z1z1 = F.sq(z1)      +z2z2 = F.sq(z2)      +u1 = F.mul(x1, z2z2)      +s1 = F.mul(F.mul(y1, z2), z2z2)      +h = F.sub(F.mul(x2, z1z1), u1)      +r0 = F.sub(F.mul(F.mul(y2, z1), z1z1), s1)      add_case(F.is_zero(z1), F.is_zero(z2), F.is_zero(h), F.is_zero(r0), Pt{x1, y1, z1}, Pt{x2, y2, z2}, u1, s1, h, r0, z1z1, z2z2)def pick_add(b1: Bool, b2: Bool, +d: Pt, +g: Pt, +q: Pt, +gq: Pt) -> Pt:  match b1:    case True{}:      match b2:        case True{}:          add(d, gq)        case False{}:          add(d, g)    case False{}:      match b2:        case True{}:          add(d, q)        case False{}:          d# u1 G + u2 Q from both scalars' bits, most significant first.def joint(b1s: List<&2, Bool>, b2s: List<&2, Bool>, +g: Pt, +q: Pt, +gq: Pt, +acc: Pt) -> Pt:  match b1s:    case Nil{}:      acc    case Con{b1, t1}:      match b2s:        case Nil{}:          acc        case Con{b2, t2}:          joint(t1, t2, g, q, gq, pick_add(b1, b2, dbl(acc), g, q, gq))# y^2 = x^3 - 3x + b, x and y in Montgomery form.def on_curve(+x: F.Fe, +y: F.Fe) -> Bool:  F.eq(F.sq(y), F.add(F.sub(F.mul(F.sq(x), x), F.add(F.add(x, x), x)), b()))# The public point from its big-endian coordinates: both below p and on the curve.def point_ok(+qx: List<&2, U32>, +qy: List<&2, U32>) -> Bool:  +x = F.of_bytes(qx)  +y = F.of_bytes(qy)  F.is_reduced(x) && F.is_reduced(y) && on_curve(F.to_mont(x), F.to_mont(y))def point_of(+qx: List<&2, U32>, +qy: List<&2, U32>) -> Pt:  Pt{F.to_mont(F.of_bytes(qx)), F.to_mont(F.of_bytes(qy)), F.one()}# The affine x as big-endian bytes (one inversion).def affine_x(p: Pt) -> List<&2, U32>:  match p:    case Pt{x, y, +z}:      +zi = F.inv(z)      F.to_bytes(F.mul(x, F.sq(zi)))# X then Y, affine, big-endian.def affine(p: Pt) -> List<&2, U32>:  match p:    case Pt{x, y, +z}:      +zi = F.inv(z)      +zi2 = F.sq(zi)      List.append(&2, U32, F.to_bytes(F.mul(x, zi2)), F.to_bytes(F.mul(y, F.mul(zi2, zi))))def finite_x(inf: Bool, +p: Pt) -> Maybe<&2, List<&2, U32>>:  match inf:    case True{}:      None{}    case False{}:      Some{affine_x(p)}# x of u1 G + u2 Q (the scalars as bits, top first), or None at infinity.# The caller has checked the point (point_ok).def joint_x(b1s: List<&2, Bool>, b2s: List<&2, Bool>, +qx: List<&2, U32>, +qy: List<&2, U32>) -> Maybe<&2, List<&2, U32>>:  +q = point_of(qx, qy)  +g = base()  +sum = joint(b1s, b2s, g, q, add(g, q), infinity())  finite_x(is_inf(sum), sum)# ---- ECDSA (FIPS 186-5 6.4.2) ------------------------------------------------------def byte_bools(c: Nat, +b: U32) -> List<&2, Bool>:  match c:    case 0n:      Nil{}    case 1n+(+r):      U32.is_eq(U32.and((b / U32.shln(1, r) : U32), 1), 1) <> byte_bools(r, b)# Bits of big-endian bytes, most significant first.def bools_of_bytes(bs: List<&2, U32>) -> List<&2, Bool>:  match bs:    case Nil{}:      Nil{}    case Con{b, t}:      List.append(&2, Bool, byte_bools(8n, b), bools_of_bytes(t))# 0 < x < n, for x read from bytes.def in_range_n(+x: N.Fe) -> Bool:  N.is_reduced(x) && Bool.not(N.is_zero(x))# x (below p, and p < 2n) mod n equals r.def x_matches(m: Maybe<&2, List<&2, U32>>, +r: N.Fe) -> Bool:  match m:    case None{}:      False{}    case Some{xb}:      N.eq(N.of_bytes(xb), r)# w = s^-1 (Montgomery form); u1 = e w and u2 = r w come out plain, as e and# r enter plain (a b R^-1 of a plain and a Montgomery value is plain).def verify_if(ok: Bool, +qx: List<&2, U32>, +qy: List<&2, U32>, +r: N.Fe, +s: N.Fe, +e: N.Fe) -> Bool:  match ok:    case False{}:      False{}    case True{}:      +w = N.inv(N.to_mont(s))      +u1 = N.canon_bytes(N.canon(N.mul(e, w)))      +u2 = N.canon_bytes(N.canon(N.mul(r, w)))      x_matches(joint_x(bools_of_bytes(u1), bools_of_bytes(u2), qx, qy), r)# (r, s) against the public point (qx, qy) and the truncated digest e, all# big-endian; the caller has checked that none is longer than the curve's size.def verify_rs(+qx: List<&2, U32>, +qy: List<&2, U32>, +rb: List<&2, U32>, +sb: List<&2, U32>, +eb: List<&2, U32>) -> Bool:  +r = N.of_bytes(rb)  +s = N.of_bytes(sb)  verify_if(in_range_n(r) && in_range_n(s) && point_ok(qx, qy), qx, qy, r, s, N.of_bytes(eb))# ---- scalar multiplication (ECDH) -------------------------------------------------def byte_bits(c: Nat, +b: U32) -> List<&2, U32>:  match c:    case 0n:      Nil{}    case 1n+(+r):      U32.and((b / U32.shln(1, r) : U32), 1) <> byte_bits(r, b)# Bits of big-endian bytes, most significant first, as 0 or 1.def bits_of_bytes(bs: List<&2, U32>) -> List<&2, U32>:  match bs:    case Nil{}:      Nil{}    case Con{b, t}:      List.append(&2, U32, byte_bits(8n, b), bits_of_bytes(t))def select_pt(+s: U32, a: Pt, b: Pt) -> Pt:  match a b:    case Pt{ax, ay, az} Pt{bx, by, bz}:      Pt{F.select(s, ax, bx), F.select(s, ay, by), F.select(s, az, bz)}# k P, top bit first: every bit doubles and adds, and the bit picks by a mask.def ladder(bits: List<&2, U32>, +p: Pt, +acc: Pt) -> Pt:  match bits:    case Nil{}:      acc    case Con{+bit, t}:      +d = dbl(acc)      ladder(t, p, select_pt(bit, d, add(d, p)))# k P for a scalar k as big-endian bytes.def times(+k: List<&2, U32>, +p: Pt) -> Pt:  ladder(bits_of_bytes(k), p, infinity())