• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  *
9  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 
12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
13  *
14  * All curves taken from NIST recommendation paper of July 1999
15  * Available at http://csrc.nist.gov/cryptval/dss.htm
16  */
17 #include "tomcrypt.h"
18 
19 /**
20   @file ltc_ecc_map.c
21   ECC Crypto, Tom St Denis
22 */
23 
24 #ifdef MECC
25 
26 /**
27   Map a projective jacbobian point back to affine space
28   @param P        [in/out] The point to map
29   @param modulus  The modulus of the field the ECC curve is in
30   @param mp       The "b" value from montgomery_setup()
31   @return CRYPT_OK on success
32 */
ltc_ecc_map(ecc_point * P,void * modulus,void * mp)33 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp)
34 {
35    void *t1, *t2;
36    int   err;
37 
38    LTC_ARGCHK(P       != NULL);
39    LTC_ARGCHK(modulus != NULL);
40    LTC_ARGCHK(mp      != NULL);
41 
42    if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
43       return CRYPT_MEM;
44    }
45 
46    /* first map z back to normal */
47    if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK)           { goto done; }
48 
49    /* get 1/z */
50    if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK)                      { goto done; }
51 
52    /* get 1/z^2 and 1/z^3 */
53    if ((err = mp_sqr(t1, t2)) != CRYPT_OK)                                    { goto done; }
54    if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK)                           { goto done; }
55    if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK)                                { goto done; }
56    if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK)                           { goto done; }
57 
58    /* multiply against x/y */
59    if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK)                            { goto done; }
60    if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK)           { goto done; }
61    if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK)                            { goto done; }
62    if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK)           { goto done; }
63    if ((err = mp_set(P->z, 1)) != CRYPT_OK)                                   { goto done; }
64 
65    err = CRYPT_OK;
66 done:
67    mp_clear_multi(t1, t2, NULL);
68    return err;
69 }
70 
71 #endif
72 
73 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_map.c,v $ */
74 /* $Revision: 1.5 $ */
75 /* $Date: 2006/12/04 02:50:11 $ */
76 
77