Elliptic curves.
An elliptic curve E over ℤp (p ≥ 5) is defined by an equation of the form
y^2 = x^3 + ax + b, where a, b ∈ ℤp and the discriminant ≢ 0 (mod p),
together with a special point 𝒪 called the point at infinity.
The set E(ℤp) consists of all points (x, y), with x, y ∈ ℤp,
which satisfy the above defining equation, together with 𝒪.

There is a rule for adding two points on an elliptic curve to give a third point.
This addition operation and the set of points E(ℤp) form a group with identity 𝒪.
It is this group that is used in the construction of elliptic curve cryptosystems.

The addition rule — which can be explained geometrically — is summarized as follows:

1. P + 𝒪 = 𝒪 + P = P for all P ∈ E(ℤp).

2. If P = (x, y) ∈ E(ℤp), then inverse -P = (x,-y), and P + (-P) = 𝒪.

3. Let P = (xP, yP) and Q = (xQ, yQ), both ∈ E(ℤp), where P ≠ -Q.
   Then R = P + Q = (xR, yR), where

   xR = λ^2 - xP - xQ
   yR = λ·(xP - xR) - yP,

   with

   λ = (yP - yQ) / (xP - xQ) if P ≠ Q,
       (3·xP·xP + a) / 2·yP  if P = Q (point doubling).

Remark: there already is a task page requesting “a simplified (without modular arithmetic)
version of the elliptic curve arithmetic”.
Here we do add modulo operations. If also the domain is changed from reals to rationals,
the elliptic curves are no longer continuous but break up into a finite number of distinct points.
In that form we use them to implement ECDSA:



Elliptic curve digital signature algorithm.
A digital signature is the electronic analogue of a hand-written signature
that convinces the recipient that a message has been sent intact by the presumed sender.
Anyone with access to the public key of the signer may verify this signature.
Changing even a single bit of a signed message will cause the verification procedure to fail.

ECDSA key generation. Party A does the following:

1. Select an elliptic curve E defined over ℤp.

 The number of points in E(ℤp) should be divisible by a large prime r.

2. Select a base point G ∈ E(ℤp) of order r (which means that rG = 𝒪).

3. Select a random integer s in the interval [1, r - 1].

4. Compute W = sG.

 The public key is (E, G, r, W), the private key is s.

ECDSA signature computation. To sign a message m, A does the following:

1. Compute message representative f = H(m), using a
cryptographic hash function.

 Note that f can be greater than r but not longer (measuring bits).

2. Select a random integer u in the interval [1, r - 1].

3. Compute V = uG = (xV, yV) and c ≡ xV mod r  (goto (2) if c = 0).

4. Compute d ≡ u^-1·(f + s·c) mod r  (goto (2) if d = 0).

 The signature for the message m is the pair of integers (c, d).

ECDSA signature verification. To verify A's signature, B should do the following:

1. Obtain an authentic copy of A's public key (E, G, r, W).

 Verify that c and d are integers in the interval [1, r - 1].

2. Compute f = H(m) and h ≡ d^-1 mod r.

3. Compute h1 ≡ f·h mod r and h2 ≡ c·h mod r.

4. Compute h1G + h2W = (x1, y1) and c1 ≡ x1 mod r.

 Accept the signature if and only if c1 = c.

To be cryptographically useful, the parameter r should have at least 250 bits.
The basis for the security of elliptic curve cryptosystems
is the intractability of the elliptic curve discrete logarithm problem (ECDLP) in a group of this size:
given two points G, W ∈ E(ℤp), where W lies in the subgroup of order r generated by G,
determine an integer k such that W = kG and 0 ≤ k < r.



Task.
The task is to write a toy version of the ECDSA, quasi the equal of a real-world
implementation, but utilizing parameters that fit into standard arithmetic types.
To keep things simple there's no need for key export or a hash function (just a sample
hash value and a way to tamper with it). The program should be lenient where possible
(for example: if it accepts a composite modulus N it will either function as expected,
or demonstrate the principle of elliptic curve factorization)
— but strict where required (a point G that is not on E will always cause failure).

Toy ECDSA is of course completely useless for its cryptographic purpose.
If this bothers you, please add a multiple-precision version.



