CryptX

 view release on metacpan or  search on metacpan

src/ltc/pk/ecc/ltc_ecc_projective_add_point.c  view on Meta::CPAN

      /* P is point at infinity >> Result = Q */
      err = ltc_ecc_copy_point(Q, R);
      goto done;
   }

   if ((err = ltc_ecc_is_point_at_infinity(Q, modulus, &inf)) != CRYPT_OK) return err;
   if (inf) {
      /* Q is point at infinity >> Result = P */
      err = ltc_ecc_copy_point(P, R);
      goto done;
   }

   if ((ltc_mp_cmp(P->x, Q->x) == LTC_MP_EQ) && (ltc_mp_cmp(P->z, Q->z) == LTC_MP_EQ)) {
      if (ltc_mp_cmp(P->y, Q->y) == LTC_MP_EQ) {
dbl_point:
         /* here P = Q >> Result = 2 * P (use doubling) */
         ltc_mp_deinit_multi(t1, t2, x, y, z, LTC_NULL);
         return ltc_ecc_projective_dbl_point(P, R, ma, modulus, mp);
      }
      if ((err = ltc_mp_sub(modulus, Q->y, t1)) != CRYPT_OK)                       { goto done; }
      if (ltc_mp_cmp(P->y, t1) == LTC_MP_EQ) {
         /* here Q = -P >>> Result = the point at infinity */
         err = ltc_ecc_set_point_xyz(1, 1, 0, R);
         goto done;
      }
   }

   if ((err = ltc_mp_copy(P->x, x)) != CRYPT_OK)                                   { goto done; }
   if ((err = ltc_mp_copy(P->y, y)) != CRYPT_OK)                                   { goto done; }
   if ((err = ltc_mp_copy(P->z, z)) != CRYPT_OK)                                   { goto done; }

   /* if Z is one then these are no-operations */
   if (Q->z != NULL) {
      /* T1 = Z' * Z' */
      if ((err = ltc_mp_sqr(Q->z, t1)) != CRYPT_OK)                                { goto done; }
      if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)           { goto done; }
      /* X = X * T1 */
      if ((err = ltc_mp_mul(t1, x, x)) != CRYPT_OK)                                { goto done; }
      if ((err = ltc_mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK)            { goto done; }
      /* T1 = Z' * T1 */
      if ((err = ltc_mp_mul(Q->z, t1, t1)) != CRYPT_OK)                            { goto done; }
      if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)           { goto done; }
      /* Y = Y * T1 */
      if ((err = ltc_mp_mul(t1, y, y)) != CRYPT_OK)                                { goto done; }
      if ((err = ltc_mp_montgomery_reduce(y, modulus, mp)) != CRYPT_OK)            { goto done; }
   }

   /* T1 = Z*Z */
   if ((err = ltc_mp_sqr(z, t1)) != CRYPT_OK)                                      { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)              { goto done; }
   /* T2 = X' * T1 */
   if ((err = ltc_mp_mul(Q->x, t1, t2)) != CRYPT_OK)                               { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK)              { goto done; }
   /* T1 = Z * T1 */
   if ((err = ltc_mp_mul(z, t1, t1)) != CRYPT_OK)                                  { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)              { goto done; }
   /* T1 = Y' * T1 */
   if ((err = ltc_mp_mul(Q->y, t1, t1)) != CRYPT_OK)                               { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)              { goto done; }

   /* Same-affine dispatch: A==B means P,Q share affine x; dispatch to dbl or O before the formulas */
   if (ltc_mp_cmp(x, t2) == LTC_MP_EQ) {
      if (ltc_mp_cmp(y, t1) == LTC_MP_EQ) goto dbl_point;
      err = ltc_ecc_set_point_xyz(1, 1, 0, R);
      goto done;
   }

   /* Y = Y - T1 */
   if ((err = ltc_mp_sub(y, t1, y)) != CRYPT_OK)                                   { goto done; }
   if (ltc_mp_cmp_d(y, 0) == LTC_MP_LT) {
      if ((err = ltc_mp_add(y, modulus, y)) != CRYPT_OK)                           { goto done; }
   }
   /* T1 = 2T1 */
   if ((err = ltc_mp_add(t1, t1, t1)) != CRYPT_OK)                                 { goto done; }
   if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) {
      if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK)                         { goto done; }
   }
   /* T1 = Y + T1 */
   if ((err = ltc_mp_add(t1, y, t1)) != CRYPT_OK)                                  { goto done; }
   if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) {
      if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK)                         { goto done; }
   }
   /* X = X - T2 */
   if ((err = ltc_mp_sub(x, t2, x)) != CRYPT_OK)                                   { goto done; }
   if (ltc_mp_cmp_d(x, 0) == LTC_MP_LT) {
      if ((err = ltc_mp_add(x, modulus, x)) != CRYPT_OK)                           { goto done; }
   }
   /* T2 = 2T2 */
   if ((err = ltc_mp_add(t2, t2, t2)) != CRYPT_OK)                                 { goto done; }
   if (ltc_mp_cmp(t2, modulus) != LTC_MP_LT) {
      if ((err = ltc_mp_sub(t2, modulus, t2)) != CRYPT_OK)                         { goto done; }
   }
   /* T2 = X + T2 */
   if ((err = ltc_mp_add(t2, x, t2)) != CRYPT_OK)                                  { goto done; }
   if (ltc_mp_cmp(t2, modulus) != LTC_MP_LT) {
      if ((err = ltc_mp_sub(t2, modulus, t2)) != CRYPT_OK)                         { goto done; }
   }

   /* if Z' != 1 */
   if (Q->z != NULL) {
      /* Z = Z * Z' */
      if ((err = ltc_mp_mul(z, Q->z, z)) != CRYPT_OK)                              { goto done; }
      if ((err = ltc_mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK)            { goto done; }
   }

   /* Z = Z * X */
   if ((err = ltc_mp_mul(z, x, z)) != CRYPT_OK)                                    { goto done; }
   if ((err = ltc_mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK)               { goto done; }

   /* T1 = T1 * X  */
   if ((err = ltc_mp_mul(t1, x, t1)) != CRYPT_OK)                                  { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)              { goto done; }
   /* X = X * X */
   if ((err = ltc_mp_sqr(x, x)) != CRYPT_OK)                                       { goto done; }
   if ((err = ltc_mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK)               { goto done; }
   /* T2 = T2 * x */
   if ((err = ltc_mp_mul(t2, x, t2)) != CRYPT_OK)                                  { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK)              { goto done; }
   /* T1 = T1 * X  */
   if ((err = ltc_mp_mul(t1, x, t1)) != CRYPT_OK)                                  { goto done; }
   if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK)              { goto done; }



( run in 1.723 second using v1.01-cache-2.11-cpan-5735350b133 )