summaryrefslogtreecommitdiff
path: root/lib/nettle/ecc_projective_dbl_point_3.c
blob: 8695044c4863b3e2bb2706d33cb379c5d580d954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2011 Free Software Foundation, Inc.
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

/* Based on public domain code of LibTomCrypt by Tom St Denis.
 * Adapted to gmp and nettle by Nikos Mavrogiannopoulos.
 */

#include "ecc.h"

/*
  @file ecc_projective_dbl_point.c
  ECC Crypto, Tom St Denis
*/  

#ifdef ECC_SECP_CURVES_ONLY

/*
   Double an ECC point
   @param P   The point to double
   @param R   [out] The destination of the double
   @param modulus  The modulus of the field the ECC curve is in
   @param mp       The "b" value from montgomery_setup()
   @return 0 on success
*/
int
ecc_projective_dbl_point (ecc_point * P, ecc_point * R, mpz_t a /* a is -3 */,
                              mpz_t modulus)
{
   mpz_t t1, t2;
   int   err;

   assert(P       != NULL);
   assert(R       != NULL);
   assert(modulus != NULL);

   if ((err = mp_init_multi(&t1, &t2, NULL)) != 0) {
      return err;
   }

   if (P != R) {
      mpz_set(R->x, P->x);
      mpz_set(R->y, P->y);
      mpz_set(R->z, P->z);
   }

   /* t1 = Z * Z */
   mpz_mul(t1, R->z, R->z);
   mpz_mod(t1, t1, modulus);
   /* Z = Y * Z */
   mpz_mul(R->z, R->y, R->z);
   mpz_mod(R->z, R->z, modulus);
   /* Z = 2Z */
   mpz_add(R->z, R->z, R->z);
   if (mpz_cmp(R->z, modulus) >= 0) {
      mpz_sub(R->z, R->z, modulus);
   }
   
   /* T2 = X - T1 */
   mpz_sub(t2, R->x, t1);
   if (mpz_cmp_ui(t2, 0) < 0) {
      mpz_add(t2, t2, modulus);
   }
   /* T1 = X + T1 */
   mpz_add(t1, t1, R->x);
   if (mpz_cmp(t1, modulus) >= 0) {
      mpz_sub(t1, t1, modulus);
   }
   /* T2 = T1 * T2 */
   mpz_mul(t2, t1, t2);
   mpz_mod(t2, t2, modulus);
   /* T1 = 2T2 */
   mpz_add(t1, t2, t2);
   if (mpz_cmp(t1, modulus) >= 0) {
      mpz_sub(t1, t1, modulus);
   }
   /* T1 = T1 + T2 */
   mpz_add(t1, t1, t2);
   if (mpz_cmp(t1, modulus) >= 0) {
      mpz_sub(t1, t1, modulus);
   }

   /* Y = 2Y */
   mpz_add(R->y, R->y, R->y);
   if (mpz_cmp(R->y, modulus) >= 0) {
      mpz_sub(R->y, R->y, modulus);
   }
   /* Y = Y * Y */
   mpz_mul(R->y, R->y, R->y);
   mpz_mod(R->y, R->y, modulus);
   /* T2 = Y * Y */
   mpz_mul(t2, R->y, R->y);
   mpz_mod(t2, t2, modulus);
   /* T2 = T2/2 */
   if (mp_isodd(t2)) {
      mpz_add(t2, t2, modulus);
   }
   mpz_divexact_ui(t2, t2, 2);
   /* Y = Y * X */
   mpz_mul(R->y, R->y, R->x);
   mpz_mod(R->y, R->y, modulus);

   /* X  = T1 * T1 */
   mpz_mul(R->x, t1, t1);
   mpz_mod(R->x, R->x, modulus);
   /* X = X - Y */
   mpz_sub(R->x, R->x, R->y);
   if (mpz_cmp_ui(R->x, 0) < 0) {
      mpz_add(R->x, R->x, modulus);
   }
   /* X = X - Y */
   mpz_sub(R->x, R->x, R->y);
   if (mpz_cmp_ui(R->x, 0) < 0) {
      mpz_add(R->x, R->x, modulus);
   }

   /* Y = Y - X */     
   mpz_sub(R->y, R->y, R->x);
   if (mpz_cmp_ui(R->y, 0) < 0) {
      mpz_add(R->y, R->y, modulus);
   }
   /* Y = Y * T1 */
   mpz_mul(R->y, R->y, t1);
   mpz_mod(R->y, R->y, modulus);
   /* Y = Y - T2 */
   mpz_sub(R->y, R->y, t2);
   if (mpz_cmp_ui(R->y, 0) < 0) {
      mpz_add( R->y, R->y, modulus);
   }
 
   err = 0;

   mp_clear_multi(&t1, &t2, NULL);
   return err;
}
#endif
/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_projective_dbl_point.c,v $ */
/* $Revision: 1.11 $ */
/* $Date: 2007/05/12 14:32:35 $ */