fix: support for ECP_TwEdwards in toHex() (#261)

This commit is contained in:
Advaita Saha 2023-08-31 15:37:31 +05:30 committed by GitHub
parent ad04e6ea57
commit 4981c383bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -294,7 +294,7 @@ func diff*(r: var ECP_TwEdwards_Prj,
r.sum(P, nQ) r.sum(P, nQ)
func affine*[F]( func affine*[F](
aff: var ECP_TwEdwards_Prj[F], aff: var ECP_TwEdwards_Aff[F],
proj: ECP_TwEdwards_Prj[F]) = proj: ECP_TwEdwards_Prj[F]) =
var invZ {.noInit.}: F var invZ {.noInit.}: F
invZ.inv(proj.z) invZ.inv(proj.z)
@ -303,7 +303,7 @@ func affine*[F](
aff.y.prod(proj.y, invZ) aff.y.prod(proj.y, invZ)
func projective*[F]( func projective*[F](
proj: var ECP_TwEdwards_Prj[F], proj: var ECP_TwEdwards_Aff[F],
aff: ECP_TwEdwards_Prj[F]) {.inline.} = aff: ECP_TwEdwards_Prj[F]) {.inline.} =
proj.x = aff.x proj.x = aff.x
proj.y = aff.y proj.y = aff.y

View File

@ -15,7 +15,9 @@ import
ec_shortweierstrass_affine, ec_shortweierstrass_affine,
ec_shortweierstrass_projective, ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian, ec_shortweierstrass_jacobian,
ec_shortweierstrass_jacobian_extended ec_shortweierstrass_jacobian_extended,
ec_twistededwards_projective,
ec_twistededwards_affine
] ]
# No exceptions allowed # 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.appendHex(aff.y)
result &= "\n" & sp & ")" 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 = func fromHex*(dst: var (ECP_ShortW_Prj or ECP_ShortW_Jac), x, y: string): bool =
## Convert hex strings to a G1 curve point ## Convert hex strings to a G1 curve point
## Returns true if point exist or if input is the point at infinity (all 0) ## Returns true if point exist or if input is the point at infinity (all 0)