Path: blob/master/libs/tomcrypt/src/pk/ecc/ltc_ecc_map.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b10*11* All curves taken from NIST recommendation paper of July 199912* Available at http://csrc.nist.gov/cryptval/dss.htm13*/14#include "tomcrypt.h"1516/**17@file ltc_ecc_map.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC2223/**24Map a projective jacbobian point back to affine space25@param P [in/out] The point to map26@param modulus The modulus of the field the ECC curve is in27@param mp The "b" value from montgomery_setup()28@return CRYPT_OK on success29*/30int ltc_ecc_map(ecc_point *P, void *modulus, void *mp)31{32void *t1, *t2;33int err;3435LTC_ARGCHK(P != NULL);36LTC_ARGCHK(modulus != NULL);37LTC_ARGCHK(mp != NULL);3839if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {40return err;41}4243/* first map z back to normal */44if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK) { goto done; }4546/* get 1/z */47if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK) { goto done; }4849/* get 1/z^2 and 1/z^3 */50if ((err = mp_sqr(t1, t2)) != CRYPT_OK) { goto done; }51if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK) { goto done; }52if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK) { goto done; }53if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK) { goto done; }5455/* multiply against x/y */56if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK) { goto done; }57if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK) { goto done; }58if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK) { goto done; }59if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK) { goto done; }60if ((err = mp_set(P->z, 1)) != CRYPT_OK) { goto done; }6162err = CRYPT_OK;63done:64mp_clear_multi(t1, t2, NULL);65return err;66}6768#endif697071