comparison src/pk/ecc/ltc_ecc_projective_dbl_point.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16

Update to LibTomCrypt 1.16
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:22:00 +0000
parents
children
comparison
equal deleted inserted replaced
280:59400faa4b44 380:d5faf4814ddb
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, [email protected], 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_projective_dbl_point.c
21 ECC Crypto, Tom St Denis
22 */
23
24 #if defined(MECC) && (!defined(MECC_ACCEL) || defined(LTM_DESC))
25
26 /**
27 Double an ECC point
28 @param P The point to double
29 @param R [out] The destination of the double
30 @param modulus The modulus of the field the ECC curve is in
31 @param mp The "b" value from montgomery_setup()
32 @return CRYPT_OK on success
33 */
34 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp)
35 {
36 void *t1, *t2;
37 int err;
38
39 LTC_ARGCHK(P != NULL);
40 LTC_ARGCHK(R != NULL);
41 LTC_ARGCHK(modulus != NULL);
42 LTC_ARGCHK(mp != NULL);
43
44 if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
45 return err;
46 }
47
48 if (P != R) {
49 if ((err = mp_copy(P->x, R->x)) != CRYPT_OK) { goto done; }
50 if ((err = mp_copy(P->y, R->y)) != CRYPT_OK) { goto done; }
51 if ((err = mp_copy(P->z, R->z)) != CRYPT_OK) { goto done; }
52 }
53
54 /* t1 = Z * Z */
55 if ((err = mp_sqr(R->z, t1)) != CRYPT_OK) { goto done; }
56 if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; }
57 /* Z = Y * Z */
58 if ((err = mp_mul(R->z, R->y, R->z)) != CRYPT_OK) { goto done; }
59 if ((err = mp_montgomery_reduce(R->z, modulus, mp)) != CRYPT_OK) { goto done; }
60 /* Z = 2Z */
61 if ((err = mp_add(R->z, R->z, R->z)) != CRYPT_OK) { goto done; }
62 if (mp_cmp(R->z, modulus) != LTC_MP_LT) {
63 if ((err = mp_sub(R->z, modulus, R->z)) != CRYPT_OK) { goto done; }
64 }
65
66 /* T2 = X - T1 */
67 if ((err = mp_sub(R->x, t1, t2)) != CRYPT_OK) { goto done; }
68 if (mp_cmp_d(t2, 0) == LTC_MP_LT) {
69 if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
70 }
71 /* T1 = X + T1 */
72 if ((err = mp_add(t1, R->x, t1)) != CRYPT_OK) { goto done; }
73 if (mp_cmp(t1, modulus) != LTC_MP_LT) {
74 if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
75 }
76 /* T2 = T1 * T2 */
77 if ((err = mp_mul(t1, t2, t2)) != CRYPT_OK) { goto done; }
78 if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
79 /* T1 = 2T2 */
80 if ((err = mp_add(t2, t2, t1)) != CRYPT_OK) { goto done; }
81 if (mp_cmp(t1, modulus) != LTC_MP_LT) {
82 if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
83 }
84 /* T1 = T1 + T2 */
85 if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; }
86 if (mp_cmp(t1, modulus) != LTC_MP_LT) {
87 if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; }
88 }
89
90 /* Y = 2Y */
91 if ((err = mp_add(R->y, R->y, R->y)) != CRYPT_OK) { goto done; }
92 if (mp_cmp(R->y, modulus) != LTC_MP_LT) {
93 if ((err = mp_sub(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
94 }
95 /* Y = Y * Y */
96 if ((err = mp_sqr(R->y, R->y)) != CRYPT_OK) { goto done; }
97 if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
98 /* T2 = Y * Y */
99 if ((err = mp_sqr(R->y, t2)) != CRYPT_OK) { goto done; }
100 if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; }
101 /* T2 = T2/2 */
102 if (mp_isodd(t2)) {
103 if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; }
104 }
105 if ((err = mp_div_2(t2, t2)) != CRYPT_OK) { goto done; }
106 /* Y = Y * X */
107 if ((err = mp_mul(R->y, R->x, R->y)) != CRYPT_OK) { goto done; }
108 if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
109
110 /* X = T1 * T1 */
111 if ((err = mp_sqr(t1, R->x)) != CRYPT_OK) { goto done; }
112 if ((err = mp_montgomery_reduce(R->x, modulus, mp)) != CRYPT_OK) { goto done; }
113 /* X = X - Y */
114 if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; }
115 if (mp_cmp_d(R->x, 0) == LTC_MP_LT) {
116 if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; }
117 }
118 /* X = X - Y */
119 if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; }
120 if (mp_cmp_d(R->x, 0) == LTC_MP_LT) {
121 if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; }
122 }
123
124 /* Y = Y - X */
125 if ((err = mp_sub(R->y, R->x, R->y)) != CRYPT_OK) { goto done; }
126 if (mp_cmp_d(R->y, 0) == LTC_MP_LT) {
127 if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
128 }
129 /* Y = Y * T1 */
130 if ((err = mp_mul(R->y, t1, R->y)) != CRYPT_OK) { goto done; }
131 if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; }
132 /* Y = Y - T2 */
133 if ((err = mp_sub(R->y, t2, R->y)) != CRYPT_OK) { goto done; }
134 if (mp_cmp_d(R->y, 0) == LTC_MP_LT) {
135 if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; }
136 }
137
138 err = CRYPT_OK;
139 done:
140 mp_clear_multi(t1, t2, NULL);
141 return err;
142 }
143 #endif
144 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_projective_dbl_point.c,v $ */
145 /* $Revision: 1.8 $ */
146 /* $Date: 2006/12/04 05:07:59 $ */
147