Skip to content

Commit

Permalink
Fix generation of polynomial g
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuktu committed Dec 31, 2015
1 parent 880ef38 commit 5e3ed62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
15 changes: 1 addition & 14 deletions src/ntru.c
Expand Up @@ -16,24 +16,11 @@ const int8_t NTRU_COEFF2_TABLE[] = {0, 1, -1, 0, 1, -1, 0, 1};
/* Generates a random g. If NTRU_CHECK_INVERTIBILITY_G, g will be invertible mod q */
uint8_t ntru_gen_g(const NtruEncParams *params, NtruPrivPoly *g, NtruRandContext *rand_ctx) {
uint16_t N = params->N;
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
uint16_t df1 = params->df1;
uint16_t df2 = params->df2;
uint16_t df3 = params->df3;
#endif /* NTRU_AVOID_HAMMING_WT_PATENT */
uint16_t dg = N / 3;
uint16_t dg = params->dg;
for (;;) {
#ifndef NTRU_AVOID_HAMMING_WT_PATENT
if (params->prod_flag && !ntru_rand_prod(N, df1, df2, df3, df3, &g->poly.prod, rand_ctx))
return NTRU_ERR_PRNG;
if (!params->prod_flag && !ntru_rand_tern(N, dg, dg, &g->poly.tern, rand_ctx))
return NTRU_ERR_PRNG;
g->prod_flag = params->prod_flag;
#else
if (!ntru_rand_tern(N, dg, dg, &g->poly.tern, rand_ctx))
return NTRU_ERR_PRNG;
g->prod_flag = 0;
#endif /* NTRU_AVOID_HAMMING_WT_PATENT */

if (!NTRU_CHECK_INVERTIBILITY_G)
break;
Expand Down
24 changes: 12 additions & 12 deletions tests/test_ntru.c
Expand Up @@ -258,18 +258,18 @@ uint8_t test_encr_decr() {
0x2e, 0x1a, 0x64, 0x8c, 0x68, 0x02, 0xb3, 0xaf, 0x18, 0xa7},
{0xf3, 0x16, 0xdf, 0x16, 0xe9, 0xa3, 0x4c, 0x40, 0x30, 0xff, /* EES1499EP1 */
0x5d, 0x66, 0xd8, 0x53, 0x2b, 0x07, 0x8a, 0x17, 0x48, 0xb4},
{0x7f, 0xbb, 0x91, 0x11, 0xea, 0x2e, 0x59, 0x5d, 0x25, 0x42, /* EES401EP2 */
0xea, 0x07, 0x88, 0x05, 0x1a, 0xab, 0x37, 0xda, 0x9b, 0x33},
{0x55, 0x76, 0x32, 0x9f, 0x18, 0x0a, 0xbb, 0x14, 0x63, 0xd9, /* EES439EP1 */
0x23, 0xc3, 0x5d, 0xd0, 0x8e, 0x17, 0xa5, 0xa2, 0x61, 0x75},
{0x5e, 0xe5, 0x6c, 0x47, 0xde, 0x6e, 0x2f, 0x9c, 0x5f, 0x47, /* EES443EP1 */
0x7, 0x46, 0xe9, 0x1f, 0x90, 0xb3, 0x47, 0x7d, 0x40, 0x38},
{0xb9, 0x40, 0x15, 0xbe, 0x43, 0xb9, 0x0f, 0xb3, 0x27, 0xfe, /* EES593EP1 */
0x41, 0x8a, 0x44, 0x76, 0x9e, 0xfe, 0xbe, 0xe5, 0x82, 0xae},
{0xbd, 0xa1, 0xf5, 0x07, 0x48, 0x9e, 0x49, 0x94, 0xca, 0x18, /* EES587EP1 */
0x3e, 0x4d, 0xa5, 0xf7, 0xee, 0x8a, 0x23, 0x67, 0x62, 0xf9},
{0xbd, 0xd9, 0x2a, 0x79, 0x89, 0x07, 0x9f, 0x64, 0xf9, 0x35, /* EES743EP1 */
0xb6, 0x90, 0x4f, 0xd3, 0xfa, 0x70, 0xc9, 0xf9, 0x30, 0xb0}
{0xb0, 0x50, 0x79, 0xd8, 0x43, 0x8b, 0xaf, 0x42, 0x74, 0x21, /* EES401EP2 */
0x45, 0x7b, 0x7a, 0xc6, 0x35, 0x0a, 0x85, 0xfa, 0x92, 0xdf},
{0x54, 0x5b, 0x8e, 0x77, 0x5d, 0x75, 0x33, 0x9d, 0xc4, 0x41, /* EES439EP1 */
0x47, 0xa7, 0x1a, 0x1e, 0x77, 0x20, 0xa0, 0x22, 0xeb, 0xc6},
{0x91, 0xe8, 0x84, 0xd7, 0xc0, 0xec, 0xc2, 0x65, 0x94, 0x80, /* EES443EP1 */
0x4b, 0xf3, 0x1b, 0x51, 0x68, 0x8d, 0xb4, 0x0f, 0xc2, 0x69},
{0xc3, 0x24, 0xc2, 0xe8, 0x31, 0xdb, 0xdf, 0x43, 0xdf, 0x0f, /* EES593EP1 */
0xc7, 0x45, 0x58, 0x23, 0x8a, 0x25, 0x3e, 0x0e, 0xce, 0xce},
{0x03, 0x64, 0x39, 0xf6, 0x63, 0xd7, 0xd6, 0x4c, 0x4c, 0xe0, /* EES587EP1 */
0x35, 0x4a, 0xcb, 0x45, 0xf4, 0xfd, 0x86, 0xd2, 0xa2, 0xe0},
{0x8d, 0xc2, 0x6a, 0x90, 0x0b, 0x6c, 0x32, 0xe0, 0x9d, 0x02, /* EES743EP1 */
0x30, 0xaf, 0xe4, 0xe7, 0x2a, 0x74, 0xaa, 0xf8, 0x3d, 0xb1}
};

for (i=0; i<sizeof(param_arr)/sizeof(param_arr[0]); i++) {
Expand Down

0 comments on commit 5e3ed62

Please sign in to comment.