Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MiniTLS-GPL by
crypto/ltc/ltc_ecc_projective_dbl_point.c@5:95f70ebfe61f, 2015-02-06 (annotated)
- Committer:
- shiyilei
- Date:
- Fri Feb 06 06:17:33 2015 +0000
- Revision:
- 5:95f70ebfe61f
- Parent:
- 2:527a66d0a1a9
change some parameters in the library to meet the needs of httpbin.org
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MiniTLS | 2:527a66d0a1a9 | 1 | /* |
MiniTLS | 2:527a66d0a1a9 | 2 | MiniTLS - A super trimmed down TLS/SSL Library for embedded devices |
MiniTLS | 2:527a66d0a1a9 | 3 | Author: Donatien Garnier |
MiniTLS | 2:527a66d0a1a9 | 4 | Copyright (C) 2013-2014 AppNearMe Ltd |
MiniTLS | 2:527a66d0a1a9 | 5 | |
MiniTLS | 2:527a66d0a1a9 | 6 | This program is free software; you can redistribute it and/or |
MiniTLS | 2:527a66d0a1a9 | 7 | modify it under the terms of the GNU General Public License |
MiniTLS | 2:527a66d0a1a9 | 8 | as published by the Free Software Foundation; either version 2 |
MiniTLS | 2:527a66d0a1a9 | 9 | of the License, or (at your option) any later version. |
MiniTLS | 2:527a66d0a1a9 | 10 | |
MiniTLS | 2:527a66d0a1a9 | 11 | This program is distributed in the hope that it will be useful, |
MiniTLS | 2:527a66d0a1a9 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
MiniTLS | 2:527a66d0a1a9 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MiniTLS | 2:527a66d0a1a9 | 14 | GNU General Public License for more details. |
MiniTLS | 2:527a66d0a1a9 | 15 | |
MiniTLS | 2:527a66d0a1a9 | 16 | You should have received a copy of the GNU General Public License |
MiniTLS | 2:527a66d0a1a9 | 17 | along with this program; if not, write to the Free Software |
MiniTLS | 2:527a66d0a1a9 | 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
MiniTLS | 2:527a66d0a1a9 | 19 | *//* LibTomCrypt, modular cryptographic library -- Tom St Denis |
MiniTLS | 2:527a66d0a1a9 | 20 | * |
MiniTLS | 2:527a66d0a1a9 | 21 | * LibTomCrypt is a library that provides various cryptographic |
MiniTLS | 2:527a66d0a1a9 | 22 | * algorithms in a highly modular and flexible manner. |
MiniTLS | 2:527a66d0a1a9 | 23 | * |
MiniTLS | 2:527a66d0a1a9 | 24 | * The library is free for all purposes without any express |
MiniTLS | 2:527a66d0a1a9 | 25 | * guarantee it works. |
MiniTLS | 2:527a66d0a1a9 | 26 | * |
MiniTLS | 2:527a66d0a1a9 | 27 | * Tom St Denis, tomstdenis@gmail.com, http://libtom.org |
MiniTLS | 2:527a66d0a1a9 | 28 | */ |
MiniTLS | 2:527a66d0a1a9 | 29 | |
MiniTLS | 2:527a66d0a1a9 | 30 | /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b |
MiniTLS | 2:527a66d0a1a9 | 31 | * |
MiniTLS | 2:527a66d0a1a9 | 32 | * All curves taken from NIST recommendation paper of July 1999 |
MiniTLS | 2:527a66d0a1a9 | 33 | * Available at http://csrc.nist.gov/cryptval/dss.htm |
MiniTLS | 2:527a66d0a1a9 | 34 | */ |
MiniTLS | 2:527a66d0a1a9 | 35 | #include "ltc.h" |
MiniTLS | 2:527a66d0a1a9 | 36 | |
MiniTLS | 2:527a66d0a1a9 | 37 | /** |
MiniTLS | 2:527a66d0a1a9 | 38 | @file ltc_ecc_projective_dbl_point.c |
MiniTLS | 2:527a66d0a1a9 | 39 | ECC Crypto, Tom St Denis |
MiniTLS | 2:527a66d0a1a9 | 40 | */ |
MiniTLS | 2:527a66d0a1a9 | 41 | |
MiniTLS | 2:527a66d0a1a9 | 42 | #if defined(LTC_MECC) && (!defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC)) |
MiniTLS | 2:527a66d0a1a9 | 43 | |
MiniTLS | 2:527a66d0a1a9 | 44 | /** |
MiniTLS | 2:527a66d0a1a9 | 45 | Double an ECC point |
MiniTLS | 2:527a66d0a1a9 | 46 | @param P The point to double |
MiniTLS | 2:527a66d0a1a9 | 47 | @param R [out] The destination of the double |
MiniTLS | 2:527a66d0a1a9 | 48 | @param modulus The modulus of the field the ECC curve is in |
MiniTLS | 2:527a66d0a1a9 | 49 | @param mp The "b" value from montgomery_setup() |
MiniTLS | 2:527a66d0a1a9 | 50 | @return MINITLS_OK on success |
MiniTLS | 2:527a66d0a1a9 | 51 | */ |
MiniTLS | 2:527a66d0a1a9 | 52 | int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp) |
MiniTLS | 2:527a66d0a1a9 | 53 | { |
MiniTLS | 2:527a66d0a1a9 | 54 | fp_int t1, t2; |
MiniTLS | 2:527a66d0a1a9 | 55 | int err; |
MiniTLS | 2:527a66d0a1a9 | 56 | |
MiniTLS | 2:527a66d0a1a9 | 57 | LTC_ARGCHK(P != NULL); |
MiniTLS | 2:527a66d0a1a9 | 58 | LTC_ARGCHK(R != NULL); |
MiniTLS | 2:527a66d0a1a9 | 59 | LTC_ARGCHK(modulus != NULL); |
MiniTLS | 2:527a66d0a1a9 | 60 | LTC_ARGCHK(mp != NULL); |
MiniTLS | 2:527a66d0a1a9 | 61 | |
MiniTLS | 2:527a66d0a1a9 | 62 | if ((err = mp_init_multi(&t1, &t2, NULL)) != MINITLS_OK){ |
MiniTLS | 2:527a66d0a1a9 | 63 | return err; |
MiniTLS | 2:527a66d0a1a9 | 64 | } |
MiniTLS | 2:527a66d0a1a9 | 65 | |
MiniTLS | 2:527a66d0a1a9 | 66 | if (P != R) { |
MiniTLS | 2:527a66d0a1a9 | 67 | /*if ((err = */mp_copy(&P->x, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 68 | /*if ((err = */mp_copy(&P->y, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 69 | /*if ((err = */mp_copy(&P->z, &R->z);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 70 | } |
MiniTLS | 2:527a66d0a1a9 | 71 | |
MiniTLS | 2:527a66d0a1a9 | 72 | /* &t1 = Z * Z */ |
MiniTLS | 2:527a66d0a1a9 | 73 | /*if ((err = */mp_sqr(&R->z, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 74 | /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 75 | /* Z = Y * Z */ |
MiniTLS | 2:527a66d0a1a9 | 76 | /*if ((err = */mp_mul(&R->z, &R->y, &R->z);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 77 | /*if ((err = */mp_montgomery_reduce(&R->z, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 78 | /* Z = 2Z */ |
MiniTLS | 2:527a66d0a1a9 | 79 | /*if ((err = */mp_add(&R->z, &R->z, &R->z);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 80 | if (mp_cmp(&R->z, modulus) != MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 81 | /*if ((err = */mp_sub(&R->z, modulus, &R->z);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 82 | } |
MiniTLS | 2:527a66d0a1a9 | 83 | |
MiniTLS | 2:527a66d0a1a9 | 84 | /* T2 = X - T1 */ |
MiniTLS | 2:527a66d0a1a9 | 85 | /*if ((err = */mp_sub(&R->x, &t1, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 86 | if (mp_cmp_d(&t2, 0) == MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 87 | /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 88 | } |
MiniTLS | 2:527a66d0a1a9 | 89 | /* T1 = X + T1 */ |
MiniTLS | 2:527a66d0a1a9 | 90 | /*if ((err = */mp_add(&t1, &R->x, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 91 | if (mp_cmp(&t1, modulus) != MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 92 | /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 93 | } |
MiniTLS | 2:527a66d0a1a9 | 94 | /* T2 = T1 * T2 */ |
MiniTLS | 2:527a66d0a1a9 | 95 | /*if ((err = */mp_mul(&t1, &t2, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 96 | /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 97 | /* T1 = 2T2 */ |
MiniTLS | 2:527a66d0a1a9 | 98 | /*if ((err = */mp_add(&t2, &t2, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 99 | if (mp_cmp(&t1, modulus) != MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 100 | /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 101 | } |
MiniTLS | 2:527a66d0a1a9 | 102 | /* T1 = T1 + T2 */ |
MiniTLS | 2:527a66d0a1a9 | 103 | /*if ((err = */mp_add(&t1, &t2, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 104 | if (mp_cmp(&t1, modulus) != MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 105 | /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 106 | } |
MiniTLS | 2:527a66d0a1a9 | 107 | |
MiniTLS | 2:527a66d0a1a9 | 108 | /* Y = 2Y */ |
MiniTLS | 2:527a66d0a1a9 | 109 | /*if ((err = */mp_add(&R->y, &R->y, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 110 | if (mp_cmp(&R->y, modulus) != MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 111 | /*if ((err = */mp_sub(&R->y, modulus, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 112 | } |
MiniTLS | 2:527a66d0a1a9 | 113 | /* Y = Y * Y */ |
MiniTLS | 2:527a66d0a1a9 | 114 | /*if ((err = */mp_sqr(&R->y, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 115 | /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 116 | /* T2 = Y * Y */ |
MiniTLS | 2:527a66d0a1a9 | 117 | /*if ((err = */mp_sqr(&R->y, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 118 | /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 119 | /* T2 = T2/2 */ |
MiniTLS | 2:527a66d0a1a9 | 120 | if (mp_isodd(&t2)) { |
MiniTLS | 2:527a66d0a1a9 | 121 | /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 122 | } |
MiniTLS | 2:527a66d0a1a9 | 123 | /*if ((err = */mp_div_2(&t2, &t2);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 124 | /* Y = Y * X */ |
MiniTLS | 2:527a66d0a1a9 | 125 | /*if ((err = */mp_mul(&R->y, &R->x, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 126 | /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 127 | |
MiniTLS | 2:527a66d0a1a9 | 128 | /* X = T1 * T1 */ |
MiniTLS | 2:527a66d0a1a9 | 129 | /*if ((err = */mp_sqr(&t1, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 130 | /*if ((err = */mp_montgomery_reduce(&R->x, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 131 | /* X = X - Y */ |
MiniTLS | 2:527a66d0a1a9 | 132 | /*if ((err = */mp_sub(&R->x, &R->y, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 133 | if (mp_cmp_d(&R->x, 0) == MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 134 | /*if ((err = */mp_add(&R->x, modulus, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 135 | } |
MiniTLS | 2:527a66d0a1a9 | 136 | /* X = X - Y */ |
MiniTLS | 2:527a66d0a1a9 | 137 | /*if ((err = */mp_sub(&R->x, &R->y, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 138 | if (mp_cmp_d(&R->x, 0) == MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 139 | /*if ((err = */mp_add(&R->x, modulus, &R->x);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 140 | } |
MiniTLS | 2:527a66d0a1a9 | 141 | |
MiniTLS | 2:527a66d0a1a9 | 142 | /* Y = Y - X */ |
MiniTLS | 2:527a66d0a1a9 | 143 | /*if ((err = */mp_sub(&R->y, &R->x, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 144 | if (mp_cmp_d(&R->y, 0) == MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 145 | /*if ((err = */mp_add(&R->y, modulus, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 146 | } |
MiniTLS | 2:527a66d0a1a9 | 147 | /* Y = Y * T1 */ |
MiniTLS | 2:527a66d0a1a9 | 148 | /*if ((err = */mp_mul(&R->y, &t1, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 149 | /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 150 | /* Y = Y - T2 */ |
MiniTLS | 2:527a66d0a1a9 | 151 | /*if ((err = */mp_sub(&R->y, &t2, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 152 | if (mp_cmp_d(&R->y, 0) == MP_LT) { |
MiniTLS | 2:527a66d0a1a9 | 153 | /*if ((err = */mp_add(&R->y, modulus, &R->y);/*) != MINITLS_OK) { goto done; }*/ |
MiniTLS | 2:527a66d0a1a9 | 154 | } |
MiniTLS | 2:527a66d0a1a9 | 155 | |
MiniTLS | 2:527a66d0a1a9 | 156 | err = MINITLS_OK; |
MiniTLS | 2:527a66d0a1a9 | 157 | /*done:*/ //Unused |
MiniTLS | 2:527a66d0a1a9 | 158 | mp_clear_multi(&t1, &t2, NULL); |
MiniTLS | 2:527a66d0a1a9 | 159 | return err; |
MiniTLS | 2:527a66d0a1a9 | 160 | } |
MiniTLS | 2:527a66d0a1a9 | 161 | #endif |
MiniTLS | 2:527a66d0a1a9 | 162 | /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_projective_dbl_point.c,v $ */ |
MiniTLS | 2:527a66d0a1a9 | 163 | /* $Revision: 1.11 $ */ |
MiniTLS | 2:527a66d0a1a9 | 164 | /* $Date: 2007/05/12 14:32:35 $ */ |
MiniTLS | 2:527a66d0a1a9 | 165 |