1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
12 * All curves taken from NIST recommendation paper of July 1999
13 * Available at http://csrc.nist.gov/cryptval/dss.htm
19 ECC Crypto, Tom St Denis
25 Map a projective jacbobian point back to affine space
26 @param P [in/out] The point to map
27 @param modulus The modulus of the field the ECC curve is in
28 @param mp The "b" value from montgomery_setup()
29 @return CRYPT_OK on success
31 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp)
36 LTC_ARGCHK(P != NULL);
37 LTC_ARGCHK(modulus != NULL);
38 LTC_ARGCHK(mp != NULL);
40 if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
44 /* first map z back to normal */
45 if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK) { goto done; }
48 if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK) { goto done; }
50 /* get 1/z^2 and 1/z^3 */
51 if ((err = mp_sqr(t1, t2)) != CRYPT_OK) { goto done; }
52 if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK) { goto done; }
53 if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK) { goto done; }
54 if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK) { goto done; }
56 /* multiply against x/y */
57 if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK) { goto done; }
58 if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK) { goto done; }
59 if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK) { goto done; }
60 if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK) { goto done; }
61 if ((err = mp_set(P->z, 1)) != CRYPT_OK) { goto done; }
65 mp_clear_multi(t1, t2, NULL);
71 /* ref: $Format:%D$ */
72 /* git commit: $Format:%H$ */
73 /* commit time: $Format:%ai$ */