curve25519.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 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. // Package curve25519 provides an implementation of the X25519 function, which
  5. // performs scalar multiplication on the elliptic curve known as Curve25519.
  6. // See RFC 7748.
  7. //
  8. // Starting in Go 1.20, this package is a wrapper for the X25519 implementation
  9. // in the crypto/ecdh package.
  10. package curve25519 // import "golang.org/x/crypto/curve25519"
  11. // ScalarMult sets dst to the product scalar * point.
  12. //
  13. // Deprecated: when provided a low-order point, ScalarMult will set dst to all
  14. // zeroes, irrespective of the scalar. Instead, use the X25519 function, which
  15. // will return an error.
  16. func ScalarMult(dst, scalar, point *[32]byte) {
  17. scalarMult(dst, scalar, point)
  18. }
  19. // ScalarBaseMult sets dst to the product scalar * base where base is the
  20. // standard generator.
  21. //
  22. // It is recommended to use the X25519 function with Basepoint instead, as
  23. // copying into fixed size arrays can lead to unexpected bugs.
  24. func ScalarBaseMult(dst, scalar *[32]byte) {
  25. scalarBaseMult(dst, scalar)
  26. }
  27. const (
  28. // ScalarSize is the size of the scalar input to X25519.
  29. ScalarSize = 32
  30. // PointSize is the size of the point input to X25519.
  31. PointSize = 32
  32. )
  33. // Basepoint is the canonical Curve25519 generator.
  34. var Basepoint []byte
  35. var basePoint = [32]byte{9}
  36. func init() { Basepoint = basePoint[:] }
  37. // X25519 returns the result of the scalar multiplication (scalar * point),
  38. // according to RFC 7748, Section 5. scalar, point and the return value are
  39. // slices of 32 bytes.
  40. //
  41. // scalar can be generated at random, for example with crypto/rand. point should
  42. // be either Basepoint or the output of another X25519 call.
  43. //
  44. // If point is Basepoint (but not if it's a different slice with the same
  45. // contents) a precomputed implementation might be used for performance.
  46. func X25519(scalar, point []byte) ([]byte, error) {
  47. // Outline the body of function, to let the allocation be inlined in the
  48. // caller, and possibly avoid escaping to the heap.
  49. var dst [32]byte
  50. return x25519(&dst, scalar, point)
  51. }