// 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 nistecimport ()// p384ElementLength is the length of an element of the base or scalar field,// which have the same bytes length for all NIST P curves.constp384ElementLength = 48// P384Point is a P384 point. The zero value is NOT valid.typeP384Pointstruct {// The point is represented in projective coordinates (X:Y:Z), // where x = X/Z and y = Y/Z.x, y, z *fiat.P384Element}// NewP384Point returns a new P384Point representing the point at infinity point.func () *P384Point {return &P384Point{x: new(fiat.P384Element),y: new(fiat.P384Element).One(),z: new(fiat.P384Element), }}// SetGenerator sets p to the canonical generator and returns p.func ( *P384Point) () *P384Point { .x.SetBytes([]byte{0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x5, 0x37, 0x8e, 0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74, 0x6e, 0x1d, 0x3b, 0x62, 0x8b, 0xa7, 0x9b, 0x98, 0x59, 0xf7, 0x41, 0xe0, 0x82, 0x54, 0x2a, 0x38, 0x55, 0x2, 0xf2, 0x5d, 0xbf, 0x55, 0x29, 0x6c, 0x3a, 0x54, 0x5e, 0x38, 0x72, 0x76, 0xa, 0xb7}) .y.SetBytes([]byte{0x36, 0x17, 0xde, 0x4a, 0x96, 0x26, 0x2c, 0x6f, 0x5d, 0x9e, 0x98, 0xbf, 0x92, 0x92, 0xdc, 0x29, 0xf8, 0xf4, 0x1d, 0xbd, 0x28, 0x9a, 0x14, 0x7c, 0xe9, 0xda, 0x31, 0x13, 0xb5, 0xf0, 0xb8, 0xc0, 0xa, 0x60, 0xb1, 0xce, 0x1d, 0x7e, 0x81, 0x9d, 0x7a, 0x43, 0x1d, 0x7c, 0x90, 0xea, 0xe, 0x5f}) .z.One()return}// Set sets p = q and returns p.func ( *P384Point) ( *P384Point) *P384Point { .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 ( *P384Point) ( []byte) (*P384Point, error) {switch {// Point at infinity.caselen() == 1 && [0] == 0:return .Set(NewP384Point()), nil// Uncompressed form.caselen() == 1+2*p384ElementLength && [0] == 4: , := new(fiat.P384Element).SetBytes([1 : 1+p384ElementLength])if != nil {returnnil, } , := new(fiat.P384Element).SetBytes([1+p384ElementLength:])if != nil {returnnil, }if := p384CheckOnCurve(, ); != nil {returnnil, } .x.Set() .y.Set() .z.One()return , nil// Compressed form.caselen() == 1+p384ElementLength && ([0] == 2 || [0] == 3): , := new(fiat.P384Element).SetBytes([1:])if != nil {returnnil, }// y² = x³ - 3x + b := p384Polynomial(new(fiat.P384Element), )if !p384Sqrt(, ) {returnnil, errors.New("invalid P384 compressed point encoding") }// Select the positive or negative root, as indicated by the least // significant bit, based on the encoding type byte. := new(fiat.P384Element) .Sub(, ) := .Bytes()[p384ElementLength-1]&1 ^ [0]&1 .Select(, , int()) .x.Set() .y.Set() .z.One()return , nildefault:returnnil, errors.New("invalid P384 point encoding") }}var_p384B *fiat.P384Elementvar_p384BOncesync.Oncefunc () *fiat.P384Element {_p384BOnce.Do(func() {_p384B, _ = new(fiat.P384Element).SetBytes([]byte{0xb3, 0x31, 0x2f, 0xa7, 0xe2, 0x3e, 0xe7, 0xe4, 0x98, 0x8e, 0x5, 0x6b, 0xe3, 0xf8, 0x2d, 0x19, 0x18, 0x1d, 0x9c, 0x6e, 0xfe, 0x81, 0x41, 0x12, 0x3, 0x14, 0x8, 0x8f, 0x50, 0x13, 0x87, 0x5a, 0xc6, 0x56, 0x39, 0x8d, 0x8a, 0x2e, 0xd1, 0x9d, 0x2a, 0x85, 0xc8, 0xed, 0xd3, 0xec, 0x2a, 0xef}) })return_p384B}// p384Polynomial sets y2 to x³ - 3x + b, and returns y2.func (, *fiat.P384Element) *fiat.P384Element { .Square() .Mul(, ) := new(fiat.P384Element).Add(, ) .Add(, ) .Sub(, )return .Add(, p384B())}func (, *fiat.P384Element) error {// y² = x³ - 3x + b := p384Polynomial(new(fiat.P384Element), ) := new(fiat.P384Element).Square()if .Equal() != 1 {returnerrors.New("P384 point not on curve") }returnnil}// 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 ( *P384Point) () []byte {// This function is outlined to make the allocations inline in the caller // rather than happen on the heap.var [1 + 2*p384ElementLength]bytereturn .bytes(&)}func ( *P384Point) ( *[1 + 2*p384ElementLength]byte) []byte {if .z.IsZero() == 1 {returnappend([:0], 0) } := new(fiat.P384Element).Invert(.z) := new(fiat.P384Element).Mul(.x, ) := new(fiat.P384Element).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 ( *P384Point) () ([]byte, error) {// This function is outlined to make the allocations inline in the caller // rather than happen on the heap.var [p384ElementLength]bytereturn .bytesX(&)}func ( *P384Point) ( *[p384ElementLength]byte) ([]byte, error) {if .z.IsZero() == 1 {returnnil, errors.New("P384 point is the point at infinity") } := new(fiat.P384Element).Invert(.z) := new(fiat.P384Element).Mul(.x, )returnappend([: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 ( *P384Point) () []byte {// This function is outlined to make the allocations inline in the caller // rather than happen on the heap.var [1 + p384ElementLength]bytereturn .bytesCompressed(&)}func ( *P384Point) ( *[1 + p384ElementLength]byte) []byte {if .z.IsZero() == 1 {returnappend([:0], 0) } := new(fiat.P384Element).Invert(.z) := new(fiat.P384Element).Mul(.x, ) := new(fiat.P384Element).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()[p384ElementLength-1] & 1 = append(, .Bytes()...)return}// Add sets q = p1 + p2, and returns q. The points may overlap.func ( *P384Point) (, *P384Point) *P384Point {// 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.P384Element).Mul(.x, .x) // t0 := X1 * X2 := new(fiat.P384Element).Mul(.y, .y) // t1 := Y1 * Y2 := new(fiat.P384Element).Mul(.z, .z) // t2 := Z1 * Z2 := new(fiat.P384Element).Add(.x, .y) // t3 := X1 + Y1 := new(fiat.P384Element).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.P384Element).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.P384Element).Add(.x, .z) // Y3 := X2 + Z2 .Mul(, ) // X3 := X3 * Y3 .Add(, ) // Y3 := t0 + t2 .Sub(, ) // Y3 := X3 - Y3 := new(fiat.P384Element).Mul(p384B(), ) // Z3 := b * t2 .Sub(, ) // X3 := Y3 - Z3 .Add(, ) // Z3 := X3 + X3 .Add(, ) // X3 := X3 + Z3 .Sub(, ) // Z3 := t1 - X3 .Add(, ) // X3 := t1 + X3 .Mul(p384B(), ) // 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 ( *P384Point) ( *P384Point) *P384Point {// 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.P384Element).Square(.x) // t0 := X ^ 2 := new(fiat.P384Element).Square(.y) // t1 := Y ^ 2 := new(fiat.P384Element).Square(.z) // t2 := Z ^ 2 := new(fiat.P384Element).Mul(.x, .y) // t3 := X * Y .Add(, ) // t3 := t3 + t3 := new(fiat.P384Element).Mul(.x, .z) // Z3 := X * Z .Add(, ) // Z3 := Z3 + Z3 := new(fiat.P384Element).Mul(p384B(), ) // Y3 := b * t2 .Sub(, ) // Y3 := Y3 - Z3 := new(fiat.P384Element).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(p384B(), ) // 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 ( *P384Point) (, *P384Point, int) *P384Point { .x.Select(.x, .x, ) .y.Select(.y, .y, ) .z.Select(.z, .z, )return}// A p384Table 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.typep384Table [15]*P384Point// 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 ( *p384Table) ( *P384Point, uint8) {if >= 16 {panic("nistec: internal error: p384Table called with out-of-bounds value") } .Set(NewP384Point())for := uint8(1); < 16; ++ { := subtle.ConstantTimeByteEq(, ) .Select([-1], , ) }}// ScalarMult sets p = scalar * q, and returns p.func ( *P384Point) ( *P384Point, []byte) (*P384Point, error) {// Compute a p384Table for the base point q. The explicit NewP384Point // calls get inlined, letting the allocations live on the stack.var = p384Table{NewP384Point(), NewP384Point(), NewP384Point(),NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point(),NewP384Point(), NewP384Point(), NewP384Point(), NewP384Point()} [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. := NewP384Point() .Set(NewP384Point())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}varp384GeneratorTable *[p384ElementLength * 2]p384Tablevarp384GeneratorTableOncesync.Once// generatorTable returns a sequence of p384Tables. The first table contains// multiples of G. Each successive table is the previous table doubled four// times.func ( *P384Point) () *[p384ElementLength * 2]p384Table {p384GeneratorTableOnce.Do(func() {p384GeneratorTable = new([p384ElementLength * 2]p384Table) := NewP384Point().SetGenerator()for := 0; < p384ElementLength*2; ++ {p384GeneratorTable[][0] = NewP384Point().Set()for := 1; < 15; ++ {p384GeneratorTable[][] = NewP384Point().Add(p384GeneratorTable[][-1], ) } .Double() .Double() .Double() .Double() } })returnp384GeneratorTable}// ScalarBaseMult sets p = scalar * B, where B is the canonical generator, and// returns p.func ( *P384Point) ( []byte) (*P384Point, error) {iflen() != p384ElementLength {returnnil, 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. := NewP384Point() .Set(NewP384Point()) := len() - 1for , := range { := >> 4 [].Select(, ) .Add(, ) -- = & 0b1111 [].Select(, ) .Add(, ) -- }return , nil}// p384Sqrt sets e to a square root of x. If x is not a square, p384Sqrt returns// false and e is unchanged. e and x can overlap.func (, *fiat.P384Element) ( bool) { := new(fiat.P384Element)p384SqrtCandidate(, ) := new(fiat.P384Element).Square()if .Equal() != 1 {returnfalse } .Set()returntrue}// p384SqrtCandidate sets z to a square root candidate for x. z and x must not overlap.func (, *fiat.P384Element) {// Since p = 3 mod 4, exponentiation by (p + 1) / 4 yields a square root candidate. // // The sequence of 14 multiplications and 381 squarings is derived from the // following addition chain generated with github.com/mmcloughlin/addchain v0.4.0. // // _10 = 2*1 // _11 = 1 + _10 // _110 = 2*_11 // _111 = 1 + _110 // _111000 = _111 << 3 // _111111 = _111 + _111000 // _1111110 = 2*_111111 // _1111111 = 1 + _1111110 // x12 = _1111110 << 5 + _111111 // x24 = x12 << 12 + x12 // x31 = x24 << 7 + _1111111 // x32 = 2*x31 + 1 // x63 = x32 << 31 + x31 // x126 = x63 << 63 + x63 // x252 = x126 << 126 + x126 // x255 = x252 << 3 + _111 // return ((x255 << 33 + x32) << 64 + 1) << 30 //var = new(fiat.P384Element)var = new(fiat.P384Element)var = new(fiat.P384Element) .Square() .Mul(, ) .Square() .Mul(, ) .Square()for := 1; < 3; ++ { .Square() } .Mul(, ) .Square() .Mul(, )for := 0; < 5; ++ { .Square() } .Mul(, ) .Square()for := 1; < 12; ++ { .Square() } .Mul(, )for := 0; < 7; ++ { .Square() } .Mul(, ) .Square() .Mul(, ) .Square()for := 1; < 31; ++ { .Square() } .Mul(, ) .Square()for := 1; < 63; ++ { .Square() } .Mul(, ) .Square()for := 1; < 126; ++ { .Square() } .Mul(, )for := 0; < 3; ++ { .Square() } .Mul(, )for := 0; < 33; ++ { .Square() } .Mul(, )for := 0; < 64; ++ { .Square() } .Mul(, )for := 0; < 30; ++ { .Square() }}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.