Elliptic curve point multiplication

From HandWiki
Short description: Mathematical operation on points on an elliptic curve

Elliptic curve scalar multiplication is the operation of successively adding a point along an elliptic curve to itself repeatedly. It is used in elliptic curve cryptography (ECC). The literature presents this operation as scalar multiplication, as written in Hessian form of an elliptic curve. A widespread name for this operation is also elliptic curve point multiplication, but this can convey the wrong impression of being a multiplication between two points.

Basics

Given a curve, E, defined by some equation in a finite field (such as E: y2 = x3 + ax + b), point multiplication is defined as the repeated addition of a point along that curve. Denote as nP = P + P + P + … + P for some scalar (integer) n and a point P = (x, y) that lies on the curve, E. This type of curve is known as a Weierstrass curve.

The security of modern ECC depends on the intractability of determining n from Q = nP given known values of Q and P if n is large (known as the elliptic curve discrete logarithm problem by analogy to other cryptographic systems). This is because the addition of two points on an elliptic curve (or the addition of one point to itself) yields a third point on the elliptic curve whose location has no immediately obvious relationship to the locations of the first two, and repeating this many times over yields a point nP that may be essentially anywhere. Intuitively, this is not dissimilar to the fact that if you had a point P on a circle, adding 42.57 degrees to its angle may still be a point "not too far" from P, but adding 1000 or 1001 times 42.57 degrees will yield a point that requires a bit more complex calculation to find the original angle. Reversing this process, i.e., given Q=nP and P, and determining n, can only be done by trying out all possible n—an effort that is computationally intractable if n is large.

Point operations

Elliptic curve point operations: Addition (shown in facet 1), doubling (facets 2 and 4) and negation (facet 3).

There are three commonly defined operations for elliptic curve points: addition, doubling and negation.

Point at infinity

Point at infinity [math]\displaystyle{ \mathcal{O} }[/math] is the identity element of elliptic curve arithmetic. Adding it to any point results in that other point, including adding point at infinity to itself. That is:

[math]\displaystyle{ \begin{align} \mathcal{O} + \mathcal{O} = \mathcal{O}\\ \mathcal{O} + P = P \end{align} }[/math]

Point at infinity is also written as 0.

Point negation

Point negation is finding such a point, that adding it to itself will result in point at infinity ([math]\displaystyle{ \mathcal{O} }[/math]).

[math]\displaystyle{ \begin{align} P + (-P) = \mathcal{O} \end{align} }[/math]

For elliptic curves of the form E: y2 = x3 + ax + b, negation is a point with the same x coordinate but negated y coordinate:

[math]\displaystyle{ \begin{align} (x, y) + (-(x, y)) &= \mathcal{O}\\ (x, y) + (x, -y) &= \mathcal{O}\\ (x, -y) &= -(x, y) \end{align} }[/math]

Point addition

With 2 distinct points, P and Q, addition is defined as the negation of the point resulting from the intersection of the curve, E, and the straight line defined by the points P and Q, giving the point, R.[1]

[math]\displaystyle{ \begin{align} P + Q &= R \\ (x_p, y_p) + (x_q, y_q) &= (x_r, y_r) \end{align} }[/math]

Assuming the elliptic curve, E, is given by y2 = x3 + ax + b, this can be calculated as:

[math]\displaystyle{ \begin{align} \lambda &= \frac{y_q - y_p}{x_q - x_p} \\ x_r &= \lambda^2 - x_p - x_q \\ y_r &= \lambda(x_p - x_r) - y_p \\ \end{align} }[/math]

