std/p384
import std/p384 to use it. Source: src/std/p384.iyi at v0.22.0.
import std/p384::{P384PrivateKey}
import std/ecdsa::{EcdsaHash, EcdsaError} key = P384PrivateKey.from_pkcs8(der).or_panic # or from_sec1, from_bytes
signature = key.sign(EcdsaHash::SHA384, message) # DER Ecdsa-Sig-Value
key.public_key.verify(EcdsaHash::SHA384, message, signature) # true
key.public_point # 0x04, x, y: what a key share sends
key.shared_secret(peer_point).or_panic # x of [d]Q, 48 bytesstd/p256's sibling for the next NIST curve, which TLS 1.3 names secp384r1 and ecdsa_secp384r1_sha384, and the curve most roots that are not RSA sit on. std/ecdsa checks P-384 signatures in std/big's variable-time arithmetic, fine for public values; a private key needs this.
A module of its own, not std/p256 made general. std/p256's numbers are four limbs held in a struct's fields, which live in registers and on the stack; a limb count chosen at run time would put them in an Array, allocated on the heap for every product - and the 4-limb code, unrolled by hand, is what makes P-256 as fast as it is. So this is the same design at six limbs, step for step: what one module does, the other does, and a reader of one has read both.
Constant time, as std/p256 is: nothing branches on a secret and no memory index depends on one. Numbers modulo p (the field) and modulo n (the group's order) are six 64-bit limbs in Montgomery form (R = 2^384), products by CIOS in UInt128, the final subtraction chosen by a mask. Points are projective, added and doubled by the complete formulas of Renes, Costello and Batina (2016, algorithms 4 and 6, a = -3, which P-384 is), with no case to branch on. [k]P runs 96 windows of four bits from the top, each four doublings and one addition of a table entry read whole: all sixteen entries are read and the one wanted kept by masks, each mask's selector passed through std/digest's ValueBarrier first, so that the compiler cannot work out that it is 0 or all ones and make the choice a branch or a single load (P384Point.lookup, kept out of line for bench/std_p384_exercise.sh to read its assembly). Inversion is Fermat's, x^(m - 2), whose exponent is public.
The nonce is RFC 6979's, HMAC under the hash the signature names. P-384 is wider than SHA-256: the candidate k is two HMAC blocks' worth, cut to 384 bits (section 3.2, step h), and a SHA-512 digest is cut to its leftmost 384 bits before anything else (bits2int). sign_digest signs a digest already taken, as Python's Prehashed does - which is also how the reduction of a digest of n or more is held to an oracle, a digest no message reaches but about once in 2^194.
ECDH takes the peer's point uncompressed (0x04, x, y) and refuses one whose coordinates are not below p or that is not on the curve, before any arithmetic with the private key; and an answer that is the point at infinity, which SEC 1 section 3.3.1 step 2 refuses, though no point that passes the first checks reaches it (P-384's cofactor is 1): it is the second wall, which bench/std_p384_exercise.sh shows holding (0, 0) off with the curve check taken out.
Measured (--release, medians of 1,001, bench/std_p384_exercise.sh): a key made from d in 0.348 ms, std/ecdsa's check of its point included; ECDH in 0.341 ms; a SHA-384 signature in 0.377 ms. OpenSSL 3.5.5 on the same machine: a new key in 0.091 ms (generate_private_key through Python's cryptography, the call included), ECDH in 0.212 ms and a signature in 0.124 ms (openssl speed) - this is 1.6 to 3.8 times slower. The same work for every d: ECDH took 0.341, 0.342 and 0.344 ms for RFC 6979's d, 1 and n - 1, and signing 0.377, 0.376 and 0.376.
A private key is 48 bytes, d in [1, n - 1]: from_bytes, or read from what OpenSSL writes - PKCS #8 (RFC 5958, id-ecPublicKey with secp384r1) or SEC 1's ECPrivateKey (RFC 5915), whose public key, when it carries one, must be d's.
Pure computation: std/digest, std/crypto's HMAC, std/asn1, and std/ecdsa for the public key and its checking.
bench/ct_timing.sh, sign with one key, a fixed message against random ones: |t| = 0.75 to 1.7 over 50,000 signatures (under 10 is no leak seen). The --release assembly of P384Point.mult branches on counters alone (a jump table on the digit loop's counter picks the limb) and the allocator's slow path, and that of P384Point.lookup, which it calls, on its own counter and the bounds of the table.
P384PrivateKeystruct
A P-384 private key, and the public key that goes with it.
pub struct P384PrivateKey
# The key whose d is *bytes*, 48 big-endian, or why it is not one: not
# 48 bytes, or not in [1, n - 1].
def self.from_bytes(bytes : String | Array(UInt8)) : P384PrivateKey | EcdsaError
# A PKCS #8 OneAsymmetricKey (RFC 5958) holding a P-384 key: version 0 or
# 1, the algorithm id-ecPublicKey with secp384r1, and an ECPrivateKey
# inside the privateKey OCTET STRING.
def self.from_pkcs8(der : Array(UInt8)) : P384PrivateKey | EcdsaError
# SEC 1's ECPrivateKey (RFC 5915): version 1, the 48-byte private key,
# then optionally the curve ([0], which must be secp384r1) and the
# public key ([1], which must be the private key's).
def self.from_sec1(der : Array(UInt8)) : P384PrivateKey | EcdsaError
def initialize(d : P384Num, public_key : EcdsaPublicKey, public_point : Array(UInt8))
def public_key : EcdsaPublicKey
# The public key's point, uncompressed: 0x04, then x and y, 48 bytes each.
def public_point : Array(UInt8)
# ECDH: x of [d]Q for the peer's point Q, 48 bytes big-endian - or why
# *peer* is not a point of P-384 (checked before d is used), or why the
# answer is no secret.
def shared_secret(peer : Array(UInt8)) : Array(UInt8) | EcdsaError
# The signature of *message* under *hash*: the DER of Ecdsa-Sig-Value,
# SEQUENCE { r INTEGER, s INTEGER }, as X.509 and TLS 1.3 carry it. The
# nonce is RFC 6979's, so the same key and message sign alike.
def sign(hash : EcdsaHash, message : String | Array(UInt8)) : Array(UInt8)
# The signature of a message whose digest under *hash* is *digest*, as
# `sign` makes it - or why *digest* is not one of *hash*'s.
def sign_digest(hash : EcdsaHash, digest : Array(UInt8)) : Array(UInt8) | EcdsaError
end
ECDSA signatures made on P-384 (FIPS 186-5, SEC 1, RFC 6979), and its Diffie-Hellman (ECDH, SEC 1 section 3.3.1), written in iyi, in constant time.