1 // Copyright 2013 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.
5 // Package ed25519 implements the Ed25519 signature algorithm. See
6 // http://ed25519.cr.yp.to/.
9 // This code is a port of the public domain, "ref10" implementation of ed25519
16 "github.com/tendermint/ed25519/edwards25519"
25 // MakePublicKey makes a publicKey from the first half of privateKey.
26 func MakePublicKey(privateKey *[PrivateKeySize]byte) (publicKey *[PublicKeySize]byte) {
27 publicKey = new([32]byte)
30 h.Write(privateKey[:32])
37 var A edwards25519.ExtendedGroupElement
39 copy(hBytes[:], digest)
40 edwards25519.GeScalarMultBase(&A, &hBytes)
43 copy(privateKey[32:], publicKey[:])
47 // Sign signs the message with privateKey and returns a signature.
48 func Sign(privateKey *[PrivateKeySize]byte, message []byte) *[SignatureSize]byte {
50 h.Write(privateKey[:32])
52 var digest1, messageDigest, hramDigest [64]byte
53 var expandedSecretKey [32]byte
55 copy(expandedSecretKey[:], digest1[:])
56 expandedSecretKey[0] &= 248
57 expandedSecretKey[31] &= 63
58 expandedSecretKey[31] |= 64
63 h.Sum(messageDigest[:0])
65 var messageDigestReduced [32]byte
66 edwards25519.ScReduce(&messageDigestReduced, &messageDigest)
67 var R edwards25519.ExtendedGroupElement
68 edwards25519.GeScalarMultBase(&R, &messageDigestReduced)
75 h.Write(privateKey[32:])
78 var hramDigestReduced [32]byte
79 edwards25519.ScReduce(&hramDigestReduced, &hramDigest)
82 edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced)
84 signature := new([64]byte)
85 copy(signature[:], encodedR[:])
86 copy(signature[32:], s[:])
90 // Verify returns true iff sig is a valid signature of message by publicKey.
91 func Verify(publicKey *[PublicKeySize]byte, message []byte, sig *[SignatureSize]byte) bool {
96 var A edwards25519.ExtendedGroupElement
97 if !A.FromBytes(publicKey) {
100 edwards25519.FeNeg(&A.X, &A.X)
101 edwards25519.FeNeg(&A.T, &A.T)
105 h.Write(publicKey[:])
110 var hReduced [32]byte
111 edwards25519.ScReduce(&hReduced, &digest)
113 var R edwards25519.ProjectiveGroupElement
116 edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &b)
120 return subtle.ConstantTimeCompare(sig[:32], checkR[:]) == 1