These equations are correct when neither point is the point at infinity, [math]\displaystyle{ \mathcal{O} }[/math], and if the points have different x coordinates (they're not mutual inverses). This is important for the ECDSA verification algorithm where the hash value could be zero.

Point doubling

Where the points P and Q are coincident (at the same coordinates), addition is similar, except that there is no well-defined straight line through P, so the operation is closed using a limiting case, the tangent to the curve, E, at P.

This is calculated as above, taking derivatives (dE/dx)/(dE/dy):[2]

[math]\displaystyle{ \lambda = \frac{3x_p^2 + a}{2y_p} }[/math]

where a is from the defining equation of the curve, E, above.

Point multiplication

The straightforward way of computing a point multiplication is through repeated addition. However, there are more efficient approaches to computing the multiplication.

Double-and-add

The simplest method is the double-and-add method,[3] similar to square-and-multiply in modular exponentiation. The algorithm works as follows:

To compute sP, start with the binary representation for s: [math]\displaystyle{ s = s_0 + 2s_1 + 2^2s_2 + \cdots + 2^{n-1}s_{n-1} }[/math], where [math]\displaystyle{ s_0 ~..~ s_{n-1} \in \{0, 1\}, n=\lceil \log_2{s} \rceil }[/math].

  • Iterative algorithm, index increasing:
   let bits = bit_representation(s) # the vector of bits (from LSB to MSB) representing s
   let res = [math]\displaystyle{ \begin{align}\mathcal{O}\end{align} }[/math] # point at infinity
   let temp = P # track doubled P val
   for bit in bits: 
       if bit == 1:            
           res = res + temp # point add
       temp = temp + temp # double
   return res
  • Iterative algorithm, index decreasing:
   let bits = bit_representation(s) # the vector of bits (from LSB to MSB) representing s
   let i = length(bits) - 2
   let res = P
   while (i >= 0): # traversing from second MSB to LSB
       res = res + res # double
       if bits[i] == 1:            
           res = res + P # add
       i = i - 1
   return res

Note that both of the iterative methods above are vulnerable to timing analysis. See Montgomery Ladder below for an alternative approach.

  • Recursive algorithm:
  f(P, d) is
     if d = 0 then
         return 0                         # computation complete
     else if d = 1 then
         return P
     else if d mod 2 = 1 then
         return point_add(P, f(P, d - 1)) # addition when d is odd
     else
         return f(point_double(P), d / 2)   # doubling when d is even

where f is the function for multiplying, P is the coordinate to multiply, d is the number of times to add the coordinate to itself. Example: 100P can be written as 2(2[P + 2(2[2(P + 2P)])]) and thus requires six point double operations and two point addition operations. 100P would be equal to f(P, 100).

This algorithm requires log2(d) iterations of point doubling and addition to compute the full point multiplication. There are many variations of this algorithm such as using a window, sliding window, NAF, NAF-w, vector chains, and Montgomery ladder.

Windowed method

In the windowed version of this algorithm,[3] one selects a window size w and computes all [math]\displaystyle{ 2^w }[/math] values of [math]\displaystyle{ dP }[/math] for [math]\displaystyle{ d = 0, 1, 2, \dots, 2^w - 1 }[/math]. The algorithm now uses the representation [math]\displaystyle{ d = d_0 + 2^wd_1 + 2^{2w}d_2 + \cdots + 2^{mw}d_m }[/math] and becomes

  Q ← 0
  for i from m to 0 do
      Q ← point_double_repeat(Q, w)
      if di > 0 then
          Q ← point_add(Q, diP) # using pre-computed value of diP
  return Q

This algorithm has the same complexity as the double-and-add approach with the benefit of using fewer point additions (which in practice are slower than doubling). Typically, the value of w is chosen to be fairly small making the pre-computation stage a trivial component of the algorithm. For the NIST recommended curves, [math]\displaystyle{ w = 4 }[/math] is usually the best selection. The entire complexity for a n-bit number is measured as [math]\displaystyle{ n + 1 }[/math] point doubles and [math]\displaystyle{ 2^w - 2 + \tfrac{n}{w} }[/math] point additions.

Sliding-window method

In the sliding-window version, we look to trade off point additions for point doubles. We compute a similar table as in the windowed version except we only compute the points [math]\displaystyle{ dP }[/math] for [math]\displaystyle{ d = 2^{w-1}, 2^{w-1} + 1, \dots, 2^w - 1 }[/math]. Effectively, we are only computing the values for which the most significant bit of the window is set. The algorithm then uses the original double-and-add representation of [math]\displaystyle{ d = d_0 + 2d_1 + 2^2d_2 + \cdots + 2^md_m }[/math].

  Q ← 0
  for i from m downto 0 do
      if di = 0 then
          Q ← point_double(Q)
      else 
          t ← extract j (up to w − 1) additional bits from d (including di)
          i ← i − j
          if j < w then
              Perform double-and-add using t 
              return Q
          else 
              Q ← point_double_repeat(Q, w)
              Q ← point_add(Q, tP)
  return Q

This algorithm has the benefit that the pre-computation stage is roughly half as complex as the normal windowed method while also trading slower point additions for point doublings. In effect, there is little reason to use the windowed method over this approach, except that the former can be implemented in constant time. The algorithm requires [math]\displaystyle{ w - 1 + n }[/math] point doubles and at most [math]\displaystyle{ 2^{w-1} - 1 + \tfrac{n}{w} }[/math] point additions.

w-ary non-adjacent form (wNAF) method

In the non-adjacent form we aim to make use of the fact that point subtraction is just as easy as point addition to perform fewer (of either) as compared to a sliding-window method. The NAF of the multiplicand [math]\displaystyle{ d }[/math] must be computed first with the following algorithm

   i ← 0
   while (d > 0) do
       if (d mod 2) = 1 then 
           di ← d mods 2w
           d ← d − di
       else
           di = 0
       d ← d/2
       i ← i + 1
   return (di−1, di-2, …, d0)

Where the signed modulo function mods is defined as

   if (d mod 2w) >= 2w−1
       return (d mod 2w) − 2w
   else
       return d mod 2w

This produces the NAF needed to now perform the multiplication. This algorithm requires the pre-computation of the points [math]\displaystyle{ \lbrace 1, 3, 5, \dots, 2^{w-1} - 1 \rbrace P }[/math] and their negatives, where [math]\displaystyle{ P }[/math] is the point to be multiplied. On typical Weierstrass curves, if [math]\displaystyle{ P = \lbrace x, y \rbrace }[/math] then [math]\displaystyle{ -P = \lbrace x, -y \rbrace }[/math]. So in essence the negatives are cheap to compute. Next, the following algorithm computes the multiplication [math]\displaystyle{ dP }[/math]:

   Q ← 0
   for j ← i − 1 downto 0 do
       Q ← point_double(Q)
       if (dj != 0)
           Q ← point_add(Q, djP)
   return Q

The wNAF guarantees that on average there will be a density of [math]\displaystyle{ \tfrac{1}{w + 1} }[/math] point additions (slightly better than the unsigned window). It requires 1 point doubling and [math]\displaystyle{ 2^{w-2} - 1 }[/math] point additions for precomputation. The algorithm then requires [math]\displaystyle{ n }[/math] point doublings and [math]\displaystyle{ \tfrac{n}{w + 1} }[/math] point additions for the rest of the multiplication.

One property of the NAF is that we are guaranteed that every non-zero element [math]\displaystyle{ d_i }[/math] is followed by at least [math]\displaystyle{ w - 1 }[/math] additional zeroes. This is because the algorithm clears out the lower [math]\displaystyle{ w }[/math] bits of [math]\displaystyle{ d }[/math] with every subtraction of the output of the mods function. This observation can be used for several purposes. After every non-zero element the additional zeroes can be implied and do not need to be stored. Secondly, the multiple serial divisions by 2 can be replaced by a division by [math]\displaystyle{ 2^w }[/math] after every non-zero [math]\displaystyle{ d_i }[/math] element and divide by 2 after every zero.

It has been shown that through application of a FLUSH+RELOAD side-channel attack on OpenSSL, the full private key can be revealed after performing cache-timing against as few as 200 signatures performed.[4]

Montgomery ladder

The Montgomery ladder[5] approach computes the point multiplication in a fixed number of operations. This can be beneficial when timing, power consumption, or branch measurements are exposed to an attacker performing a side-channel attack. The algorithm uses the same representation as from double-and-add.

  R0 ← 0
  R1 ← P
  for i from m downto 0 do
      if di = 0 then
          R1 ← point_add(R0, R1)
          R0 ← point_double(R0)
      else
          R0 ← point_add(R0, R1)
          R1 ← point_double(R1)

      // invariant property to maintain correctness
      assert R1 == point_add(R0, P)
  return R0

This algorithm has in effect the same speed as the double-and-add approach except that it computes the same number of point additions and doubles regardless of the value of the multiplicand d. This means that at this level the algorithm does not leak any information through branches or power consumption.

However, it has been shown that through application of a FLUSH+RELOAD side-channel attack on OpenSSL, the full private key can be revealed after performing cache-timing against only one signature at a very low cost.[6]

Rust code for Montgomery Ladder:[7]

/// Constant operation point multiplication. 
/// NOTE: not memory safe.
/// * `s`: scalar value to multiply by
/// * multiplication is defined to be P₀ + P₁ + ... Pₛ
fn sec_mul(&mut self, s: big) -> E521{
    let mut r0 = get_e521_id_point();
    let mut r1 = self.clone();
    for i in (0..=s.significant_bits()).rev()  {
        if s.get_bit(i) {
            r0 = r0.add(&r1);
            r1 = r1.add(&r1.clone());
        } else {
            r1 = r0.add(&r1);
            r0 = r0.add(&r0.clone());
        }
    }
    r0 // r0 = P * s
}

References

  1. "Elliptic Curves - Explicit Addition Formulae". https://crypto.stanford.edu/pbc/notes/elliptic/explicit.html. 
  2. "Elliptic Curves - Explicit Addition Formulae". https://crypto.stanford.edu/pbc/notes/elliptic/explicit.html. 
  3. 3.0 3.1 Hankerson, Darrel; Vanstone, Scott; Menezes, Alfred (2004). Guide to Elliptic Curve Cryptography. Springer Professional Computing. New York: Springer-Verlag. doi:10.1007/b97644. ISBN 0-387-95273-X. 
  4. Benger, Naomi; van de Pol, Joop; Smart, Nigel P.; Yarom, Yuval (2014). ""Ooh Aah... Just a Little Bit" : A Small Amount of Side Channel Can Go a Long Way". in Batina, Lejla; Robshaw, Matthew. Cryptographic Hardware and Embedded Systems – CHES 2014. 8731. Springer. pp. 72–95. doi:10.1007/978-3-662-44709-3_5. ISBN 978-3-662-44708-6. https://www.iacr.org/archive/ches2014/87310103/87310103.pdf. 
  5. Montgomery, Peter L. (1987). "Speeding the Pollard and elliptic curve methods of factorization". Math. Comp. 48 (177): 243–264. doi:10.2307/2007888. 
  6. Yarom, Yuval; Benger, Naomi (2014). "Recovering OpenSSL ECDSA Nonces Using the FLUSH+RELOAD Cache Side-channel Attack". IACR Cryptology ePrint Archive. https://eprint.iacr.org/2014/140. 
  7. Ray, Dustin. "E521". https://github.com/drcapybara/e521-rust.