// GenerateKey generates a key using a random number generator, returning // the private scalar and the corresponding public key points from a // random secret. func GenerateKey(curve *TwistedEdwardsCurve, rand io.Reader) (priv []byte, x, y *big.Int, err error) { var pub *[PubKeyBytesLen]byte var privArray *[PrivKeyBytesLen]byte pub, privArray, err = ed25519.GenerateKey(rand) priv = privArray[:] x, y, err = curve.EncodedBytesToBigIntPoint(pub) if err != nil { return nil, nil, nil, err } return }
func TestCurve25519Conversion(t *testing.T) { public, private, _ := ed25519.GenerateKey(rand.Reader) var curve25519Public, curve25519Public2, curve25519Private [32]byte PrivateKeyToCurve25519(&curve25519Private, private) curve25519.ScalarBaseMult(&curve25519Public, &curve25519Private) if !PublicKeyToCurve25519(&curve25519Public2, public) { t.Fatalf("PublicKeyToCurve25519 failed") } if !bytes.Equal(curve25519Public[:], curve25519Public2[:]) { t.Errorf("Values didn't match: curve25519 produced %x, conversion produced %x", curve25519Public[:], curve25519Public2[:]) } }
// PrivKeyFromSecret returns a private and public key for `curve' based on the // 32-byte private key secret passed as an argument as a byte slice. func PrivKeyFromSecret(curve *TwistedEdwardsCurve, s []byte) (*PrivateKey, *PublicKey) { if len(s) != PrivKeyBytesLen/2 { return nil, nil } // Instead of using rand to generate our scalar, use the scalar // itself as a reader. sReader := bytes.NewReader(s) _, pk, err := ed25519.GenerateKey(sReader) if err != nil { return nil, nil } return PrivKeyFromBytes(curve, pk[:]) }