github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/crypto/elliptic/internal/nistec/p384.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package nistec
     6  
     7  import (
     8  	"crypto/elliptic/internal/fiat"
     9  	"crypto/subtle"
    10  	"errors"
    11  )
    12  
    13  var p384B, _ = new(fiat.P384Element).SetBytes([]byte{
    14  	0xb3, 0x31, 0x2f, 0xa7, 0xe2, 0x3e, 0xe7, 0xe4, 0x98, 0x8e, 0x05, 0x6b,
    15  	0xe3, 0xf8, 0x2d, 0x19, 0x18, 0x1d, 0x9c, 0x6e, 0xfe, 0x81, 0x41, 0x12,
    16  	0x03, 0x14, 0x08, 0x8f, 0x50, 0x13, 0x87, 0x5a, 0xc6, 0x56, 0x39, 0x8d,
    17  	0x8a, 0x2e, 0xd1, 0x9d, 0x2a, 0x85, 0xc8, 0xed, 0xd3, 0xec, 0x2a, 0xef})
    18  
    19  var p384G, _ = NewP384Point().SetBytes([]byte{0x4,
    20  	0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37, 0x8e, 0xb1, 0xc7, 0x1e,
    21  	0xf3, 0x20, 0xad, 0x74, 0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98,
    22  	0x59, 0xf7, 0x41, 0xe0, 0x82, 0x54, 0x2a, 0x38, 0x55, 0x02, 0xf2, 0x5d,
    23  	0xbf, 0x55, 0x29, 0x6c, 0x3a, 0x54, 0x5e, 0x38, 0x72, 0x76, 0x0a, 0xb7,
    24  	0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f, 0x5d, 0x9e, 0x98, 0xbf,
    25  	0x92, 0x92, 0xdc, 0x29, 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c,
    26  	0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0, 0x0a, 0x60, 0xb1, 0xce,
    27  	0x1d, 0x7e, 0x81, 0x9d, 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0x0e, 0x5f})
    28  
    29  const p384ElementLength = 48
    30  
    31  // P384Point is a P-384 point. The zero value is NOT valid.
    32  type P384Point struct {
    33  	// The point is represented in projective coordinates (X:Y:Z),
    34  	// where x = X/Z and y = Y/Z.
    35  	x, y, z *fiat.P384Element
    36  }
    37  
    38  // NewP384Point returns a new P384Point representing the point at infinity point.
    39  func NewP384Point() *P384Point {
    40  	return &P384Point{
    41  		x: new(fiat.P384Element),
    42  		y: new(fiat.P384Element).One(),
    43  		z: new(fiat.P384Element),
    44  	}
    45  }
    46  
    47  // NewP384Generator returns a new P384Point set to the canonical generator.
    48  func NewP384Generator() *P384Point {
    49  	return (&P384Point{
    50  		x: new(fiat.P384Element),
    51  		y: new(fiat.P384Element),
    52  		z: new(fiat.P384Element),
    53  	}).Set(p384G)
    54  }
    55  
    56  // Set sets p = q and returns p.
    57  func (p *P384Point) Set(q *P384Point) *P384Point {
    58  	p.x.Set(q.x)
    59  	p.y.Set(q.y)
    60  	p.z.Set(q.z)
    61  	return p
    62  }
    63  
    64  // SetBytes sets p to the compressed, uncompressed, or infinity value encoded in
    65  // b, as specified in SEC 1, Version 2.0, Section 2.3.4. If the point is not on
    66  // the curve, it returns nil and an error, and the receiver is unchanged.
    67  // Otherwise, it returns p.
    68  func (p *P384Point) SetBytes(b []byte) (*P384Point, error) {
    69  	switch {
    70  	// Point at infinity.
    71  	case len(b) == 1 && b[0] == 0:
    72  		return p.Set(NewP384Point()), nil
    73  
    74  	// Uncompressed form.
    75  	case len(b) == 1+2*p384ElementLength && b[0] == 4:
    76  		x, err := new(fiat.P384Element).SetBytes(b[1 : 1+p384ElementLength])
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  		y, err := new(fiat.P384Element).SetBytes(b[1+p384ElementLength:])
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  		if err := p384CheckOnCurve(x, y); err != nil {
    85  			return nil, err
    86  		}
    87  		p.x.Set(x)
    88  		p.y.Set(y)
    89  		p.z.One()
    90  		return p, nil
    91  
    92  	// Compressed form
    93  	case len(b) == 1+p384ElementLength && b[0] == 0:
    94  		return nil, errors.New("unimplemented") // TODO(filippo)
    95  
    96  	default:
    97  		return nil, errors.New("invalid P384 point encoding")
    98  	}
    99  }
   100  
   101  func p384CheckOnCurve(x, y *fiat.P384Element) error {
   102  	// x³ - 3x + b.
   103  	x3 := new(fiat.P384Element).Square(x)
   104  	x3.Mul(x3, x)
   105  
   106  	threeX := new(fiat.P384Element).Add(x, x)
   107  	threeX.Add(threeX, x)
   108  
   109  	x3.Sub(x3, threeX)
   110  	x3.Add(x3, p384B)
   111  
   112  	// y² = x³ - 3x + b
   113  	y2 := new(fiat.P384Element).Square(y)
   114  
   115  	if x3.Equal(y2) != 1 {
   116  		return errors.New("P384 point not on curve")
   117  	}
   118  	return nil
   119  }
   120  
   121  // Bytes returns the uncompressed or infinity encoding of p, as specified in
   122  // SEC 1, Version 2.0, Section 2.3.3. Note that the encoding of the point at
   123  // infinity is shorter than all other encodings.
   124  func (p *P384Point) Bytes() []byte {
   125  	// This function is outlined to make the allocations inline in the caller
   126  	// rather than happen on the heap.
   127  	var out [133]byte
   128  	return p.bytes(&out)
   129  }
   130  
   131  func (p *P384Point) bytes(out *[133]byte) []byte {
   132  	if p.z.IsZero() == 1 {
   133  		return append(out[:0], 0)
   134  	}
   135  
   136  	zinv := new(fiat.P384Element).Invert(p.z)
   137  	xx := new(fiat.P384Element).Mul(p.x, zinv)
   138  	yy := new(fiat.P384Element).Mul(p.y, zinv)
   139  
   140  	buf := append(out[:0], 4)
   141  	buf = append(buf, xx.Bytes()...)
   142  	buf = append(buf, yy.Bytes()...)
   143  	return buf
   144  }
   145  
   146  // Add sets q = p1 + p2, and returns q. The points may overlap.
   147  func (q *P384Point) Add(p1, p2 *P384Point) *P384Point {
   148  	// Complete addition formula for a = -3 from "Complete addition formulas for
   149  	// prime order elliptic curves" (https://eprint.iacr.org/2015/1060), §A.2.
   150  
   151  	t0 := new(fiat.P384Element).Mul(p1.x, p2.x) // t0 := X1 * X2
   152  	t1 := new(fiat.P384Element).Mul(p1.y, p2.y) // t1 := Y1 * Y2
   153  	t2 := new(fiat.P384Element).Mul(p1.z, p2.z) // t2 := Z1 * Z2
   154  	t3 := new(fiat.P384Element).Add(p1.x, p1.y) // t3 := X1 + Y1
   155  	t4 := new(fiat.P384Element).Add(p2.x, p2.y) // t4 := X2 + Y2
   156  	t3.Mul(t3, t4)                              // t3 := t3 * t4
   157  	t4.Add(t0, t1)                              // t4 := t0 + t1
   158  	t3.Sub(t3, t4)                              // t3 := t3 - t4
   159  	t4.Add(p1.y, p1.z)                          // t4 := Y1 + Z1
   160  	x3 := new(fiat.P384Element).Add(p2.y, p2.z) // X3 := Y2 + Z2
   161  	t4.Mul(t4, x3)                              // t4 := t4 * X3
   162  	x3.Add(t1, t2)                              // X3 := t1 + t2
   163  	t4.Sub(t4, x3)                              // t4 := t4 - X3
   164  	x3.Add(p1.x, p1.z)                          // X3 := X1 + Z1
   165  	y3 := new(fiat.P384Element).Add(p2.x, p2.z) // Y3 := X2 + Z2
   166  	x3.Mul(x3, y3)                              // X3 := X3 * Y3
   167  	y3.Add(t0, t2)                              // Y3 := t0 + t2
   168  	y3.Sub(x3, y3)                              // Y3 := X3 - Y3
   169  	z3 := new(fiat.P384Element).Mul(p384B, t2)  // Z3 := b * t2
   170  	x3.Sub(y3, z3)                              // X3 := Y3 - Z3
   171  	z3.Add(x3, x3)                              // Z3 := X3 + X3
   172  	x3.Add(x3, z3)                              // X3 := X3 + Z3
   173  	z3.Sub(t1, x3)                              // Z3 := t1 - X3
   174  	x3.Add(t1, x3)                              // X3 := t1 + X3
   175  	y3.Mul(p384B, y3)                           // Y3 := b * Y3
   176  	t1.Add(t2, t2)                              // t1 := t2 + t2
   177  	t2.Add(t1, t2)                              // t2 := t1 + t2
   178  	y3.Sub(y3, t2)                              // Y3 := Y3 - t2
   179  	y3.Sub(y3, t0)                              // Y3 := Y3 - t0
   180  	t1.Add(y3, y3)                              // t1 := Y3 + Y3
   181  	y3.Add(t1, y3)                              // Y3 := t1 + Y3
   182  	t1.Add(t0, t0)                              // t1 := t0 + t0
   183  	t0.Add(t1, t0)                              // t0 := t1 + t0
   184  	t0.Sub(t0, t2)                              // t0 := t0 - t2
   185  	t1.Mul(t4, y3)                              // t1 := t4 * Y3
   186  	t2.Mul(t0, y3)                              // t2 := t0 * Y3
   187  	y3.Mul(x3, z3)                              // Y3 := X3 * Z3
   188  	y3.Add(y3, t2)                              // Y3 := Y3 + t2
   189  	x3.Mul(t3, x3)                              // X3 := t3 * X3
   190  	x3.Sub(x3, t1)                              // X3 := X3 - t1
   191  	z3.Mul(t4, z3)                              // Z3 := t4 * Z3
   192  	t1.Mul(t3, t0)                              // t1 := t3 * t0
   193  	z3.Add(z3, t1)                              // Z3 := Z3 + t1
   194  
   195  	q.x.Set(x3)
   196  	q.y.Set(y3)
   197  	q.z.Set(z3)
   198  	return q
   199  }
   200  
   201  // Double sets q = p + p, and returns q. The points may overlap.
   202  func (q *P384Point) Double(p *P384Point) *P384Point {
   203  	// Complete addition formula for a = -3 from "Complete addition formulas for
   204  	// prime order elliptic curves" (https://eprint.iacr.org/2015/1060), §A.2.
   205  
   206  	t0 := new(fiat.P384Element).Square(p.x)    // t0 := X ^ 2
   207  	t1 := new(fiat.P384Element).Square(p.y)    // t1 := Y ^ 2
   208  	t2 := new(fiat.P384Element).Square(p.z)    // t2 := Z ^ 2
   209  	t3 := new(fiat.P384Element).Mul(p.x, p.y)  // t3 := X * Y
   210  	t3.Add(t3, t3)                             // t3 := t3 + t3
   211  	z3 := new(fiat.P384Element).Mul(p.x, p.z)  // Z3 := X * Z
   212  	z3.Add(z3, z3)                             // Z3 := Z3 + Z3
   213  	y3 := new(fiat.P384Element).Mul(p384B, t2) // Y3 := b * t2
   214  	y3.Sub(y3, z3)                             // Y3 := Y3 - Z3
   215  	x3 := new(fiat.P384Element).Add(y3, y3)    // X3 := Y3 + Y3
   216  	y3.Add(x3, y3)                             // Y3 := X3 + Y3
   217  	x3.Sub(t1, y3)                             // X3 := t1 - Y3
   218  	y3.Add(t1, y3)                             // Y3 := t1 + Y3
   219  	y3.Mul(x3, y3)                             // Y3 := X3 * Y3
   220  	x3.Mul(x3, t3)                             // X3 := X3 * t3
   221  	t3.Add(t2, t2)                             // t3 := t2 + t2
   222  	t2.Add(t2, t3)                             // t2 := t2 + t3
   223  	z3.Mul(p384B, z3)                          // Z3 := b * Z3
   224  	z3.Sub(z3, t2)                             // Z3 := Z3 - t2
   225  	z3.Sub(z3, t0)                             // Z3 := Z3 - t0
   226  	t3.Add(z3, z3)                             // t3 := Z3 + Z3
   227  	z3.Add(z3, t3)                             // Z3 := Z3 + t3
   228  	t3.Add(t0, t0)                             // t3 := t0 + t0
   229  	t0.Add(t3, t0)                             // t0 := t3 + t0
   230  	t0.Sub(t0, t2)                             // t0 := t0 - t2
   231  	t0.Mul(t0, z3)                             // t0 := t0 * Z3
   232  	y3.Add(y3, t0)                             // Y3 := Y3 + t0
   233  	t0.Mul(p.y, p.z)                           // t0 := Y * Z
   234  	t0.Add(t0, t0)                             // t0 := t0 + t0
   235  	z3.Mul(t0, z3)                             // Z3 := t0 * Z3
   236  	x3.Sub(x3, z3)                             // X3 := X3 - Z3
   237  	z3.Mul(t0, t1)                             // Z3 := t0 * t1
   238  	z3.Add(z3, z3)                             // Z3 := Z3 + Z3
   239  	z3.Add(z3, z3)                             // Z3 := Z3 + Z3
   240  
   241  	q.x.Set(x3)
   242  	q.y.Set(y3)
   243  	q.z.Set(z3)
   244  	return q
   245  }
   246  
   247  // Select sets q to p1 if cond == 1, and to p2 if cond == 0.
   248  func (q *P384Point) Select(p1, p2 *P384Point, cond int) *P384Point {
   249  	q.x.Select(p1.x, p2.x, cond)
   250  	q.y.Select(p1.y, p2.y, cond)
   251  	q.z.Select(p1.z, p2.z, cond)
   252  	return q
   253  }
   254  
   255  // ScalarMult sets p = scalar * q, and returns p.
   256  func (p *P384Point) ScalarMult(q *P384Point, scalar []byte) *P384Point {
   257  	// table holds the first 16 multiples of q. The explicit newP384Point calls
   258  	// get inlined, letting the allocations live on the stack.
   259  	var table = [16]*P384Point{
   260  		NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),
   261  		NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),
   262  		NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),
   263  		NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),
   264  	}
   265  	for i := 1; i < 16; i++ {
   266  		table[i].Add(table[i-1], q)
   267  	}
   268  
   269  	// Instead of doing the classic double-and-add chain, we do it with a
   270  	// four-bit window: we double four times, and then add [0-15]P.
   271  	t := NewP384Point()
   272  	p.Set(NewP384Point())
   273  	for _, byte := range scalar {
   274  		p.Double(p)
   275  		p.Double(p)
   276  		p.Double(p)
   277  		p.Double(p)
   278  
   279  		for i := uint8(0); i < 16; i++ {
   280  			cond := subtle.ConstantTimeByteEq(byte>>4, i)
   281  			t.Select(table[i], t, cond)
   282  		}
   283  		p.Add(p, t)
   284  
   285  		p.Double(p)
   286  		p.Double(p)
   287  		p.Double(p)
   288  		p.Double(p)
   289  
   290  		for i := uint8(0); i < 16; i++ {
   291  			cond := subtle.ConstantTimeByteEq(byte&0b1111, i)
   292  			t.Select(table[i], t, cond)
   293  		}
   294  		p.Add(p, t)
   295  	}
   296  
   297  	return p
   298  }