예제 #1
0
파일: secp192r1.c 프로젝트: codeguru1/ecc
NN_UINT
omega_mul(NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *omega, NN_UINT digits)
{

#ifdef EIGHT_BIT_PROCESSOR

    NN_Assign(a, b, digits);
    NN_Add(a+8, a+8, b, digits+1);
    return (digits+9);
    /* EIGHT_BIT_PROCESSOR */
#elif defined(SIXTEEN_BIT_PROCESSOR)

    NN_Assign(a, b, digits);
    NN_Add(a+4, a+4, b, digits+1);
    return (digits+5);

    /* SIXTEEN_BIT_PROCESSOR */
#elif defined(THIRTYTWO_BIT_PROCESSOR)

    NN_Assign(a, b, digits);
    NN_Add(&a[2], &a[2], b, digits+1);
    return (digits+3);

#endif /* THIRTYTWO_BIT_PROCESSOR */

}
예제 #2
0
파일: secp128r2.c 프로젝트: codeguru1/ecc
NN_UINT
omega_mul(NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *omega, NN_UINT digits)
{

#ifdef EIGHT_BIT_PROCESSOR
    NN_Assign(a, b, digits);
    a[digits+12] += NN_AddDigitMult(&a[12], &a[12], omega[12], b, digits);
    return (digits+13);
  /* EIGHT_BIT_PROCESSOR */
#elif defined(SIXTEEN_BIT_PROCESSOR)
    //memset(a, 0, digits*NN_DIGIT_LEN);
    NN_Assign(a, b, digits);
    a[digits+6] += NN_AddDigitMult(&a[6], &a[6], omega[6], b, digits);
    return (digits+7);

    /* SIXTEEN_BIT_PROCESSOR */
#elif defined(THIRTYTWO_BIT_PROCESSOR)

  NN_Assign(a, b, digits);
  a[digits+3] += NN_AddDigitMult(&a[3], &a[3], omega[3], b, digits);
  return (digits+4);

#endif /* THIRTYTWO_BIT_PROCESSOR */

}
예제 #3
0
/*---------------------------------------------------------------------------*/
void
ecc_win_precompute(point_t * baseP, point_t * pointArray)
{
  uint8_t i;

  NN_Assign(pointArray[0].x, baseP->x, NUMWORDS);
  NN_Assign(pointArray[0].y, baseP->y, NUMWORDS);

  for(i = 1; i < NUM_POINTS; i++) {
    ecc_add(&(pointArray[i]), &(pointArray[i-1]), baseP);
  }

  for(i = 0; i < NUM_MASKS; i++) {
    mask[i] = BASIC_MASK << (W_BITS*i);
  }

}
예제 #4
0
파일: prime.cpp 프로젝트: eiginn/waste
/* Generates a probable prime a between b and c such that a-1 is divisible by d.

Lengths: a[digits], b[digits], c[digits], d[digits].
Assumes b < c, digits < MAX_NN_DIGITS.

Returns RE_NEED_RANDOM if randomStruct not seeded, RE_DATA if
unsuccessful.
*/
int GeneratePrime(NN_DIGIT *a, NN_DIGIT *b, NN_DIGIT *c, NN_DIGIT *d, unsigned int digits, R_RANDOM_STRUCT *randomStruct)
{
	int status;
	unsigned char block[MAX_NN_DIGITS * NN_DIGIT_LEN];
	NN_DIGIT t[MAX_NN_DIGITS], u[MAX_NN_DIGITS];

	/* Generate random number between b and c.
	*/
	if ((status = R_GenerateBytes (block, digits * NN_DIGIT_LEN, randomStruct))!=0) return (status);
	NN_Decode (a, digits, block, digits * NN_DIGIT_LEN);
	NN_Sub (t, c, b, digits);
	NN_ASSIGN_DIGIT (u, 1, digits);
	NN_Add (t, t, u, digits);
	NN_Mod (a, a, digits, t, digits);
	NN_Add (a, a, b, digits);

	/* Adjust so that a-1 is divisible by d.
	*/
	NN_Mod (t, a, digits, d, digits);
	NN_Sub (a, a, t, digits);
	NN_Add (a, a, u, digits);
	if (NN_Cmp (a, b, digits) < 0) NN_Add (a, a, d, digits);
	if (NN_Cmp (a, c, digits) > 0) NN_Sub (a, a, d, digits);

	/* Search to c in steps of d.
	*/
	NN_Assign (t, c, digits);
	NN_Sub (t, t, d, digits);

	while (! ProbablePrime (a, digits)) {
		if (NN_Cmp (a, t, digits) > 0) return (RE_DATA);
		NN_Add(a, a, d, digits);
	};

	return (0);
}
예제 #5
0
/**
 * \brief             P0 = P1
 */
