// Copyright 2019 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 curve25519 provides an implementation of the X25519 function, which// performs scalar multiplication on the elliptic curve known as Curve25519.// See RFC 7748.
package curve25519 // import "golang.org/x/crypto/curve25519"import ()// ScalarMult sets dst to the product scalar * point.//// Deprecated: when provided a low-order point, ScalarMult will set dst to all// zeroes, irrespective of the scalar. Instead, use the X25519 function, which// will return an error.func (, , *[32]byte) {var [32]bytecopy([:], [:]) [0] &= 248 [31] &= 127 [31] |= 64var , , , , , , field.Element .SetBytes([:]) .One() .Set(&) .One() := 0for := 254; >= 0; -- { := [/8] >> uint(&7) &= 1 ^= int() .Swap(&, ) .Swap(&, ) = int() .Subtract(&, &) .Subtract(&, &) .Add(&, &) .Add(&, &) .Multiply(&, &) .Multiply(&, &) .Square(&) .Square(&) .Add(&, &) .Subtract(&, &) .Multiply(&, &) .Subtract(&, &) .Square(&) .Mult32(&, 121666) .Square(&) .Add(&, &) .Multiply(&, &) .Multiply(&, &) } .Swap(&, ) .Swap(&, ) .Invert(&) .Multiply(&, &)copy([:], .Bytes())}// ScalarBaseMult sets dst to the product scalar * base where base is the// standard generator.//// It is recommended to use the X25519 function with Basepoint instead, as// copying into fixed size arrays can lead to unexpected bugs.func (, *[32]byte) {ScalarMult(, , &basePoint)}const (// ScalarSize is the size of the scalar input to X25519.ScalarSize = 32// PointSize is the size of the point input to X25519.PointSize = 32)// Basepoint is the canonical Curve25519 generator.varBasepoint []bytevar basePoint = [32]byte{9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}func init() { Basepoint = basePoint[:] }func checkBasepoint() {ifsubtle.ConstantTimeCompare(Basepoint, []byte{0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }) != 1 {panic("curve25519: global Basepoint value was modified") }}// X25519 returns the result of the scalar multiplication (scalar * point),// according to RFC 7748, Section 5. scalar, point and the return value are// slices of 32 bytes.//// scalar can be generated at random, for example with crypto/rand. point should// be either Basepoint or the output of another X25519 call.//// If point is Basepoint (but not if it's a different slice with the same// contents) a precomputed implementation might be used for performance.func (, []byte) ([]byte, error) {// Outline the body of function, to let the allocation be inlined in the // caller, and possibly avoid escaping to the heap.var [32]bytereturnx25519(&, , )}func x25519( *[32]byte, , []byte) ([]byte, error) {var [32]byteif := len(); != 32 {returnnil, fmt.Errorf("bad scalar length: %d, expected %d", , 32) }if := len(); != 32 {returnnil, fmt.Errorf("bad point length: %d, expected %d", , 32) }copy([:], )if &[0] == &Basepoint[0] {checkBasepoint()ScalarBaseMult(, &) } else {var , [32]bytecopy([:], )ScalarMult(, &, &)ifsubtle.ConstantTimeCompare([:], [:]) == 1 {returnnil, fmt.Errorf("bad input point: low order point") } }return [:], nil}
The pages are generated with Goldsv0.4.5. (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.