mirror of https://github.com/status-im/nim-abc.git
Conversion of public key to bytes
This commit is contained in:
parent
483d60707f
commit
1257b42a65
|
@ -3,6 +3,7 @@ author = "ABC Authors"
|
|||
description = "Asynchronous Blockchain"
|
||||
license = "MIT"
|
||||
|
||||
requires "questionable >= 0.10.0 & < 0.11.0"
|
||||
requires "https://github.com/markspanbroek/nim-blscurve#fix-nimble"
|
||||
requires "nimcrypto"
|
||||
requires "stew"
|
||||
|
|
13
abc/keys.nim
13
abc/keys.nim
|
@ -1,5 +1,6 @@
|
|||
import pkg/blscurve as bls
|
||||
import pkg/nimcrypto
|
||||
import pkg/questionable
|
||||
|
||||
type
|
||||
PrivateKey* = distinct bls.SecretKey
|
||||
|
@ -32,3 +33,15 @@ func verify*(key: PublicKey,
|
|||
## modified BLS multi-signature construction as described in:
|
||||
## https://crypto.stanford.edu/~dabo/pubs/papers/BLSmultisig.html
|
||||
bls.PublicKey(key).verify(message, bls.Signature(signature))
|
||||
|
||||
func toBytes*(key: PublicKey): seq[byte] =
|
||||
var bytes: array[48, byte]
|
||||
doAssert serialize(bytes, bls.PublicKey(key))
|
||||
@bytes
|
||||
|
||||
func fromBytes*(_: type PublicKey, bytes: openArray[byte]): ?PublicKey =
|
||||
var key: bls.PublicKey
|
||||
if key.fromBytes(bytes):
|
||||
PublicKey(key).some
|
||||
else:
|
||||
PublicKey.none
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import std/unittest
|
||||
import pkg/questionable
|
||||
import pkg/stew/byteutils
|
||||
import abc/keys
|
||||
|
||||
|
@ -29,8 +30,19 @@ suite "Keys":
|
|||
test "can be used to verify signatures":
|
||||
let message1 = "hello".toBytes
|
||||
let message2 = "hallo".toBytes
|
||||
let private = PrivateKey.random()
|
||||
let private = PrivateKey.random
|
||||
let public = private.toPublicKey
|
||||
let signature = private.sign(message1)
|
||||
check public.verify(message1, signature)
|
||||
check not public.verify(message2, signature)
|
||||
|
||||
test "public key can be converted to bytes":
|
||||
let key = PrivateKey.random.toPublicKey
|
||||
let bytes = key.toBytes
|
||||
check PublicKey.fromBytes(bytes) == key.some
|
||||
|
||||
test "conversion from bytes to public key can fail":
|
||||
let key = PrivateKey.random.toPublicKey
|
||||
let bytes = key.toBytes
|
||||
let invalid = bytes[1..^1]
|
||||
check PublicKey.fromBytes(invalid) == PublicKey.none
|
||||
|
|
Loading…
Reference in New Issue