port: serialization should use the backend as well

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2022-05-26 15:48:56 +02:00
parent f45ec3c23c
commit 8a76817b76
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E
2 changed files with 59 additions and 27 deletions

View File

@ -11,8 +11,8 @@
# Shacham H., Waters B., "Compact Proofs of Retrievability"
# using pairing over BLS12-381 ECC
import blscurve
import blscurve/blst/blst_abi
import pkg/blscurve
import pkg/blscurve/blst/blst_abi
type
ec_SecretKey* = blscurve.SecretKey
@ -25,16 +25,40 @@ type
ec_fr* = blst_fr
ec_signature* = Signature
const
EC_SUCCESS*: bool = true
# these need to be template as a workaround for const
template EC_G1* : blst_p1_affine = BLS12_381_G1
template EC_G2* : blst_p2_affine = BLS12_381_G2
func ec_p1_serialize*(dst: var array[96, byte]; src: ec_p1) =
blst_p1_serialize(dst, src)
func ec_p1_deserialize*(dst: var ec_p1_affine; src: array[96, byte]): bool =
blst_p1_deserialize(dst, src) == BLST_SUCCESS
func ec_p2_serialize*(dst: var array[192, byte]; src: ec_p2) =
blst_p2_serialize(dst, src)
func ec_p2_deserialize*(dst: var ec_p2_affine; src: array[192, byte]): bool =
blst_p2_deserialize(dst, src) == BLST_SUCCESS
func ec_export_uncompressed*(publicKey: ec_PublicKey): array[96, byte] {.inline, noinit.} =
blscurve.exportUncompressed(publicKey)
func ec_export_uncompressed*(signature: ec_Signature): array[192, byte] {.inline, noinit.} =
blscurve.exportUncompressed(signature)
func ec_p1_from_affine*(dst: var ec_p1, src: ec_p1_affine) =
blst_p1_from_affine(dst, src)
func ec_scalar_from_bendian*(ret: var ec_scalar, a: array[32, byte]) =
blst_scalar_from_bendian(ret, a)
func ec_bendian_from_scalar*(ret: var array[32, byte], a: ec_scalar) =
blst_bendian_from_scalar(ret, a)
func ec_scalar_fr_check*(a: ec_scalar): CTBool =
blst_scalar_fr_check(a)
@ -130,6 +154,12 @@ func ec_from_bytes*(
): bool {.inline.} =
fromBytes(obj, raw)
func ec_from_bytes*(
obj: var PublicKey,
raw: array[48, byte] or array[96, byte] or openArray[byte]
): bool {.inline.} =
fromBytes(obj, raw)
func ec_verify*[T: byte|char](
publicKey: PublicKey,
message: openarray[T],

View File

@ -12,8 +12,10 @@ import std/sequtils
import pkg/protobuf_serialization
import pkg/stew/results
import pkg/stew/objects
import pkg/blscurve
import pkg/blscurve/blst/blst_abi
when defined(por_backend_constantine):
import ../../backends/backend_constantine
else:
import ../../backends/backend_blst
import_proto3 "por.proto"
@ -33,10 +35,10 @@ func toMessage*(self: Proof): ProofMessage =
for mu in self.mu:
var
serialized: array[32, byte]
blst_bendian_from_scalar(serialized, mu)
ec_bendian_from_scalar(serialized, mu)
message.mu.add(toSeq(serialized))
blst_p1_serialize(sigma, self.sigma)
ec_p1_serialize(sigma, self.sigma)
message.sigma = toSeq(sigma)
message
@ -44,17 +46,17 @@ func toMessage*(self: Proof): ProofMessage =
func fromMessage*(self: ProofMessage): Result[Proof, string] =
var
proof = Proof()
sigmaAffine: blst_p1_affine
sigmaAffine: ec_p1_affine
if blst_p1_deserialize(sigmaAffine, toArray(96, self.sigma)) != BLST_SUCCESS:
if ec_p1_deserialize(sigmaAffine, toArray(96, self.sigma)) != EC_SUCCESS:
return err("Unable to decompress sigma")
blst_p1_from_affine(proof.sigma, sigmaAffine)
ec_p1_from_affine(proof.sigma, sigmaAffine)
for mu in self.mu:
var
muScalar: blst_scalar
blst_scalar_from_bendian(muScalar, toArray(32, mu))
muScalar: ec_scalar
ec_scalar_from_bendian(muScalar, toArray(32, mu))
proof.mu.add(muScalar)
@ -71,7 +73,7 @@ func toMessage*(self: TauZero): TauZeroMessage =
serialized: array[96, byte]
# serialized and compresses the points
blst_p1_serialize(serialized, u)
ec_p1_serialize(serialized, u)
message.u.add(toSeq(serialized))
message
@ -85,13 +87,13 @@ func fromMessage*(self: TauZeroMessage): Result[TauZero, string] =
for u in self.u:
var
uuAffine: blst_p1_affine
uu: blst_p1
uuAffine: ec_p1_affine
uu: ec_p1
if blst_p1_deserialize(uuAffine, toArray(96, u)) != BLST_SUCCESS:
if ec_p1_deserialize(uuAffine, toArray(96, u)) != EC_SUCCESS:
return err("Unable to decompress u")
blst_p1_from_affine(uu, uuAffine)
ec_p1_from_affine(uu, uuAffine)
tauZero.u.add(uu)
ok(tauZero)
@ -111,11 +113,11 @@ func fromMessage*(self: TauMessage): Result[Tau, string] =
func toMessage*(self: por.PublicKey): PubKeyMessage =
var
signkey = toSeq(self.signkey.exportUncompressed())
signkey = toSeq(self.signkey.ec_export_uncompressed())
message = PubKeyMessage(signkey: signkey)
key: array[192, byte]
blst_p2_serialize(key, self.key)
ec_p2_serialize(key, self.key)
message.key = toSeq(key)
message
@ -123,15 +125,15 @@ func toMessage*(self: por.PublicKey): PubKeyMessage =
func fromMessage*(self: PubKeyMessage): Result[por.PublicKey, string] =
var
spk: por.PublicKey
keyAffine: blst_p2_affine
keyAffine: ec_p2_affine
if not spk.signkey.fromBytes(self.signkey.toOpenArray(0, 95)):
if not spk.signkey.ec_from_bytes(self.signkey.toOpenArray(0, 95)):
return err("Unable to deserialize public key!")
if blst_p2_deserialize(keyAffine, toArray(192, self.key)) != BLST_SUCCESS:
if ec_p2_deserialize(keyAffine, toArray(192, self.key)) != EC_SUCCESS:
return err("Unable to decompress key!")
blst_p2_from_affine(spk.key, keyAffine)
ec_p2_from_affine(spk.key, keyAffine)
ok(spk)
@ -145,7 +147,7 @@ func toMessage*(self: PoR): PorMessage =
var
serialized: array[96, byte]
blst_p1_serialize(serialized, sigma)
ec_p1_serialize(serialized, sigma)
message.authenticators.add(toSeq(serialized))
message
@ -158,13 +160,13 @@ func fromMessage*(self: PorMessage): Result[PoR, string] =
for sigma in self.authenticators:
var
sigmaAffine: blst_p1_affine
authenticator: blst_p1
sigmaAffine: ec_p1_affine
authenticator: ec_p1
if blst_p1_deserialize(sigmaAffine, toArray(96, sigma)) != BLST_SUCCESS:
if ec_p1_deserialize(sigmaAffine, toArray(96, sigma)) != EC_SUCCESS:
return err("Unable to decompress sigma")
blst_p1_from_affine(authenticator, sigmaAffine)
ec_p1_from_affine(authenticator, sigmaAffine)
por.authenticators.add(authenticator)
return ok(por)