Reference.
Elliptic curves are in the IEEE Std 1363-2000 (Standard Specifications for Public-Key Cryptography), see:

7. Primitives based on the elliptic curve discrete logarithm problem (p. 27ff.)

7.1 The EC setting

7.1.2 EC domain parameters

7.1.3 EC key pairs

7.2 Primitives

7.2.7 ECSP-DSA (p. 35)

7.2.8 ECVP-DSA (p. 36)

Annex A. Number-theoretic background

A.9 Elliptic curves: overview (p. 115)

A.10 Elliptic curves: algorithms (p. 121)



import math, random, strformat
 
const
  MaxN = 1073741789       # Maximum modulus.
  MaxR = MaxN + 65536     # Maximum order "g".
  Infinity = int64.high   # Symbolic infinity.
 
type
 
  Point = tuple[x, y: int64]
 
  Curve = object
    a, b: int64
    n: int64
    g: Point
    r: int64
 
  Pair = tuple[a, b: int64]
 
  Parameters = tuple[a, b, n, gx, gy, r: int]
 
const ZerO: Point = (Infinity, 0i64)
 
type
  InversionError = object of ValueError
  InvalidParamError = object of ValueError
 
 
template `%`(a, n: int64): int64 =
  ## To simplify the writing.
  floorMod(a, n)
 
 
proc exgcd(v, u: int64): int64 =
  ## Return 1/v mod u.
  var u = u
  var v = v
  if v < 0: v += u
 
  var r = 0i64
  var s = 1i64
  while v != 0:
    let q = u div v
    u = u mod v
    swap u, v
    r -= q * s
    swap r, s
 
  if u != 1:
    raise newException(InversionError, "impossible inverse mod N, gcd = " & $u)
  result = r
 
 
func discr(e: Curve): int64 =
  ## Return the discriminant of "e".
  let c = e.a * e.a % e.n * e.a % e.n * 4
  result = -16 * (e.b * e.b % e.n * 27 + c) % e.n
 
 
func isO(p: Point): bool =
  ## Return true if "p" is zero.
  p.x == Infinity and p.y == 0
 
 
func isOn(e: Curve; p: Point): bool =
  ## Return true if "p" is on curve "e".
  if p.isO: return true
  let r = ((e.a + p.x * p.x) % e.n * p.x + e.b) % e.n
  let s = p.y * p.y % e.n
  result = r == s
 
 
proc add(e: Curve; p, q: Point): Point =
  ## Full Point addition.
 
  if p.isO: return q
  if q.isO: return p
 
  var la: int64
  if p.x != q.x:
    la = (p.y - q.y) * exgcd(p.x - q.x, e.n) % e.n
  elif p.y == q.y and p.y != 0:
    la = (p.x * p.x % e.n * 3 + e.a) % e.n * exgcd(2 * p.y, e.n) % e.n
  else:
    return ZerO
 
  result.x = (la * la - p.x - q.x) % e.n
  result.y = (la * (p.x - result.x) - p.y) % e.n
 
 
proc mul(e: Curve; p: Point; k: int64): Point =
  ## Return "kp".
  var q = p
  var k = k
  result = ZerO
 
  while k != 0:
    if (k and 1) != 0:
      result = e.add(result, q)
    q = e.add(q, q)
    k = k shr 1
 
 
proc print(e: Curve; prefix: string; p: Point) =
  ## Print a point with a prefix.
  var y = p.y
  if p.isO:
    echo prefix, " (0)"
  else:
    if y > e.n - y: y -= e.n
    echo prefix, &" ({p.x}, {y})"
 
 
proc initCurve(params: Parameters): Curve =
  ## Initialize the curve.
 
  result.n = params.n
  if result.n notin 5..MaxN:
    raise newException(ValueError, "invalid value for N: " & $result.n)
  result.a = params.a.int64 % result.n
  result.b = params.b.int64 % result.n
  result.g.x = params.gx.int64 % result.n
  result.g.y = params.gy.int64 % result.n
  result.r = params.r
 
  if result.r notin 5..MaxR:
    raise newException(ValueError, "invalid value for r: " & $result.r)
 
  echo &"\nE: y^2 = x^3 + {result.a}x + {result.b} (mod {result.n})"
  result.print("base point G", result.g)
  echo &"order(G, E) = {result.r}"
 
 
