mirror of https://github.com/status-im/nim-abc.git
Introduce BLS based keys
This commit is contained in:
parent
0342502830
commit
6c5113e08a
|
@ -1,4 +1,7 @@
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
author = "ABC Authors"
|
author = "ABC Authors"
|
||||||
description = "Asynchronous Blockchain"
|
description = "Asynchronous Blockchain"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
|
requires "https://github.com/markspanbroek/nim-blscurve#fix-nimble"
|
||||||
|
requires "nimcrypto"
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
import pkg/blscurve as bls
|
||||||
|
import pkg/nimcrypto
|
||||||
|
|
||||||
|
type
|
||||||
|
PrivateKey* = distinct bls.SecretKey
|
||||||
|
PublicKey* = distinct bls.PublicKey
|
||||||
|
|
||||||
|
proc `==`*(a, b: PrivateKey): bool {.borrow.}
|
||||||
|
proc `==`*(a, b: PublicKey): bool {.borrow.}
|
||||||
|
|
||||||
|
proc random*(_: type PrivateKey): PrivateKey =
|
||||||
|
var seed = newSeq[byte](64)
|
||||||
|
doAssert randomBytes(seed) == seed.len
|
||||||
|
doAssert deriveMasterSecretKey(bls.SecretKey(result), seed)
|
||||||
|
burnArray(seed)
|
||||||
|
|
||||||
|
proc erase*(key: var PrivateKey) =
|
||||||
|
burnMem(key)
|
||||||
|
|
||||||
|
proc toPublicKey*(private: PrivateKey): PublicKey =
|
||||||
|
doAssert publicFromSecret(bls.PublicKey(result), bls.SecretKey(private))
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
--path:".."
|
|
@ -0,0 +1,20 @@
|
||||||
|
import std/unittest
|
||||||
|
import abc/keys
|
||||||
|
|
||||||
|
suite "Keys":
|
||||||
|
|
||||||
|
test "generates random private keys":
|
||||||
|
check PrivateKey.random != PrivateKey.random
|
||||||
|
|
||||||
|
test "erases memory associated with a private key":
|
||||||
|
var key = PrivateKey.random
|
||||||
|
let bytes = cast[ptr[uint64]](addr key)
|
||||||
|
check bytes[] != 0
|
||||||
|
erase key
|
||||||
|
check bytes[] == 0
|
||||||
|
|
||||||
|
test "derives public key from private key":
|
||||||
|
let key1, key2 = PrivateKey.random()
|
||||||
|
check key1.toPublicKey == key1.toPublicKey
|
||||||
|
check key2.toPublicKey == key2.toPublicKey
|
||||||
|
check key1.toPublicKey != key2.toPublicKey
|
Loading…
Reference in New Issue