This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
moneroutil/key.go
2017-05-13 17:36:32 -07:00

58 lines
1 KiB
Go

package moneroutil
import (
"crypto/rand"
)
const (
KeyLength = 32
)
// Key can be a Scalar or a Point
type Key [KeyLength]byte
func (p *Key) FromBytes(b [KeyLength]byte) {
*p = b
}
func (p *Key) ToBytes() (result [KeyLength]byte) {
result = [KeyLength]byte(*p)
return
}
func (p *Key) PubKey() (pubKey *Key) {
point := new(ExtendedGroupElement)
GeScalarMultBase(point, p)
pubKey = new(Key)
point.ToBytes(pubKey)
return
}
// Creates a point on the Edwards Curve by hashing the key
func (p *Key) HashToEC() (result *ExtendedGroupElement) {
result = new(ExtendedGroupElement)
var p1 ProjectiveGroupElement
var p2 CompletedGroupElement
h := Key(Keccak256(p[:]))
p1.FromBytes(&h)
GeMul8(&p2, &p1)
p2.ToExtended(result)
return
}
func RandomScalar() (result *Key) {
result = new(Key)
var reduceFrom [KeyLength * 2]byte
tmp := make([]byte, KeyLength*2)
rand.Read(tmp)
copy(reduceFrom[:], tmp)
ScReduce(result, &reduceFrom)
return
}
func NewKeyPair() (privKey *Key, pubKey *Key) {
privKey = RandomScalar()
pubKey = privKey.PubKey()
return
}