Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag SecretKey export as distinct type #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion blscurve.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import
./blscurve/eth2_keygen
export
SecretKey, PublicKey, Signature, ProofOfPossession,
`==`,
`==`, SecretByte,
aggregate,
sign, verify, aggregateVerify, fastAggregateVerify,
keyGen, privToPub,
Expand Down
15 changes: 8 additions & 7 deletions blscurve/bls_sig_io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# This file should be included to have access to private fields
# It is kept separated as it does not fall under the IETF BLS specification
export SecretByte

func fromHex*[T: SecretKey|PublicKey|Signature|ProofOfPossession](
obj: var T,
Expand All @@ -39,16 +40,16 @@ func fromBytes*[T: SecretKey|PublicKey|Signature|ProofOfPossession](
else:
result = obj.point.fromBytes(raw)

func toHex*(obj: SecretKey|PublicKey|Signature|ProofOfPossession): string {.inline.} =
func toHex*(pbj: SecretKey): distinct string {.error: "Returning the hex representation of a secret key is forbidden.".}
## Prevent returning the hex representation of a SecretKey

func toHex*(obj: 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()
result = obj.point.toHex()

func serialize*(
dst: var openarray[byte],
dst: var openarray[byte or SecretByte],
obj: SecretKey|PublicKey|Signature|ProofOfPossession): bool {.inline.} =
## Serialize the input `obj` in raw binary form and write it
## in `dst`.
Expand All @@ -63,7 +64,7 @@ const
RawPublicKeySize = MODBYTES_384
RawSignatureSize = MODBYTES_384 * 2

func exportRaw*(secretKey: SecretKey): array[RawSecretKeySize, byte] {.inline.}=
func exportRaw*(secretKey: SecretKey): array[RawSecretKeySize, SecretByte] {.inline.}=
## Serialize a secret key into its raw binary representation
# TODO: the SecretKey size is actually not 384 bit
# but 255 bit since the curve order requires 255-bit
Expand Down
7 changes: 5 additions & 2 deletions blscurve/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const

type
Domain* = array[8, byte]
SecretHex* = distinct string
SecretByte* = distinct byte

when sizeof(int) == 4 or defined(use32):
const
Expand Down Expand Up @@ -549,19 +551,20 @@ proc isOnCurve*(x: FP2_BLS12381, y: FP2_BLS12381): bool =
else:
result = (sqr(y) == rhs(x))

proc toBytes*(a: BIG_384, res: var openarray[byte]): bool =
proc toBytes*(a: BIG_384, res: var openarray[byte or SecretByte]): bool =
## Serialize big integer ``a`` to ``res``. Length of ``res`` array
## must be at least ``MODBYTES_384``.
##
## Returns ``true`` if ``a`` was succesfully serialized,
## ``false`` otherwise.
type B = typeof(res[0]) # byte or SecretByte
if len(res) >= MODBYTES_384:
var c: BIG_384
BIG_384_copy(c, a)
# BIG_384_norm() function in Milagro operates inplace.
discard BIG_384_norm(c)
for i in countdown(MODBYTES_384 - 1, 0):
res[i] = byte(c[0] and 0xFF)
res[i] = B(c[0] and 0xFF)
discard BIG_384_fshr(c, 8)
result = true

Expand Down