Skip to content

Commit

Permalink
Return an int from ntru_rand_prod()
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuktu committed Mar 5, 2014
1 parent 966c348 commit fec302c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/ntru.c
Expand Up @@ -26,7 +26,8 @@ int ntru_gen_key_pair_internal(NtruEncParams *params, NtruEncKeyPair *kp, int (*
NtruIntPoly f;
for (;;) {
/* choose random t, calculate f=3t+1 */
ntru_rand_prod(N, df1, df2, df3, df3, &t, rng, rand_ctx);
if (!ntru_rand_prod(N, df1, df2, df3, df3, &t, rng, rand_ctx))
return NTRU_ERR_PRNG;
ntru_prod_to_int(&t, &f);
ntru_mult_fac(&f, 3);
f.coeffs[0] += 1;
Expand Down
9 changes: 5 additions & 4 deletions src/poly.c
Expand Up @@ -63,11 +63,12 @@ int ntru_rand_tern(int N, int num_ones, int num_neg_ones, NtruTernPoly *poly, in
return 1;
}

void ntru_rand_prod(int N, int df1, int df2, int df3_ones, int df3_neg_ones, NtruProdPoly *poly, int (*rng)(unsigned[], int, NtruRandContext*), NtruRandContext *rand_ctx) {
int ntru_rand_prod(int N, int df1, int df2, int df3_ones, int df3_neg_ones, NtruProdPoly *poly, int (*rng)(unsigned[], int, NtruRandContext*), NtruRandContext *rand_ctx) {
poly->N = N;
ntru_rand_tern(N, df1, df1, &poly->f1, rng, rand_ctx);
ntru_rand_tern(N, df2, df2, &poly->f2, rng, rand_ctx);
ntru_rand_tern(N, df3_ones, df3_neg_ones, &poly->f3, rng, rand_ctx);
int result = ntru_rand_tern(N, df1, df1, &poly->f1, rng, rand_ctx);
result &= ntru_rand_tern(N, df2, df2, &poly->f2, rng, rand_ctx);
result &= ntru_rand_tern(N, df3_ones, df3_neg_ones, &poly->f3, rng, rand_ctx);
return result;
}

void ntru_add_tern(NtruIntPoly *a, NtruTernPoly *b) {
Expand Down
5 changes: 3 additions & 2 deletions src/poly.h
Expand Up @@ -31,9 +31,10 @@ int ntru_rand_tern(int N, int num_ones, int num_neg_ones, NtruTernPoly *poly, in
* @param df3_neg_ones number of negative ones in the third ternary polynomial
* @param poly output parameter; a pointer to store the new polynomial
* @param rng a pointer to a function that takes an array and an array size, and fills the array
with random data. See the ntru_rand_* functions.
* with random data. See the ntru_rand_* functions.
* @return 1 for success, 0 for failure
*/
void ntru_rand_prod(int N, int df1, int df2, int df3_ones, int df3_neg_ones, NtruProdPoly *poly, int (*rng)(unsigned[], int, NtruRandContext*), NtruRandContext *rand_ctx);
int ntru_rand_prod(int N, int df1, int df2, int df3_ones, int df3_neg_ones, NtruProdPoly *poly, int (*rng)(unsigned[], int, NtruRandContext*), NtruRandContext *rand_ctx);

/**
* @brief Ternary to general integer polynomial
Expand Down
4 changes: 2 additions & 2 deletions tests/test_poly.c
Expand Up @@ -62,9 +62,9 @@ int test_mult_prod() {
int i;
for (i=0; i<10; i++) {
NtruProdPoly a;
ntru_rand_prod(853, 8, 8, 8, 9, &a, ntru_rand_default, NULL);
valid &= ntru_rand_prod(853, 8, 8, 8, 9, &a, ntru_rand_default, NULL);
NtruIntPoly b;
valid = rand_int(853, 11, &b, ntru_rand_default, NULL);
valid &= rand_int(853, 11, &b, ntru_rand_default, NULL);
NtruIntPoly c_prod;
ntru_mult_prod(&b, &a, &c_prod);
NtruIntPoly a_int;
Expand Down

0 comments on commit fec302c

Please sign in to comment.