// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package nistec

import (
	
	
	
)

var p224B, _ = new(fiat.P224Element).SetBytes([]byte{0xb4, 0x05, 0x0a, 0x85,
	0x0c, 0x04, 0xb3, 0xab, 0xf5, 0x41, 0x32, 0x56, 0x50, 0x44, 0xb0, 0xb7,
	0xd7, 0xbf, 0xd8, 0xba, 0x27, 0x0b, 0x39, 0x43, 0x23, 0x55, 0xff, 0xb4})

var p224G, _ = NewP224Point().SetBytes([]byte{0x04,
	0xb7, 0x0e, 0x0c, 0xbd, 0x6b, 0xb4, 0xbf, 0x7f, 0x32, 0x13, 0x90, 0xb9,
	0x4a, 0x03, 0xc1, 0xd3, 0x56, 0xc2, 0x11, 0x22, 0x34, 0x32, 0x80, 0xd6,
	0x11, 0x5c, 0x1d, 0x21, 0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb,
	0x4c, 0x22, 0xdf, 0xe6, 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64,
	0x44, 0xd5, 0x81, 0x99, 0x85, 0x0, 0x7e, 0x34})

const p224ElementLength = 28

// P224Point is a P-224 point. The zero value is NOT valid.
type P224Point struct {
	// The point is represented in projective coordinates (X:Y:Z),
	// where x = X/Z and y = Y/Z.
	x, y, z *fiat.P224Element
}

// NewP224Point returns a new P224Point representing the point at infinity point.
func () *P224Point {
	return &P224Point{
		x: new(fiat.P224Element),
		y: new(fiat.P224Element).One(),
		z: new(fiat.P224Element),
	}
}

// NewP224Generator returns a new P224Point set to the canonical generator.
func () *P224Point {
	return (&P224Point{
		x: new(fiat.P224Element),
		y: new(fiat.P224Element),
		z: new(fiat.P224Element),
	}).Set(p224G)
}

// Set sets p = q and returns p.
func ( *P224Point) ( *P224Point) *P224Point {
	.x.Set(.x)
	.y.Set(.y)
	.z.Set(.z)
	return 
}

// SetBytes sets p to the compressed, uncompressed, or infinity value encoded in
// b, as specified in SEC 1, Version 2.0, Section 2.3.4. If the point is not on
// the curve, it returns nil and an error, and the receiver is unchanged.
// Otherwise, it returns p.
func ( *P224Point) ( []byte) (*P224Point, error) {
	switch {
	// Point at infinity.
	case len() == 1 && [0] == 0:
		return .Set(NewP224Point()), nil

	// Uncompressed form.
	case len() == 1+2*p224ElementLength && [0] == 4:
		,  := new(fiat.P224Element).SetBytes([1 : 1+p224ElementLength])
		if  != nil {
			return nil, 
		}
		,  := new(fiat.P224Element).SetBytes([1+p224ElementLength:])
		if  != nil {
			return nil, 
		}
		if  := p224CheckOnCurve(, );  != nil {
			return nil, 
		}
		.x.Set()
		.y.Set()
		.z.One()
		return , nil

	// Compressed form
	case len() == 1+p224ElementLength && [0] == 0:
		return nil, errors.New("unimplemented") // TODO(filippo)

	default:
		return nil, errors.New("invalid P224 point encoding")
	}
}

func (,  *fiat.P224Element) error {
	// x³ - 3x + b.
	 := new(fiat.P224Element).Square()
	.Mul(, )

	 := new(fiat.P224Element).Add(, )
	.Add(, )

	.Sub(, )
	.Add(, p224B)

	// y² = x³ - 3x + b
	 := new(fiat.P224Element).Square()

	if .Equal() != 1 {
		return errors.New("P224 point not on curve")
	}
	return nil
}

