github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/crypto/internal/edwards25519/edwards25519.go (about)

     1  // Copyright (c) 2017 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 edwards25519
     6  
     7  import (
     8  	"github.com/shogo82148/std/crypto/internal/edwards25519/field"
     9  )
    10  
    11  // Point represents a point on the edwards25519 curve.
    12  //
    13  // This type works similarly to math/big.Int, and all arguments and receivers
    14  // are allowed to alias.
    15  //
    16  // The zero value is NOT valid, and it may be used only as a receiver.
    17  type Point struct {
    18  	// Make the type not comparable (i.e. used with == or as a map key), as
    19  	// equivalent points can be represented by different Go values.
    20  	_ incomparable
    21  
    22  	// The point is internally represented in extended coordinates (X, Y, Z, T)
    23  	// where x = X/Z, y = Y/Z, and xy = T/Z per https://eprint.iacr.org/2008/522.
    24  	x, y, z, t field.Element
    25  }
    26  
    27  // identity is the point at infinity.
    28  var _ = new(Point).SetBytes([]byte{
    29  	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    30  	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
    31  
    32  // NewIdentityPoint returns a new Point set to the identity.
    33  func NewIdentityPoint() *Point
    34  
    35  // generator is the canonical curve basepoint. See TestGenerator for the
    36  // correspondence of this encoding with the values in RFC 8032.
    37  var _ = new(Point).SetBytes([]byte{
    38  	0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
    39  	0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
    40  	0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
    41  	0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66})
    42  
    43  // NewGeneratorPoint returns a new Point set to the canonical generator.
    44  func NewGeneratorPoint() *Point
    45  
    46  // Set sets v = u, and returns v.
    47  func (v *Point) Set(u *Point) *Point
    48  
    49  // Bytes returns the canonical 32-byte encoding of v, according to RFC 8032,
    50  // Section 5.1.2.
    51  func (v *Point) Bytes() []byte
    52  
    53  // SetBytes sets v = x, where x is a 32-byte encoding of v. If x does not
    54  // represent a valid point on the curve, SetBytes returns nil and an error and
    55  // the receiver is unchanged. Otherwise, SetBytes returns v.
    56  //
    57  // Note that SetBytes accepts all non-canonical encodings of valid points.
    58  // That is, it follows decoding rules that match most implementations in
    59  // the ecosystem rather than RFC 8032.
    60  func (v *Point) SetBytes(x []byte) (*Point, error)
    61  
    62  // d is a constant in the curve equation.
    63  var _ = new(field.Element).SetBytes([]byte{
    64  	0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
    65  	0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
    66  	0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
    67  	0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52})
    68  
    69  // Add sets v = p + q, and returns v.
    70  func (v *Point) Add(p, q *Point) *Point
    71  
    72  // Subtract sets v = p - q, and returns v.
    73  func (v *Point) Subtract(p, q *Point) *Point
    74  
    75  // Negate sets v = -p, and returns v.
    76  func (v *Point) Negate(p *Point) *Point
    77  
    78  // Equal returns 1 if v is equivalent to u, and 0 otherwise.
    79  func (v *Point) Equal(u *Point) int