Skip to content

Commit

Permalink
Export privToPub (temporary), serialize, exportRaw
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Mar 4, 2020
1 parent f017051 commit 9a14335
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions blscurve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ when BLS_USE_IETF_API:
`==`,
aggregate,
sign, verify, aggregateVerify, fastAggregateVerify,
keyGen,
fromHex, fromBytes, toHex
keyGen, privToPub,
fromHex, fromBytes, toHex, serialize, exportRaw
else:
import ./blscurve/bls_old_spec
export bls_old_spec
34 changes: 31 additions & 3 deletions blscurve/bls_sig_io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,42 @@ func fromBytes*[T: SecretKey|PublicKey|Signature|ProofOfPossession](
## its raw bytes representation.
## Returns true on success and false otherwise
when obj is SecretKey:
result = obj.intVal.fromBytes(hexStr)
result = obj.intVal.fromBytes(raw)
else:
result = obj.point.fromBytes(hexStr)
result = obj.point.fromBytes(raw)

func toHex*(obj: SecretKey|PublicKey|Signature|ProofOfPossession): string =
func toHex*(obj: SecretKey|PublicKey|Signature|ProofOfPossession): string {.inline.} =
## Return the hex representation of a BLS signature scheme object
## Signature and Proof-of-posessions are serialized in compressed form
when obj is SecretKey:
result = obj.intVal.toHex()
else:
result = obj.point.toHex()

func serialize*(
dst: var openarray[byte],
obj: SecretKey|PublicKey|Signature|ProofOfPossession): bool {.inline.} =
## Serialize the input `obj` in raw binary form and write it
## in `dst`.
## Returns `true` if the export is succesful, `false` otherwise
when obj is SecretKey:
result = obj.intVal.toBytes(dst)
else:
result = obj.point.toBytes(dst)

const
RawSecretKeySize = MODBYTES_384
RawPublicKeySize = MODBYTES_384
RawSignatureSize = MODBYTES_384 * 2

func exportRaw*(secretKey: SecretKey): array[RawSecretKeySize, byte] {.inline.}=
## Serialize a secret key into its raw binary representation
discard result.serialize(secretKey)

func exportRaw*(publicKey: PublicKey): array[RawPublicKeySize, byte] {.inline.}=
## Serialize a public key into its raw binary representation
discard result.serialize(publicKey)

func exportRaw*(signature: Signature): array[RawSignatureSize, byte] {.inline.}=
## Serialize a signature into its raw binary representation
discard result.serialize(signature)
4 changes: 2 additions & 2 deletions blscurve/bls_signature_scheme.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func subgroupCheck(P: GroupG1 or GroupG2): bool =
rP.mul(CURVE_Order)
result = rP.isInf()

func privToPub(secretKey: SecretKey): PublicKey =
## Generates a public key from a signature key
func privToPub*(secretKey: SecretKey): PublicKey =
## Generates a public key from a secret key
result.point = generator1()
result.point.mul(secretKey.intVal)

Expand Down

0 comments on commit 9a14335

Please sign in to comment.