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
ltc_ecc_projective_add_point.c
00001 /* 00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices 00003 Author: Donatien Garnier 00004 Copyright (C) 2013-2014 AppNearMe Ltd 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *//* LibTomCrypt, modular cryptographic library -- Tom St Denis 00020 * 00021 * LibTomCrypt is a library that provides various cryptographic 00022 * algorithms in a highly modular and flexible manner. 00023 * 00024 * The library is free for all purposes without any express 00025 * guarantee it works. 00026 * 00027 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org 00028 */ 00029 00030 /* Implements ECC over Z/pZ for curve &y^2 = &x^3 - 3x + b 00031 * 00032 * All curves taken from NIST recommendation paper of July 1999 00033 * Available at http://csrc.nist.gov/cryptval/dss.htm 00034 */ 00035 #include "ltc.h" 00036 00037 /** 00038 @file ltc_ecc_projective_add_point.c 00039 ECC Crypto, Tom St Denis 00040 */ 00041 00042 #if defined(LTC_MECC) & (!defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC)) 00043 00044 /** 00045 Add two ECC points 00046 @param P The point to add 00047 @param Q The point to add 00048 @param R [out] The destination of the double 00049 @param modulus The modulus of the field the ECC curve is in 00050 @param mp The "b" value from montgomery_setup() 00051 @return MINITLS_OK on success 00052 */ 00053 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp) 00054 { 00055 fp_int t1, t2, x, y, z; 00056 int err; 00057 00058 LTC_ARGCHK(P != NULL); 00059 LTC_ARGCHK(Q != NULL); 00060 LTC_ARGCHK(R != NULL); 00061 LTC_ARGCHK(modulus != NULL); 00062 LTC_ARGCHK(mp != NULL); 00063 00064 if ((err = mp_init_multi(&t1, &t2, &x, &y, &z, NULL)) != MINITLS_OK) { 00065 return err; 00066 } 00067 00068 /* should we dbl instead? */ 00069 /*if ((err =*/ mp_sub(modulus, &Q->y, &t1);/*) != MINITLS_OK) { goto done; }*/ 00070 00071 if ( (mp_cmp(&P->x, &Q->x) == MP_EQ) && 00072 ((&Q->z != NULL) && mp_cmp(&P->z, &Q->z) == MP_EQ) && 00073 (mp_cmp(&P->y, &Q->y) == MP_EQ || mp_cmp(&P->y, &t1) == MP_EQ)) { 00074 mp_clear_multi(&t1, &t2, &x, &y, &z, NULL); 00075 return ltc_ecc_projective_dbl_point(P, R, modulus, mp); 00076 } 00077 00078 /*if ((err =*/ mp_copy(&P->x, &x);/*) != MINITLS_OK) { goto done; }*/ 00079 /*if ((err =*/ mp_copy(&P->y, &y);/*) != MINITLS_OK) { goto done; }*/ 00080 /*if ((err =*/ mp_copy(&P->z, &z);/*) != MINITLS_OK) { goto done; }*/ 00081 00082 /* if Z is one then these are no-operations */ 00083 if (&Q->z != NULL) { 00084 /* T1 = Z' * Z' */ 00085 /*if ((err = */mp_sqr(&Q->z, &t1);/*) != MINITLS_OK) { goto done; }*/ 00086 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00087 /* X = X * T1 */ 00088 /*if ((err = */mp_mul(&t1, &x, &x);/*) != MINITLS_OK) { goto done; }*/ 00089 /*if ((err = */mp_montgomery_reduce(&x, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00090 /* T1 = Z' * T1 */ 00091 /*if ((err = */mp_mul(&Q->z, &t1, &t1);/*) != MINITLS_OK) { goto done; }*/ 00092 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00093 /* Y = Y * T1 */ 00094 /*if ((err = */mp_mul(&t1, &y, &y);/*) != MINITLS_OK) { goto done; }*/ 00095 /*if ((err = */mp_montgomery_reduce(&y, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00096 } 00097 00098 /* T1 = Z*Z */ 00099 /*if ((err = */mp_sqr(&z, &t1);/*) != MINITLS_OK) { goto done; }*/ 00100 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00101 /* T2 = X' * T1 */ 00102 /*if ((err = */mp_mul(&Q->x, &t1, &t2);/*) != MINITLS_OK) { goto done; }*/ 00103 /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00104 /* T1 = Z * T1 */ 00105 /*if ((err = */mp_mul(&z, &t1, &t1);/*) != MINITLS_OK) { goto done; }*/ 00106 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00107 /* T1 = Y' * T1 */ 00108 /*if ((err = */mp_mul(&Q->y, &t1, &t1);/*) != MINITLS_OK) { goto done; }*/ 00109 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00110 00111 /* Y = Y - T1 */ 00112 /*if ((err = */mp_sub(&y, &t1, &y);/*) != MINITLS_OK) { goto done; }*/ 00113 if (mp_cmp_d(&y, 0) == MP_LT) { 00114 /*if ((err = */mp_add(&y, modulus, &y);/*) != MINITLS_OK) { goto done; }*/ 00115 } 00116 /* T1 = 2T1 */ 00117 /*if ((err = */mp_add(&t1, &t1, &t1);/*) != MINITLS_OK) { goto done; }*/ 00118 if (mp_cmp(&t1, modulus) != MP_LT) { 00119 /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK) { goto done; }*/ 00120 } 00121 /* T1 = Y + T1 */ 00122 /*if ((err = */mp_add(&t1, &y, &t1);/*) != MINITLS_OK) { goto done; }*/ 00123 if (mp_cmp(&t1, modulus) != MP_LT) { 00124 /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK) { goto done; }*/ 00125 } 00126 /* X = X - T2 */ 00127 /*if ((err = */mp_sub(&x, &t2, &x);/*) != MINITLS_OK) { goto done; }*/ 00128 if (mp_cmp_d(&x, 0) == MP_LT) { 00129 /*if ((err = */mp_add(&x, modulus, &x);/*) != MINITLS_OK) { goto done; }*/ 00130 } 00131 /* T2 = 2T2 */ 00132 /*if ((err = */mp_add(&t2, &t2, &t2);/*) != MINITLS_OK) { goto done; }*/ 00133 if (mp_cmp(&t2, modulus) != MP_LT) { 00134 /*if ((err = */mp_sub(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ 00135 } 00136 /* T2 = X + T2 */ 00137 /*if ((err = */mp_add(&t2, &x, &t2);/*) != MINITLS_OK) { goto done; }*/ 00138 if (mp_cmp(&t2, modulus) != MP_LT) { 00139 /*if ((err = */mp_sub(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ 00140 } 00141 00142 /* if Z' != 1 */ 00143 if (&Q->z != NULL) { 00144 /* Z = Z * Z' */ 00145 /*if ((err = */mp_mul(&z, &Q->z, &z);/*) != MINITLS_OK) { goto done; }*/ 00146 /*if ((err = */mp_montgomery_reduce(&z, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00147 } 00148 00149 /* Z = Z * X */ 00150 /*if ((err = */mp_mul(&z, &x, &z);/*) != MINITLS_OK) { goto done; }*/ 00151 /*if ((err = */mp_montgomery_reduce(&z, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00152 00153 /* T1 = T1 * X */ 00154 /*if ((err = */mp_mul(&t1, &x, &t1);/*) != MINITLS_OK) { goto done; }*/ 00155 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00156 /* X = X * X */ 00157 /*if ((err = */mp_sqr(&x, &x);/*) != MINITLS_OK) { goto done; }*/ 00158 /*if ((err = */mp_montgomery_reduce(&x, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00159 /* T2 = T2 * &x */ 00160 /*if ((err = */mp_mul(&t2, &x, &t2);/*) != MINITLS_OK) { goto done; }*/ 00161 /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00162 /* T1 = T1 * X */ 00163 /*if ((err = */mp_mul(&t1, &x, &t1);/*) != MINITLS_OK) { goto done; }*/ 00164 /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00165 00166 /* X = Y*Y */ 00167 /*if ((err = */mp_sqr(&y, &x);/*) != MINITLS_OK) { goto done; }*/ 00168 /*if ((err = */mp_montgomery_reduce(&x, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00169 /* X = X - T2 */ 00170 /*if ((err = */mp_sub(&x, &t2, &x);/*) != MINITLS_OK) { goto done; }*/ 00171 if (mp_cmp_d(&x, 0) == MP_LT) { 00172 /*if ((err = */mp_add(&x, modulus, &x);/*) != MINITLS_OK) { goto done; }*/ 00173 } 00174 00175 /* T2 = T2 - X */ 00176 /*if ((err = */mp_sub(&t2, &x, &t2);/*) != MINITLS_OK) { goto done; }*/ 00177 if (mp_cmp_d(&t2, 0) == MP_LT) { 00178 /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ 00179 } 00180 /* T2 = T2 - X */ 00181 /*if ((err = */mp_sub(&t2, &x, &t2);/*) != MINITLS_OK) { goto done; }*/ 00182 if (mp_cmp_d(&t2, 0) == MP_LT) { 00183 /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK) { goto done; }*/ 00184 } 00185 /* T2 = T2 * Y */ 00186 /*if ((err = */mp_mul(&t2, &y, &t2);/*) != MINITLS_OK) { goto done; }*/ 00187 /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK) { goto done; }*/ 00188 /* Y = T2 - T1 */ 00189 /*if ((err = */mp_sub(&t2, &t1, &y);/*) != MINITLS_OK) { goto done; }*/ 00190 if (mp_cmp_d(&y, 0) == MP_LT) { 00191 /*if ((err = */mp_add(&y, modulus, &y);/*) != MINITLS_OK) { goto done; }*/ 00192 } 00193 /* Y = Y/2 */ 00194 if (mp_isodd(&y)) { 00195 /*if ((err = */mp_add(&y, modulus, &y);/*) != MINITLS_OK) { goto done; }*/ 00196 } 00197 /*if ((err = */mp_div_2(&y, &y);/*) != MINITLS_OK) { goto done; }*/ 00198 00199 /*if ((err = */mp_copy(&x, &R->x);/*) != MINITLS_OK) { goto done; }*/ 00200 /*if ((err = */mp_copy(&y, &R->y);/*) != MINITLS_OK) { goto done; }*/ 00201 /*if ((err = */mp_copy(&z, &R->z);/*) != MINITLS_OK) { goto done; }*/ 00202 00203 err = MINITLS_OK; 00204 /*done:*/ //Not used 00205 mp_clear_multi(&t1, &t2, &x, &y, &z, NULL); 00206 return err; 00207 } 00208 00209 #endif 00210 00211 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_projective_add_point.c,v $ */ 00212 /* $Revision: 1.16 $ */ 00213 /* $Date: 2007/05/12 14:32:35 $ */ 00214
Generated on Tue Jul 12 2022 19:20:10 by
1.7.2