// Bytes returns the uncompressed or infinity encoding of p, as specified in
// SEC 1, Version 2.0, Section 2.3.3. Note that the encoding of the point at
// infinity is shorter than all other encodings.
func ( *P224Point) () []byte {
	// This function is outlined to make the allocations inline in the caller
	// rather than happen on the heap.
	var  [133]byte
	return .bytes(&)
}

func ( *P224Point) ( *[133]byte) []byte {
	if .z.IsZero() == 1 {
		return append([:0], 0)
	}

	 := new(fiat.P224Element).Invert(.z)
	 := new(fiat.P224Element).Mul(.x, )
	 := new(fiat.P224Element).Mul(.y, )

	 := append([:0], 4)
	 = append(, .Bytes()...)
	 = append(, .Bytes()...)
	return 
}

// Add sets q = p1 + p2, and returns q. The points may overlap.
func ( *P224Point) (,  *P224Point) *P224Point {
	// Complete addition formula for a = -3 from "Complete addition formulas for
	// prime order elliptic curves" (https://eprint.iacr.org/2015/1060), §A.2.

	 := new(fiat.P224Element).Mul(.x, .x) // t0 := X1 * X2
	 := new(fiat.P224Element).Mul(.y, .y) // t1 := Y1 * Y2
	 := new(fiat.P224Element).Mul(.z, .z) // t2 := Z1 * Z2
	 := new(fiat.P224Element).Add(.x, .y) // t3 := X1 + Y1
	 := new(fiat.P224Element).Add(.x, .y) // t4 := X2 + Y2
	.Mul(, )                              // t3 := t3 * t4
	.Add(, )                              // t4 := t0 + t1
	.Sub(, )                              // t3 := t3 - t4
	.Add(.y, .z)                          // t4 := Y1 + Z1
	 := new(fiat.P224Element).Add(.y, .z) // X3 := Y2 + Z2
	.Mul(, )                              // t4 := t4 * X3
	.Add(, )                              // X3 := t1 + t2
	.Sub(, )                              // t4 := t4 - X3
	.Add(.x, .z)                          // X3 := X1 + Z1
	 := new(fiat.P224Element).Add(.x, .z) // Y3 := X2 + Z2
	.Mul(, )                              // X3 := X3 * Y3
	.Add(, )                              // Y3 := t0 + t2
	.Sub(, )                              // Y3 := X3 - Y3
	 := new(fiat.P224Element).Mul(p224B, )  // Z3 := b * t2
	.Sub(, )                              // X3 := Y3 - Z3
	.Add(, )                              // Z3 := X3 + X3
	.Add(, )                              // X3 := X3 + Z3
	.Sub(, )                              // Z3 := t1 - X3
	.Add(, )                              // X3 := t1 + X3
	.Mul(p224B, )                           // Y3 := b * Y3
	.Add(, )                              // t1 := t2 + t2
	.Add(, )                              // t2 := t1 + t2
	.Sub(, )                              // Y3 := Y3 - t2
	.Sub(, )                              // Y3 := Y3 - t0
	.Add(, )                              // t1 := Y3 + Y3
	.Add(, )                              // Y3 := t1 + Y3
	.Add(, )                              // t1 := t0 + t0
	.Add(, )                              // t0 := t1 + t0
	.Sub(, )                              // t0 := t0 - t2
	.Mul(, )                              // t1 := t4 * Y3
	.Mul(, )                              // t2 := t0 * Y3
	.Mul(, )                              // Y3 := X3 * Z3
	.Add(, )                              // Y3 := Y3 + t2
	.Mul(, )                              // X3 := t3 * X3
	.Sub(, )                              // X3 := X3 - t1
	.Mul(, )                              // Z3 := t4 * Z3
	.Mul(, )                              // t1 := t3 * t0
	.Add(, )                              // Z3 := Z3 + t1

	.x.Set()
	.y.Set()
	.z.Set()
	return 
}

