gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

curve25519_go120.go (1113B)


      1 // Copyright 2022 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 //go:build go1.20
      6 
      7 package curve25519
      8 
      9 import "crypto/ecdh"
     10 
     11 func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
     12 	curve := ecdh.X25519()
     13 	pub, err := curve.NewPublicKey(point)
     14 	if err != nil {
     15 		return nil, err
     16 	}
     17 	priv, err := curve.NewPrivateKey(scalar)
     18 	if err != nil {
     19 		return nil, err
     20 	}
     21 	out, err := priv.ECDH(pub)
     22 	if err != nil {
     23 		return nil, err
     24 	}
     25 	copy(dst[:], out)
     26 	return dst[:], nil
     27 }
     28 
     29 func scalarMult(dst, scalar, point *[32]byte) {
     30 	if _, err := x25519(dst, scalar[:], point[:]); err != nil {
     31 		// The only error condition for x25519 when the inputs are 32 bytes long
     32 		// is if the output would have been the all-zero value.
     33 		for i := range dst {
     34 			dst[i] = 0
     35 		}
     36 	}
     37 }
     38 
     39 func scalarBaseMult(dst, scalar *[32]byte) {
     40 	curve := ecdh.X25519()
     41 	priv, err := curve.NewPrivateKey(scalar[:])
     42 	if err != nil {
     43 		panic("curve25519: internal error: scalarBaseMult was not 32 bytes")
     44 	}
     45 	copy(dst[:], priv.PublicKey().Bytes())
     46 }