// Copyright 2022 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.

// Code generated by generate.go. DO NOT EDIT.

package nistec

import (
	
	
	
	
)

// p521ElementLength is the length of an element of the base or scalar field,
// which have the same bytes length for all NIST P curves.
const p521ElementLength = 66

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

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

// SetGenerator sets p to the canonical generator and returns p.
func ( *P521Point) () *P521Point {
	.x.SetBytes([]byte{0x0, 0xc6, 0x85, 0x8e, 0x6, 0xb7, 0x4, 0x4, 0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x5, 0x3f, 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff, 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66})
	.y.SetBytes([]byte{0x1, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x4, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40, 0xc5, 0x50, 0xb9, 0x1, 0x3f, 0xad, 0x7, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50})
	.z.One()
	return 
}

// Set sets p = q and returns p.
func ( *P521Point) ( *P521Point) *P521Point {
	.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 ( *P521Point) ( []byte) (*P521Point, error) {
	switch {
	// Point at infinity.
	case len() == 1 && [0] == 0:
		return .Set(NewP521Point()), nil

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

	// Compressed form.
	case len() == 1+p521ElementLength && ([0] == 2 || [0] == 3):
		,  := new(fiat.P521Element).SetBytes([1:])
		if  != nil {
			return nil, 
		}

		// y² = x³ - 3x + b
		 := p521Polynomial(new(fiat.P521Element), )
		if !p521Sqrt(, ) {
			return nil, errors.New("invalid P521 compressed point encoding")
		}

		// Select the positive or negative root, as indicated by the least
		// significant bit, based on the encoding type byte.
		 := new(fiat.P521Element)
		.Sub(, )
		 := .Bytes()[p521ElementLength-1]&1 ^ [0]&1
		.Select(, , int())

		.x.Set()
		.y.Set()
		.z.One()
		return , nil

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

var _p521B *fiat.P521Element
var _p521BOnce sync.Once

func p521B() *fiat.P521Element {
	_p521BOnce.Do(func() {
		_p521B, _ = new(fiat.P521Element).SetBytes([]byte{0x0, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x9, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1, 0xbf, 0x7, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x0})
	})
	return _p521B
}

// p521Polynomial sets y2 to x³ - 3x + b, and returns y2.
func p521Polynomial(,  *fiat.P521Element) *fiat.P521Element {
	.Square()
	.Mul(, )

	 := new(fiat.P521Element).Add(, )
	.Add(, )
	.Sub(, )

	return .Add(, p521B())
}

func p521CheckOnCurve(,  *fiat.P521Element) error {
	// y² = x³ - 3x + b
	 := p521Polynomial(new(fiat.P521Element), )
	 := new(fiat.P521Element).Square()
	if .Equal() != 1 {
		return errors.New("P521 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 ( *P521Point) () []byte {
	// This function is outlined to make the allocations inline in the caller
	// rather than happen on the heap.
	var  [1 + 2*p521ElementLength]byte
	return .bytes(&)
}

func ( *P521Point) ( *[1 + 2*p521ElementLength]byte) []byte {
	if .z.IsZero() == 1 {
		return append([:0], 0)
	}

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

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

// BytesX returns the encoding of the x-coordinate of p, as specified in SEC 1,
// Version 2.0, Section 2.3.5, or an error if p is the point at infinity.
func ( *P521Point) () ([]byte, error) {
	// This function is outlined to make the allocations inline in the caller
	// rather than happen on the heap.
	var  [p521ElementLength]byte
	return .bytesX(&)
}

func ( *P521Point) ( *[p521ElementLength]byte) ([]byte, error) {
	if .z.IsZero() == 1 {
		return nil, errors.New("P521 point is the point at infinity")
	}

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

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

// BytesCompressed returns the compressed 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 ( *P521Point) () []byte {
	// This function is outlined to make the allocations inline in the caller
	// rather than happen on the heap.
	var  [1 + p521ElementLength]byte
	return .bytesCompressed(&)
}

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

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

	// Encode the sign of the y coordinate (indicated by the least significant
	// bit) as the encoding type (2 or 3).
	 := append([:0], 2)
	[0] |= .Bytes()[p521ElementLength-1] & 1
	 = append(, .Bytes()...)
	return 
}

// Add sets q = p1 + p2, and returns q. The points may overlap.
func ( *P521Point) (,  *P521Point) *P521Point {
	// 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.P521Element).Mul(.x, .x)  // t0 := X1 * X2
	 := new(fiat.P521Element).Mul(.y, .y)  // t1 := Y1 * Y2
	 := new(fiat.P521Element).Mul(.z, .z)  // t2 := Z1 * Z2
	 := new(fiat.P521Element).Add(.x, .y)  // t3 := X1 + Y1
	 := new(fiat.P521Element).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.P521Element).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.P521Element).Add(.x, .z)  // Y3 := X2 + Z2
	.Mul(, )                               // X3 := X3 * Y3
	.Add(, )                               // Y3 := t0 + t2
	.Sub(, )                               // Y3 := X3 - Y3
	 := new(fiat.P521Element).Mul(p521B(), ) // Z3 := b * t2
	.Sub(, )                               // X3 := Y3 - Z3
	.Add(, )                               // Z3 := X3 + X3
	.Add(, )                               // X3 := X3 + Z3
	.Sub(, )                               // Z3 := t1 - X3
	.Add(, )                               // X3 := t1 + X3
	.Mul(p521B(), )                          // 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 ( *P521Point) ( *P521Point) *P521Point {
	// 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.P521Element).Square(.x)      // t0 := X ^ 2
	 := new(fiat.P521Element).Square(.y)      // t1 := Y ^ 2
	 := new(fiat.P521Element).Square(.z)      // t2 := Z ^ 2
	 := new(fiat.P521Element).Mul(.x, .y)    // t3 := X * Y
	.Add(, )                               // t3 := t3 + t3
	 := new(fiat.P521Element).Mul(.x, .z)    // Z3 := X * Z
	.Add(, )                               // Z3 := Z3 + Z3
	 := new(fiat.P521Element).Mul(p521B(), ) // Y3 := b * t2
	.Sub(, )                               // Y3 := Y3 - Z3
	 := new(fiat.P521Element).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(p521B(), )                          // 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 ( *P521Point) (,  *P521Point,  int) *P521Point {
	.x.Select(.x, .x, )
	.y.Select(.y, .y, )
	.z.Select(.z, .z, )
	return 
}

// A p521Table holds the first 15 multiples of a point at offset -1, so [1]P
// is at table[0], [15]P is at table[14], and [0]P is implicitly the identity
// point.
type p521Table [15]*P521Point

// Select selects the n-th multiple of the table base point into p. It works in
// constant time by iterating over every entry of the table. n must be in [0, 15].
func ( *p521Table) ( *P521Point,  uint8) {
	if  >= 16 {
		panic("nistec: internal error: p521Table called with out-of-bounds value")
	}
	.Set(NewP521Point())
	for  := uint8(1);  < 16; ++ {
		 := subtle.ConstantTimeByteEq(, )
		.Select([-1], , )
	}
}

// ScalarMult sets p = scalar * q, and returns p.
func ( *P521Point) ( *P521Point,  []byte) (*P521Point, error) {
	// Compute a p521Table for the base point q. The explicit NewP521Point
	// calls get inlined, letting the allocations live on the stack.
	var  = p521Table{NewP521Point(), NewP521Point(), NewP521Point(),
		NewP521Point(), NewP521Point(), NewP521Point(), NewP521Point(),
		NewP521Point(), NewP521Point(), NewP521Point(), NewP521Point(),
		NewP521Point(), NewP521Point(), NewP521Point(), NewP521Point()}
	[0].Set()
	for  := 1;  < 15;  += 2 {
		[].Double([/2])
		[+1].Add([], )
	}

	// 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.
	 := NewP521Point()
	.Set(NewP521Point())
	for ,  := range  {
		// No need to double on the first iteration, as p is the identity at
		// this point, and [N]∞ = ∞.
		if  != 0 {
			.Double()
			.Double()
			.Double()
			.Double()
		}

		 :=  >> 4
		.Select(, )
		.Add(, )

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

		 =  & 0b1111
		.Select(, )
		.Add(, )
	}

	return , nil
}

var p521GeneratorTable *[p521ElementLength * 2]p521Table
var p521GeneratorTableOnce sync.Once

// generatorTable returns a sequence of p521Tables. The first table contains
// multiples of G. Each successive table is the previous table doubled four
// times.
func ( *P521Point) () *[p521ElementLength * 2]p521Table {
	p521GeneratorTableOnce.Do(func() {
		p521GeneratorTable = new([p521ElementLength * 2]p521Table)
		 := NewP521Point().SetGenerator()
		for  := 0;  < p521ElementLength*2; ++ {
			p521GeneratorTable[][0] = NewP521Point().Set()
			for  := 1;  < 15; ++ {
				p521GeneratorTable[][] = NewP521Point().Add(p521GeneratorTable[][-1], )
			}
			.Double()
			.Double()
			.Double()
			.Double()
		}
	})
	return p521GeneratorTable
}

// ScalarBaseMult sets p = scalar * B, where B is the canonical generator, and
// returns p.
func ( *P521Point) ( []byte) (*P521Point, error) {
	if len() != p521ElementLength {
		return nil, errors.New("invalid scalar length")
	}
	 := .generatorTable()

	// This is also a scalar multiplication with a four-bit window like in
	// ScalarMult, but in this case the doublings are precomputed. The value
	// [windowValue]G added at iteration k would normally get doubled
	// (totIterations-k)×4 times, but with a larger precomputation we can
	// instead add [2^((totIterations-k)×4)][windowValue]G and avoid the
	// doublings between iterations.
	 := NewP521Point()
	.Set(NewP521Point())
	 := len() - 1
	for ,  := range  {
		 :=  >> 4
		[].Select(, )
		.Add(, )
		--

		 =  & 0b1111
		[].Select(, )
		.Add(, )
		--
	}

	return , nil
}

// p521Sqrt sets e to a square root of x. If x is not a square, p521Sqrt returns
// false and e is unchanged. e and x can overlap.
func p521Sqrt(,  *fiat.P521Element) ( bool) {
	 := new(fiat.P521Element)
	p521SqrtCandidate(, )
	 := new(fiat.P521Element).Square()
	if .Equal() != 1 {
		return false
	}
	.Set()
	return true
}

// p521SqrtCandidate sets z to a square root candidate for x. z and x must not overlap.
func p521SqrtCandidate(,  *fiat.P521Element) {
	// Since p = 3 mod 4, exponentiation by (p + 1) / 4 yields a square root candidate.
	//
	// The sequence of 0 multiplications and 519 squarings is derived from the
	// following addition chain generated with github.com/mmcloughlin/addchain v0.4.0.
	//
	//	return  1 << 519
	//

	.Square()
	for  := 1;  < 519; ++ {
		.Square()
	}
}