user
Elliptic curves.

An elliptic curve E over ℤp (p ≥ 5) is defined by an equation of the form
**y²= x³ + 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)
assistant
(formerly Perl 6)

```
module EC {
  use FiniteField;

  our class Point {
    has ($.x, $.y);
    submethod TWEAK { fail unless $!y**2 == $!x**3 + $*a*$!x + $*b }
    multi method gist(::?CLASS:U:) { "Point at Horizon" }
    multi method new($x, $y) { samewith :$x, :$y }
  }
  multi infix:<==>(Point:D $A, Point:D $B) is export { $A.x == $B.x and $A.y == $B.y }

  multi prefix:<->(Point $P) returns Point is export { Point.new: $P.x, -$P.y }
  multi infix:<+>(Point $A, Point:U) is export { $A }
  multi infix:<+>(Point:U, Point $B) is export { $B }
  multi infix:<+>(Point:D $A, Point:D $B) returns Point is export {
    my $λ;
    if $A.x == $B.x and $A.y == -$B.y { return Point }
    elsif $A == $B {
      return Point if $A.y == 0;
      $λ = (3*$A.x² + $*a) / (2*$A.y);
    }
    else { $λ = ($A.y - $B.y) / ($A.x - $B.x); }

    given $λ**2 - $A.x - $B.x {
      return Point.new: $_, $λ*($A.x - $_) - $A.y;
    }
  }
  multi infix:<*>(0, Point     ) is export { Point }
  multi infix:<*>(1, Point   $p) is export { $p }
  multi infix:<*>(2, Point:D $p) is export { $p + $p }
  multi infix:<*>(Int $n, Point $p) is export { 2*(($n div 2)*$p) + ($n mod 2)*$p }
}

import EC;

module ECDSA {
  use Digest::SHA256::Native;
  our class Signature {
    has UInt ($.c, $.d);
    multi method new(Str $message, UInt :$private-key) {
      my $z = :16(sha256-hex $message) % $*n;
      loop (my $k = my $s = my $r = 0; $r == 0; ) {
        loop ( $r = $s = 0 ; $r == 0 ; ) {
          $r = (( $k = (1..^$*n).roll ) * $*G).x % $*n;
        }
        {
          use FiniteField; my $*modulus = $*n;
          $s = ($z + $r*$private-key) / $k;
        }
      }
      samewith c => $r, d => $s;
    }
    multi method verify(Str $message, EC::Point :$public-key) {
      my $z = :16(sha256-hex $message) % $*n;
      my ($u1, $u2);
      {
        use FiniteField;
        my $*modulus = $*n;
        my $w = 1/$!d;
        ($u1, $u2) = $z*$w, $!c*$w;
      }
      my $p = ($u1 * $*G) + ($u2 * $public-key);
      die unless ($p.x mod $*n) == ($!c mod $*n);
    }
  }
}

my ($*a, $*b) = 355, 671;
my $*modulus = my $*p = 1073741789;

my $*G = EC::Point.new: 13693, 10088;
my $*n = 1073807281;

die "G is not of order n" if $*n*$*G;

my $private-key = ^$*n .pick;
my $public-key = $private-key*$*G; 

my $message = "Show me the monKey";
my $signature = ECDSA::Signature.new: $message, :$private-key;

printf "The curve E is        : 𝑦² = 𝑥³ + %s 𝑥 + %s (mod %s)\n", $*a, $*b, $*p;
printf "with generator G at   : (%s, %s)\n", $*G.x, $*G.y;
printf "G's order is          : %d\n", $*n;
printf "The private key is    : %d\n", $private-key;
printf "The public key is at  : (%s, %s)\n", $public-key.x, $public-key.y;
printf "The message is        : %s\n", $message;
printf "The signature is      : (%s, %s)\n", $signature.c, $signature.d;

{
  use Test;

  lives-ok {
    $signature.verify: $message, :$public-key;
  }, "good signature for <$message>";

  my $altered = $message.subst(/monKey/, "money");
  dies-ok {
    $signature.verify: $altered, :$public-key
  }, "wrong signature for <$altered>";
}

```

Output:

```
The curve E is        : 𝑦² = 𝑥³ + 355 𝑥 + 671 (mod 1073741789)
with generator G at   : (13693, 10088)
G's order is          : 1073807281
The private key is    : 405586338
The public key is at  : (457744420, 236326628)
The message is        : Show me the monKey
The signature is      : (839907635, 23728690)
ok 1 - good signature for <Show me the monKey>
ok 2 - wrong signature for <Show me the money>
```