Elliptic arithmetic notes / 10

ECDSA and ECDH arithmetic needs

ECDSA and ECDH combine scalar multiplication with protocol-specific validation, integer conversion, encoding, and failure rules.

The reference book presents ECDSA, ECIES, and key establishment after curve arithmetic because these schemes consume the arithmetic layer. This article separates the arithmetic operations from the protocol rules that determine their inputs and interpret their outputs.

ECDSA signing

Let \(G\) have prime order \(n\), let \(d\in\{1,\ldots,n-1\}\) be the private key, and set \(Q=[d]G\). Let \(e\) be the integer obtained from the message hash by the protocol’s truncation rule. For nonce \(k\in\{1,\ldots,n-1\}\), signing computes \(R=[k]G=(x_1,y_1)\) and

\[r=x_1\bmod n,\qquad s=k^{-1}(e+dr)\bmod n.\]

Here \(x_1\) is first converted to its canonical integer representative modulo \(p\), then reduced modulo \(n\). The signing algorithm retries with a new nonce if \(r=0\) or \(s=0\), so every returned signature has \(r,s\in\{1,\ldots,n-1\}\).

The nonce \(k\) is as sensitive as the private key. If one nonce leaks,

\[d=r^{-1}(ks-e)\bmod n.\]

If a nonce repeats for two messages with distinct hash representatives modulo \(n\), the private key can be recovered. The arithmetic layer must provide constant-time scalar multiplication for \([k]G\) and scalar-field inversion/multiplication modulo \(n\), but nonce generation is a protocol responsibility.

ECDSA verification

Verification first checks \(1\le r,s<n\) and validates the public key \(Q\). It then computes

\[u_1=es^{-1}\bmod n,\qquad u_2=rs^{-1}\bmod n,\qquad X=[u_1]G+[u_2]Q.\]

Reject if \(X=\mathcal O\). Otherwise accept exactly when the canonical integer x-coordinate of \(X\) satisfies \(x(X)\bmod n=r\). For an honestly generated signature, the signing relation gives \([u_1]G+[u_2]Q=[s^{-1}(e+rd)]G=[k]G\), proving verification correctness. These steps follow SEC 1, Section 4.1. When the signature, public key, and message hash are public, this computation can use a dedicated public multi-scalar routine.

ECDH

Basic ECDH computes \(Z=[d]Q\) with secret \(d\) and public peer point \(Q\). The arithmetic layer must support:

  1. encoding and curve-membership validation, followed by the subgroup check or cofactor operation specified by the protocol;
  2. constant-time multiplication by the private scalar;
  3. well-defined handling of infinity and malformed encodings.

For valid P-256 inputs, \(1\le d<n\) and \(Q\ne\mathcal O\) in the order-\(n\) group imply \([d]Q\ne\mathcal O\). Protocols for other cofactors specify their own identity checks. The KDF and transcript binding are above the arithmetic layer.

Two concrete failures

Nonce reuse. If the same key and nonce produce signatures \((r,s_1)\) and \((r,s_2)\) with \(e_1\not\equiv e_2\pmod n\), then \(s_1-s_2\ne0\pmod n\) and

\[k=(e_1-e_2)(s_1-s_2)^{-1}\bmod n.\]

Substituting the recovered \(k\) into \(d=r^{-1}(ks_1-e_1)\bmod n\) recovers the private key.

Missing validation in ECDH. If ecdh(d, Q) accepts an invalid point, an attacker can make the output depend on \(d\) modulo small values. The point-validation contract prevents these inputs from reaching the secret multiplication.

SageMath scalar-recovery identity

The following calculation checks the scalar identity for chosen nonzero \(r\) and \(k\). It does not construct a curve or derive \(r\) from a nonce point.

n = 101
d, k, e = 37, 29, 44
r = 17
s = inverse_mod(k, n) * (e + d*r) % n
print(s)
print((inverse_mod(r, n) * (k*s - e)) % n == d)