proc rnd(): float =
  ## Return a pseudorandom number in range [0..1[.
  while true:
    result = rand(1.0)
    if result != 1: break
 
proc signature(e: Curve; s, f: int64): Pair =
  ## Compute the signature.
 
  var
    c, d = 0i64
    u: int64
    v: Point
 
  echo "Signature computation"
 
  while true:
    while true:
      u = 1 + int64(rnd() * float(e.r - 1))
      v = e.mul(e.g, u)
      c = v.x % e.r
      if c != 0: break
    d = exgcd(u, e.r) * (f + s * c % e.r) % e.r
    if d != 0: break
 
  echo "one-time u = ", u
  e.print("V = uG", v)
 
  result = (c, d)
 
 
proc verify(e: Curve; w: Point; f: int64; sg: Pair): bool =
  ## Verify a signature.
 
  # Domain check.
  if sg.a notin 1..<e.r or sg.b notin 1..<e.r:
    return false
 
  echo "\nsignature verification"
  let h = exgcd(sg.b, e.r)
  let h1 = f * h % e.r
  let h2 = sg.a * h % e.r
  echo &"h1, h2 = {h1}, {h2}"
  var v = e.mul(e.g, h1)
  let v2 = e.mul(w, h2)
  e.print("h1G", v)
  e.print("h2W", v2)
  v = e.add(v, v2)
  e.print("+ =", v)
  if v.isO: return false
  let c1 = v.x % e.r
  echo "c’ = ", c1
  result = c1 == sg.a
 
 
proc ecdsa(e: Curve; f: int64; d: int) =
  ## Build digital signature for message hash "f" with error bit "d".
 
  # Parameter check.
  var w = e.mul(e.g, e.r)
  if e.discr() == 0 or e.g.isO or not w.isO or not e.isOn(e.g):
    raise newException(InvalidParamError, "invalid parameter set")
 
  echo "\nkey generation"
  let s = 1 + int64(rnd() * float(e.r - 1))
  w = e.mul(e.g, s)
  echo "private key s = ", s
  e.print("public key W = sG", w)
 
  # Find next highest power of two minus one.
  var t = e.r
  var i = 1
  while i < 64:
    t = t or t shr i
    i = i shl 1
  var f = f
  while f > t: f = f shr 1
  echo &"\naligned hash {f:x}"
 
  let sg = e.signature(s, f)
  echo &"signature c, d = {sg.a}, {sg.b}"
 
  var d = d
  if d > 0:
    while d > t: d = d shr 1
    f = f xor d
    echo &"\ncorrupted hash {f:x}"
 
  echo if e.verify(w, f, sg): "Valid" else: "Invalid"
 
when isMainModule:
 
  randomize()
 
  # Test vectors: elliptic curve domain parameters,
  # short Weierstrass model y^2 = x^3 + ax + b (mod N)
 
  const Sets = [
    #    a,   b,  modulus N, base point G, order(G, E), cofactor
      (355, 671, 1073741789, 13693, 10088, 1073807281),
      (  0,   7,   67096021,  6580,   779,   16769911),
      ( -3,   1,     877073,     0,     1,     878159),
      (  0,  14,      22651,    63,    30,        151),
      (  3,   2,          5,     2,     1,          5),
 
    # ECDSA may fail if...
    # the base point is of composite order
      (  0,   7,   67096021,  2402,  6067,   33539822),
    # the given order is of composite order
      (  0,   7,   67096021,  6580,   779,   67079644),
    # the modulus is not prime (deceptive example)
      (  0,   7,     877069,     3, 97123,     877069),
    # fails if the modulus divides the discriminant
      ( 39, 387,      22651,    95,    27,      22651)
    ]
 
  # Digital signature on message hash f,
  # set d > 0 to simulate corrupted data.
  let f = 0x789abcdei64
  let d = 0
 
  for s in Sets:
    let e = initCurve(s)
    try:
      e.ecdsa(f, d)
    except ValueError:
      echo getCurrentExceptionMsg()
    echo "——————————————"