// Double sets q = p + p, and returns q. The points may overlap.
func ( *P224Point) ( *P224Point) *P224Point {
	// Complete addition formula for a = -3 from "Complete addition formulas for
	// prime order elliptic curves" (https://eprint.iacr.org/2015/1060), §A.2.

	 := new(fiat.P224Element).Square(.x)    // t0 := X ^ 2
	 := new(fiat.P224Element).Square(.y)    // t1 := Y ^ 2
	 := new(fiat.P224Element).Square(.z)    // t2 := Z ^ 2
	 := new(fiat.P224Element).Mul(.x, .y)  // t3 := X * Y
	.Add(, )                             // t3 := t3 + t3
	 := new(fiat.P224Element).Mul(.x, .z)  // Z3 := X * Z
	.Add(, )                             // Z3 := Z3 + Z3
	 := new(fiat.P224Element).Mul(p224B, ) // Y3 := b * t2
	.Sub(, )                             // Y3 := Y3 - Z3
	 := new(fiat.P224Element).Add(, )    // X3 := Y3 + Y3
	.Add(, )                             // Y3 := X3 + Y3
	.Sub(, )                             // X3 := t1 - Y3
	.Add(, )                             // Y3 := t1 + Y3
	.Mul(, )                             // Y3 := X3 * Y3
	.Mul(, )                             // X3 := X3 * t3
	.Add(, )                             // t3 := t2 + t2
	.Add(, )                             // t2 := t2 + t3
	.Mul(p224B, )                          // Z3 := b * Z3
	.Sub(, )                             // Z3 := Z3 - t2
	.Sub(, )                             // Z3 := Z3 - t0
	.Add(, )                             // t3 := Z3 + Z3
	.Add(, )                             // Z3 := Z3 + t3
	.Add(, )                             // t3 := t0 + t0
	.Add(, )                             // t0 := t3 + t0
	.Sub(, )                             // t0 := t0 - t2
	.Mul(, )                             // t0 := t0 * Z3
	.Add(, )                             // Y3 := Y3 + t0
	.Mul(.y, .z)                           // t0 := Y * Z
	.Add(, )                             // t0 := t0 + t0
	.Mul(, )                             // Z3 := t0 * Z3
	.Sub(, )                             // X3 := X3 - Z3
	.Mul(, )                             // Z3 := t0 * t1
	.Add(, )                             // Z3 := Z3 + Z3
	.Add(, )                             // Z3 := Z3 + Z3

	.x.Set()
	.y.Set()
	.z.Set()
	return 
}

// Select sets q to p1 if cond == 1, and to p2 if cond == 0.
func ( *P224Point) (,  *P224Point,  int) *P224Point {
	.x.Select(.x, .x, )
	.y.Select(.y, .y, )
	.z.Select(.z, .z, )
	return 
}

// ScalarMult sets p = scalar * q, and returns p.
func ( *P224Point) ( *P224Point,  []byte) *P224Point {
	// table holds the first 16 multiples of q. The explicit newP224Point calls
	// get inlined, letting the allocations live on the stack.
	var  = [16]*P224Point{
		NewP224Point(), NewP224Point(), NewP224Point(), NewP224Point(),
		NewP224Point(), NewP224Point(), NewP224Point(), NewP224Point(),
		NewP224Point(), NewP224Point(), NewP224Point(), NewP224Point(),
		NewP224Point(), NewP224Point(), NewP224Point(), NewP224Point(),
	}
	for  := 1;  < 16; ++ {
		[].Add([-1], )
	}

	// Instead of doing the classic double-and-add chain, we do it with a
	// four-bit window: we double four times, and then add [0-15]P.
	 := NewP224Point()
	.Set(NewP224Point())
	for ,  := range  {
		.Double()
		.Double()
		.Double()
		.Double()

		for  := uint8(0);  < 16; ++ {
			 := subtle.ConstantTimeByteEq(>>4, )
			.Select([], , )
		}
		.Add(, )

		.Double()
		.Double()
		.Double()
		.Double()

		for  := uint8(0);  < 16; ++ {
			 := subtle.ConstantTimeByteEq(&0b1111, )
			.Select([], , )
		}
		.Add(, )
	}

	return 
}