static void
p_copy(point_t * P0, point_t * P1)
{
  NN_Assign(P0->x, P1->x, NUMWORDS);
  NN_Assign(P0->y, P1->y, NUMWORDS);
}
예제 #6
0
/*---------------------------------------------------------------------------*/
void
ecc_add_proj(point_t * P0, NN_DIGIT *Z0, point_t * P1, NN_DIGIT * Z1, point_t * P2, NN_DIGIT * Z2)
{
  NN_DIGIT n0[NUMWORDS];
  NN_DIGIT n1[NUMWORDS];
  NN_DIGIT n2[NUMWORDS];
  NN_DIGIT n3[NUMWORDS];
  NN_DIGIT n4[NUMWORDS];
  NN_DIGIT n5[NUMWORDS];
  NN_DIGIT n6[NUMWORDS];

  if(NN_Zero(Z1, NUMWORDS)) {
    p_copy(P0, P2);
    NN_Assign(Z0, Z2, NUMWORDS);
    return;
  }

  if(NN_Zero(Z2, NUMWORDS)) {
    p_copy(P0, P1);
    NN_Assign(Z0, Z1, NUMWORDS);
    return;
  }

  /* double */
  if(p_equal(P1, P2)) {
    ecc_dbl_proj(P0, Z0, P1, Z1);
    return;
  }

  /* add_proj
   * n1, n2
   */
  if(Z_is_one(Z2)) {
    /* n1 = P1->x, n2 = P1->y */
    NN_Assign(n1, P1->x, NUMWORDS);
    NN_Assign(n2, P1->y, NUMWORDS);
  } else {
    /* n1 = P1->x * Z2^2 */
    NN_ModSqrOpt(n0, Z2, param.p, param.omega, NUMWORDS);
    NN_ModMultOpt(n1, P1->x, n0, param.p, param.omega, NUMWORDS);
    /* n2 = P1->y * Z2^3 */
    NN_ModMultOpt(n0, n0, Z2, param.p, param.omega, NUMWORDS);
    NN_ModMultOpt(n2, P1->y, n0, param.p, param.omega, NUMWORDS);
  }

  /* n3, n4 */
  if(Z_is_one(Z1)) {
    /* n3 = P2->x, n4 = P2->y */
    NN_Assign(n3, P2->x, NUMWORDS);
    NN_Assign(n4, P2->y, NUMWORDS);
  } else {
    /* n3 = P2->x * Z1^2 */
    NN_ModSqrOpt(n0, Z1, param.p, param.omega, NUMWORDS);
    NN_ModMultOpt(n3, P2->x, n0, param.p, param.omega, NUMWORDS);
    /* n4 = P2->y * Z1^3 */
    NN_ModMultOpt(n0, n0, Z1, param.p, param.omega, NUMWORDS);
    NN_ModMultOpt(n4, P2->y, n0, param.p, param.omega, NUMWORDS);
  }

  /* n5 = n1 - n3, n6 = n2 - n4 */
  NN_ModSub(n5, n1, n3, param.p, NUMWORDS);
  NN_ModSub(n6, n2, n4, param.p, NUMWORDS);

  if(NN_Zero(n5, NUMWORDS)) {
    if(NN_Zero(n6, NUMWORDS)) {
      /* P1 and P2 are same point */
      ecc_dbl_proj(P0, Z0, P1, Z1);
      return;
    }
  } else {
    /* P1 is the inverse of P2 */
    NN_AssignZero(Z0, NUMWORDS);
    return;
  }

  /* 'n7' = n1 + n3, 'n8' = n2 + n4 */
  NN_ModAdd(n1, n1, n3, param.p, NUMWORDS);
  NN_ModAdd(n2, n2, n4, param.p, NUMWORDS);

  /* Z0 = Z1 * Z2 * n5 */
  if(Z_is_one(Z1) && Z_is_one(Z2)) {
    NN_Assign(Z0, n5, NUMWORDS);
  } else {
    if(Z_is_one(Z1)) {
      NN_Assign(n0, Z2, NUMWORDS);
    } else if(Z_is_one(Z2)) {
      NN_Assign(n0, Z1, NUMWORDS);
    } else {
      NN_ModMultOpt(n0, Z1, Z2, param.p, param.omega, NUMWORDS);
    }
    NN_ModMultOpt(Z0, n0, n5, param.p, param.omega, NUMWORDS);
  }

  /* P0->x = n6^2 - n5^2 * 'n7' */
  NN_ModSqrOpt(n0, n6, param.p, param.omega, NUMWORDS);
  NN_ModSqrOpt(n4, n5, param.p, param.omega, NUMWORDS);
  NN_ModMultOpt(n3, n1, n4, param.p, param.omega, NUMWORDS);
  NN_ModSub(P0->x, n0, n3, param.p, NUMWORDS);

  /* 'n9' = n5^2 * 'n7' - 2 * P0->x */
  NN_LShift(n0, P0->x, 1, NUMWORDS);
  NN_ModSmall(n0, param.p, NUMWORDS);
  NN_ModSub(n0, n3, n0, param.p, NUMWORDS);

  /* P0->y = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
  NN_ModMultOpt(n0, n0, n6, param.p, param.omega, NUMWORDS);
  NN_ModMultOpt(n5, n4, n5, param.p, param.omega, NUMWORDS);
  NN_ModMultOpt(n1, n2, n5, param.p, param.omega, NUMWORDS);
  NN_ModSub(n0, n0, n1, param.p, NUMWORDS);

  if((n0[0] % 2) == 1) {
    NN_Add(n0, n0, param.p, NUMWORDS);
  }

  NN_RShift(P0->y, n0, 1, NUMWORDS);

}
예제 #7
0
/*---------------------------------------------------------------------------*/
void
ecc_dbl_proj(point_t * P0, NN_DIGIT *Z0, point_t * P1, NN_DIGIT * Z1)
{
  NN_DIGIT n0[NUMWORDS];
  NN_DIGIT n1[NUMWORDS];
  NN_DIGIT n2[NUMWORDS];
  NN_DIGIT n3[NUMWORDS];

  if(NN_Zero(Z1, NUMWORDS)) {
    NN_AssignZero(Z0, NUMWORDS);
    return;
  }

  // n1
  if(Z_is_one(Z1)) {
    /* n1 = 3 * P1->x^2 + param.E.a */
    NN_ModSqrOpt(n0, P1->x, param.p, param.omega, NUMWORDS);
    NN_LShift(n1, n0, 1, NUMWORDS);
    NN_ModSmall(n1, param.p, NUMWORDS);
    NN_ModAdd(n0, n0, n1, param.p, NUMWORDS);
    NN_ModAdd(n1, n0, param.E.a, param.p, NUMWORDS);
  } else {
    if(param.E.a_minus3) {
      /* for a = -3
       * n1 = 3 * (X1 + Z1^2) * (X1 - Z1^2) = 3 * X1^2 - 3 * Z1^4
       */
      NN_ModSqrOpt(n1, Z1, param.p, param.omega, NUMWORDS);
      NN_ModAdd(n0, P1->x, n1, param.p, NUMWORDS);
      NN_ModSub(n2, P1->x, n1, param.p, NUMWORDS);
      NN_ModMultOpt(n1, n0, n2, param.p, param.omega, NUMWORDS);
      NN_LShift(n0, n1, 1, NUMWORDS);
      NN_ModSmall(n0, param.p, NUMWORDS);
      NN_ModAdd(n1, n0, n1, param.p, NUMWORDS);

    } else if (param.E.a_zero) {
      /* n1 = 3 * P1->x^2 */
      NN_ModSqrOpt(n0, P1->x, param.p, param.omega, NUMWORDS);
      NN_LShift(n1, n0, 1, NUMWORDS);
      NN_ModSmall(n1, param.p, NUMWORDS);
      NN_ModAdd(n1, n0, n1, param.p, NUMWORDS);
    } else {
      /* n1 = 3 * P1->x^2 + param.E.a * Z1^4 */
      NN_ModSqrOpt(n0, P1->x, param.p, param.omega, NUMWORDS);
      NN_LShift(n1, n0, 1, NUMWORDS);
      NN_ModSmall(n1, param.p, NUMWORDS);
      NN_ModAdd(n0, n0, n1, param.p, NUMWORDS);
      NN_ModSqrOpt(n1, Z1, param.p, param.omega, NUMWORDS);
      NN_ModSqrOpt(n1, n1, param.p, param.omega, NUMWORDS);
      NN_ModMultOpt(n1, n1, param.E.a, param.p, param.omega, NUMWORDS);
      NN_ModAdd(n1, n1, n0, param.p, NUMWORDS);
    }
  }

  /* Z0 = 2 * P1->y * Z1 */
  if(Z_is_one(Z1)) {
    NN_Assign(n0, P1->y, NUMWORDS);
  } else {
    NN_ModMultOpt(n0, P1->y, Z1, param.p, param.omega, NUMWORDS);
  }
  NN_LShift(Z0, n0, 1, NUMWORDS);
  NN_ModSmall(Z0, param.p, NUMWORDS);

  /* n2 = 4 * P1->x * P1->y^2 */
  NN_ModSqrOpt(n3, P1->y, param.p, param.omega, NUMWORDS);
  NN_ModMultOpt(n2, P1->x, n3, param.p, param.omega, NUMWORDS);
  NN_LShift(n2, n2, 2, NUMWORDS);
  NN_ModSmall(n2, param.p, NUMWORDS);

  /* P0->x = n1^2 - 2 * n2 */
  NN_LShift(n0, n2, 1, NUMWORDS);
  NN_ModSmall(n0, param.p, NUMWORDS);
  NN_ModSqrOpt(P0->x, n1, param.p, param.omega, NUMWORDS);
  NN_ModSub(P0->x, P0->x, n0, param.p, NUMWORDS);

  /* n3 = 8 * P1->y^4 */
  NN_ModSqrOpt(n0, n3, param.p, param.omega, NUMWORDS);
  NN_LShift(n3, n0, 3, NUMWORDS);
  NN_ModSmall(n3, param.p, NUMWORDS);

  /* P0->y = n1 * (n2 - P0->x) - n3 */
  NN_ModSub(n0, n2, P0->x, param.p, NUMWORDS);
  NN_ModMultOpt(n0, n1, n0, param.p, param.omega, NUMWORDS);
  NN_ModSub(P0->y, n0, n3, param.p, NUMWORDS);

}
예제 #8
0
/*---------------------------------------------------------------------------*/
void
ecc_get_order(NN_DIGIT * order)
{
  NN_Assign(order, param.r, NUMWORDS);
}
예제 #9
0
static void
c_add_mix(point_t * P0, NN_DIGIT *Z0, point_t * P1, NN_DIGIT * Z1, point_t * P2)
{
  NN_DIGIT t1[NUMWORDS];
  NN_DIGIT t2[NUMWORDS];
  NN_DIGIT t3[NUMWORDS];
  NN_DIGIT t4[NUMWORDS];
  NN_DIGIT Z2[NUMWORDS];

  /* P2 == infinity */
  if(NN_Zero(P2->x, NUMWORDS)) {
    if(NN_Zero(P2->y, NUMWORDS)) {
      p_copy(P0, P1);
      NN_Assign(Z0, Z1, NUMWORDS);
      return;
    }
  }

  /* P1 == infinity */
  if(NN_Zero(Z1, NUMWORDS)) {
    p_copy(P0, P2);
    NN_AssignDigit(Z0, 1, NUMWORDS);
    return;
  }

  /* T1 = Z1^2 */
  NN_ModSqrOpt(t1, Z1, param.p, param.omega, NUMWORDS);
  /* T2 = T1*Z1 */
  NN_ModMultOpt(t2, t1, Z1, param.p, param.omega, NUMWORDS);
  /* T1 = T1*P2->x */
  NN_ModMultOpt(t1, t1, P2->x, param.p, param.omega, NUMWORDS);
  /* T2 = T2*P2->y */
  NN_ModMultOpt(t2, t2, P2->y, param.p, param.omega, NUMWORDS);
  /* T1 = T1-P1->x */
  NN_ModSub(t1, t1, P1->x, param.p, NUMWORDS);
  /* T2 = T2-P1->y */
  NN_ModSub(t2, t2, P1->y, param.p, NUMWORDS);

  if(NN_Zero(t1, NUMWORDS)) {
    if(NN_Zero(t2, NUMWORDS)) {
      NN_AssignDigit(Z2, 1, NUMWORDS);
      ecc_dbl_proj(P0, Z0, P2, Z2);
      return;
    } else {
      NN_AssignDigit(Z0, 0, NUMWORDS);
      return;
    }
  }
  /* Z3 = Z1*T1 */
  NN_ModMultOpt(Z0, Z1, t1, param.p, param.omega, NUMWORDS);
  /* T3 = T1^2 */
  NN_ModSqrOpt(t3, t1, param.p, param.omega, NUMWORDS);
  /* T4 = T3*T1 */
  NN_ModMultOpt(t4, t3, t1, param.p, param.omega, NUMWORDS);
  /* T3 = T3*P1->x */
  NN_ModMultOpt(t3, t3, P1->x, param.p, param.omega, NUMWORDS);
  /* T1 = 2*T3 */
  NN_LShift(t1, t3, 1, NUMWORDS);
  NN_ModSmall(t1, param.p, NUMWORDS);
  /* P0->x = T2^2 */
  NN_ModSqrOpt(P0->x, t2, param.p, param.omega, NUMWORDS);
  /* P0->x = P0->x-T1 */
  NN_ModSub(P0->x, P0->x, t1, param.p, NUMWORDS);
  /* P0->x = P0->x-T4 */
  NN_ModSub(P0->x, P0->x, t4, param.p, NUMWORDS);
  /* T3 = T3-P0->x */
  NN_ModSub(t3, t3, P0->x, param.p, NUMWORDS);
  /* T3 = T3*T2 */
  NN_ModMultOpt(t3, t3, t2, param.p, param.omega, NUMWORDS);
  /* T4 = T4*P1->y */
  NN_ModMultOpt(t4, t4, P1->y, param.p, param.omega, NUMWORDS);
  /* P0->y = T3-T4 */
  NN_ModSub(P0->y, t3, t4, param.p, NUMWORDS);

  return;
}