deserialization of signatures and identifiers

This commit is contained in:
Mark Spanbroek 2024-12-16 17:27:17 +01:00
parent b91b62a67d
commit 4e86fcb581
6 changed files with 57 additions and 3 deletions

View File

@ -1,7 +1,9 @@
import std/sequtils
import pkg/stint
import pkg/questionable
import pkg/questionable/results
export sequtils
export stint
export questionable
export results

View File

@ -2,8 +2,10 @@ import ./signatures/identity
import ./signatures/signing
import ./signatures/signed
import ./signatures/serialization
import ./signatures/deserialization
export identity
export signing
export signed
export serialization
export deserialization

View File

@ -0,0 +1,18 @@
import pkg/blscurve
import ../basics
import ./identity
import ./signing
func fromBytes*(_: type Identifier, bytes: openArray[byte]): ?!Identifier =
var identifier: Identifier
if blscurve.fromBytes(identifier, bytes):
success identifier
else:
failure "invalid identifier"
func fromBytes*(_: type Signature, bytes: openArray[byte]): ?!Signature =
var signature: Signature
if blscurve.fromBytes(signature, bytes):
success signature
else:
failure "invalid signature"

View File

@ -1,11 +1,15 @@
import std/unittest
import std/sequtils
import pkg/stint
export unittest
export sequtils
import pkg/stint
export stint
import ./examples
import pkg/questionable
import pkg/questionable/results
export questionable
export results
import ./examples
export examples

View File

@ -74,6 +74,12 @@ proc example*(_: type Transaction): Transaction =
proc example*(_: type Identity): Identity =
Identity.random(result)
proc example*(_: type Identifier): Identifier =
Identity.example.identifier
proc example*(_: type Signature): Signature =
Identity.example.sign(seq[byte].example)
proc example*(_: type CommitteeMember): CommitteeMember =
CommitteeMember(uint32.example.int)

View File

@ -49,3 +49,25 @@ suite "Signature scheme":
let hash = Hash.example
let signature = identity.sign(hash)
check signature.verify(identity.identifier, hash)
test "identifier can be serialized and deserialized":
let identifier = Identifier.example
let serialized = identifier.toBytes()
check Identifier.fromBytes(serialized) == success identifier
test "identifier deserialization fails when wrong number of bytes":
let identifier = Identifier.example
let serialized = identifier.toBytes()
let invalid = serialized & 42'u8
check isFailure Identifier.fromBytes(invalid)
test "signature can be serialized and deserialized":
let signature = Signature.example
let serialized = signature.toBytes()
check Signature.fromBytes(serialized) == success signature
test "signature deserialization fails when wrong number of bytes":
let signature = Signature.example
let serialized = signature.toBytes()
let invalid = serialized & 42'u8
check isFailure Signature.fromBytes(invalid)