int ecc_check_point ( point_t *P ) { NN_DIGIT tmp1[NUMWORDS], tmp2[NUMWORDS]; if ( NN_Zero ( P->x, NUMWORDS ) ) return -1; if ( NN_Cmp ( P->x, param.p, NUMWORDS ) >= 0 ) return -1; if ( NN_Zero ( P->y, NUMWORDS ) ) return -1; if ( NN_Cmp ( P->y, param.p, NUMWORDS ) >= 0 ) return -1; memset ( tmp1, 0, NUMWORDS*NN_DIGIT_LEN ); memset ( tmp2, 0, NUMWORDS*NN_DIGIT_LEN ); NN_ModSqrOpt ( tmp2, P->x, param.p, param.omega, NUMWORDS ); NN_ModMultOpt ( tmp2, tmp2, P->x, param.p, param.omega, NUMWORDS ); NN_ModMultOpt ( tmp1, P->x, param.E.a, param.p, param.omega, NUMWORDS ); NN_ModAdd ( tmp2, tmp1, tmp2, param.p, NUMWORDS ); NN_ModAdd ( tmp2, tmp2, param.E.b, param.p, NUMWORDS ); NN_ModSqrOpt ( tmp1, P->y, param.p, param.omega, NUMWORDS ); if ( NN_Cmp ( tmp1, tmp2, NUMWORDS ) != 0 ) return -2; return 1; }
/* 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); }
/* Returns nonzero iff a has a prime factor in SMALL_PRIMES. Lengths: a[aDigits]. Assumes aDigits < MAX_NN_DIGITS. */ static int SmallFactor(NN_DIGIT *a, unsigned int aDigits) { int status; NN_DIGIT t[1]; unsigned int i; status = 0; for (i = 0; i < SMALL_PRIME_COUNT; i++) { NN_ASSIGN_DIGIT(t, SMALL_PRIMES[i], 1); if ((aDigits == 1) && ! NN_Cmp (a, t, 1)) break; NN_Mod(t, a, aDigits, t, 1); if (NN_Zero(t, 1)) { status = 1; break; }; }; //Zeroize sensitive information. i = 0; R_memset((POINTER)t, 0, sizeof (t)); return (status); }