From 4981c383bb53862b7f5e6ada3686d9a0170163bd Mon Sep 17 00:00:00 2001 From: Advaita Saha Date: Thu, 31 Aug 2023 15:37:31 +0530 Subject: [PATCH] fix: support for ECP_TwEdwards in toHex() (#261) --- .../elliptic/ec_twistededwards_projective.nim | 4 +-- constantine/math/io/io_ec.nim | 30 ++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/constantine/math/elliptic/ec_twistededwards_projective.nim b/constantine/math/elliptic/ec_twistededwards_projective.nim index 9e5cbaf..7bb3214 100644 --- a/constantine/math/elliptic/ec_twistededwards_projective.nim +++ b/constantine/math/elliptic/ec_twistededwards_projective.nim @@ -294,7 +294,7 @@ func diff*(r: var ECP_TwEdwards_Prj, r.sum(P, nQ) func affine*[F]( - aff: var ECP_TwEdwards_Prj[F], + aff: var ECP_TwEdwards_Aff[F], proj: ECP_TwEdwards_Prj[F]) = var invZ {.noInit.}: F invZ.inv(proj.z) @@ -303,7 +303,7 @@ func affine*[F]( aff.y.prod(proj.y, invZ) func projective*[F]( - proj: var ECP_TwEdwards_Prj[F], + proj: var ECP_TwEdwards_Aff[F], aff: ECP_TwEdwards_Prj[F]) {.inline.} = proj.x = aff.x proj.y = aff.y diff --git a/constantine/math/io/io_ec.nim b/constantine/math/io/io_ec.nim index 4bb3ba0..cee3e01 100644 --- a/constantine/math/io/io_ec.nim +++ b/constantine/math/io/io_ec.nim @@ -15,7 +15,9 @@ import ec_shortweierstrass_affine, ec_shortweierstrass_projective, ec_shortweierstrass_jacobian, - ec_shortweierstrass_jacobian_extended + ec_shortweierstrass_jacobian_extended, + ec_twistededwards_projective, + ec_twistededwards_affine ] # No exceptions allowed @@ -54,6 +56,32 @@ func toHex*[EC: ECP_ShortW_Prj or ECP_ShortW_Jac or ECP_ShortW_Aff or ECP_ShortW result.appendHex(aff.y) result &= "\n" & sp & ")" +func toHex*[EC: ECP_TwEdwards_Aff or ECP_TwEdwards_Prj](P: EC, indent: static int = 0): string = + ## Stringify an elliptic curve point to Hex for Twisted Edwards Curve + ## Note. Leading zeros are not removed. + ## Result is prefixed with 0x + ## + ## Output will be padded with 0s to maintain constant-time. + ## + ## CT: + ## - no leaks + ## + ## This proc output may change format in the future + + var aff {.noInit.}: ECP_TwEdwards_Aff[EC.F] + when EC isnot ECP_TwEdwards_Aff: + aff.affine(P) + else: + aff = P + + const sp = spaces(indent) + + result = sp & $EC & "(\n" & sp & " x: " + result.appendHex(aff.x) + result &= ",\n" & sp & " y: " + result.appendHex(aff.y) + result &= "\n" & sp & ")" + func fromHex*(dst: var (ECP_ShortW_Prj or ECP_ShortW_Jac), x, y: string): bool = ## Convert hex strings to a G1 curve point ## Returns true if point exist or if input is the point at infinity (all 0)