68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package moneroutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"io"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func ParseKey(buf io.Reader) (result Key, err error) {
|
|
key := make([]byte, KeyLength)
|
|
if _, err = buf.Read(key); err != nil {
|
|
return
|
|
}
|
|
copy(result[:], key)
|
|
return
|
|
}
|