diff --git a/.gitignore b/.gitignore index 7ba3eda..6884745 100644 --- a/.gitignore +++ b/.gitignore @@ -25,7 +25,7 @@ massif.* # QtCreator files *.c.* *.h.* -*.pro.user +*.pro.user* *.pri *.config *.creator @@ -61,3 +61,5 @@ build dist tmp +# indent files +*~ diff --git a/lib/src/bitpunch/asn1/asn1.c b/lib/src/bitpunch/asn1/asn1.c index d248e8c..4a44e8e 100644 --- a/lib/src/bitpunch/asn1/asn1.c +++ b/lib/src/bitpunch/asn1/asn1.c @@ -24,7 +24,7 @@ #include #include -int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) { +int BPU_asn1EncodePriKey (char **buffer, int *size, const BPU_T_Mecs_Ctx * ctx) { int rc = 0; asn1_node definitions = NULL; asn1_node asn1_element = NULL; @@ -32,107 +32,135 @@ int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) { BPU_T_GF2_16x *h_mat = NULL; uint32_t h_mat_bs; int i, j; + *size = 0; // TODO: structure cleanup on error - rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc); + rc = asn1_parser2tree (BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("ErrorDescription = %s\n\n", error_desc); + BPU_printError ("ErrorDescription = %s\n\n", error_desc); return rc; } - rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, &asn1_element); + rc = asn1_create_element (definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, + &asn1_element); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_create_element(): %d", rc); + BPU_printError ("asn1_create_element(): %d", rc); return rc; } // write elements rc = -1; if (ctx->type == BPU_EN_MECS_BASIC_GOPPA) { - rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)); + rc = asn1_write_value (asn1_element, "oid", + BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, + strlen (BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)); } #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA if (ctx->type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { - rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)); + rc = asn1_write_value (asn1_element, "oid", + BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, + strlen + (BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)); } #endif if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): oid %d", rc); + BPU_printError ("asn1_write_value(): oid %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "m", (void *)&(ctx->code_ctx->math_ctx->mod_deg), sizeof((ctx->code_ctx->math_ctx->mod_deg))); + rc = asn1_write_value (asn1_element, "m", + (void *) &(ctx->code_ctx->math_ctx->mod_deg), + sizeof ((ctx->code_ctx->math_ctx->mod_deg))); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): m %d", rc); + BPU_printError ("asn1_write_value(): m %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "t", (void *)&(ctx->code_ctx->t), sizeof((ctx->code_ctx->t))); + rc = asn1_write_value (asn1_element, "t", (void *) &(ctx->code_ctx->t), + sizeof ((ctx->code_ctx->t))); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): t %d", rc); + BPU_printError ("asn1_write_value(): t %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "mod", (void *)&(ctx->code_ctx->math_ctx->mod), sizeof(ctx->code_ctx->math_ctx->mod)); + rc = asn1_write_value (asn1_element, "mod", + (void *) &(ctx->code_ctx->math_ctx->mod), + sizeof (ctx->code_ctx->math_ctx->mod)); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): mod %d", rc); + BPU_printError ("asn1_write_value(): mod %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "g", (void *)(ctx->code_ctx->code_spec->goppa->g->coef), sizeof(ctx->code_ctx->code_spec->goppa->g->coef[0]) * (ctx->code_ctx->code_spec->goppa->g->deg + 1)); + rc = asn1_write_value (asn1_element, "g", + (void *) (ctx->code_ctx->code_spec->goppa->g->coef), + sizeof (ctx->code_ctx->code_spec->goppa->g-> + coef[0]) * + (ctx->code_ctx->code_spec->goppa->g->deg + 1)); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): g %d", rc); + BPU_printError ("asn1_write_value(): g %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "p", (void *)(ctx->code_ctx->code_spec->goppa->permutation->elements), sizeof(ctx->code_ctx->code_spec->goppa->permutation->elements[0]) * ctx->code_ctx->code_spec->goppa->permutation->size); + rc = asn1_write_value (asn1_element, "p", + (void *) (ctx->code_ctx->code_spec->goppa-> + permutation->elements), + sizeof (ctx->code_ctx->code_spec->goppa-> + permutation->elements[0]) * + ctx->code_ctx->code_spec->goppa->permutation->size); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): p %d", rc); + BPU_printError ("asn1_write_value(): p %d", rc); return rc; } if (ctx->code_ctx->type == BPU_EN_CODE_GOPPA) { if (!ctx->code_ctx->code_spec->goppa->h_mat) { - BPU_printError("not supported w/o H"); + BPU_printError ("not supported w/o H"); return -1; } // prepare H matrix - h_mat_bs = sizeof(BPU_T_GF2_16x) * (ctx->code_ctx->code_spec->goppa->h_mat->k * ctx->code_ctx->code_spec->goppa->h_mat->n); - h_mat = (BPU_T_GF2_16x *) malloc(h_mat_bs); + h_mat_bs = + sizeof (BPU_T_GF2_16x) * + (ctx->code_ctx->code_spec->goppa->h_mat->k * + ctx->code_ctx->code_spec->goppa->h_mat->n); + h_mat = (BPU_T_GF2_16x *) malloc (h_mat_bs); if (!h_mat) { - BPU_printError("Can not allocate memory for H matrix export"); + BPU_printError ("Can not allocate memory for H matrix export"); return -1; } j = 0; for (i = 0; i < ctx->code_ctx->code_spec->goppa->h_mat->k; i++) { - memcpy(h_mat + j, ctx->code_ctx->code_spec->goppa->h_mat->elements[i], ctx->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x)); + memcpy (h_mat + j, + ctx->code_ctx->code_spec->goppa->h_mat->elements[i], + ctx->code_ctx->code_spec->goppa->h_mat->n * + sizeof (BPU_T_GF2_16x)); j += ctx->code_ctx->code_spec->goppa->h_mat->n; } } else { - BPU_printError("Not supported for this CODE type."); + BPU_printError ("Not supported for this CODE type."); return -1; } - rc = asn1_write_value(asn1_element, "h_mat", (void *)(h_mat), h_mat_bs); - free(h_mat); + rc = asn1_write_value (asn1_element, "h_mat", (void *) (h_mat), h_mat_bs); + free (h_mat); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): h_mat %d", rc); + BPU_printError ("asn1_write_value(): h_mat %d", rc); return rc; } /* Clear the definition structures */ - asn1_delete_structure(&definitions); + asn1_delete_structure (&definitions); // get size of data - asn1_der_coding(asn1_element, "", NULL, size, NULL); - *buffer = (char *) malloc(*size); + asn1_der_coding (asn1_element, "", NULL, size, NULL); + *buffer = (char *) malloc (*size); - rc = asn1_der_coding(asn1_element, "", *buffer, size, NULL); + rc = asn1_der_coding (asn1_element, "", *buffer, size, NULL); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_der_coding(): %d", rc); + BPU_printError ("asn1_der_coding(): %d", rc); return rc; } /* Clear the definition structures */ - asn1_delete_structure(&asn1_element); + asn1_delete_structure (&asn1_element); return rc; } -int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size) { +int BPU_asn1DecodePriKey (BPU_T_Mecs_Ctx ** ctx, const char *buffer, + const int size) { int rc = 0; asn1_node definitions = NULL; asn1_node asn1_element = NULL; @@ -140,102 +168,123 @@ int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int siz char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; char *tmp_buf; BPU_T_EN_Mecs_Types type; - BPU_T_UN_Mecs_Params params; + BPU_T_UN_Mecs_Params params; uint8_t m, t; int i, j; // TODO:structure cleanup on error - rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc); + rc = asn1_parser2tree (BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("ErrorDescription = %s", error_desc); + BPU_printError ("ErrorDescription = %s", error_desc); return rc; } - rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, &asn1_element); + rc = asn1_create_element (definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, + &asn1_element); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_create_element(): %d", rc); + BPU_printError ("asn1_create_element(): %d", rc); return rc; } /* Clear the definition structures */ - asn1_delete_structure(&definitions); + asn1_delete_structure (&definitions); // decode buffer - rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc); + rc = asn1_der_decoding (&asn1_element, buffer, size, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("Decoding error %d %s", rc, error_desc); + BPU_printError ("Decoding error %d %s", rc, error_desc); return rc; } // section of data decoding - type = BPU_asn1GetMecsTypeFromOid(asn1_element); + type = BPU_asn1GetMecsTypeFromOid (asn1_element); if (type == -1) { - BPU_printError("Wrong OID"); + BPU_printError ("Wrong OID"); return -1; } - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "m", asn1_element)) { + if (BPU_asn1ReadValue (&tmp_buf, &tmp_len, 1, "m", asn1_element)) { return -1; } m = *(uint8_t *) tmp_buf; - free(tmp_buf); + free (tmp_buf); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "t", asn1_element)) { + if (BPU_asn1ReadValue (&tmp_buf, &tmp_len, 1, "t", asn1_element)) { return -1; } t = *(uint8_t *) tmp_buf; - free(tmp_buf); + free (tmp_buf); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x), "mod", asn1_element)) { + if (BPU_asn1ReadValue + (&tmp_buf, &tmp_len, sizeof (BPU_T_GF2_16x), "mod", asn1_element)) { return -1; } - if (type == BPU_EN_MECS_BASIC_GOPPA || type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { - if (BPU_mecsInitParamsGoppa(¶ms, m, t, *((BPU_T_GF2_16x *) tmp_buf))) { - return -1; - } - free(tmp_buf); - if (BPU_mecsInitCtx(ctx, ¶ms, type)) { - return -1; - } - BPU_mecsFreeParamsGoppa(¶ms); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x) * (t + 1), "g", asn1_element)) { + if (type == BPU_EN_MECS_BASIC_GOPPA + || type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { + if (BPU_mecsInitParamsGoppa + (¶ms, m, t, *((BPU_T_GF2_16x *) tmp_buf))) { + return -1; + } + free (tmp_buf); + if (BPU_mecsInitCtx (ctx, ¶ms, type)) { + return -1; + } + BPU_mecsFreeParamsGoppa (¶ms); + if (BPU_asn1ReadValue + (&tmp_buf, &tmp_len, sizeof (BPU_T_GF2_16x) * (t + 1), "g", + asn1_element)) { return -1; } - if (BPU_gf2xPolyMalloc(&((*ctx)->code_ctx->code_spec->goppa->g), t)) { + if (BPU_gf2xPolyMalloc (&((*ctx)->code_ctx->code_spec->goppa->g), t)) { return -1; } - memcpy((*ctx)->code_ctx->code_spec->goppa->g->coef, tmp_buf, tmp_len); + memcpy ((*ctx)->code_ctx->code_spec->goppa->g->coef, tmp_buf, tmp_len); (*ctx)->code_ctx->code_spec->goppa->g->deg = t; - free(tmp_buf); + free (tmp_buf); } else { - BPU_printError("Not supported for this code type."); + BPU_printError ("Not supported for this code type."); return -1; } - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_Perm_Element) * (*ctx)->code_ctx->code_len, "p", asn1_element)) { + if (BPU_asn1ReadValue + (&tmp_buf, &tmp_len, + sizeof (BPU_T_Perm_Element) * (*ctx)->code_ctx->code_len, "p", + asn1_element)) { return -1; } - if (BPU_permMalloc(&((*ctx)->code_ctx->code_spec->goppa->permutation), (*ctx)->code_ctx->code_len)) { + if (BPU_permMalloc + (&((*ctx)->code_ctx->code_spec->goppa->permutation), + (*ctx)->code_ctx->code_len)) { return -1; } - memcpy((*ctx)->code_ctx->code_spec->goppa->permutation->elements, tmp_buf, tmp_len); - free(tmp_buf); + memcpy ((*ctx)->code_ctx->code_spec->goppa->permutation->elements, tmp_buf, + tmp_len); + free (tmp_buf); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x) * (*ctx)->code_ctx->t * (*ctx)->code_ctx->code_len, "h_mat", asn1_element)) { + if (BPU_asn1ReadValue + (&tmp_buf, &tmp_len, + sizeof (BPU_T_GF2_16x) * (*ctx)->code_ctx->t * + (*ctx)->code_ctx->code_len, "h_mat", asn1_element)) { return -1; } - if (BPU_gf2xMatMalloc(&((*ctx)->code_ctx->code_spec->goppa->h_mat), (*ctx)->code_ctx->t, (*ctx)->code_ctx->code_len)) { + if (BPU_gf2xMatMalloc + (&((*ctx)->code_ctx->code_spec->goppa->h_mat), (*ctx)->code_ctx->t, + (*ctx)->code_ctx->code_len)) { return -1; } j = 0; for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->h_mat->k; i++) { - memcpy((*ctx)->code_ctx->code_spec->goppa->h_mat->elements[i], tmp_buf + j, (*ctx)->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x)); - j += (*ctx)->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x); + memcpy ((*ctx)->code_ctx->code_spec->goppa->h_mat->elements[i], + tmp_buf + j, + (*ctx)->code_ctx->code_spec->goppa->h_mat->n * + sizeof (BPU_T_GF2_16x)); + j += (*ctx)->code_ctx->code_spec->goppa->h_mat->n * + sizeof (BPU_T_GF2_16x); } - free(tmp_buf); + free (tmp_buf); - asn1_delete_structure(&asn1_element); + asn1_delete_structure (&asn1_element); return rc; } -int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) { +int BPU_asn1EncodePubKey (char **buffer, int *size, const BPU_T_Mecs_Ctx * ctx) { int rc = 0; asn1_node definitions = NULL; asn1_node asn1_element = NULL; @@ -243,87 +292,104 @@ int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) { BPU_T_GF2 *g_mat = NULL; uint32_t g_mat_bs; int i, j; + *size = 0; // TODO:structure cleanup on error - rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc); + rc = asn1_parser2tree (BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("ErrorDescription = %s", error_desc); + BPU_printError ("ErrorDescription = %s", error_desc); return rc; } - rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, &asn1_element); + rc = asn1_create_element (definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, + &asn1_element); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_create_element(): %d", rc); + BPU_printError ("asn1_create_element(): %d", rc); return rc; } // write elements rc = -1; if (ctx->type == BPU_EN_MECS_BASIC_GOPPA) { - rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)); + rc = asn1_write_value (asn1_element, "oid", + BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, + strlen (BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)); } #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA if (ctx->type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { - rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)); + rc = asn1_write_value (asn1_element, "oid", + BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, + strlen + (BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)); } #endif if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): oid %d", rc); + BPU_printError ("asn1_write_value(): oid %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "m", (void *)&(ctx->code_ctx->math_ctx->mod_deg), sizeof((ctx->code_ctx->math_ctx->mod_deg))); + rc = asn1_write_value (asn1_element, "m", + (void *) &(ctx->code_ctx->math_ctx->mod_deg), + sizeof ((ctx->code_ctx->math_ctx->mod_deg))); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): m %d", rc); + BPU_printError ("asn1_write_value(): m %d", rc); return rc; } - rc = asn1_write_value(asn1_element, "t", (void *)&(ctx->code_ctx->t), sizeof((ctx->code_ctx->t))); + rc = asn1_write_value (asn1_element, "t", (void *) &(ctx->code_ctx->t), + sizeof ((ctx->code_ctx->t))); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): t %d", rc); + BPU_printError ("asn1_write_value(): t %d", rc); return rc; } if (ctx->code_ctx->type == BPU_EN_CODE_GOPPA) { - g_mat_bs = sizeof(BPU_T_GF2) * (ctx->code_ctx->code_spec->goppa->g_mat->k * ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row); + g_mat_bs = + sizeof (BPU_T_GF2) * (ctx->code_ctx->code_spec->goppa->g_mat->k * + ctx->code_ctx->code_spec->goppa->g_mat-> + elements_in_row); // prepare G matrix - g_mat = (BPU_T_GF2 *) malloc(g_mat_bs); + g_mat = (BPU_T_GF2 *) malloc (g_mat_bs); if (!g_mat) { - BPU_printError("Can not allocate memory for G matrix export"); + BPU_printError ("Can not allocate memory for G matrix export"); return -1; } j = 0; for (i = 0; i < ctx->code_ctx->code_spec->goppa->g_mat->k; i++) { - memcpy(g_mat + j, ctx->code_ctx->code_spec->goppa->g_mat->elements[i], ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row * sizeof(BPU_T_GF2)); + memcpy (g_mat + j, + ctx->code_ctx->code_spec->goppa->g_mat->elements[i], + ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row * + sizeof (BPU_T_GF2)); j += ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row; } } else { - BPU_printError("Not supported for this MECS type."); + BPU_printError ("Not supported for this MECS type."); return -1; } - rc = asn1_write_value(asn1_element, "g_mat", (void *)(g_mat), g_mat_bs); - free(g_mat); + rc = asn1_write_value (asn1_element, "g_mat", (void *) (g_mat), g_mat_bs); + free (g_mat); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_write_value(): g_mat %d", rc); + BPU_printError ("asn1_write_value(): g_mat %d", rc); return rc; } /* Clear the definition structures */ - asn1_delete_structure(&definitions); + asn1_delete_structure (&definitions); // get size of data - asn1_der_coding(asn1_element, "", NULL, size, NULL); - *buffer = (char *) malloc(*size); + asn1_der_coding (asn1_element, "", NULL, size, NULL); + *buffer = (char *) malloc (*size); - rc = asn1_der_coding(asn1_element, "", *buffer, size, NULL); + rc = asn1_der_coding (asn1_element, "", *buffer, size, NULL); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_der_coding(): %d", rc); + BPU_printError ("asn1_der_coding(): %d", rc); } /* Clear the definition structures */ - asn1_delete_structure(&asn1_element); + asn1_delete_structure (&asn1_element); return rc; } -int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size) { +int BPU_asn1DecodePubKey (BPU_T_Mecs_Ctx ** ctx, const char *buffer, + const int size) { int rc = 0; asn1_node definitions = NULL; asn1_node asn1_element = NULL; @@ -331,298 +397,321 @@ int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int siz char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE]; char *tmp_buf = NULL; BPU_T_EN_Mecs_Types type; - BPU_T_UN_Mecs_Params params; + BPU_T_UN_Mecs_Params params; uint8_t m, t; int i, j, tmp; // TODO:structure cleanup on error - rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc); + rc = asn1_parser2tree (BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("ErrorDescription = %s", error_desc); + BPU_printError ("ErrorDescription = %s", error_desc); return rc; } - rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, &asn1_element); + rc = asn1_create_element (definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, + &asn1_element); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_create_element(): %d", rc); + BPU_printError ("asn1_create_element(): %d", rc); return rc; } /* Clear the definition structures */ - asn1_delete_structure(&definitions); + asn1_delete_structure (&definitions); // decode buffer - rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc); + rc = asn1_der_decoding (&asn1_element, buffer, size, error_desc); if (rc != ASN1_SUCCESS) { - BPU_printError("Decoding error %d, %s", rc, error_desc); + BPU_printError ("Decoding error %d, %s", rc, error_desc); return rc; } // section of data decoding - type = BPU_asn1GetMecsTypeFromOid(asn1_element); + type = BPU_asn1GetMecsTypeFromOid (asn1_element); if (type == -1) { - BPU_printError("Wrong OID"); + BPU_printError ("Wrong OID"); return -1; } - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "m", asn1_element)) { + if (BPU_asn1ReadValue (&tmp_buf, &tmp_len, 1, "m", asn1_element)) { return -1; } m = *(uint8_t *) tmp_buf; - free(tmp_buf); + free (tmp_buf); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "t", asn1_element)) { + if (BPU_asn1ReadValue (&tmp_buf, &tmp_len, 1, "t", asn1_element)) { return -1; } t = *(uint8_t *) tmp_buf; - free(tmp_buf); + free (tmp_buf); // init mecs context without mod - if (type == BPU_EN_MECS_BASIC_GOPPA || type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { - if (BPU_mecsInitParamsGoppa(¶ms, m, t, -1)) { - return -1; - } - } - else { - BPU_printError("Type not supported"); - return -1; - } - if ((rc = BPU_mecsInitCtx(ctx, ¶ms, type))) { - BPU_printError("can not initialize mecs ctx"); + if (type == BPU_EN_MECS_BASIC_GOPPA + || type == BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA) { + if (BPU_mecsInitParamsGoppa (¶ms, m, t, -1)) { + return -1; + } + } + else { + BPU_printError ("Type not supported"); + return -1; + } + if ((rc = BPU_mecsInitCtx (ctx, ¶ms, type))) { + BPU_printError ("can not initialize mecs ctx"); return rc; } - BPU_mecsFreeParamsGoppa(¶ms); + BPU_mecsFreeParamsGoppa (¶ms); - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, (*ctx)->code_ctx->code_len * (*ctx)->code_ctx->msg_len, "g_mat", asn1_element)) { + if (BPU_asn1ReadValue + (&tmp_buf, &tmp_len, + (*ctx)->code_ctx->code_len * (*ctx)->code_ctx->msg_len, "g_mat", + asn1_element)) { return -1; } - if (BPU_gf2MatMalloc(&((*ctx)->code_ctx->code_spec->goppa->g_mat), (*ctx)->code_ctx->code_len - (*ctx)->code_ctx->msg_len, (*ctx)->code_ctx->msg_len)) { + if (BPU_gf2MatMalloc + (&((*ctx)->code_ctx->code_spec->goppa->g_mat), + (*ctx)->code_ctx->code_len - (*ctx)->code_ctx->msg_len, + (*ctx)->code_ctx->msg_len)) { return -1; } j = 0; - tmp = (*ctx)->code_ctx->code_spec->goppa->g_mat->elements_in_row * ((*ctx)->code_ctx->code_spec->goppa->g_mat->element_bit_size / 8); + tmp = + (*ctx)->code_ctx->code_spec->goppa->g_mat->elements_in_row * + ((*ctx)->code_ctx->code_spec->goppa->g_mat->element_bit_size / 8); for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->g_mat->k; i++) { - memcpy((*ctx)->code_ctx->code_spec->goppa->g_mat->elements[i], tmp_buf + j, tmp); + memcpy ((*ctx)->code_ctx->code_spec->goppa->g_mat->elements[i], + tmp_buf + j, tmp); j += tmp; } - free(tmp_buf); + free (tmp_buf); - asn1_delete_structure(&asn1_element); + asn1_delete_structure (&asn1_element); return rc; } -int BPU_asn1ReadValue(char **buffer, int *len, const int max_len, const char *name, const asn1_node node) { +int BPU_asn1ReadValue (char **buffer, int *len, const int max_len, + const char *name, const asn1_node node) { int rc; // get size and malloc buffer - if ((rc = BPU_asn1MallocBuffer(buffer, len, max_len, node, name))) { - return rc; + if ((rc = BPU_asn1MallocBuffer (buffer, len, max_len, node, name))) { + return rc; } // decode data - rc = asn1_read_value(node, name, *buffer, len); - BPU_printDebug("data len: %d", *len); + rc = asn1_read_value (node, name, *buffer, len); + BPU_printDebug ("data len: %d", *len); if (rc != ASN1_SUCCESS) { - BPU_printError("asn1_read_value(): %s %d", name, rc); + BPU_printError ("asn1_read_value(): %s %d", name, rc); return -1; } - BPU_printDebug("data %s: %02x", name, **buffer); + BPU_printDebug ("data %s: %02x", name, **buffer); return rc; } -BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid(asn1_node node) { +BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid (asn1_node node) { int rc, tmp_len; char *tmp_buf = NULL; - if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 60, "oid", node)) { + if (BPU_asn1ReadValue (&tmp_buf, &tmp_len, 60, "oid", node)) { return -1; } tmp_buf[tmp_len - 1] = '\0'; - BPU_printDebug("data oid: %s", tmp_buf); + BPU_printDebug ("data oid: %s", tmp_buf); - if (!strcmp(tmp_buf, BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)) { + if (!strcmp (tmp_buf, BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)) { rc = BPU_EN_MECS_BASIC_GOPPA; } #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - else if (!strcmp(tmp_buf, BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)) { + else if (!strcmp (tmp_buf, BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)) { rc = BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA; } #endif else { rc = -1; } - free(tmp_buf); + free (tmp_buf); return rc; } -int BPU_asn1MallocBuffer(char **buffer, int *len, const int max_len, const asn1_node node, const char *name) { +int BPU_asn1MallocBuffer (char **buffer, int *len, const int max_len, + const asn1_node node, const char *name) { int rc; - rc = asn1_read_value(node, name, NULL, len); + rc = asn1_read_value (node, name, NULL, len); - if (*len > max_len) { - BPU_printError("requested length %d is bigger than max expected %d", *len, max_len); - return -1; - } - else if (*len > 0) { + if (*len > max_len) { + BPU_printError ("requested length %d is bigger than max expected %d", + *len, max_len); + return -1; + } + else if (*len > 0) { // malloc data - *buffer = (char *) malloc(*len); + *buffer = (char *) malloc (*len); if (!(*buffer)) { - BPU_printError("can not allocate memory."); + BPU_printError ("can not allocate memory."); return -1; } } else { - BPU_printError("can not get size of data"); + BPU_printError ("can not get size of data"); return rc; } return 0; } -int BPU_asn1LoadFileToBuffer(char **buffer, long *size, const char *file_name) { - FILE *fp = fopen(file_name, "rb"); +int BPU_asn1LoadFileToBuffer (char **buffer, long *size, const char *file_name) { + FILE *fp = fopen (file_name, "rb"); long tmp; if (!fp) { - BPU_printError("can not open private key file: %s", file_name); + BPU_printError ("can not open private key file: %s", file_name); return -1; } - fseek(fp, 0L, SEEK_END); - *size = ftell(fp); - fseek(fp, 0L, SEEK_SET); + fseek (fp, 0L, SEEK_END); + *size = ftell (fp); + fseek (fp, 0L, SEEK_SET); if (*size == -1L) { - BPU_printError("can get file size"); - fclose(fp); + BPU_printError ("can get file size"); + fclose (fp); return -1; } - *buffer = (char *) malloc(*size); + *buffer = (char *) malloc (*size); if (!*buffer) { - BPU_printError("can malloc buffer"); - fclose(fp); + BPU_printError ("can malloc buffer"); + fclose (fp); return -1; } - tmp = fread(*buffer, 1, *size, fp); + tmp = fread (*buffer, 1, *size, fp); if (tmp != *size) { - BPU_printError("can not read data from file, read only %ld B, instead of %ld B", tmp, *size); - free(*buffer); - fclose(fp); + BPU_printError + ("can not read data from file, read only %ld B, instead of %ld B", + tmp, *size); + free (*buffer); + fclose (fp); return -1; } - fclose(fp); + fclose (fp); return 0; } -int BPU_asn1LoadKeyPair(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file, const char *pub_key_file) { - if (BPU_asn1LoadPriKey(ctx, pri_key_file) || BPU_asn1LoadPubKey(ctx, pub_key_file)) { - BPU_printError("can not load key pair"); +int BPU_asn1LoadKeyPair (BPU_T_Mecs_Ctx ** ctx, const char *pri_key_file, + const char *pub_key_file) { + if (BPU_asn1LoadPriKey (ctx, pri_key_file) + || BPU_asn1LoadPubKey (ctx, pub_key_file)) { + BPU_printError ("can not load key pair"); return -1; } return 0; } -int BPU_asn1LoadPriKey(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file) { +int BPU_asn1LoadPriKey (BPU_T_Mecs_Ctx ** ctx, const char *pri_key_file) { long size; char *buffer; - if (BPU_asn1LoadFileToBuffer(&buffer, &size, pri_key_file)) { + if (BPU_asn1LoadFileToBuffer (&buffer, &size, pri_key_file)) { return -1; } - if (BPU_asn1DecodePriKey(ctx, buffer, size)) { - free(buffer); + if (BPU_asn1DecodePriKey (ctx, buffer, size)) { + free (buffer); return -1; } - free(buffer); + free (buffer); return 0; } -int BPU_asn1LoadPubKey(BPU_T_Mecs_Ctx **ctx, const char *pub_key_file) { +int BPU_asn1LoadPubKey (BPU_T_Mecs_Ctx ** ctx, const char *pub_key_file) { long size; char *buffer; - if (BPU_asn1LoadFileToBuffer(&buffer, &size, pub_key_file)) { + if (BPU_asn1LoadFileToBuffer (&buffer, &size, pub_key_file)) { return -1; } - if (BPU_asn1DecodePubKey(ctx, buffer, size)) { - free(buffer); + if (BPU_asn1DecodePubKey (ctx, buffer, size)) { + free (buffer); return -1; } - free(buffer); + free (buffer); return 0; } -int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file) { - if (BPU_asn1SavePriKey(ctx, pri_key_file) || BPU_asn1SavePubKey(ctx, pub_key_file)) { - BPU_printError("can not save key pair"); +int BPU_asn1SaveKeyPair (const BPU_T_Mecs_Ctx * ctx, const char *pri_key_file, + const char *pub_key_file) { + if (BPU_asn1SavePriKey (ctx, pri_key_file) + || BPU_asn1SavePubKey (ctx, pub_key_file)) { + BPU_printError ("can not save key pair"); return -1; } return 0; } -int BPU_asn1SavePriKey(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file) { +int BPU_asn1SavePriKey (const BPU_T_Mecs_Ctx * ctx, const char *pri_key_file) { char *buffer = NULL; int size; - if (BPU_asn1EncodePriKey(&buffer, &size, ctx)) { + if (BPU_asn1EncodePriKey (&buffer, &size, ctx)) { return -1; } - if (BPU_asn1WriteBufferToFile(buffer, size, pri_key_file)) { - BPU_printError("can not write pri key data"); - free(buffer); + if (BPU_asn1WriteBufferToFile (buffer, size, pri_key_file)) { + BPU_printError ("can not write pri key data"); + free (buffer); return -1; } - free(buffer); + free (buffer); return 0; } -int BPU_asn1SavePubKey(const BPU_T_Mecs_Ctx *ctx, const char *pub_key_file) { +int BPU_asn1SavePubKey (const BPU_T_Mecs_Ctx * ctx, const char *pub_key_file) { char *buffer = NULL; int size; - if (BPU_asn1EncodePubKey(&buffer, &size, ctx)) { + if (BPU_asn1EncodePubKey (&buffer, &size, ctx)) { return -1; } - if (BPU_asn1WriteBufferToFile(buffer, size, pub_key_file)) { - BPU_printError("can not write pub key data"); - free(buffer); + if (BPU_asn1WriteBufferToFile (buffer, size, pub_key_file)) { + BPU_printError ("can not write pub key data"); + free (buffer); return -1; } - free(buffer); + free (buffer); return 0; } -int BPU_asn1WriteBufferToFile(const char *buffer, const long size, const char *file_name) { +int BPU_asn1WriteBufferToFile (const char *buffer, const long size, + const char *file_name) { FILE *fp; long tmp; - fp = fopen(file_name, "wb"); + fp = fopen (file_name, "wb"); if (!fp) { - BPU_printError("can not open file %s", file_name); + BPU_printError ("can not open file %s", file_name); return -1; } - tmp = fwrite(buffer, 1, size, fp); + tmp = fwrite (buffer, 1, size, fp); if (tmp != size) { - BPU_printError("can not write buffer to file, written %ld B, instead of %ld B", tmp, size); - fclose(fp); + BPU_printError + ("can not write buffer to file, written %ld B, instead of %ld B", + tmp, size); + fclose (fp); return -1; } - fclose(fp); + fclose (fp); return 0; } diff --git a/lib/src/bitpunch/asn1/asn1.h b/lib/src/bitpunch/asn1/asn1.h index bc88014..b9567eb 100644 --- a/lib/src/bitpunch/asn1/asn1.h +++ b/lib/src/bitpunch/asn1/asn1.h @@ -44,35 +44,42 @@ * @return 0 */ /// sasa -int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx); +int BPU_asn1EncodePriKey (char **buffer, int *size, const BPU_T_Mecs_Ctx * ctx); -int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size); +int BPU_asn1DecodePriKey (BPU_T_Mecs_Ctx ** ctx, const char *buffer, + const int size); -int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx); +int BPU_asn1EncodePubKey (char **buffer, int *size, const BPU_T_Mecs_Ctx * ctx); -int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size); +int BPU_asn1DecodePubKey (BPU_T_Mecs_Ctx ** ctx, const char *buffer, + const int size); -int BPU_asn1ReadValue(char **buffer, int *len, const int max_len, const char *name, const asn1_node node); +int BPU_asn1ReadValue (char **buffer, int *len, const int max_len, + const char *name, const asn1_node node); -BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid(asn1_node node); +BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid (asn1_node node); -int BPU_asn1MallocBuffer(char **buffer, int *len, const int max_len, const asn1_node node, const char *name); +int BPU_asn1MallocBuffer (char **buffer, int *len, const int max_len, + const asn1_node node, const char *name); -int BPU_asn1LoadFileToBuffer(char **buffer, long *size, const char *file_name); +int BPU_asn1LoadFileToBuffer (char **buffer, long *size, const char *file_name); -int BPU_asn1LoadKeyPair(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file, const char *pub_key_file); +int BPU_asn1LoadKeyPair (BPU_T_Mecs_Ctx ** ctx, const char *pri_key_file, + const char *pub_key_file); -int BPU_asn1LoadPriKey(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file); +int BPU_asn1LoadPriKey (BPU_T_Mecs_Ctx ** ctx, const char *pri_key_file); -int BPU_asn1LoadPubKey(BPU_T_Mecs_Ctx **ctx, const char *pub_key_file); +int BPU_asn1LoadPubKey (BPU_T_Mecs_Ctx ** ctx, const char *pub_key_file); -int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file); +int BPU_asn1SaveKeyPair (const BPU_T_Mecs_Ctx * ctx, const char *pri_key_file, + const char *pub_key_file); -int BPU_asn1SavePriKey(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file); +int BPU_asn1SavePriKey (const BPU_T_Mecs_Ctx * ctx, const char *pri_key_file); -int BPU_asn1SavePubKey(const BPU_T_Mecs_Ctx *ctx, const char *pub_key_file); +int BPU_asn1SavePubKey (const BPU_T_Mecs_Ctx * ctx, const char *pub_key_file); -int BPU_asn1WriteBufferToFile(const char *buffer, const long size, const char *file_name); +int BPU_asn1WriteBufferToFile (const char *buffer, const long size, + const char *file_name); #endif // BPU_CONF_ASN1 #endif // ASN1_H diff --git a/lib/src/bitpunch/code/codectx.c b/lib/src/bitpunch/code/codectx.c index a805228..7cc6571 100644 --- a/lib/src/bitpunch/code/codectx.c +++ b/lib/src/bitpunch/code/codectx.c @@ -28,174 +28,189 @@ along with this program. If not, see . #include #include -int BPU_codeInitCtx(BPU_T_Code_Ctx **ctx, const BPU_T_UN_Code_Params *params, const BPU_T_EN_Code_Types type) { - int tmp; - BPU_T_Code_Ctx *ctx_p; - - *ctx = (BPU_T_Code_Ctx *) calloc(1, sizeof(BPU_T_Code_Ctx)); - if (!*ctx) { - BPU_printError("Can not malloc BPU_T_Code_Ctx"); - - return BPU_EC_MALLOC_ERROR; - } - ctx_p = *ctx; - ctx_p->type = type; - - ctx_p->code_spec = (BPU_T_UN_Code_Spec *) calloc(1, sizeof(BPU_T_UN_Code_Spec)); - if (!ctx_p->code_spec) { - BPU_printError("Can not malloc BPU_T_UN_Code_Spec"); - - return BPU_EC_MALLOC_ERROR; - } - switch (type) { - case BPU_EN_CODE_GOPPA: -#ifdef BPU_CONF_ENCRYPTION - ctx_p->_encode = BPU_goppaEncode; -#endif -#ifdef BPU_CONF_DECRYPTION - ctx_p->_decode = BPU_goppaDecode; -#endif - tmp = BPU_codeInitMathCtx(&ctx_p->math_ctx, params->goppa->m, params->goppa->t, params->goppa->mod); - if (tmp) { - BPU_printError("Code math context initialization ERROR."); - - return tmp; - } - ctx_p->code_spec->goppa = (BPU_T_Goppa_Spec *) calloc(1, sizeof(BPU_T_Goppa_Spec)); - if (!ctx_p->code_spec->goppa) { - BPU_printError("Can not malloc BPU_T_Goppa_Spec"); - - return BPU_EC_MALLOC_ERROR; - } - ctx_p->code_spec->goppa->support_len = (1 << params->goppa->m); // ctx->math_ctx->ord + 1; - ctx_p->code_len = ctx_p->code_spec->goppa->support_len; - ctx_p->msg_len = ctx_p->code_spec->goppa->support_len - params->goppa->m*params->goppa->t; // n - m*t - ctx_p->t = params->goppa->t; - - break; - case BPU_EN_CODE_QCMDPC: - ctx_p->code_spec->qcmdpc = (BPU_T_Qcmdpc_Spec *) calloc(1, sizeof(BPU_T_Qcmdpc_Spec)); +int BPU_codeInitCtx (BPU_T_Code_Ctx ** ctx, const BPU_T_UN_Code_Params * params, + const BPU_T_EN_Code_Types type) { + int tmp; + BPU_T_Code_Ctx *ctx_p; + + *ctx = (BPU_T_Code_Ctx *) calloc (1, sizeof (BPU_T_Code_Ctx)); + if (!*ctx) { + BPU_printError ("Can not malloc BPU_T_Code_Ctx"); + + return BPU_EC_MALLOC_ERROR; + } + ctx_p = *ctx; + ctx_p->type = type; + + ctx_p->code_spec = + (BPU_T_UN_Code_Spec *) calloc (1, sizeof (BPU_T_UN_Code_Spec)); + if (!ctx_p->code_spec) { + BPU_printError ("Can not malloc BPU_T_UN_Code_Spec"); + + return BPU_EC_MALLOC_ERROR; + } + switch (type) { + case BPU_EN_CODE_GOPPA: #ifdef BPU_CONF_ENCRYPTION - ctx_p->_encode = BPU_mecsQcmdpcEncode; - // ctx_p->code_spec->qcmdpc->G = (BPU_T_GF2_QC_Matrix *) malloc(sizeof(BPU_T_GF2_QC_Matrix)); + ctx_p->_encode = BPU_goppaEncode; #endif #ifdef BPU_CONF_DECRYPTION - ctx_p->_decode = BPU_mecsQcmdpcDecrypt; - // ctx_p->code_spec->qcmdpc->H = (BPU_T_GF2_Sparse_Qc_Matrix *) malloc(sizeof(BPU_T_GF2_Sparse_Qc_Matrix)); + ctx_p->_decode = BPU_goppaDecode; #endif - if (!ctx_p->code_spec->qcmdpc) { - BPU_printError("Can not malloc BPU_T_Goppa_Spec"); - - return BPU_EC_MALLOC_ERROR; - } - ctx_p->code_spec->qcmdpc->m = params->qcmdpc->m; - ctx_p->code_spec->qcmdpc->n0 = params->qcmdpc->n0; - ctx_p->code_spec->qcmdpc->w = params->qcmdpc->w; - ctx_p->code_len = params->qcmdpc->m * params->qcmdpc->n0; - ctx_p->msg_len = ctx_p->code_len - params->qcmdpc->m; - ctx_p->t = params->qcmdpc->t; - - break; - /* EXAMPLE please DO NOT REMOVE - case BPU_EN_CODE_*****: + tmp = + BPU_codeInitMathCtx (&ctx_p->math_ctx, params->goppa->m, + params->goppa->t, params->goppa->mod); + if (tmp) { + BPU_printError ("Code math context initialization ERROR."); + + return tmp; + } + ctx_p->code_spec->goppa = + (BPU_T_Goppa_Spec *) calloc (1, sizeof (BPU_T_Goppa_Spec)); + if (!ctx_p->code_spec->goppa) { + BPU_printError ("Can not malloc BPU_T_Goppa_Spec"); + + return BPU_EC_MALLOC_ERROR; + } + ctx_p->code_spec->goppa->support_len = (1 << params->goppa->m); // ctx->math_ctx->ord + 1; + ctx_p->code_len = ctx_p->code_spec->goppa->support_len; + ctx_p->msg_len = ctx_p->code_spec->goppa->support_len - params->goppa->m * params->goppa->t; // n - m*t + ctx_p->t = params->goppa->t; + + break; + case BPU_EN_CODE_QCMDPC: + ctx_p->code_spec->qcmdpc = + (BPU_T_Qcmdpc_Spec *) calloc (1, sizeof (BPU_T_Qcmdpc_Spec)); #ifdef BPU_CONF_ENCRYPTION - ctx_p->_encode = FUNC_FROM_YOUR_FILE; + ctx_p->_encode = BPU_mecsQcmdpcEncode; + // ctx_p->code_spec->qcmdpc->G = (BPU_T_GF2_QC_Matrix *) malloc(sizeof(BPU_T_GF2_QC_Matrix)); #endif #ifdef BPU_CONF_DECRYPTION - ctx_p->_decode = FUNC_FROM_YOUR_FILE; + ctx_p->_decode = BPU_mecsQcmdpcDecrypt; + // ctx_p->code_spec->qcmdpc->H = (BPU_T_GF2_Sparse_Qc_Matrix *) malloc(sizeof(BPU_T_GF2_Sparse_Qc_Matrix)); #endif - ctx->code_spec->YOURS = ALLOC OR NULL; - - ctx_p->code_len = LEN; - ctx_p->msg_len = LEN; - ctx_p->t = T; - break; - */ - default: - BPU_printError("Code type not supported: %d", type); - return BPU_EC_CODE_TYPE_NOT_SUPPORTED; - } - // error vector - if (BPU_gf2VecMalloc(&ctx_p->e, ctx_p->code_len)) { - BPU_printError("can not allocate error vector"); - return BPU_EC_MALLOC_ERROR; - } - return 0; + if (!ctx_p->code_spec->qcmdpc) { + BPU_printError ("Can not malloc BPU_T_Goppa_Spec"); + + return BPU_EC_MALLOC_ERROR; + } + ctx_p->code_spec->qcmdpc->m = params->qcmdpc->m; + ctx_p->code_spec->qcmdpc->n0 = params->qcmdpc->n0; + ctx_p->code_spec->qcmdpc->w = params->qcmdpc->w; + ctx_p->code_len = params->qcmdpc->m * params->qcmdpc->n0; + ctx_p->msg_len = ctx_p->code_len - params->qcmdpc->m; + ctx_p->t = params->qcmdpc->t; + + break; + /* EXAMPLE please DO NOT REMOVE + case BPU_EN_CODE_*****: + #ifdef BPU_CONF_ENCRYPTION + ctx_p->_encode = FUNC_FROM_YOUR_FILE; + #endif + #ifdef BPU_CONF_DECRYPTION + ctx_p->_decode = FUNC_FROM_YOUR_FILE; + #endif + ctx->code_spec->YOURS = ALLOC OR NULL; + + ctx_p->code_len = LEN; + ctx_p->msg_len = LEN; + ctx_p->t = T; + break; + */ + default: + BPU_printError ("Code type not supported: %d", type); + return BPU_EC_CODE_TYPE_NOT_SUPPORTED; + } + // error vector + if (BPU_gf2VecMalloc (&ctx_p->e, ctx_p->code_len)) { + BPU_printError ("can not allocate error vector"); + return BPU_EC_MALLOC_ERROR; + } + return 0; } -int BPU_codeInitMathCtx(BPU_T_Math_Ctx **ctx, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) { +int BPU_codeInitMathCtx (BPU_T_Math_Ctx ** ctx, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod) { int rc = 0; - if (mod == (BPU_T_GF2_16x) -1) { - *ctx = (BPU_T_Math_Ctx *) calloc(1, sizeof(BPU_T_Math_Ctx)); - if (!*ctx) { - BPU_printError("Can not malloc BPU_T_Math_Ctx"); + if (mod == (BPU_T_GF2_16x) - 1) { + *ctx = (BPU_T_Math_Ctx *) calloc (1, sizeof (BPU_T_Math_Ctx)); + if (!*ctx) { + BPU_printError ("Can not malloc BPU_T_Math_Ctx"); - return -1; - } - (*ctx)->mod_deg = m; + return -1; + } + (*ctx)->mod_deg = m; } else if (mod != 0) { - rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, mod); + rc = BPU_mathInitCtx (ctx, (BPU_T_GF2_16x) 2, mod); } else if (m == 5 && t == 5) { - rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_5); - } - else if (m == 6 && t == 6) { - rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_6); - } - else if (m == 6 && t == 7) { - rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_6); - } - else if (m == 11 && t == 50) { - rc = BPU_mathInitCtx(ctx, (BPU_T_GF2_16x)2, (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_11); - } - else { - BPU_printError("Code params not supported. Supported only (m,t): (5,5), (6,6), (6,7), (11,50)"); - - return BPU_EC_CODE_PARAMS_NOT_SUPPORTED; - } - return rc; + rc = BPU_mathInitCtx (ctx, (BPU_T_GF2_16x) 2, + (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_5); + } + else if (m == 6 && t == 6) { + rc = BPU_mathInitCtx (ctx, (BPU_T_GF2_16x) 2, + (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_6); + } + else if (m == 6 && t == 7) { + rc = BPU_mathInitCtx (ctx, (BPU_T_GF2_16x) 2, + (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_6); + } + else if (m == 11 && t == 50) { + rc = BPU_mathInitCtx (ctx, (BPU_T_GF2_16x) 2, + (BPU_T_GF2_16x) BPU_GF2_POLY_DEG_11); + } + else { + BPU_printError + ("Code params not supported. Supported only (m,t): (5,5), (6,6), (6,7), (11,50)"); + + return BPU_EC_CODE_PARAMS_NOT_SUPPORTED; + } + return rc; } -void BPU_codeFreeCtx(BPU_T_Code_Ctx **ctx) { - BPU_T_Code_Ctx *ctx_p = *ctx; - - if (!ctx_p) { - return; - } - switch (ctx_p->type) { - case BPU_EN_CODE_GOPPA: - BPU_goppaFreeSpec(ctx_p->code_spec->goppa); - free(ctx_p->code_spec->goppa); - break; - case BPU_EN_CODE_QCMDPC: - BPU_qcmdpcFreeSpec(ctx_p->code_spec->qcmdpc); - // free(ctx_p->code_spec->qcmdpc); - break; - default: - BPU_printError("Code type not supported: %d", ctx_p->type); - } - BPU_gf2VecFree(&ctx_p->e); - BPU_mathFreeCtx(&ctx_p->math_ctx); - - free(ctx_p->code_spec); - free(ctx_p); - *ctx = NULL; +void BPU_codeFreeCtx (BPU_T_Code_Ctx ** ctx) { + BPU_T_Code_Ctx *ctx_p = *ctx; + + if (!ctx_p) { + return; + } + switch (ctx_p->type) { + case BPU_EN_CODE_GOPPA: + BPU_goppaFreeSpec (ctx_p->code_spec->goppa); + free (ctx_p->code_spec->goppa); + break; + case BPU_EN_CODE_QCMDPC: + BPU_qcmdpcFreeSpec (ctx_p->code_spec->qcmdpc); + // free(ctx_p->code_spec->qcmdpc); + break; + default: + BPU_printError ("Code type not supported: %d", ctx_p->type); + } + BPU_gf2VecFree (&ctx_p->e); + BPU_mathFreeCtx (&ctx_p->math_ctx); + + free (ctx_p->code_spec); + free (ctx_p); + *ctx = NULL; } -int BPU_codeInitParamsGoppa(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) { - return BPU_goppaInitParams(¶ms->goppa, m, t, mod); +int BPU_codeInitParamsGoppa (BPU_T_UN_Code_Params * params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod) { + return BPU_goppaInitParams (¶ms->goppa, m, t, mod); } -void BPU_codeFreeParamsGoppa(BPU_T_UN_Code_Params *params) { - BPU_goppaFreeParams(¶ms->goppa); +void BPU_codeFreeParamsGoppa (BPU_T_UN_Code_Params * params) { + BPU_goppaFreeParams (¶ms->goppa); } -int BPU_codeInitParamsQcmdpc(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t) { - return BPU_qcmdpcInitParams(¶ms->qcmdpc, m, n0, w, t); +int BPU_codeInitParamsQcmdpc (BPU_T_UN_Code_Params * params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t) { + return BPU_qcmdpcInitParams (¶ms->qcmdpc, m, n0, w, t); } -void BPU_codeFreeParamsQcmdpc(BPU_T_UN_Code_Params *params) { - BPU_qcmdpcFreeParams(¶ms->qcmdpc); +void BPU_codeFreeParamsQcmdpc (BPU_T_UN_Code_Params * params) { + BPU_qcmdpcFreeParams (¶ms->qcmdpc); } diff --git a/lib/src/bitpunch/code/codectx.h b/lib/src/bitpunch/code/codectx.h index 2b74dd6..c732475 100644 --- a/lib/src/bitpunch/code/codectx.h +++ b/lib/src/bitpunch/code/codectx.h @@ -31,36 +31,36 @@ along with this program. If not, see . /// Possible types of codes typedef enum _BPU_T_EN_Code_Types { - BPU_EN_CODE_GOPPA, - BPU_EN_CODE_QCMDPC - // HERE you can add your code type -}BPU_T_EN_Code_Types; + BPU_EN_CODE_GOPPA, + BPU_EN_CODE_QCMDPC + // HERE you can add your code type +} BPU_T_EN_Code_Types; /// Code specifics union type -typedef union _BPU_T_UN_Code_Spec{ - BPU_T_Goppa_Spec *goppa; - BPU_T_Qcmdpc_Spec *qcmdpc; - // HERE you add your code spec structure -}BPU_T_UN_Code_Spec; - -typedef union _BPU_T_UN_Code_Params{ - BPU_T_Goppa_Params *goppa; - BPU_T_Qcmdpc_Params *qcmdpc; - // HERE you add your code spec structure -}BPU_T_UN_Code_Params; +typedef union _BPU_T_UN_Code_Spec { + BPU_T_Goppa_Spec *goppa; + BPU_T_Qcmdpc_Spec *qcmdpc; + // HERE you add your code spec structure +} BPU_T_UN_Code_Spec; + +typedef union _BPU_T_UN_Code_Params { + BPU_T_Goppa_Params *goppa; + BPU_T_Qcmdpc_Params *qcmdpc; + // HERE you add your code spec structure +} BPU_T_UN_Code_Params; typedef struct _BPU_T_Code_Ctx { - BPU_T_EN_Code_Types type; - int (* _encode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); ///< This is reference to encode function. It will be set in BPU_codeInitCtx - int (* _decode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); ///< This is reference to decode function. It will be set in BPU_codeInitCtx - BPU_T_Math_Ctx *math_ctx; ///< Math context - BPU_T_GF2_Vector *e; ///< Error vector - BPU_T_UN_Code_Spec *code_spec; ///< Code specific structure, like generator matrix, control matrix, gen. poly ... - - uint16_t code_len; ///< Code len - uint16_t msg_len; ///< Code dimenzion - uint8_t t; ///< Error code correction capability -}BPU_T_Code_Ctx; + BPU_T_EN_Code_Types type; + int (*_encode) (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, const struct _BPU_T_Code_Ctx * ctx); ///< This is reference to encode function. It will be set in BPU_codeInitCtx + int (*_decode) (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, const struct _BPU_T_Code_Ctx * ctx); ///< This is reference to decode function. It will be set in BPU_codeInitCtx + BPU_T_Math_Ctx *math_ctx; ///< Math context + BPU_T_GF2_Vector *e; ///< Error vector + BPU_T_UN_Code_Spec *code_spec; ///< Code specific structure, like generator matrix, control matrix, gen. poly ... + + uint16_t code_len; ///< Code len + uint16_t msg_len; ///< Code dimenzion + uint8_t t; ///< Error code correction capability +} BPU_T_Code_Ctx; /** * @brief BPU_codeInitCtx Initialize (register) code functions encode, decode and code spec structure based on code type. @@ -69,7 +69,8 @@ typedef struct _BPU_T_Code_Ctx { * @param type * @return */ -int BPU_codeInitCtx(BPU_T_Code_Ctx **ctx, const BPU_T_UN_Code_Params *params, const BPU_T_EN_Code_Types type); +int BPU_codeInitCtx (BPU_T_Code_Ctx ** ctx, const BPU_T_UN_Code_Params * params, + const BPU_T_EN_Code_Types type); /** * @brief BPU_codeInitMathCtx @@ -79,13 +80,14 @@ int BPU_codeInitCtx(BPU_T_Code_Ctx **ctx, const BPU_T_UN_Code_Params *params, co * @param mod * @return */ -int BPU_codeInitMathCtx(BPU_T_Math_Ctx **ctx, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod); +int BPU_codeInitMathCtx (BPU_T_Math_Ctx ** ctx, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod); /** * @brief BPU_codeFreeCtx * @param ctx */ -void BPU_codeFreeCtx(BPU_T_Code_Ctx **ctx); +void BPU_codeFreeCtx (BPU_T_Code_Ctx ** ctx); // Available init params for all codes, it is only wrapper for code params init /** @@ -96,13 +98,14 @@ void BPU_codeFreeCtx(BPU_T_Code_Ctx **ctx); * @param mod * @return */ -int BPU_codeInitParamsGoppa(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod); +int BPU_codeInitParamsGoppa (BPU_T_UN_Code_Params * params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod); /** * @brief BPU_codeFreeParamsGoppa * @param params */ -void BPU_codeFreeParamsGoppa(BPU_T_UN_Code_Params *params); +void BPU_codeFreeParamsGoppa (BPU_T_UN_Code_Params * params); /** * Wrapper for param initialization. After work you have to free memory using call BPU_codeFreeParamsQcmdpc @@ -113,13 +116,15 @@ void BPU_codeFreeParamsGoppa(BPU_T_UN_Code_Params *params); * @param t count of errors * @return [description] */ -int BPU_codeInitParamsQcmdpc(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t); +int BPU_codeInitParamsQcmdpc (BPU_T_UN_Code_Params * params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t); /** * Free memory for params. * @param params pointer to structure */ -void BPU_codeFreeParamsQcmdpc(BPU_T_UN_Code_Params *params); +void BPU_codeFreeParamsQcmdpc (BPU_T_UN_Code_Params * params); // add yours additional params if needed #endif // BPU_CODECTX_H diff --git a/lib/src/bitpunch/code/goppa/goppa.c b/lib/src/bitpunch/code/goppa/goppa.c index d018f14..a5e0532 100644 --- a/lib/src/bitpunch/code/goppa/goppa.c +++ b/lib/src/bitpunch/code/goppa/goppa.c @@ -29,265 +29,327 @@ along with this program. If not, see . /***********************************************************************************************************/ #ifdef BPU_CONF_ENCRYPTION -int BPU_goppaEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx) { - int rc = 0; - int i, j; - uint8_t out_bit = 0; - BPU_T_GF2 out_dword; +int BPU_goppaEncode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx) { + int rc = 0; + int i, j; + uint8_t out_bit = 0; + BPU_T_GF2 out_dword; BPU_T_GF2_Vector *tmp; - BPU_gf2VecNull(out); - - // test the size of message and g_m - if (in->len != ctx->code_spec->goppa->g_mat->n) { - BPU_printError("message length have to be of length %d", ctx->code_spec->goppa->g_mat->n); - - return -1; - } - BPU_gf2VecMalloc(&tmp, ctx->code_spec->goppa->g_mat->k); - for (j = 0; j < ctx->code_spec->goppa->g_mat->k; j++) { - out_dword = 0; - for (i = 0; i < ctx->code_spec->goppa->g_mat->elements_in_row; i++) { - out_dword ^= in->elements[i] & ctx->code_spec->goppa->g_mat->elements[j][i]; - } - out_bit = BPU_getParity(out_dword); - BPU_gf2VecSetBit(tmp, j, out_bit); - } - - rc += BPU_gf2VecConcat(out, tmp, in); - BPU_gf2VecFree(&tmp); - - return rc; + BPU_gf2VecNull (out); + + // test the size of message and g_m + if (in->len != ctx->code_spec->goppa->g_mat->n) { + BPU_printError ("message length have to be of length %d", + ctx->code_spec->goppa->g_mat->n); + + return -1; + } + BPU_gf2VecMalloc (&tmp, ctx->code_spec->goppa->g_mat->k); + for (j = 0; j < ctx->code_spec->goppa->g_mat->k; j++) { + out_dword = 0; + for (i = 0; i < ctx->code_spec->goppa->g_mat->elements_in_row; i++) { + out_dword ^= + in->elements[i] & ctx->code_spec->goppa->g_mat->elements[j][i]; + } + out_bit = BPU_getParity (out_dword); + BPU_gf2VecSetBit (tmp, j, out_bit); + } + + rc += BPU_gf2VecConcat (out, tmp, in); + BPU_gf2VecFree (&tmp); + + return rc; } #endif // BPU_CONF_ENCRYPTION /***********************************************************************************************************/ #ifdef BPU_CONF_DECRYPTION -int BPU_goppaDecode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx) { +int BPU_goppaDecode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx) { BPU_T_GF2_Vector *orig_enc; - int rc; + int rc; - // get error vector - rc = BPU_goppaGetError(ctx->e, in, ctx); + // get error vector + rc = BPU_goppaGetError (ctx->e, in, ctx); - // remove error - rc += BPU_gf2VecMalloc(&orig_enc, in->len); - BPU_gf2VecCopy(orig_enc, in); - rc += BPU_gf2VecXor(orig_enc, ctx->e); + // remove error + rc += BPU_gf2VecMalloc (&orig_enc, in->len); + BPU_gf2VecCopy (orig_enc, in); + rc += BPU_gf2VecXor (orig_enc, ctx->e); - // get message - rc += BPU_gf2VecCrop(out, orig_enc, in->len - ctx->msg_len, ctx->msg_len); - BPU_gf2VecFree(&orig_enc); + // get message + rc += BPU_gf2VecCrop (out, orig_enc, in->len - ctx->msg_len, ctx->msg_len); + BPU_gf2VecFree (&orig_enc); - return rc; + return rc; } -int BPU_goppaGetError(BPU_T_GF2_Vector *error, const BPU_T_GF2_Vector *encoded, const BPU_T_Code_Ctx *ctx) { +int BPU_goppaGetError (BPU_T_GF2_Vector * error, + const BPU_T_GF2_Vector * encoded, + const BPU_T_Code_Ctx * ctx) { BPU_T_Perm_Vector *inv_perm; - BPU_T_GF2_16x_Poly *syndrome, *tau, *a, *b, *sigma, *inv_syndrome, *tmp, *tmp2; - int l; - BPU_T_GF2_16x tmp_eval; + BPU_T_GF2_16x_Poly *syndrome, *tau, *a, *b, *sigma, *inv_syndrome, *tmp, + *tmp2; + int l; + BPU_T_GF2_16x tmp_eval; BPU_T_GF2_Vector *enc_permuted; - // permute code word - BPU_gf2VecMalloc(&enc_permuted, encoded->len); - BPU_gf2VecCopy(enc_permuted, encoded); - - BPU_permMalloc(&inv_perm, ctx->code_spec->goppa->permutation->size); - BPU_permGetInv(inv_perm, ctx->code_spec->goppa->permutation); - BPU_gf2VecPermute(enc_permuted, inv_perm); - BPU_permFree(&inv_perm); - - // Beginning of patterson - BPU_gf2xPolyMalloc(&syndrome, ctx->code_spec->goppa->g->deg - 1); - BPU_goppaDetSyndrome(syndrome, enc_permuted, ctx); - BPU_gf2VecFree(&enc_permuted); - - BPU_gf2xPolyMalloc(&inv_syndrome, (syndrome->deg > ctx->code_spec->goppa->g->deg) ? syndrome->deg : ctx->code_spec->goppa->g->deg); - BPU_gf2xPolyInv(inv_syndrome, syndrome, ctx->code_spec->goppa->g, ctx->math_ctx); - BPU_gf2xPolyFree(&syndrome); + // permute code word + BPU_gf2VecMalloc (&enc_permuted, encoded->len); + BPU_gf2VecCopy (enc_permuted, encoded); + + BPU_permMalloc (&inv_perm, ctx->code_spec->goppa->permutation->size); + BPU_permGetInv (inv_perm, ctx->code_spec->goppa->permutation); + BPU_gf2VecPermute (enc_permuted, inv_perm); + BPU_permFree (&inv_perm); + + // Beginning of patterson + BPU_gf2xPolyMalloc (&syndrome, ctx->code_spec->goppa->g->deg - 1); + BPU_goppaDetSyndrome (syndrome, enc_permuted, ctx); + BPU_gf2VecFree (&enc_permuted); + + BPU_gf2xPolyMalloc (&inv_syndrome, + (syndrome->deg > + ctx->code_spec->goppa->g->deg) ? syndrome->deg : ctx-> + code_spec->goppa->g->deg); + BPU_gf2xPolyInv (inv_syndrome, syndrome, ctx->code_spec->goppa->g, + ctx->math_ctx); + BPU_gf2xPolyFree (&syndrome); inv_syndrome->coef[1] = inv_syndrome->coef[1] ^ 1; - // get square root - BPU_gf2xPolyMalloc(&tau, ctx->code_spec->goppa->g->deg); - BPU_gf2xPolyRoot(tau, inv_syndrome, ctx->code_spec->goppa->g, ctx->math_ctx); - BPU_gf2xPolyFree(&inv_syndrome); - /**************** FROM NOW WE ARE NOT USING MODULUS g for a, b ********************/ - BPU_gf2xPolyMalloc(&a, (tau->deg > ctx->code_spec->goppa->g->deg) ? tau->deg : ctx->code_spec->goppa->g->deg); - BPU_gf2xPolyMalloc(&b, a->max_deg); - BPU_goppaFindPolyAB(a, b, tau, ctx->code_spec->goppa->g, ctx->math_ctx); - BPU_gf2xPolyFree(&tau); - - BPU_gf2xPolyMalloc(&tmp2, 2 * ctx->code_spec->goppa->g->deg); - BPU_gf2xPolyMalloc(&tmp, 2 * ctx->code_spec->goppa->g->deg); - - // a^2, b^2 - BPU_gf2xPolyMul(tmp, a, a, ctx->math_ctx); - BPU_gf2xPolyMul(tmp2, b, b, ctx->math_ctx); - - // copy a^2, b^2 to a, b - BPU_gf2xPolyCopy(a, tmp); - BPU_gf2xPolyFree(&tmp); - - BPU_gf2xPolyCopy(b, tmp2); - BPU_gf2xPolyFree(&tmp2); - - // b^2 * x - BPU_gf2xPolyShl(b, 1); - BPU_gf2xPolyMalloc(&sigma, ctx->code_spec->goppa->g->deg); - - // calculate sigma = a^2 + x * b^2 - BPU_gf2xPolyAdd(sigma, a, b); - BPU_gf2xPolyFree(&a); - BPU_gf2xPolyFree(&b); - // check if there is enough space + // get square root + BPU_gf2xPolyMalloc (&tau, ctx->code_spec->goppa->g->deg); + BPU_gf2xPolyRoot (tau, inv_syndrome, ctx->code_spec->goppa->g, + ctx->math_ctx); + BPU_gf2xPolyFree (&inv_syndrome); + /**************** FROM NOW WE ARE NOT USING MODULUS g for a, b ********************/ + BPU_gf2xPolyMalloc (&a, + (tau->deg > + ctx->code_spec->goppa->g->deg) ? tau->deg : ctx-> + code_spec->goppa->g->deg); + BPU_gf2xPolyMalloc (&b, a->max_deg); + BPU_goppaFindPolyAB (a, b, tau, ctx->code_spec->goppa->g, ctx->math_ctx); + BPU_gf2xPolyFree (&tau); + + BPU_gf2xPolyMalloc (&tmp2, 2 * ctx->code_spec->goppa->g->deg); + BPU_gf2xPolyMalloc (&tmp, 2 * ctx->code_spec->goppa->g->deg); + + // a^2, b^2 + BPU_gf2xPolyMul (tmp, a, a, ctx->math_ctx); + BPU_gf2xPolyMul (tmp2, b, b, ctx->math_ctx); + + // copy a^2, b^2 to a, b + BPU_gf2xPolyCopy (a, tmp); + BPU_gf2xPolyFree (&tmp); + + BPU_gf2xPolyCopy (b, tmp2); + BPU_gf2xPolyFree (&tmp2); + + // b^2 * x + BPU_gf2xPolyShl (b, 1); + BPU_gf2xPolyMalloc (&sigma, ctx->code_spec->goppa->g->deg); + + // calculate sigma = a^2 + x * b^2 + BPU_gf2xPolyAdd (sigma, a, b); + BPU_gf2xPolyFree (&a); + BPU_gf2xPolyFree (&b); + // check if there is enough space if (error->len < ctx->code_spec->goppa->support_len) { - BPU_gf2VecResize(error, ctx->code_spec->goppa->support_len); - } - else { - BPU_gf2VecNull(error); - } - sigma->deg = ctx->t; - for (l = 0; l < ctx->code_spec->goppa->support_len; l++) { - tmp_eval = BPU_gf2xPolyEval(sigma, ctx->math_ctx->exp_table[l], ctx->math_ctx); - BPU_gf2VecSetBit(error, l, !tmp_eval); - } - // permute error vector - BPU_gf2VecPermute(error, ctx->code_spec->goppa->permutation); - BPU_gf2xPolyFree(&sigma); - return 0; + BPU_gf2VecResize (error, ctx->code_spec->goppa->support_len); + } + else { + BPU_gf2VecNull (error); + } + sigma->deg = ctx->t; + for (l = 0; l < ctx->code_spec->goppa->support_len; l++) { + tmp_eval = + BPU_gf2xPolyEval (sigma, ctx->math_ctx->exp_table[l], + ctx->math_ctx); + BPU_gf2VecSetBit (error, l, !tmp_eval); + } + // permute error vector + BPU_gf2VecPermute (error, ctx->code_spec->goppa->permutation); + BPU_gf2xPolyFree (&sigma); + return 0; } -void BPU_goppaDetSyndrome(BPU_T_GF2_16x_Poly *syndrome, const BPU_T_GF2_Vector *z, const BPU_T_Code_Ctx *ctx) { - int row, column; +void BPU_goppaDetSyndrome (BPU_T_GF2_16x_Poly * syndrome, + const BPU_T_GF2_Vector * z, + const BPU_T_Code_Ctx * ctx) { + int row, column; + #ifdef BPU_CONF_GOPPA_WO_H - int k, e; - BPU_T_GF2_16x element, divider; + int k, e; + BPU_T_GF2_16x element, divider; #endif - BPU_gf2xPolyNull(syndrome); + BPU_gf2xPolyNull (syndrome); #ifdef BPU_CONF_GOPPA_WO_H - for(column = 0; column < z->len; column++) { - divider = BPU_gf2xPowerModT(BPU_gf2xPolyEval(ctx->code_spec->goppa->g, ctx->math_ctx->exp_table[column], ctx->math_ctx), -1, ctx->math_ctx); - if (BPU_gf2VecGetBit(z, column)) { - for(row = 0; row < ctx->code_spec->goppa->g->deg; row++) { - element = 0; - for(k = ctx->code_spec->goppa->g->deg - row, e = 0; k <= ctx->code_spec->goppa->g->deg; k++, e++) { - element ^= BPU_gf2xMulMod(ctx->code_spec->goppa->g->coef[k], BPU_gf2xPowerModT (ctx->math_ctx->exp_table[column], e, ctx->math_ctx), ctx->math_ctx->mod); - } - element = BPU_gf2xMulMod(element, divider, ctx->math_ctx->mod); - syndrome->coef[syndrome->max_deg - row] ^= element; - } - } - } + for (column = 0; column < z->len; column++) { + divider = + BPU_gf2xPowerModT (BPU_gf2xPolyEval + (ctx->code_spec->goppa->g, + ctx->math_ctx->exp_table[column], + ctx->math_ctx), -1, ctx->math_ctx); + if (BPU_gf2VecGetBit (z, column)) { + for (row = 0; row < ctx->code_spec->goppa->g->deg; row++) { + element = 0; + for (k = ctx->code_spec->goppa->g->deg - row, e = 0; + k <= ctx->code_spec->goppa->g->deg; k++, e++) { + element ^= + BPU_gf2xMulMod (ctx->code_spec->goppa->g->coef[k], + BPU_gf2xPowerModT (ctx->math_ctx-> + exp_table[column], e, + ctx->math_ctx), + ctx->math_ctx->mod); + } + element = BPU_gf2xMulMod (element, divider, ctx->math_ctx->mod); + syndrome->coef[syndrome->max_deg - row] ^= element; + } + } + } #else - for (column = 0; column < z->len; column++) { - for (row = 0; row < ctx->code_spec->goppa->h_mat->k; row++) { - syndrome->coef[syndrome->max_deg - row] ^= BPU_gf2VecGetBit(z, column) * ctx->code_spec->goppa->h_mat->elements[row][column]; - } - } + for (column = 0; column < z->len; column++) { + for (row = 0; row < ctx->code_spec->goppa->h_mat->k; row++) { + syndrome->coef[syndrome->max_deg - row] ^= + BPU_gf2VecGetBit (z, + column) * + ctx->code_spec->goppa->h_mat->elements[row][column]; + } + } #endif - syndrome->deg = BPU_gf2xPolyGetDeg(syndrome); + syndrome->deg = BPU_gf2xPolyGetDeg (syndrome); } -void BPU_goppaFindPolyAB(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x_Poly *b, const BPU_T_GF2_16x_Poly *tau, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx) { +void BPU_goppaFindPolyAB (BPU_T_GF2_16x_Poly * a, BPU_T_GF2_16x_Poly * b, + const BPU_T_GF2_16x_Poly * tau, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx) { BPU_T_GF2_16x_Poly *tmp; - int end_deg = mod->deg / 2; + int end_deg = mod->deg / 2; - BPU_gf2xPolyMalloc(&tmp, (tau->deg > mod->deg) ? tau->deg : mod->deg); - BPU_gf2xPolyExtEuclid(a, b, tmp, tau, mod, end_deg, math_ctx); - BPU_gf2xPolyFree(&tmp); + BPU_gf2xPolyMalloc (&tmp, (tau->deg > mod->deg) ? tau->deg : mod->deg); + BPU_gf2xPolyExtEuclid (a, b, tmp, tau, mod, end_deg, math_ctx); + BPU_gf2xPolyFree (&tmp); } #endif // BPU_CONF_DECRYPTION /***********************************************************************************************************/ #ifdef BPU_CONF_KEY_GEN -int BPU_goppaInitMatH2(BPU_T_GF2_Matrix *h2, BPU_T_GF2_16x_Matrix *hx, const BPU_T_Code_Ctx *ctx) { - int bit, bit_in_element = -1, act_element = 0; - int element_bit_size = ctx->math_ctx->mod_deg; - int k, row, column, e; - BPU_T_GF2_16x element, divider; - - if (h2->k != ctx->code_spec->goppa->g->deg * element_bit_size || h2->n != ctx->code_spec->goppa->support_len) { - BPU_printError("Matrix h2 dimension should be %dx%d, current is %dx%d", ctx->code_spec->goppa->g->deg * element_bit_size, ctx->code_spec->goppa->support_len, h2->k, h2->n); - - return -1; - } +int BPU_goppaInitMatH2 (BPU_T_GF2_Matrix * h2, BPU_T_GF2_16x_Matrix * hx, + const BPU_T_Code_Ctx * ctx) { + int bit, bit_in_element = -1, act_element = 0; + int element_bit_size = ctx->math_ctx->mod_deg; + int k, row, column, e; + BPU_T_GF2_16x element, divider; + + if (h2->k != ctx->code_spec->goppa->g->deg * element_bit_size + || h2->n != ctx->code_spec->goppa->support_len) { + BPU_printError ("Matrix h2 dimension should be %dx%d, current is %dx%d", + ctx->code_spec->goppa->g->deg * element_bit_size, + ctx->code_spec->goppa->support_len, h2->k, h2->n); + + return -1; + } #ifndef BPU_CONF_GOPPA_WO_H - if (hx->k != ctx->code_spec->goppa->g->deg || hx->n != ctx->code_spec->goppa->support_len) { - BPU_printError("Matrix hx dimension should be %dx%d, current is %dx%d", ctx->code_spec->goppa->g->deg, ctx->math_ctx->ord, hx->k, hx->n); - - return -1; - } + if (hx->k != ctx->code_spec->goppa->g->deg + || hx->n != ctx->code_spec->goppa->support_len) { + BPU_printError ("Matrix hx dimension should be %dx%d, current is %dx%d", + ctx->code_spec->goppa->g->deg, ctx->math_ctx->ord, + hx->k, hx->n); + + return -1; + } #endif - for(column = 0; column < h2->n; column++) { - divider = BPU_gf2xPowerModT(BPU_gf2xPolyEval(ctx->code_spec->goppa->g, ctx->math_ctx->exp_table[column], ctx->math_ctx), -1, ctx->math_ctx); - if ((column - act_element * h2->element_bit_size) >= h2->element_bit_size) { // next elemenet, first bit - act_element++; - bit_in_element = 0; - } - else // same element, next bit - bit_in_element++; - for(row = 0; row < ctx->code_spec->goppa->g->deg; row++) { - element = 0; - for(k = ctx->code_spec->goppa->g->deg - row, e = 0; k <= ctx->code_spec->goppa->g->deg; k++, e++) { - element ^= BPU_gf2xMulModT(ctx->code_spec->goppa->g->coef[k], BPU_gf2xPowerModT(ctx->math_ctx->exp_table[column], e, ctx->math_ctx), ctx->math_ctx); - } - element = BPU_gf2xMulModT(element, divider, ctx->math_ctx); + for (column = 0; column < h2->n; column++) { + divider = + BPU_gf2xPowerModT (BPU_gf2xPolyEval + (ctx->code_spec->goppa->g, + ctx->math_ctx->exp_table[column], + ctx->math_ctx), -1, ctx->math_ctx); + if ((column - act_element * h2->element_bit_size) >= h2->element_bit_size) { // next elemenet, first bit + act_element++; + bit_in_element = 0; + } + else // same element, next bit + bit_in_element++; + for (row = 0; row < ctx->code_spec->goppa->g->deg; row++) { + element = 0; + for (k = ctx->code_spec->goppa->g->deg - row, e = 0; + k <= ctx->code_spec->goppa->g->deg; k++, e++) { + element ^= + BPU_gf2xMulModT (ctx->code_spec->goppa->g->coef[k], + BPU_gf2xPowerModT (ctx->math_ctx-> + exp_table[column], e, + ctx->math_ctx), + ctx->math_ctx); + } + element = BPU_gf2xMulModT (element, divider, ctx->math_ctx); #ifndef BPU_CONF_GOPPA_WO_H - hx->elements[row][column] = element; + hx->elements[row][column] = element; #endif - for (bit = 0; bit < element_bit_size; bit++) { // bit loop through element of matrix - h2->elements[row*element_bit_size + bit][act_element] ^= BPU_getBit(element, bit) << (bit_in_element); // get bit from element and shift it - } - } - } - return 0; + for (bit = 0; bit < element_bit_size; bit++) { // bit loop through element of matrix + h2->elements[row * element_bit_size + bit][act_element] ^= BPU_getBit (element, bit) << (bit_in_element); // get bit from element and shift it + } + } + } + return 0; } -int BPU_goppaGenCode(BPU_T_Code_Ctx *ctx) { - int rc = 0; - int permute = -1; // needed for equivalent codes +int BPU_goppaGenCode (BPU_T_Code_Ctx * ctx) { + int rc = 0; + int permute = -1; // needed for equivalent codes BPU_T_Perm_Vector *temp; - BPU_gf2xPolyMalloc(&ctx->code_spec->goppa->g, ctx->t); - BPU_gf2xPolyGenGoppa(ctx->code_spec->goppa->g, ctx->t, ctx->math_ctx); + BPU_gf2xPolyMalloc (&ctx->code_spec->goppa->g, ctx->t); + BPU_gf2xPolyGenGoppa (ctx->code_spec->goppa->g, ctx->t, ctx->math_ctx); #ifdef BPU_CONF_GOPPA_WO_H - ctx->code_spec->goppa->h_mat = NULL; + ctx->code_spec->goppa->h_mat = NULL; #else - BPU_gf2xMatMalloc(&ctx->code_spec->goppa->h_mat, ctx->code_spec->goppa->g->deg, ctx->code_spec->goppa->support_len); + BPU_gf2xMatMalloc (&ctx->code_spec->goppa->h_mat, + ctx->code_spec->goppa->g->deg, + ctx->code_spec->goppa->support_len); #endif - BPU_gf2MatMalloc(&(ctx->code_spec->goppa->g_mat), ctx->code_spec->goppa->g->deg * ctx->math_ctx->mod_deg, ctx->code_spec->goppa->support_len); - rc = BPU_goppaInitMatH2(ctx->code_spec->goppa->g_mat, ctx->code_spec->goppa->h_mat, ctx); - if (rc) { - BPU_printError("Can not initialize H matrix."); - - return rc; - } - // prepare permutations - BPU_permMalloc(&ctx->code_spec->goppa->permutation, ctx->code_spec->goppa->support_len); - BPU_permMalloc(&temp, ctx->code_spec->goppa->permutation->size); - BPU_permRandomize(ctx->code_spec->goppa->permutation); - BPU_gf2MatPermute(ctx->code_spec->goppa->g_mat, ctx->code_spec->goppa->permutation); - - while (permute != 0) { - permute = BPU_gf2MatMakeSystematic(ctx->code_spec->goppa->g_mat); - if (permute != 0) { - BPU_permRandomize(temp); - BPU_permPermute(ctx->code_spec->goppa->permutation, temp); - BPU_gf2MatPermute(ctx->code_spec->goppa->g_mat, temp); - } - } - rc = BPU_gf2MatCrop(ctx->code_spec->goppa->g_mat, (ctx->code_spec->goppa->g_mat->n - ctx->code_spec->goppa->g_mat->k)); - BPU_permFree(&temp); - - if (rc != 0) { + BPU_gf2MatMalloc (&(ctx->code_spec->goppa->g_mat), + ctx->code_spec->goppa->g->deg * ctx->math_ctx->mod_deg, + ctx->code_spec->goppa->support_len); + rc = BPU_goppaInitMatH2 (ctx->code_spec->goppa->g_mat, + ctx->code_spec->goppa->h_mat, ctx); + if (rc) { + BPU_printError ("Can not initialize H matrix."); + + return rc; + } + // prepare permutations + BPU_permMalloc (&ctx->code_spec->goppa->permutation, + ctx->code_spec->goppa->support_len); + BPU_permMalloc (&temp, ctx->code_spec->goppa->permutation->size); + BPU_permRandomize (ctx->code_spec->goppa->permutation); + BPU_gf2MatPermute (ctx->code_spec->goppa->g_mat, + ctx->code_spec->goppa->permutation); + + while (permute != 0) { + permute = BPU_gf2MatMakeSystematic (ctx->code_spec->goppa->g_mat); + if (permute != 0) { + BPU_permRandomize (temp); + BPU_permPermute (ctx->code_spec->goppa->permutation, temp); + BPU_gf2MatPermute (ctx->code_spec->goppa->g_mat, temp); + } + } + rc = BPU_gf2MatCrop (ctx->code_spec->goppa->g_mat, + (ctx->code_spec->goppa->g_mat->n - + ctx->code_spec->goppa->g_mat->k)); + BPU_permFree (&temp); + + if (rc != 0) { #ifdef BPU_CONF_PRINT - BPU_printGf2Mat(ctx->code_spec->goppa->g_mat); + BPU_printGf2Mat (ctx->code_spec->goppa->g_mat); #endif - BPU_printError("BPU_genKeyPair: can not crop matrix"); + BPU_printError ("BPU_genKeyPair: can not crop matrix"); - return -1; - } - return rc; + return -1; + } + return rc; } #endif // BPU_CONF_KEY_GEN diff --git a/lib/src/bitpunch/code/goppa/goppa.h b/lib/src/bitpunch/code/goppa/goppa.h index 476ebc3..318d3c2 100644 --- a/lib/src/bitpunch/code/goppa/goppa.h +++ b/lib/src/bitpunch/code/goppa/goppa.h @@ -37,7 +37,8 @@ along with this program. If not, see . * @param ctx * @return */ -int BPU_goppaEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); +int BPU_goppaEncode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx); #endif #ifdef BPU_CONF_DECRYPTION @@ -49,7 +50,8 @@ int BPU_goppaEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const str * * @return still 0 */ -int BPU_goppaDecode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); +int BPU_goppaDecode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx); /** * Get error vector Patterson alg. @@ -59,7 +61,9 @@ int BPU_goppaDecode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const str * * @return still 0 */ -int BPU_goppaGetError(BPU_T_GF2_Vector *error, const BPU_T_GF2_Vector *encoded, const BPU_T_Code_Ctx *ctx); +int BPU_goppaGetError (BPU_T_GF2_Vector * error, + const BPU_T_GF2_Vector * encoded, + const BPU_T_Code_Ctx * ctx); /** * Find polynomials a, b of degree <= (t div 2). We are using GCD to find them, gcd(tau, mod) = a, so: a = tau * b + mod * (some s***s). Out a and b are allocated inside, after use must be freed using BPU_freePoly(). @@ -70,7 +74,10 @@ int BPU_goppaGetError(BPU_T_GF2_Vector *error, const BPU_T_GF2_Vector *encoded, * @param math_ctx[in] [description] */ /// Find polynomials a, b of degree <= (t div 2) -void BPU_goppaFindPolyAB(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x_Poly *b, const BPU_T_GF2_16x_Poly *tau, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx); +void BPU_goppaFindPolyAB (BPU_T_GF2_16x_Poly * a, BPU_T_GF2_16x_Poly * b, + const BPU_T_GF2_16x_Poly * tau, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx); /** * @brief BPU_goppaDetSyndrome @@ -78,7 +85,9 @@ void BPU_goppaFindPolyAB(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x_Poly *b, const BPU * @param z * @param ctx */ -void BPU_goppaDetSyndrome(BPU_T_GF2_16x_Poly *syndrome, const BPU_T_GF2_Vector *z, const BPU_T_Code_Ctx *ctx); +void BPU_goppaDetSyndrome (BPU_T_GF2_16x_Poly * syndrome, + const BPU_T_GF2_Vector * z, + const BPU_T_Code_Ctx * ctx); #endif #ifdef BPU_CONF_KEY_GEN @@ -87,7 +96,7 @@ void BPU_goppaDetSyndrome(BPU_T_GF2_16x_Poly *syndrome, const BPU_T_GF2_Vector * * @param ctx * @return */ -int BPU_goppaGenCode(BPU_T_Code_Ctx *ctx); +int BPU_goppaGenCode (BPU_T_Code_Ctx * ctx); /** * @brief BPU_goppaInitMatH2 Initialize control matrix. @@ -96,7 +105,8 @@ int BPU_goppaGenCode(BPU_T_Code_Ctx *ctx); * @param ctx * @return */ -int BPU_goppaInitMatH2(BPU_T_GF2_Matrix *h2, BPU_T_GF2_16x_Matrix *hx, const BPU_T_Code_Ctx *ctx); +int BPU_goppaInitMatH2 (BPU_T_GF2_Matrix * h2, BPU_T_GF2_16x_Matrix * hx, + const BPU_T_Code_Ctx * ctx); #endif #endif // BPU_GOPPA_H diff --git a/lib/src/bitpunch/code/goppa/goppatypes.c b/lib/src/bitpunch/code/goppa/goppatypes.c index e3ad0fb..6b0edc4 100644 --- a/lib/src/bitpunch/code/goppa/goppatypes.c +++ b/lib/src/bitpunch/code/goppa/goppatypes.c @@ -24,33 +24,34 @@ along with this program. If not, see . #include #include -void BPU_goppaFreeSpec(BPU_T_Goppa_Spec *spec) { - BPU_gf2xPolyFree(&spec->g); - BPU_gf2MatFree(&spec->g_mat); +void BPU_goppaFreeSpec (BPU_T_Goppa_Spec * spec) { + BPU_gf2xPolyFree (&spec->g); + BPU_gf2MatFree (&spec->g_mat); #ifndef BPU_CONF_GOPPA_WO_H - BPU_gf2xMatFree(&spec->h_mat); + BPU_gf2xMatFree (&spec->h_mat); #endif - BPU_permFree(&spec->permutation); + BPU_permFree (&spec->permutation); } -int BPU_goppaInitParams(BPU_T_Goppa_Params **params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) { - *params = (BPU_T_Goppa_Params*) calloc(sizeof(BPU_T_Goppa_Params), 1); +int BPU_goppaInitParams (BPU_T_Goppa_Params ** params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod) { + *params = (BPU_T_Goppa_Params *) calloc (sizeof (BPU_T_Goppa_Params), 1); - if (!params) { - BPU_printError("Can't init Code params"); + if (!params) { + BPU_printError ("Can't init Code params"); - return -1; - } - (*params)->m = m; - (*params)->t = t; - (*params)->mod = mod; + return -1; + } + (*params)->m = m; + (*params)->t = t; + (*params)->mod = mod; - return 0; + return 0; } -void BPU_goppaFreeParams(BPU_T_Goppa_Params **params) { - if (*params) { - free(*params); - } - *params = NULL; +void BPU_goppaFreeParams (BPU_T_Goppa_Params ** params) { + if (*params) { + free (*params); + } + *params = NULL; } diff --git a/lib/src/bitpunch/code/goppa/goppatypes.h b/lib/src/bitpunch/code/goppa/goppatypes.h index 5cdc1d6..76e84aa 100644 --- a/lib/src/bitpunch/code/goppa/goppatypes.h +++ b/lib/src/bitpunch/code/goppa/goppatypes.h @@ -25,27 +25,27 @@ along with this program. If not, see . Internal Goppa code structure. */ typedef struct _BPU_T_Goppa_Spec { - BPU_T_GF2_Matrix *g_mat; ///< Generator matrix - BPU_T_GF2_16x_Matrix *h_mat; ///< Control matrix H - BPU_T_GF2_16x_Poly *g; ///< gen. polynomial - BPU_T_Perm_Vector *permutation; ///< permutation "matrix" - uint16_t support_len; ///< number of elements in support -}BPU_T_Goppa_Spec; + BPU_T_GF2_Matrix *g_mat; ///< Generator matrix + BPU_T_GF2_16x_Matrix *h_mat; ///< Control matrix H + BPU_T_GF2_16x_Poly *g; ///< gen. polynomial + BPU_T_Perm_Vector *permutation; ///< permutation "matrix" + uint16_t support_len; ///< number of elements in support +} BPU_T_Goppa_Spec; /** Goppa code params. */ typedef struct _BPU_T_Goppa_Params { - uint16_t m; ///< degree of mod polynomial - uint16_t t; ///< error capability of code - BPU_T_GF2_16x mod; ///< Galois field polynomial -}BPU_T_Goppa_Params; + uint16_t m; ///< degree of mod polynomial + uint16_t t; ///< error capability of code + BPU_T_GF2_16x mod; ///< Galois field polynomial +} BPU_T_Goppa_Params; /** * @brief BPU_goppaFreeSpec Free Goppa code internal structure. * @param spec */ -void BPU_goppaFreeSpec(BPU_T_Goppa_Spec *spec); +void BPU_goppaFreeSpec (BPU_T_Goppa_Spec * spec); /** * @brief BPU_goppaInitParams Initi Goppa code params. @@ -55,12 +55,13 @@ void BPU_goppaFreeSpec(BPU_T_Goppa_Spec *spec); * @param mod * @return */ -int BPU_goppaInitParams(BPU_T_Goppa_Params **params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod); +int BPU_goppaInitParams (BPU_T_Goppa_Params ** params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod); /** * @brief BPU_goppaFreeParams Free goppa code params. * @param params */ -void BPU_goppaFreeParams(BPU_T_Goppa_Params **params); +void BPU_goppaFreeParams (BPU_T_Goppa_Params ** params); #endif // GOPPATYPES_H diff --git a/lib/src/bitpunch/code/qcmdpc/qcmdpc.c b/lib/src/bitpunch/code/qcmdpc/qcmdpc.c index 996b770..50bf779 100644 --- a/lib/src/bitpunch/code/qcmdpc/qcmdpc.c +++ b/lib/src/bitpunch/code/qcmdpc/qcmdpc.c @@ -20,441 +20,496 @@ along with this program. If not, see . #include "qcmdpc.h" #ifdef BPU_CONF_ENCRYPTION -int BPU_mecsQcmdpcEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx) { - - BPU_T_GF2_Poly temp_ct, temp_rot_row; - int ele, bit, i, bit_in_msg = 0; - - // copy message into cipher text - for (i = 0; i < in->array_length; i++) - out->elements[i] = in->elements[i]; - - // prolong ciphertext - BPU_gf2PolySetDeg(out, ctx->code_len); - - // calc rest of cipher text (Q x m) - BPU_gf2PolyMalloc(&temp_ct, ctx->code_spec->qcmdpc->G.element_size); - // for all matrices in G - for (ele = 0; ele < ctx->code_spec->qcmdpc->G.element_count; ele++) { - // get matrix - BPU_gf2PolyCopy(&temp_rot_row, &ctx->code_spec->qcmdpc->G.matrices[ele]); - // for all bits in matrix - for (bit = 0; bit < ctx->code_spec->qcmdpc->G.element_size; bit++) { - // if is set bit in message - if (BPU_gf2PolyGetBit(in, bit_in_msg)) { - for (i = 0; i < temp_rot_row.array_length; i++) - temp_ct.elements[i] ^= temp_rot_row.elements[i]; - } - bit_in_msg++; - // get next row by shift - BPU_gf2PolyMulX(&temp_rot_row); +int BPU_mecsQcmdpcEncode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx) { + + BPU_T_GF2_Poly temp_ct, temp_rot_row; + int ele, bit, i, bit_in_msg = 0; + + // copy message into cipher text + for (i = 0; i < in->array_length; i++) + out->elements[i] = in->elements[i]; + + // prolong ciphertext + BPU_gf2PolySetDeg (out, ctx->code_len); + + // calc rest of cipher text (Q x m) + BPU_gf2PolyMalloc (&temp_ct, ctx->code_spec->qcmdpc->G.element_size); + // for all matrices in G + for (ele = 0; ele < ctx->code_spec->qcmdpc->G.element_count; ele++) { + // get matrix + BPU_gf2PolyCopy (&temp_rot_row, + &ctx->code_spec->qcmdpc->G.matrices[ele]); + // for all bits in matrix + for (bit = 0; bit < ctx->code_spec->qcmdpc->G.element_size; bit++) { + // if is set bit in message + if (BPU_gf2PolyGetBit (in, bit_in_msg)) { + for (i = 0; i < temp_rot_row.array_length; i++) + temp_ct.elements[i] ^= temp_rot_row.elements[i]; + } + bit_in_msg++; + // get next row by shift + BPU_gf2PolyMulX (&temp_rot_row); + } + BPU_gf2PolyFree (&temp_rot_row, 0); } - BPU_gf2PolyFree(&temp_rot_row, 0); - } - // join ciphertext - BPU_gf2PolyShiftLeft(&temp_ct, ctx->code_spec->qcmdpc->G.element_size); - BPU_gf2PolyAdd(out, &temp_ct, 0); - BPU_gf2PolyFree(&temp_ct, 0); + // join ciphertext + BPU_gf2PolyShiftLeft (&temp_ct, ctx->code_spec->qcmdpc->G.element_size); + BPU_gf2PolyAdd (out, &temp_ct, 0); + BPU_gf2PolyFree (&temp_ct, 0); - return 0; + return 0; } #endif #ifdef BPU_CONF_DECRYPTION -int BPU_mecsQcmdpcDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx) { - - int ret = 0, delta = BPU_QCMDPC_PARAM_DELTA; - int i; - - // BPU_printGf2Vec(ctx->e); - // null error vector - BPU_gf2VecNull(ctx->e); - - // try to decode with faster algorithm - if (!BPU_mecsQcmdpcDecode2(ctx->e, in, ctx)) { - // free decoded - BPU_gf2VecNull(ctx->e); - while(1) { - // if not decoded, try algorithm with lower DFR - if (!BPU_mecsQcmdpcDecode1(ctx->e, in, delta, ctx)) { +int BPU_mecsQcmdpcDecrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx) { + + int ret = 0, delta = BPU_QCMDPC_PARAM_DELTA; + int i; + + // BPU_printGf2Vec(ctx->e); + // null error vector + BPU_gf2VecNull (ctx->e); + + // try to decode with faster algorithm + if (!BPU_mecsQcmdpcDecode2 (ctx->e, in, ctx)) { // free decoded - BPU_gf2VecNull(ctx->e); - // if not decoded decrease threshold tolerance param - delta--; - if (delta < 0) { - ret = -1; - break; + BPU_gf2VecNull (ctx->e); + while (1) { + // if not decoded, try algorithm with lower DFR + if (!BPU_mecsQcmdpcDecode1 (ctx->e, in, delta, ctx)) { + // free decoded + BPU_gf2VecNull (ctx->e); + // if not decoded decrease threshold tolerance param + delta--; + if (delta < 0) { + ret = -1; + break; + } + } + else + break; } - } - else - break; } - } - - // if decoded, get message from first param_m bits - if (ret == 0) { - // decrypt message - for (i = 0; i < out->array_length; i++) - out->elements[i] = ctx->e->elements[i] ^ in->elements[i]; - // crop last element - out->elements[out->array_length-1] <<= out->element_bit_size - (out->len % out->element_bit_size); - out->elements[out->array_length-1] >>= out->element_bit_size - (out->len % out->element_bit_size); - } - else - BPU_gf2VecNull(ctx->e); - - return ret; -} -int BPU_mecsQcmdpcDecode1(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *cipher_text, int delta, const struct _BPU_T_Code_Ctx *ctx) { - BPU_T_GF2_Poly syndrom; - BPU_T_GF2_Sparse_Poly row; - int iter = -1, max, bit, upc, upc_counts[cipher_text->len], isSyndromZero = 0; - int flipped_bits_iter = 0; - uint8_t bit_value; - - // allocate output error vector - // BPU_gf2PolyMalloc(error_vec, cipher_text->len); - - // calc the syndrom - BPU_mecsQcmdpcCalcSyndrom(&syndrom, cipher_text, ctx); - // check syndrom - if (!BPU_gf2PolyIsZero(&syndrom)) { - // for max iterations - for (iter = 0; iter < BPU_QCMDPC_PARAM_MAX_ITER; iter++) { - max = 0; - // for every bit of cipher text - for (bit = 0; bit < error_vec->len; bit++) { - // calc #UPC - BPU_gf2SparseQcMatrixGetRow(&row, &ctx->code_spec->qcmdpc->H, bit); - upc = BPU_gf2SparsePolyAndHW(&syndrom, &row); - upc_counts[bit] = upc; - if (upc > max) - max = upc; - BPU_gf2SparsePolyFree(&row, 0); - } - - if (max == 0) { - isSyndromZero = 0; - break; - } - - - flipped_bits_iter = 0; - // check which bits to flip - for (bit = 0; bit < error_vec->len; bit++) { - if (upc_counts[bit] > 0 && upc_counts[bit] >= (max-delta)) { - flipped_bits_iter++; - // flip bit - bit_value = !BPU_gf2VecGetBit(error_vec, bit); - BPU_gf2VecSetBit(error_vec, bit, bit_value); - // update syndrom - BPU_gf2SparseQcMatrixGetRow(&row, &ctx->code_spec->qcmdpc->H, bit); - BPU_gf2SparsePolyAdd(&syndrom, &row); - BPU_gf2SparsePolyFree(&row, 0); - // check the syndrom - if (BPU_gf2PolyIsZero(&syndrom)) { - isSyndromZero = 1; - break; - } - } - } - - if (isSyndromZero) - break; + // if decoded, get message from first param_m bits + if (ret == 0) { + // decrypt message + for (i = 0; i < out->array_length; i++) + out->elements[i] = ctx->e->elements[i] ^ in->elements[i]; + // crop last element + out->elements[out->array_length - 1] <<= out->element_bit_size - + (out->len % out->element_bit_size); + out->elements[out->array_length - 1] >>= out->element_bit_size - + (out->len % out->element_bit_size); } - } - else { - isSyndromZero = 1; - } - //free - BPU_gf2PolyFree(&syndrom, 0); - - return isSyndromZero; + else + BPU_gf2VecNull (ctx->e); + + return ret; } -int BPU_mecsQcmdpcDecode2(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *cipher_text, const struct _BPU_T_Code_Ctx *ctx) { - BPU_T_GF2_Poly syndrom; - BPU_T_GF2_Sparse_Poly row; - int iter = -1, bit, upc, isSyndromZero = 0; - int flipped_bits_iter = 0; - const uint16_t B_store[BPU_QCMDPC_MAX_B_VALUES]={28,26,24,22,20}; - uint8_t bit_value; - - // calc the syndrom - BPU_mecsQcmdpcCalcSyndrom(&syndrom, cipher_text, ctx); - - // check syndrom - if (!BPU_gf2PolyIsZero(&syndrom)) { - // for max iterations - for (iter = 0; iter < BPU_QCMDPC_PARAM_MAX_ITER; iter++) { - flipped_bits_iter = 0; - // for every bit of cipher text - for (bit = 0; bit < error_vec->len; bit++) { - // calc #UPC - BPU_gf2SparseQcMatrixGetRow(&row, &ctx->code_spec->qcmdpc->H, bit); - upc = BPU_gf2SparsePolyAndHW(&syndrom, &row); - - // check which bits to flip - if (upc > 0 && upc >= B_store[iter < BPU_QCMDPC_MAX_B_VALUES ? iter : (BPU_QCMDPC_MAX_B_VALUES-1)]-BPU_QCMDPC_PARAM_DELTA_B) { - flipped_bits_iter++; - // flip bit - bit_value = !BPU_gf2VecGetBit(error_vec, bit); - BPU_gf2VecSetBit(error_vec, bit, bit_value); - // update syndrom - BPU_gf2SparsePolyAdd(&syndrom, &row); - // check the syndrom - if (BPU_gf2PolyIsZero(&syndrom)) { - isSyndromZero = 1; - BPU_gf2SparsePolyFree(&row, 0); - break; - } +int BPU_mecsQcmdpcDecode1 (BPU_T_GF2_Vector * error_vec, + const BPU_T_GF2_Vector * cipher_text, int delta, + const struct _BPU_T_Code_Ctx *ctx) { + BPU_T_GF2_Poly syndrom; + BPU_T_GF2_Sparse_Poly row; + int iter = -1, max, bit, upc, upc_counts[cipher_text->len], isSyndromZero = + 0; + int flipped_bits_iter = 0; + uint8_t bit_value; + + // allocate output error vector + // BPU_gf2PolyMalloc(error_vec, cipher_text->len); + + // calc the syndrom + BPU_mecsQcmdpcCalcSyndrom (&syndrom, cipher_text, ctx); + // check syndrom + if (!BPU_gf2PolyIsZero (&syndrom)) { + // for max iterations + for (iter = 0; iter < BPU_QCMDPC_PARAM_MAX_ITER; iter++) { + max = 0; + // for every bit of cipher text + for (bit = 0; bit < error_vec->len; bit++) { + // calc #UPC + BPU_gf2SparseQcMatrixGetRow (&row, &ctx->code_spec->qcmdpc->H, + bit); + upc = BPU_gf2SparsePolyAndHW (&syndrom, &row); + upc_counts[bit] = upc; + if (upc > max) + max = upc; + BPU_gf2SparsePolyFree (&row, 0); + } + + if (max == 0) { + isSyndromZero = 0; + break; + } + + + flipped_bits_iter = 0; + // check which bits to flip + for (bit = 0; bit < error_vec->len; bit++) { + if (upc_counts[bit] > 0 && upc_counts[bit] >= (max - delta)) { + flipped_bits_iter++; + // flip bit + bit_value = !BPU_gf2VecGetBit (error_vec, bit); + BPU_gf2VecSetBit (error_vec, bit, bit_value); + // update syndrom + BPU_gf2SparseQcMatrixGetRow (&row, + &ctx->code_spec->qcmdpc->H, + bit); + BPU_gf2SparsePolyAdd (&syndrom, &row); + BPU_gf2SparsePolyFree (&row, 0); + // check the syndrom + if (BPU_gf2PolyIsZero (&syndrom)) { + isSyndromZero = 1; + break; + } + } + } + + if (isSyndromZero) + break; } - BPU_gf2SparsePolyFree(&row, 0); - } + } + else { + isSyndromZero = 1; + } + //free + BPU_gf2PolyFree (&syndrom, 0); - if (flipped_bits_iter < 1) { - isSyndromZero = 0; - break; - } + return isSyndromZero; +} - if (isSyndromZero) - break; +int BPU_mecsQcmdpcDecode2 (BPU_T_GF2_Vector * error_vec, + const BPU_T_GF2_Vector * cipher_text, + const struct _BPU_T_Code_Ctx *ctx) { + BPU_T_GF2_Poly syndrom; + BPU_T_GF2_Sparse_Poly row; + int iter = -1, bit, upc, isSyndromZero = 0; + int flipped_bits_iter = 0; + const uint16_t B_store[BPU_QCMDPC_MAX_B_VALUES] = { 28, 26, 24, 22, 20 }; + uint8_t bit_value; + + // calc the syndrom + BPU_mecsQcmdpcCalcSyndrom (&syndrom, cipher_text, ctx); + + // check syndrom + if (!BPU_gf2PolyIsZero (&syndrom)) { + // for max iterations + for (iter = 0; iter < BPU_QCMDPC_PARAM_MAX_ITER; iter++) { + flipped_bits_iter = 0; + // for every bit of cipher text + for (bit = 0; bit < error_vec->len; bit++) { + // calc #UPC + BPU_gf2SparseQcMatrixGetRow (&row, &ctx->code_spec->qcmdpc->H, + bit); + upc = BPU_gf2SparsePolyAndHW (&syndrom, &row); + + // check which bits to flip + if (upc > 0 + && upc >= B_store[iter < + BPU_QCMDPC_MAX_B_VALUES ? iter + : (BPU_QCMDPC_MAX_B_VALUES - 1)] - + BPU_QCMDPC_PARAM_DELTA_B) { + flipped_bits_iter++; + // flip bit + bit_value = !BPU_gf2VecGetBit (error_vec, bit); + BPU_gf2VecSetBit (error_vec, bit, bit_value); + // update syndrom + BPU_gf2SparsePolyAdd (&syndrom, &row); + // check the syndrom + if (BPU_gf2PolyIsZero (&syndrom)) { + isSyndromZero = 1; + BPU_gf2SparsePolyFree (&row, 0); + break; + } + } + BPU_gf2SparsePolyFree (&row, 0); + } + + if (flipped_bits_iter < 1) { + isSyndromZero = 0; + break; + } + + if (isSyndromZero) + break; + } } - } - else { - isSyndromZero = 1; - } - //free - BPU_gf2PolyFree(&syndrom, 0); - - return isSyndromZero; -} + else { + isSyndromZero = 1; + } + //free + BPU_gf2PolyFree (&syndrom, 0); -void BPU_mecsQcmdpcCalcSyndrom(BPU_T_GF2_Vector *syndrom, const BPU_T_GF2_Vector *cipher_text, const struct _BPU_T_Code_Ctx *ctx) { - BPU_T_GF2_Sparse_Poly row; - int i; + return isSyndromZero; +} - BPU_gf2PolyMalloc(syndrom, ctx->code_spec->qcmdpc->H.n); - for (i = 0; i < cipher_text->len; i++) { - if (BPU_gf2VecGetBit(cipher_text, i) == 1ul) { - BPU_gf2SparseQcMatrixGetRow(&row, &ctx->code_spec->qcmdpc->H, i); - BPU_gf2SparsePolyAdd(syndrom, &row); - BPU_gf2SparsePolyFree(&row, 0); +void BPU_mecsQcmdpcCalcSyndrom (BPU_T_GF2_Vector * syndrom, + const BPU_T_GF2_Vector * cipher_text, + const struct _BPU_T_Code_Ctx *ctx) { + BPU_T_GF2_Sparse_Poly row; + int i; + + BPU_gf2PolyMalloc (syndrom, ctx->code_spec->qcmdpc->H.n); + for (i = 0; i < cipher_text->len; i++) { + if (BPU_gf2VecGetBit (cipher_text, i) == 1ul) { + BPU_gf2SparseQcMatrixGetRow (&row, &ctx->code_spec->qcmdpc->H, i); + BPU_gf2SparsePolyAdd (syndrom, &row); + BPU_gf2SparsePolyFree (&row, 0); + } } - } } #endif #ifdef BPU_CONF_KEY_GEN -int BPU_mecsQcmdpcGenKeys(BPU_T_Code_Ctx *ctx) { - - BPU_T_GF2_Poly H_temp[ctx->code_spec->qcmdpc->n0]; - BPU_T_GF2_Poly G_temp[ctx->code_spec->qcmdpc->n0-1]; - BPU_T_GF2_Poly H_last_inv, mod, test_inv; - BPU_T_GF2_QC_Matrix G_temp_mat, H_temp_mat; - BPU_T_GF2_Sparse_Qc_Matrix H_temp_sparse; - int wi[ctx->code_spec->qcmdpc->n0]; - int ret = 0, i, err = 0; - - // init modulus like 1000000...0001 - BPU_gf2PolyMalloc(&mod, ctx->code_spec->qcmdpc->m+1); - BPU_gf2VecSetBit(&mod, ctx->code_spec->qcmdpc->m, 1ul); - BPU_gf2VecSetBit(&mod, 0, 1ul); - - #if defined(DEBUG_L) - BPU_printDebug("modulus: "); +int BPU_mecsQcmdpcGenKeys (BPU_T_Code_Ctx * ctx) { + + BPU_T_GF2_Poly H_temp[ctx->code_spec->qcmdpc->n0]; + BPU_T_GF2_Poly G_temp[ctx->code_spec->qcmdpc->n0 - 1]; + BPU_T_GF2_Poly H_last_inv, mod, test_inv; + BPU_T_GF2_QC_Matrix G_temp_mat, H_temp_mat; + BPU_T_GF2_Sparse_Qc_Matrix H_temp_sparse; + int wi[ctx->code_spec->qcmdpc->n0]; + int ret = 0, i, err = 0; + + // init modulus like 1000000...0001 + BPU_gf2PolyMalloc (&mod, ctx->code_spec->qcmdpc->m + 1); + BPU_gf2VecSetBit (&mod, ctx->code_spec->qcmdpc->m, 1ul); + BPU_gf2VecSetBit (&mod, 0, 1ul); + +#if defined(DEBUG_L) + BPU_printDebug ("modulus: "); // BPU_printGf2Poly(&mod); - BPU_printDebug("generating H vectors"); - #endif - - // alloc parity-check matrix - for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) { - // calc weight of polynomials (last poly must be odd) - if ((ctx->code_spec->qcmdpc->w/ctx->code_spec->qcmdpc->n0) % 2 == 1) - wi[i] = ctx->code_spec->qcmdpc->w/ctx->code_spec->qcmdpc->n0 + (int)(i < (ctx->code_spec->qcmdpc->w%ctx->code_spec->qcmdpc->n0)); - else - wi[i] = ctx->code_spec->qcmdpc->w/ctx->code_spec->qcmdpc->n0 + (int)(i < (ctx->code_spec->qcmdpc->w%ctx->code_spec->qcmdpc->n0)) + (int)(i == 0) - (int)(i == ctx->code_spec->qcmdpc->n0-1); + BPU_printDebug ("generating H vectors"); +#endif - // generate random polynomials of given weight - err += BPU_gf2PolyInitRand(&H_temp[i], ctx->code_spec->qcmdpc->m, wi[i], 1); - #if defined(DEBUG_L) - BPU_printDebug("H[%i]: ", i); + // alloc parity-check matrix + for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) { + // calc weight of polynomials (last poly must be odd) + if ((ctx->code_spec->qcmdpc->w / ctx->code_spec->qcmdpc->n0) % 2 == 1) + wi[i] = + ctx->code_spec->qcmdpc->w / ctx->code_spec->qcmdpc->n0 + + (int) (i < + (ctx->code_spec->qcmdpc->w % + ctx->code_spec->qcmdpc->n0)); + else + wi[i] = + ctx->code_spec->qcmdpc->w / ctx->code_spec->qcmdpc->n0 + + (int) (i < + (ctx->code_spec->qcmdpc->w % + ctx->code_spec->qcmdpc->n0)) + (int) (i == + 0) - (int) (i == + ctx-> + code_spec-> + qcmdpc-> + n0 - + 1); + + // generate random polynomials of given weight + err += + BPU_gf2PolyInitRand (&H_temp[i], ctx->code_spec->qcmdpc->m, wi[i], + 1); +#if defined(DEBUG_L) + BPU_printDebug ("H[%i]: ", i); // BPU_printGf2Poly(&H_temp[i]); - #endif - } - - if (!err) { - BPU_printDebug("generation successful"); - } - else { - BPU_printError("generation failed"); - } - - BPU_printDebug("finding inversion to H[%i]", ctx->code_spec->qcmdpc->n0-1); - // check if H[n0-1] has inversion - ret = 0; - while (!ret) { - BPU_gf2PolySetDeg(&H_temp[ctx->code_spec->qcmdpc->n0-1], -1); - // find inversion using XGCD - ret = BPU_gf2PolyInv(&H_last_inv, &H_temp[ctx->code_spec->qcmdpc->n0-1], &mod); - - // if inversion exists, test it (poly x inversion modulo = 1) - if (ret) { - BPU_printDebug("testing inversion"); - BPU_gf2PolyMulMod(&H_last_inv, &H_temp[ctx->code_spec->qcmdpc->n0-1], &test_inv, &mod, 1); - if (test_inv.len != 1 || test_inv.elements[0] != 1ul) { - ret = 0; - BPU_printWarning("inversion failed"); - } - else{ - BPU_printDebug("inversion OK"); - } - BPU_gf2PolyFree(&test_inv, 0); +#endif + } + + if (!err) { + BPU_printDebug ("generation successful"); } + else { + BPU_printError ("generation failed"); + } + + BPU_printDebug ("finding inversion to H[%i]", + ctx->code_spec->qcmdpc->n0 - 1); + // check if H[n0-1] has inversion + ret = 0; + while (!ret) { + BPU_gf2PolySetDeg (&H_temp[ctx->code_spec->qcmdpc->n0 - 1], -1); + // find inversion using XGCD + ret = + BPU_gf2PolyInv (&H_last_inv, + &H_temp[ctx->code_spec->qcmdpc->n0 - 1], &mod); + + // if inversion exists, test it (poly x inversion modulo = 1) + if (ret) { + BPU_printDebug ("testing inversion"); + BPU_gf2PolyMulMod (&H_last_inv, + &H_temp[ctx->code_spec->qcmdpc->n0 - 1], + &test_inv, &mod, 1); + if (test_inv.len != 1 || test_inv.elements[0] != 1ul) { + ret = 0; + BPU_printWarning ("inversion failed"); + } + else { + BPU_printDebug ("inversion OK"); + } + BPU_gf2PolyFree (&test_inv, 0); + } - // inversion not found, regenerate last poly and try to find inversion again - if (!ret) { - BPU_printDebug("inversion not found"); - BPU_printDebug("generating new H[%i]", ctx->code_spec->qcmdpc->n0-1); - BPU_gf2PolyFree(&H_temp[ctx->code_spec->qcmdpc->n0-1], 0); - ret += BPU_gf2PolyInitRand(&H_temp[ctx->code_spec->qcmdpc->n0-1], ctx->code_spec->qcmdpc->m, wi[ctx->code_spec->qcmdpc->n0-1], 1); - #if defined(DEBUG_L) - BPU_printGf2Poly(&H_temp[ctx->code_spec->qcmdpc->n0-1]); - #endif - BPU_gf2PolyFree(&H_last_inv, 0); + // inversion not found, regenerate last poly and try to find inversion again + if (!ret) { + BPU_printDebug ("inversion not found"); + BPU_printDebug ("generating new H[%i]", + ctx->code_spec->qcmdpc->n0 - 1); + BPU_gf2PolyFree (&H_temp[ctx->code_spec->qcmdpc->n0 - 1], 0); + ret += + BPU_gf2PolyInitRand (&H_temp[ctx->code_spec->qcmdpc->n0 - 1], + ctx->code_spec->qcmdpc->m, + wi[ctx->code_spec->qcmdpc->n0 - 1], 1); +#if defined(DEBUG_L) + BPU_printGf2Poly (&H_temp[ctx->code_spec->qcmdpc->n0 - 1]); +#endif + BPU_gf2PolyFree (&H_last_inv, 0); + } } - } - #if defined(DEBUG_L) - BPU_printDebug("inversion to H[%i] found ", ctx->code_spec->qcmdpc->n0-1); +#if defined(DEBUG_L) + BPU_printDebug ("inversion to H[%i] found ", + ctx->code_spec->qcmdpc->n0 - 1); // BPU_printGf2Poly(&H_last_inv); - BPU_printDebug("creating H matrix"); - #endif + BPU_printDebug ("creating H matrix"); +#endif - // create H temp matrix - BPU_gf2QcMatrixMalloc(&H_temp_mat, ctx->code_spec->qcmdpc->n0, ctx->code_spec->qcmdpc->m, 0, 0); - for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) { - H_temp[i].len = ctx->code_spec->qcmdpc->m; - BPU_gf2PolyCopy(&H_temp_mat.matrices[i], &H_temp[i]); - } + // create H temp matrix + BPU_gf2QcMatrixMalloc (&H_temp_mat, ctx->code_spec->qcmdpc->n0, + ctx->code_spec->qcmdpc->m, 0, 0); + for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) { + H_temp[i].len = ctx->code_spec->qcmdpc->m; + BPU_gf2PolyCopy (&H_temp_mat.matrices[i], &H_temp[i]); + } - BPU_gf2QcMatrixToSparse(&H_temp_sparse, &H_temp_mat, wi); + BPU_gf2QcMatrixToSparse (&H_temp_sparse, &H_temp_mat, wi); - #if defined(DEBUG_L) - BPU_printDebug("H: "); +#if defined(DEBUG_L) + BPU_printDebug ("H: "); // BPU_printGf2QcMatrix(&H_temp_mat); - BPU_printDebug("H sparse: "); + BPU_printDebug ("H sparse: "); // BPU_printGf2SparseQcMatrix(&H_temp_sparse); - BPU_printDebug("creating G matrix"); - #endif - - // create G temp matrix - for (i = 0; i < ctx->code_spec->qcmdpc->n0-1; i++) { - BPU_printDebug("multiplicating vectors H[%i]^-1 x H[%i]", ctx->code_spec->qcmdpc->n0-1, i); - BPU_gf2PolyMulMod(&H_last_inv, &H_temp[i], &G_temp[i], &mod, 0); - #if defined(DEBUG_L) + BPU_printDebug ("creating G matrix"); +#endif + + // create G temp matrix + for (i = 0; i < ctx->code_spec->qcmdpc->n0 - 1; i++) { + BPU_printDebug ("multiplicating vectors H[%i]^-1 x H[%i]", + ctx->code_spec->qcmdpc->n0 - 1, i); + BPU_gf2PolyMulMod (&H_last_inv, &H_temp[i], &G_temp[i], &mod, 0); +#if defined(DEBUG_L) // BPU_printGf2Poly(&G_temp[i]); - #endif - } - - BPU_printDebug("creating temp G for GH^T test"); - BPU_gf2QcMatrixMalloc(&G_temp_mat, ctx->code_spec->qcmdpc->n0-1, ctx->code_spec->qcmdpc->m, 0, 1); - for (i = 0; i < ctx->code_spec->qcmdpc->n0-1; i++) { - BPU_gf2PolyCopy(&G_temp_mat.matrices[i], &G_temp[i]); - } - - ret = 0; - - BPU_printDebug("testing GH^T"); - - // test if G x H^T = 0 - if (BPU_mecsQcmdpcTestGHmatrices(&G_temp_mat, &H_temp_sparse) != 0) { - BPU_printError("generator x parity check matrix ERROR"); - ret = -1; - } - else { - BPU_printDebug("GH^t = 0"); - } - - // transpose G matrix - if (ret == 0) { - BPU_gf2QcMatrixTransp(&ctx->code_spec->qcmdpc->G, &G_temp_mat); - #if defined(DEBUG_L) - BPU_printDebug("transposing G matrix"); +#endif + } + + BPU_printDebug ("creating temp G for GH^T test"); + BPU_gf2QcMatrixMalloc (&G_temp_mat, ctx->code_spec->qcmdpc->n0 - 1, + ctx->code_spec->qcmdpc->m, 0, 1); + for (i = 0; i < ctx->code_spec->qcmdpc->n0 - 1; i++) { + BPU_gf2PolyCopy (&G_temp_mat.matrices[i], &G_temp[i]); + } + + ret = 0; + + BPU_printDebug ("testing GH^T"); + + // test if G x H^T = 0 + if (BPU_mecsQcmdpcTestGHmatrices (&G_temp_mat, &H_temp_sparse) != 0) { + BPU_printError ("generator x parity check matrix ERROR"); + ret = -1; + } + else { + BPU_printDebug ("GH^t = 0"); + } + + // transpose G matrix + if (ret == 0) { + BPU_gf2QcMatrixTransp (&ctx->code_spec->qcmdpc->G, &G_temp_mat); +#if defined(DEBUG_L) + BPU_printDebug ("transposing G matrix"); // BPU_printGf2QcMatrix(&ctx->code_spec->qcmdpc->G); - #endif - } - - // transpose H matrix - if (ret == 0) { - BPU_gf2SparseQcMatrixTransp(&ctx->code_spec->qcmdpc->H, &H_temp_sparse); - #if defined(DEBUG_L) - BPU_printDebug("transposing H matrix"); +#endif + } + + // transpose H matrix + if (ret == 0) { + BPU_gf2SparseQcMatrixTransp (&ctx->code_spec->qcmdpc->H, + &H_temp_sparse); +#if defined(DEBUG_L) + BPU_printDebug ("transposing H matrix"); // BPU_printGf2SparseQcMatrix(&ctx->code_spec->qcmdpc->H); - #endif - } +#endif + } - BPU_printDebug("free and exit"); + BPU_printDebug ("free and exit"); - // free - for (i = 0; i < ctx->code_spec->qcmdpc->n0-1; i++) - BPU_gf2PolyFree(&G_temp[i], 0); + // free + for (i = 0; i < ctx->code_spec->qcmdpc->n0 - 1; i++) + BPU_gf2PolyFree (&G_temp[i], 0); - for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) - BPU_gf2PolyFree(&H_temp[i], 0); + for (i = 0; i < ctx->code_spec->qcmdpc->n0; i++) + BPU_gf2PolyFree (&H_temp[i], 0); - BPU_gf2PolyFree(&H_last_inv, 0); - BPU_gf2PolyFree(&mod, 0); - BPU_gf2QcMatrixFree(&G_temp_mat, 0); - BPU_gf2QcMatrixFree(&H_temp_mat, 0); - BPU_gf2SparseQcMatrixFree(&H_temp_sparse, 0); + BPU_gf2PolyFree (&H_last_inv, 0); + BPU_gf2PolyFree (&mod, 0); + BPU_gf2QcMatrixFree (&G_temp_mat, 0); + BPU_gf2QcMatrixFree (&H_temp_mat, 0); + BPU_gf2SparseQcMatrixFree (&H_temp_sparse, 0); - return ret; + return ret; } -int BPU_mecsQcmdpcTestGHmatrices(const BPU_T_GF2_QC_Matrix *G, const BPU_T_GF2_Sparse_Qc_Matrix *H) { - int i, element, err = 0; - BPU_T_GF2_Poly temp; - BPU_T_GF2_Sparse_Poly row; - BPU_T_Element tmp; - - // get I * H[0] + ... + I * H[n0-2] - BPU_gf2PolyMalloc(&temp, G->n); - for (i = 0; i < G->element_count; i++) { - BPU_gf2SparsePolyAdd(&temp, &H->matrices[i]); - } - - // get other rows - for (element = 0; element < G->element_count; element++) { - for (i = 0; i < G->element_size; i++) { - if (BPU_gf2VecGetBit(&G->matrices[element], i) == 1ul) { - BPU_gf2SparseQcMatrixGetRow(&row, H, i + G->element_size * (G->element_count)); - BPU_gf2SparsePolyAdd(&temp, &row); - BPU_gf2SparsePolyFree(&row, 0); - } +int BPU_mecsQcmdpcTestGHmatrices (const BPU_T_GF2_QC_Matrix * G, + const BPU_T_GF2_Sparse_Qc_Matrix * H) { + int i, element, err = 0; + BPU_T_GF2_Poly temp; + BPU_T_GF2_Sparse_Poly row; + BPU_T_Element tmp; + + // get I * H[0] + ... + I * H[n0-2] + BPU_gf2PolyMalloc (&temp, G->n); + for (i = 0; i < G->element_count; i++) { + BPU_gf2SparsePolyAdd (&temp, &H->matrices[i]); + } + + // get other rows + for (element = 0; element < G->element_count; element++) { + for (i = 0; i < G->element_size; i++) { + if (BPU_gf2VecGetBit (&G->matrices[element], i) == 1ul) { + BPU_gf2SparseQcMatrixGetRow (&row, H, + i + + G->element_size * + (G->element_count)); + BPU_gf2SparsePolyAdd (&temp, &row); + BPU_gf2SparsePolyFree (&row, 0); + } + } } - } - // check result poly - for (i = 0; i < temp.array_length; i++) { - if ((tmp = temp.elements[i]) != 0ul) { - err++; - break; + // check result poly + for (i = 0; i < temp.array_length; i++) { + if ((tmp = temp.elements[i]) != 0ul) { + err++; + break; + } } - } #if defined(DEBUG_L) BPU_T_GF2_QC_Matrix GH_result; - BPU_gf2QcMatrixMalloc(&GH_result, 1, G->element_size, 0, 0); - BPU_gf2PolyCopy(&GH_result.matrices[0], &temp); + + BPU_gf2QcMatrixMalloc (&GH_result, 1, G->element_size, 0, 0); + BPU_gf2PolyCopy (&GH_result.matrices[0], &temp); // BPU_printGf2QcMatrix(&GH_result); - BPU_gf2QcMatrixFree(&GH_result, 0); + BPU_gf2QcMatrixFree (&GH_result, 0); #endif - BPU_gf2PolyFree(&temp, 0); + BPU_gf2PolyFree (&temp, 0); - return err; + return err; } #endif diff --git a/lib/src/bitpunch/code/qcmdpc/qcmdpc.h b/lib/src/bitpunch/code/qcmdpc/qcmdpc.h index da31f1b..8fd911c 100644 --- a/lib/src/bitpunch/code/qcmdpc/qcmdpc.h +++ b/lib/src/bitpunch/code/qcmdpc/qcmdpc.h @@ -30,14 +30,14 @@ DECODE PARAMS ************************************************/ // universal -#define BPU_QCMDPC_PARAM_MAX_ITER 10 ///< maximum count of iterations in decoding +#define BPU_QCMDPC_PARAM_MAX_ITER 10 ///< maximum count of iterations in decoding // decode1 alg -#define BPU_QCMDPC_PARAM_DELTA 5 ///< starting param delta (threshold) for decode1 algorithm +#define BPU_QCMDPC_PARAM_DELTA 5 ///< starting param delta (threshold) for decode1 algorithm // decode2 alg -#define BPU_QCMDPC_PARAM_DELTA_B 0 ///< threshold for decode2 -#define BPU_QCMDPC_MAX_B_VALUES 5 ///< count of precalculated values for decode2 algorithm +#define BPU_QCMDPC_PARAM_DELTA_B 0 ///< threshold for decode2 +#define BPU_QCMDPC_MAX_B_VALUES 5 ///< count of precalculated values for decode2 algorithm #ifdef BPU_CONF_ENCRYPTION /** @@ -47,7 +47,8 @@ DECODE PARAMS * @param ctx QC-MDPC McEliece context * @return 0 if OK, else error */ -int BPU_mecsQcmdpcEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); +int BPU_mecsQcmdpcEncode (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx); #endif #ifdef BPU_CONF_DECRYPTION @@ -58,7 +59,8 @@ int BPU_mecsQcmdpcEncode(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, cons * @param ctx QC-MDPC McEliece context * @return 0 if OK, else error */ -int BPU_mecsQcmdpcDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx); +int BPU_mecsQcmdpcDecrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Code_Ctx *ctx); /** * Decoding algorithm 1 for QC-MDPC codes capable of correct param_t errors. @@ -72,7 +74,9 @@ int BPU_mecsQcmdpcDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, con * @param ctx QC-MDPC McEliece context * @return 0 if OK, else error */ -int BPU_mecsQcmdpcDecode1(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *cipher_text, int delta, const struct _BPU_T_Code_Ctx *ctx); +int BPU_mecsQcmdpcDecode1 (BPU_T_GF2_Vector * error_vec, + const BPU_T_GF2_Vector * cipher_text, int delta, + const struct _BPU_T_Code_Ctx *ctx); /** * Decoding algorithm 2 for QC-MDPC codes capable of correct param_t errors. @@ -85,7 +89,9 @@ int BPU_mecsQcmdpcDecode1(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *c * @param ctx QC-MDPC McEliece context * @return 0 if OK, else error */ -int BPU_mecsQcmdpcDecode2(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *cipher_text, const struct _BPU_T_Code_Ctx *ctx); +int BPU_mecsQcmdpcDecode2 (BPU_T_GF2_Vector * error_vec, + const BPU_T_GF2_Vector * cipher_text, + const struct _BPU_T_Code_Ctx *ctx); /** * Calc syndrom of cipher_text. @@ -94,7 +100,9 @@ int BPU_mecsQcmdpcDecode2(BPU_T_GF2_Vector *error_vec, const BPU_T_GF2_Vector *c * @param ctx QC-MDPC McEliece context * */ -void BPU_mecsQcmdpcCalcSyndrom(BPU_T_GF2_Vector *syndrom, const BPU_T_GF2_Vector *cipher_text, const struct _BPU_T_Code_Ctx *ctx); +void BPU_mecsQcmdpcCalcSyndrom (BPU_T_GF2_Vector * syndrom, + const BPU_T_GF2_Vector * cipher_text, + const struct _BPU_T_Code_Ctx *ctx); #endif #ifdef BPU_CONF_KEY_GEN @@ -104,7 +112,8 @@ void BPU_mecsQcmdpcCalcSyndrom(BPU_T_GF2_Vector *syndrom, const BPU_T_GF2_Vector * @param H parity-check matrix of code * @return 0 if OK, 1 if error */ -int BPU_mecsQcmdpcTestGHmatrices(const BPU_T_GF2_QC_Matrix *G, const BPU_T_GF2_Sparse_Qc_Matrix *H); +int BPU_mecsQcmdpcTestGHmatrices (const BPU_T_GF2_QC_Matrix * G, + const BPU_T_GF2_Sparse_Qc_Matrix * H); /** * Generate key pair of QC-MDPC code for McEliece cryptosystem. @@ -114,7 +123,7 @@ int BPU_mecsQcmdpcTestGHmatrices(const BPU_T_GF2_QC_Matrix *G, const BPU_T_GF2_S * @param ctx QC-MDPC McEliece context * @return 0 if OK, else error */ -int BPU_mecsQcmdpcGenKeys(BPU_T_Code_Ctx *ctx); +int BPU_mecsQcmdpcGenKeys (BPU_T_Code_Ctx * ctx); #endif #endif // BPU_QCMDPC_H diff --git a/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.c b/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.c index 53761fa..d3c7c04 100644 --- a/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.c +++ b/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.c @@ -19,31 +19,33 @@ along with this program. If not, see . #include "qcmdpctypes.h" #include -void BPU_qcmdpcFreeSpec(BPU_T_Qcmdpc_Spec *spec) { - BPU_gf2QcMatrixFree(&spec->G, 0); - BPU_gf2SparseQcMatrixFree(&spec->H, 0); - free(spec); +void BPU_qcmdpcFreeSpec (BPU_T_Qcmdpc_Spec * spec) { + BPU_gf2QcMatrixFree (&spec->G, 0); + BPU_gf2SparseQcMatrixFree (&spec->H, 0); + free (spec); } -int BPU_qcmdpcInitParams(BPU_T_Qcmdpc_Params **params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t) { - *params = (BPU_T_Qcmdpc_Params*) calloc(sizeof(BPU_T_Qcmdpc_Params), 1); +int BPU_qcmdpcInitParams (BPU_T_Qcmdpc_Params ** params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t) { + *params = (BPU_T_Qcmdpc_Params *) calloc (sizeof (BPU_T_Qcmdpc_Params), 1); - if (!params) { - BPU_printError("Can't init Code params"); + if (!params) { + BPU_printError ("Can't init Code params"); - return -1; - } - (*params)->m = m; - (*params)->n0 = n0; - (*params)->w = w; - (*params)->t = t; + return -1; + } + (*params)->m = m; + (*params)->n0 = n0; + (*params)->w = w; + (*params)->t = t; - return 0; + return 0; } -void BPU_qcmdpcFreeParams(BPU_T_Qcmdpc_Params **params) { - if (*params) { - free(*params); - } - *params = NULL; +void BPU_qcmdpcFreeParams (BPU_T_Qcmdpc_Params ** params) { + if (*params) { + free (*params); + } + *params = NULL; } diff --git a/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.h b/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.h index 8c5cf3d..9be554f 100644 --- a/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.h +++ b/lib/src/bitpunch/code/qcmdpc/qcmdpctypes.h @@ -26,28 +26,28 @@ along with this program. If not, see . * QC-MDPC McEliece code matrices */ typedef struct _BPU_T_Qcmdpc_Spec { - BPU_T_GF2_QC_Matrix G; ///< generator matrix - BPU_T_GF2_Sparse_Qc_Matrix H; ///< parity-check matrix - uint16_t m; ///< size of cyclic matrix - uint16_t n0; ///< number of cyclic matrices - uint16_t w; ///< weight of parity-check matrix row -}BPU_T_Qcmdpc_Spec; + BPU_T_GF2_QC_Matrix G; ///< generator matrix + BPU_T_GF2_Sparse_Qc_Matrix H; ///< parity-check matrix + uint16_t m; ///< size of cyclic matrix + uint16_t n0; ///< number of cyclic matrices + uint16_t w; ///< weight of parity-check matrix row +} BPU_T_Qcmdpc_Spec; /** QC-MDPC McEliece input code params */ typedef struct _BPU_T_Qcmdpc_Params { - uint16_t m; ///< size of cyclic matrix - uint16_t n0; ///< number of cyclic matrices - uint16_t w; ///< weight of parity-check matrix row - uint16_t t; ///< count of errors -}BPU_T_Qcmdpc_Params; + uint16_t m; ///< size of cyclic matrix + uint16_t n0; ///< number of cyclic matrices + uint16_t w; ///< weight of parity-check matrix row + uint16_t t; ///< count of errors +} BPU_T_Qcmdpc_Params; /** * Free QC-MDPC McEliece code matrices * @param spec pointer to structure */ -void BPU_qcmdpcFreeSpec(BPU_T_Qcmdpc_Spec *spec); +void BPU_qcmdpcFreeSpec (BPU_T_Qcmdpc_Spec * spec); /** * Allocate memory for QC-MDPC code params. After work you have to free memory using call BPU_qcmdpcFreeParams @@ -58,12 +58,14 @@ void BPU_qcmdpcFreeSpec(BPU_T_Qcmdpc_Spec *spec); * @param t count of errors * @return 0 if OK, else error */ -int BPU_qcmdpcInitParams(BPU_T_Qcmdpc_Params **params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t); +int BPU_qcmdpcInitParams (BPU_T_Qcmdpc_Params ** params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t); /** * Free memory for QC-MDPC code params. * @param params pointer to structure */ -void BPU_qcmdpcFreeParams(BPU_T_Qcmdpc_Params **params); +void BPU_qcmdpcFreeParams (BPU_T_Qcmdpc_Params ** params); #endif // QCMDPCTYPES_H diff --git a/lib/src/bitpunch/config.h b/lib/src/bitpunch/config.h index 4f93676..2adb08b 100644 --- a/lib/src/bitpunch/config.h +++ b/lib/src/bitpunch/config.h @@ -20,43 +20,42 @@ along with this program. If not, see . // constraints #ifdef BPU_CONF_FULL - #ifndef BPU_CONF_ASN1 - #define BPU_CONF_ASN1 - #endif - #ifndef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - #define BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - #endif - #ifndef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC - #define BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC - #endif - #ifndef BPU_CONF_SHA_512 - #define BPU_CONF_SHA_512 - #endif - #ifndef BPU_CONF_FULL_MECS - #define BPU_CONF_FULL_MECS - #endif - #if !defined(BPU_CONF_NO_PRINT) && !defined(BPU_CONF_PRINT) - #define BPU_CONF_PRINT - #endif +#ifndef BPU_CONF_ASN1 +#define BPU_CONF_ASN1 +#endif +#ifndef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA +#define BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA +#endif +#ifndef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC +#define BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC +#endif +#ifndef BPU_CONF_SHA_512 +#define BPU_CONF_SHA_512 +#endif +#ifndef BPU_CONF_FULL_MECS +#define BPU_CONF_FULL_MECS +#endif +#if !defined(BPU_CONF_NO_PRINT) && !defined(BPU_CONF_PRINT) +#define BPU_CONF_PRINT +#endif #endif #ifdef BPU_CONF_FULL_MECS - #ifndef BPU_CONF_ENCRYPTION - #define BPU_CONF_ENCRYPTION - #endif - #ifndef BPU_CONF_DECRYPTION - #define BPU_CONF_DECRYPTION - #endif - #ifndef BPU_CONF_KEY_GEN - #define BPU_CONF_KEY_GEN - #endif +#ifndef BPU_CONF_ENCRYPTION +#define BPU_CONF_ENCRYPTION +#endif +#ifndef BPU_CONF_DECRYPTION +#define BPU_CONF_DECRYPTION +#endif +#ifndef BPU_CONF_KEY_GEN +#define BPU_CONF_KEY_GEN +#endif #endif #if defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA) || defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC) - #ifndef BPU_CONF_SHA_512 - #define BPU_CONF_SHA_512 - #endif +#ifndef BPU_CONF_SHA_512 +#define BPU_CONF_SHA_512 +#endif #endif #endif // CONFIG_H - diff --git a/lib/src/bitpunch/crypto/cca2/mecspointcheval.c b/lib/src/bitpunch/crypto/cca2/mecspointcheval.c index 50215e3..a8df0e6 100644 --- a/lib/src/bitpunch/crypto/cca2/mecspointcheval.c +++ b/lib/src/bitpunch/crypto/cca2/mecspointcheval.c @@ -23,112 +23,115 @@ along with this program. If not, see . #include #ifdef BPU_CONF_ENCRYPTION -int BPU_mecsPointchevalCCA2Encrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx) { +int BPU_mecsPointchevalCCA2Encrypt (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx) { BPU_T_GF2_Vector *r1, *r2, *cca2_pt, *hash, *hash_in, *enc_pt, *tmp; - int rc = 0; - - // Generate a random (k − l)-bit vector r1 and a random l-bit vector r2 - BPU_gf2VecMalloc(&r2, ctx->pt_len); - BPU_gf2VecMalloc(&r1, ctx->code_ctx->msg_len - ctx->pt_len); - BPU_gf2VecRand(r1, 0); - BPU_gf2VecRand(r2, 0); - - BPU_gf2VecMalloc(&hash, ctx->pt_len); - BPU_gf2VecMalloc(&hash_in, 2 * ctx->pt_len); - // Create CCA2-safe plaintext m= r1 ∥ hash (m ∥ r2 ) - BPU_gf2VecConcat(hash_in, in, r2); - - BPU_gf2VecHash(hash, hash_in); - BPU_gf2VecFree(&hash_in); - - BPU_gf2VecMalloc(&cca2_pt, r1->len + hash->len); - BPU_gf2VecConcat(cca2_pt, r1, hash); - - BPU_gf2VecMalloc(&enc_pt, ctx->code_ctx->code_len); - - // encrypt with basic MECS - if (BPU_mecsBasicEncrypt(enc_pt, cca2_pt, ctx)) { - return -1; - } - BPU_gf2VecFree(&cca2_pt); - - // add CCA2-safe data extension z = (z′ ⊕ e) ∥ (hash (r1) ⊕ m) ∥ (hash (e) ⊕ r2 ) - BPU_gf2VecMalloc(&tmp, enc_pt->len + hash->len); - BPU_gf2VecHash(hash, r1); - BPU_gf2VecFree(&r1); - BPU_gf2VecXor(hash, in); - BPU_gf2VecConcat(tmp, enc_pt, hash); - BPU_gf2VecFree(&enc_pt); - - BPU_gf2VecHash(hash, ctx->code_ctx->e); - BPU_gf2VecXor(hash, r2); - BPU_gf2VecFree(&r2); - BPU_gf2VecConcat(out, tmp, hash); - - BPU_gf2VecFree(&hash); - BPU_gf2VecFree(&tmp); - - return rc; + int rc = 0; + + // Generate a random (k − l)-bit vector r1 and a random l-bit vector r2 + BPU_gf2VecMalloc (&r2, ctx->pt_len); + BPU_gf2VecMalloc (&r1, ctx->code_ctx->msg_len - ctx->pt_len); + BPU_gf2VecRand (r1, 0); + BPU_gf2VecRand (r2, 0); + + BPU_gf2VecMalloc (&hash, ctx->pt_len); + BPU_gf2VecMalloc (&hash_in, 2 * ctx->pt_len); + // Create CCA2-safe plaintext m= r1 ∥ hash (m ∥ r2 ) + BPU_gf2VecConcat (hash_in, in, r2); + + BPU_gf2VecHash (hash, hash_in); + BPU_gf2VecFree (&hash_in); + + BPU_gf2VecMalloc (&cca2_pt, r1->len + hash->len); + BPU_gf2VecConcat (cca2_pt, r1, hash); + + BPU_gf2VecMalloc (&enc_pt, ctx->code_ctx->code_len); + + // encrypt with basic MECS + if (BPU_mecsBasicEncrypt (enc_pt, cca2_pt, ctx)) { + return -1; + } + BPU_gf2VecFree (&cca2_pt); + + // add CCA2-safe data extension z = (z′ ⊕ e) ∥ (hash (r1) ⊕ m) ∥ (hash (e) ⊕ r2 ) + BPU_gf2VecMalloc (&tmp, enc_pt->len + hash->len); + BPU_gf2VecHash (hash, r1); + BPU_gf2VecFree (&r1); + BPU_gf2VecXor (hash, in); + BPU_gf2VecConcat (tmp, enc_pt, hash); + BPU_gf2VecFree (&enc_pt); + + BPU_gf2VecHash (hash, ctx->code_ctx->e); + BPU_gf2VecXor (hash, r2); + BPU_gf2VecFree (&r2); + BPU_gf2VecConcat (out, tmp, hash); + + BPU_gf2VecFree (&hash); + BPU_gf2VecFree (&tmp); + + return rc; } #endif // BPU_CONF_ENCRYPTION #ifdef BPU_CONF_DECRYPTION -int BPU_mecsPointchevalCCA2Decrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx) { - BPU_T_GF2_Vector *z1, *z3; // n, l, l-bit +int BPU_mecsPointchevalCCA2Decrypt (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx) { + BPU_T_GF2_Vector *z1, *z3; // n, l, l-bit BPU_T_GF2_Vector *tmp_2, *pt_cca2; - BPU_T_GF2_Vector *r; // k - l + BPU_T_GF2_Vector *r; // k - l BPU_T_GF2_Vector *h, *h_tmp; - int rc = 0; - - // split ct in z1, z2, z3 - BPU_gf2VecMalloc(&z1, ctx->code_ctx->code_len); - // z2 is like out - BPU_gf2VecMalloc(&z3, ctx->pt_len); - - // Split z to ( z1 , z2 , z3 ) - BPU_gf2VecCrop(z1, in, 0, z1->len); - BPU_gf2VecCrop(out, in, z1->len, out->len); - BPU_gf2VecCrop(z3, in, z1->len + out->len, z3->len); - - BPU_gf2VecMalloc(&pt_cca2, ctx->code_ctx->msg_len); - // decrypt z1 using basic mecs Reconstruct the CCA2-safe plaintext m′ = z1 ⊕ e - if (BPU_mecsBasicDecrypt(pt_cca2, z1, ctx)) { - return -1; - } - BPU_gf2VecFree(&z1); - - BPU_gf2VecMalloc(&r, ctx->code_ctx->msg_len - ctx->pt_len); - BPU_gf2VecMalloc(&h, ctx->pt_len); - BPU_gf2VecCrop(r, pt_cca2, 0, r->len); - BPU_gf2VecCrop(h, pt_cca2, r->len, h->len); - BPU_gf2VecFree(&pt_cca2); - - BPU_gf2VecMalloc(&h_tmp, ctx->pt_len); - // Reconstruct plaintext candidate m = z2 ⊕ hash (r) - BPU_gf2VecHash(h_tmp, r); - BPU_gf2VecFree(&r); - BPU_gf2VecXor(out, h_tmp); - - // Determine check value h′ = hash (m ∥ hash (e) ⊕ z3 ). - BPU_gf2VecHash(h_tmp, ctx->code_ctx->e); - BPU_gf2VecXor(h_tmp, z3); - BPU_gf2VecFree(&z3); - - BPU_gf2VecMalloc(&tmp_2, ctx->pt_len * 2); - BPU_gf2VecConcat(tmp_2, out, h_tmp); - BPU_gf2VecHash(h_tmp, tmp_2); - BPU_gf2VecFree(&tmp_2); - - if (BPU_gf2VecCmp(h, h_tmp)) { - BPU_printError("Wrong check value."); - - rc = -1; - } - BPU_gf2VecFree(&h); - BPU_gf2VecFree(&h_tmp); - return rc; + int rc = 0; + + // split ct in z1, z2, z3 + BPU_gf2VecMalloc (&z1, ctx->code_ctx->code_len); + // z2 is like out + BPU_gf2VecMalloc (&z3, ctx->pt_len); + + // Split z to ( z1 , z2 , z3 ) + BPU_gf2VecCrop (z1, in, 0, z1->len); + BPU_gf2VecCrop (out, in, z1->len, out->len); + BPU_gf2VecCrop (z3, in, z1->len + out->len, z3->len); + + BPU_gf2VecMalloc (&pt_cca2, ctx->code_ctx->msg_len); + // decrypt z1 using basic mecs Reconstruct the CCA2-safe plaintext m′ = z1 ⊕ e + if (BPU_mecsBasicDecrypt (pt_cca2, z1, ctx)) { + return -1; + } + BPU_gf2VecFree (&z1); + + BPU_gf2VecMalloc (&r, ctx->code_ctx->msg_len - ctx->pt_len); + BPU_gf2VecMalloc (&h, ctx->pt_len); + BPU_gf2VecCrop (r, pt_cca2, 0, r->len); + BPU_gf2VecCrop (h, pt_cca2, r->len, h->len); + BPU_gf2VecFree (&pt_cca2); + + BPU_gf2VecMalloc (&h_tmp, ctx->pt_len); + // Reconstruct plaintext candidate m = z2 ⊕ hash (r) + BPU_gf2VecHash (h_tmp, r); + BPU_gf2VecFree (&r); + BPU_gf2VecXor (out, h_tmp); + + // Determine check value h′ = hash (m ∥ hash (e) ⊕ z3 ). + BPU_gf2VecHash (h_tmp, ctx->code_ctx->e); + BPU_gf2VecXor (h_tmp, z3); + BPU_gf2VecFree (&z3); + + BPU_gf2VecMalloc (&tmp_2, ctx->pt_len * 2); + BPU_gf2VecConcat (tmp_2, out, h_tmp); + BPU_gf2VecHash (h_tmp, tmp_2); + BPU_gf2VecFree (&tmp_2); + + if (BPU_gf2VecCmp (h, h_tmp)) { + BPU_printError ("Wrong check value."); + + rc = -1; + } + BPU_gf2VecFree (&h); + BPU_gf2VecFree (&h_tmp); + return rc; } #endif // BPU_CONF_DECRYPTION #endif // BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - diff --git a/lib/src/bitpunch/crypto/cca2/mecspointcheval.h b/lib/src/bitpunch/crypto/cca2/mecspointcheval.h index 220d435..a8135fe 100644 --- a/lib/src/bitpunch/crypto/cca2/mecspointcheval.h +++ b/lib/src/bitpunch/crypto/cca2/mecspointcheval.h @@ -35,7 +35,9 @@ along with this program. If not, see . * @param ctx * @return */ -int BPU_mecsPointchevalCCA2Encrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsPointchevalCCA2Encrypt (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx); #endif #ifdef BPU_CONF_DECRYPTION @@ -46,9 +48,10 @@ int BPU_mecsPointchevalCCA2Encrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector * @param ctx * @return */ -int BPU_mecsPointchevalCCA2Decrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsPointchevalCCA2Decrypt (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx); #endif #endif // BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA #endif // MECSPOINTCHEVAL_H - diff --git a/lib/src/bitpunch/crypto/hash/polarssl/polarssl/sha512.h b/lib/src/bitpunch/crypto/hash/polarssl/polarssl/sha512.h index d6a665f..4d564f9 100644 --- a/lib/src/bitpunch/crypto/hash/polarssl/polarssl/sha512.h +++ b/lib/src/bitpunch/crypto/hash/polarssl/polarssl/sha512.h @@ -28,11 +28,11 @@ #include #if defined(_MSC_VER) || defined(__WATCOMC__) - #define UL64(x) x##ui64 - typedef unsigned __int64 uint64_t; +#define UL64(x) x##ui64 +typedef unsigned __int64 uint64_t; #else - #include - #define UL64(x) x##ULL +#include +#define UL64(x) x##ULL #endif #define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */ @@ -48,31 +48,29 @@ extern "C" { /** * \brief SHA-512 context structure */ -typedef struct -{ - uint64_t total[2]; /*!< number of bytes processed */ - uint64_t state[8]; /*!< intermediate digest state */ - unsigned char buffer[128]; /*!< data block being processed */ - - unsigned char ipad[128]; /*!< HMAC: inner padding */ - unsigned char opad[128]; /*!< HMAC: outer padding */ - int is384; /*!< 0 => SHA-512, else SHA-384 */ -} -sha512_context; + typedef struct { + uint64_t total[2]; /*!< number of bytes processed */ + uint64_t state[8]; /*!< intermediate digest state */ + unsigned char buffer[128]; /*!< data block being processed */ + + unsigned char ipad[128]; /*!< HMAC: inner padding */ + unsigned char opad[128]; /*!< HMAC: outer padding */ + int is384; /*!< 0 => SHA-512, else SHA-384 */ + } sha512_context; /** * \brief Initialize SHA-512 context * * \param ctx SHA-512 context to be initialized */ -void sha512_init( sha512_context *ctx ); + void sha512_init (sha512_context * ctx); /** * \brief Clear SHA-512 context * * \param ctx SHA-512 context to be cleared */ -void sha512_free( sha512_context *ctx ); + void sha512_free (sha512_context * ctx); /** * \brief SHA-512 context setup @@ -80,7 +78,7 @@ void sha512_free( sha512_context *ctx ); * \param ctx context to be initialized * \param is384 0 = use SHA512, 1 = use SHA384 */ -void sha512_starts( sha512_context *ctx, int is384 ); + void sha512_starts (sha512_context * ctx, int is384); /** * \brief SHA-512 process buffer @@ -89,8 +87,8 @@ void sha512_starts( sha512_context *ctx, int is384 ); * \param input buffer holding the data * \param ilen length of the input data */ -void sha512_update( sha512_context *ctx, const unsigned char *input, - size_t ilen ); + void sha512_update (sha512_context * ctx, const unsigned char *input, + size_t ilen); /** * \brief SHA-512 final digest @@ -98,13 +96,12 @@ void sha512_update( sha512_context *ctx, const unsigned char *input, * \param ctx SHA-512 context * \param output SHA-384/512 checksum result */ -void sha512_finish( sha512_context *ctx, unsigned char output[64] ); + void sha512_finish (sha512_context * ctx, unsigned char output[64]); #ifdef __cplusplus } #endif - -#else /* POLARSSL_SHA512_ALT */ +#else /* POLARSSL_SHA512_ALT */ #include "sha512_alt.h" #endif /* POLARSSL_SHA512_ALT */ @@ -120,8 +117,8 @@ extern "C" { * \param output SHA-384/512 checksum result * \param is384 0 = use SHA512, 1 = use SHA384 */ -void sha512( const unsigned char *input, size_t ilen, - unsigned char output[64], int is384 ); + void sha512 (const unsigned char *input, size_t ilen, + unsigned char output[64], int is384); /** * \brief Output = SHA-512( file contents ) @@ -132,7 +129,7 @@ void sha512( const unsigned char *input, size_t ilen, * * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR */ -int sha512_file( const char *path, unsigned char output[64], int is384 ); + int sha512_file (const char *path, unsigned char output[64], int is384); #ifdef POLARSSL_SHA512_HMAC /** @@ -143,8 +140,8 @@ int sha512_file( const char *path, unsigned char output[64], int is384 ); * \param key HMAC secret key * \param keylen length of the HMAC key */ -void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key, - size_t keylen, int is384 ); + void sha512_hmac_starts (sha512_context * ctx, const unsigned char *key, + size_t keylen, int is384); /** * \brief SHA-512 HMAC process buffer @@ -153,8 +150,8 @@ void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key, * \param input buffer holding the data * \param ilen length of the input data */ -void sha512_hmac_update( sha512_context *ctx, const unsigned char *input, - size_t ilen ); + void sha512_hmac_update (sha512_context * ctx, const unsigned char *input, + size_t ilen); /** * \brief SHA-512 HMAC final digest @@ -162,14 +159,14 @@ void sha512_hmac_update( sha512_context *ctx, const unsigned char *input, * \param ctx HMAC context * \param output SHA-384/512 HMAC checksum result */ -void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] ); + void sha512_hmac_finish (sha512_context * ctx, unsigned char output[64]); /** * \brief SHA-512 HMAC context reset * * \param ctx HMAC context to be reset */ -void sha512_hmac_reset( sha512_context *ctx ); + void sha512_hmac_reset (sha512_context * ctx); /** * \brief Output = HMAC-SHA-512( hmac key, input buffer ) @@ -181,10 +178,10 @@ void sha512_hmac_reset( sha512_context *ctx ); * \param output HMAC-SHA-384/512 result * \param is384 0 = use SHA512, 1 = use SHA384 */ -void sha512_hmac( const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char output[64], int is384 ); -#endif // POLARSSL_SHA512_HMAC + void sha512_hmac (const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char output[64], int is384); +#endif // POLARSSL_SHA512_HMAC #ifdef POLLARSSL_SHA512_TEST /** @@ -192,13 +189,12 @@ void sha512_hmac( const unsigned char *key, size_t keylen, * * \return 0 if successful, or 1 if the test failed */ -int sha512_self_test( int verbose ); + int sha512_self_test (int verbose); #endif /* Internal use */ -void sha512_process( sha512_context *ctx, const unsigned char data[128] ); + void sha512_process (sha512_context * ctx, const unsigned char data[128]); #ifdef __cplusplus } #endif - -#endif /* sha512.h */ +#endif /* sha512.h */ diff --git a/lib/src/bitpunch/crypto/hash/polarssl/sha512.c b/lib/src/bitpunch/crypto/hash/polarssl/sha512.c index 9e9aca1..c095820 100644 --- a/lib/src/bitpunch/crypto/hash/polarssl/sha512.c +++ b/lib/src/bitpunch/crypto/hash/polarssl/sha512.c @@ -38,8 +38,11 @@ #define polarssl_printf printf /* Implementation that should never be optimized out by the compiler */ -static void polarssl_zeroize( void *v, size_t n ) { - volatile unsigned char *p = v; while( n-- ) *p++ = 0; +static void polarssl_zeroize (void *v, size_t n) { + volatile unsigned char *p = v; + + while (n--) + *p++ = 0; } #if !defined(POLARSSL_SHA512_ALT) @@ -78,101 +81,94 @@ static void polarssl_zeroize( void *v, size_t n ) { /* * Round constants */ -static const uint64_t K[80] = -{ - UL64(0x428A2F98D728AE22), UL64(0x7137449123EF65CD), - UL64(0xB5C0FBCFEC4D3B2F), UL64(0xE9B5DBA58189DBBC), - UL64(0x3956C25BF348B538), UL64(0x59F111F1B605D019), - UL64(0x923F82A4AF194F9B), UL64(0xAB1C5ED5DA6D8118), - UL64(0xD807AA98A3030242), UL64(0x12835B0145706FBE), - UL64(0x243185BE4EE4B28C), UL64(0x550C7DC3D5FFB4E2), - UL64(0x72BE5D74F27B896F), UL64(0x80DEB1FE3B1696B1), - UL64(0x9BDC06A725C71235), UL64(0xC19BF174CF692694), - UL64(0xE49B69C19EF14AD2), UL64(0xEFBE4786384F25E3), - UL64(0x0FC19DC68B8CD5B5), UL64(0x240CA1CC77AC9C65), - UL64(0x2DE92C6F592B0275), UL64(0x4A7484AA6EA6E483), - UL64(0x5CB0A9DCBD41FBD4), UL64(0x76F988DA831153B5), - UL64(0x983E5152EE66DFAB), UL64(0xA831C66D2DB43210), - UL64(0xB00327C898FB213F), UL64(0xBF597FC7BEEF0EE4), - UL64(0xC6E00BF33DA88FC2), UL64(0xD5A79147930AA725), - UL64(0x06CA6351E003826F), UL64(0x142929670A0E6E70), - UL64(0x27B70A8546D22FFC), UL64(0x2E1B21385C26C926), - UL64(0x4D2C6DFC5AC42AED), UL64(0x53380D139D95B3DF), - UL64(0x650A73548BAF63DE), UL64(0x766A0ABB3C77B2A8), - UL64(0x81C2C92E47EDAEE6), UL64(0x92722C851482353B), - UL64(0xA2BFE8A14CF10364), UL64(0xA81A664BBC423001), - UL64(0xC24B8B70D0F89791), UL64(0xC76C51A30654BE30), - UL64(0xD192E819D6EF5218), UL64(0xD69906245565A910), - UL64(0xF40E35855771202A), UL64(0x106AA07032BBD1B8), - UL64(0x19A4C116B8D2D0C8), UL64(0x1E376C085141AB53), - UL64(0x2748774CDF8EEB99), UL64(0x34B0BCB5E19B48A8), - UL64(0x391C0CB3C5C95A63), UL64(0x4ED8AA4AE3418ACB), - UL64(0x5B9CCA4F7763E373), UL64(0x682E6FF3D6B2B8A3), - UL64(0x748F82EE5DEFB2FC), UL64(0x78A5636F43172F60), - UL64(0x84C87814A1F0AB72), UL64(0x8CC702081A6439EC), - UL64(0x90BEFFFA23631E28), UL64(0xA4506CEBDE82BDE9), - UL64(0xBEF9A3F7B2C67915), UL64(0xC67178F2E372532B), - UL64(0xCA273ECEEA26619C), UL64(0xD186B8C721C0C207), - UL64(0xEADA7DD6CDE0EB1E), UL64(0xF57D4F7FEE6ED178), - UL64(0x06F067AA72176FBA), UL64(0x0A637DC5A2C898A6), - UL64(0x113F9804BEF90DAE), UL64(0x1B710B35131C471B), - UL64(0x28DB77F523047D84), UL64(0x32CAAB7B40C72493), - UL64(0x3C9EBE0A15C9BEBC), UL64(0x431D67C49C100D4C), - UL64(0x4CC5D4BECB3E42B6), UL64(0x597F299CFC657E2A), - UL64(0x5FCB6FAB3AD6FAEC), UL64(0x6C44198C4A475817) +static const uint64_t K[80] = { + UL64 (0x428A2F98D728AE22), UL64 (0x7137449123EF65CD), + UL64 (0xB5C0FBCFEC4D3B2F), UL64 (0xE9B5DBA58189DBBC), + UL64 (0x3956C25BF348B538), UL64 (0x59F111F1B605D019), + UL64 (0x923F82A4AF194F9B), UL64 (0xAB1C5ED5DA6D8118), + UL64 (0xD807AA98A3030242), UL64 (0x12835B0145706FBE), + UL64 (0x243185BE4EE4B28C), UL64 (0x550C7DC3D5FFB4E2), + UL64 (0x72BE5D74F27B896F), UL64 (0x80DEB1FE3B1696B1), + UL64 (0x9BDC06A725C71235), UL64 (0xC19BF174CF692694), + UL64 (0xE49B69C19EF14AD2), UL64 (0xEFBE4786384F25E3), + UL64 (0x0FC19DC68B8CD5B5), UL64 (0x240CA1CC77AC9C65), + UL64 (0x2DE92C6F592B0275), UL64 (0x4A7484AA6EA6E483), + UL64 (0x5CB0A9DCBD41FBD4), UL64 (0x76F988DA831153B5), + UL64 (0x983E5152EE66DFAB), UL64 (0xA831C66D2DB43210), + UL64 (0xB00327C898FB213F), UL64 (0xBF597FC7BEEF0EE4), + UL64 (0xC6E00BF33DA88FC2), UL64 (0xD5A79147930AA725), + UL64 (0x06CA6351E003826F), UL64 (0x142929670A0E6E70), + UL64 (0x27B70A8546D22FFC), UL64 (0x2E1B21385C26C926), + UL64 (0x4D2C6DFC5AC42AED), UL64 (0x53380D139D95B3DF), + UL64 (0x650A73548BAF63DE), UL64 (0x766A0ABB3C77B2A8), + UL64 (0x81C2C92E47EDAEE6), UL64 (0x92722C851482353B), + UL64 (0xA2BFE8A14CF10364), UL64 (0xA81A664BBC423001), + UL64 (0xC24B8B70D0F89791), UL64 (0xC76C51A30654BE30), + UL64 (0xD192E819D6EF5218), UL64 (0xD69906245565A910), + UL64 (0xF40E35855771202A), UL64 (0x106AA07032BBD1B8), + UL64 (0x19A4C116B8D2D0C8), UL64 (0x1E376C085141AB53), + UL64 (0x2748774CDF8EEB99), UL64 (0x34B0BCB5E19B48A8), + UL64 (0x391C0CB3C5C95A63), UL64 (0x4ED8AA4AE3418ACB), + UL64 (0x5B9CCA4F7763E373), UL64 (0x682E6FF3D6B2B8A3), + UL64 (0x748F82EE5DEFB2FC), UL64 (0x78A5636F43172F60), + UL64 (0x84C87814A1F0AB72), UL64 (0x8CC702081A6439EC), + UL64 (0x90BEFFFA23631E28), UL64 (0xA4506CEBDE82BDE9), + UL64 (0xBEF9A3F7B2C67915), UL64 (0xC67178F2E372532B), + UL64 (0xCA273ECEEA26619C), UL64 (0xD186B8C721C0C207), + UL64 (0xEADA7DD6CDE0EB1E), UL64 (0xF57D4F7FEE6ED178), + UL64 (0x06F067AA72176FBA), UL64 (0x0A637DC5A2C898A6), + UL64 (0x113F9804BEF90DAE), UL64 (0x1B710B35131C471B), + UL64 (0x28DB77F523047D84), UL64 (0x32CAAB7B40C72493), + UL64 (0x3C9EBE0A15C9BEBC), UL64 (0x431D67C49C100D4C), + UL64 (0x4CC5D4BECB3E42B6), UL64 (0x597F299CFC657E2A), + UL64 (0x5FCB6FAB3AD6FAEC), UL64 (0x6C44198C4A475817) }; -void sha512_init( sha512_context *ctx ) -{ - memset( ctx, 0, sizeof( sha512_context ) ); +void sha512_init (sha512_context * ctx) { + memset (ctx, 0, sizeof (sha512_context)); } -void sha512_free( sha512_context *ctx ) -{ - if( ctx == NULL ) +void sha512_free (sha512_context * ctx) { + if (ctx == NULL) return; - polarssl_zeroize( ctx, sizeof( sha512_context ) ); + polarssl_zeroize (ctx, sizeof (sha512_context)); } /* * SHA-512 context setup */ -void sha512_starts( sha512_context *ctx, int is384 ) -{ +void sha512_starts (sha512_context * ctx, int is384) { ctx->total[0] = 0; ctx->total[1] = 0; - if( is384 == 0 ) - { + if (is384 == 0) { /* SHA-512 */ - ctx->state[0] = UL64(0x6A09E667F3BCC908); - ctx->state[1] = UL64(0xBB67AE8584CAA73B); - ctx->state[2] = UL64(0x3C6EF372FE94F82B); - ctx->state[3] = UL64(0xA54FF53A5F1D36F1); - ctx->state[4] = UL64(0x510E527FADE682D1); - ctx->state[5] = UL64(0x9B05688C2B3E6C1F); - ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); - ctx->state[7] = UL64(0x5BE0CD19137E2179); + ctx->state[0] = UL64 (0x6A09E667F3BCC908); + ctx->state[1] = UL64 (0xBB67AE8584CAA73B); + ctx->state[2] = UL64 (0x3C6EF372FE94F82B); + ctx->state[3] = UL64 (0xA54FF53A5F1D36F1); + ctx->state[4] = UL64 (0x510E527FADE682D1); + ctx->state[5] = UL64 (0x9B05688C2B3E6C1F); + ctx->state[6] = UL64 (0x1F83D9ABFB41BD6B); + ctx->state[7] = UL64 (0x5BE0CD19137E2179); } - else - { + else { /* SHA-384 */ - ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); - ctx->state[1] = UL64(0x629A292A367CD507); - ctx->state[2] = UL64(0x9159015A3070DD17); - ctx->state[3] = UL64(0x152FECD8F70E5939); - ctx->state[4] = UL64(0x67332667FFC00B31); - ctx->state[5] = UL64(0x8EB44A8768581511); - ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); - ctx->state[7] = UL64(0x47B5481DBEFA4FA4); + ctx->state[0] = UL64 (0xCBBB9D5DC1059ED8); + ctx->state[1] = UL64 (0x629A292A367CD507); + ctx->state[2] = UL64 (0x9159015A3070DD17); + ctx->state[3] = UL64 (0x152FECD8F70E5939); + ctx->state[4] = UL64 (0x67332667FFC00B31); + ctx->state[5] = UL64 (0x8EB44A8768581511); + ctx->state[6] = UL64 (0xDB0C2E0D64F98FA7); + ctx->state[7] = UL64 (0x47B5481DBEFA4FA4); } ctx->is384 = is384; } -void sha512_process( sha512_context *ctx, const unsigned char data[128] ) -{ +void sha512_process (sha512_context * ctx, const unsigned char data[128]) { int i; uint64_t temp1, temp2, W[80]; uint64_t A, B, C, D, E, F, G, H; @@ -196,15 +192,12 @@ void sha512_process( sha512_context *ctx, const unsigned char data[128] ) d += temp1; h = temp1 + temp2; \ } - for( i = 0; i < 16; i++ ) - { - GET_UINT64_BE( W[i], data, i << 3 ); + for (i = 0; i < 16; i++) { + GET_UINT64_BE (W[i], data, i << 3); } - for( ; i < 80; i++ ) - { - W[i] = S1(W[i - 2]) + W[i - 7] + - S0(W[i - 15]) + W[i - 16]; + for (; i < 80; i++) { + W[i] = S1 (W[i - 2]) + W[i - 7] + S0 (W[i - 15]) + W[i - 16]; } A = ctx->state[0]; @@ -217,18 +210,25 @@ void sha512_process( sha512_context *ctx, const unsigned char data[128] ) H = ctx->state[7]; i = 0; - do - { - P( A, B, C, D, E, F, G, H, W[i], K[i] ); i++; - P( H, A, B, C, D, E, F, G, W[i], K[i] ); i++; - P( G, H, A, B, C, D, E, F, W[i], K[i] ); i++; - P( F, G, H, A, B, C, D, E, W[i], K[i] ); i++; - P( E, F, G, H, A, B, C, D, W[i], K[i] ); i++; - P( D, E, F, G, H, A, B, C, W[i], K[i] ); i++; - P( C, D, E, F, G, H, A, B, W[i], K[i] ); i++; - P( B, C, D, E, F, G, H, A, W[i], K[i] ); i++; + do { + P (A, B, C, D, E, F, G, H, W[i], K[i]); + i++; + P (H, A, B, C, D, E, F, G, W[i], K[i]); + i++; + P (G, H, A, B, C, D, E, F, W[i], K[i]); + i++; + P (F, G, H, A, B, C, D, E, W[i], K[i]); + i++; + P (E, F, G, H, A, B, C, D, W[i], K[i]); + i++; + P (D, E, F, G, H, A, B, C, W[i], K[i]); + i++; + P (C, D, E, F, G, H, A, B, W[i], K[i]); + i++; + P (B, C, D, E, F, G, H, A, W[i], K[i]); + i++; } - while( i < 80 ); + while (i < 80); ctx->state[0] += A; ctx->state[1] += B; @@ -243,13 +243,12 @@ void sha512_process( sha512_context *ctx, const unsigned char data[128] ) /* * SHA-512 process buffer */ -void sha512_update( sha512_context *ctx, const unsigned char *input, - size_t ilen ) -{ +void sha512_update (sha512_context * ctx, const unsigned char *input, + size_t ilen) { size_t fill; unsigned int left; - if( ilen == 0 ) + if (ilen == 0) return; left = (unsigned int) (ctx->total[0] & 0x7F); @@ -257,32 +256,29 @@ void sha512_update( sha512_context *ctx, const unsigned char *input, ctx->total[0] += (uint64_t) ilen; - if( ctx->total[0] < (uint64_t) ilen ) + if (ctx->total[0] < (uint64_t) ilen) ctx->total[1]++; - if( left && ilen >= fill ) - { - memcpy( (void *) (ctx->buffer + left), input, fill ); - sha512_process( ctx, ctx->buffer ); + if (left && ilen >= fill) { + memcpy ((void *) (ctx->buffer + left), input, fill); + sha512_process (ctx, ctx->buffer); input += fill; - ilen -= fill; + ilen -= fill; left = 0; } - while( ilen >= 128 ) - { - sha512_process( ctx, input ); + while (ilen >= 128) { + sha512_process (ctx, input); input += 128; - ilen -= 128; + ilen -= 128; } - if( ilen > 0 ) - memcpy( (void *) (ctx->buffer + left), input, ilen ); + if (ilen > 0) + memcpy ((void *) (ctx->buffer + left), input, ilen); } -static const unsigned char sha512_padding[128] = -{ - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +static const unsigned char sha512_padding[128] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -295,36 +291,34 @@ static const unsigned char sha512_padding[128] = /* * SHA-512 final digest */ -void sha512_finish( sha512_context *ctx, unsigned char output[64] ) -{ +void sha512_finish (sha512_context * ctx, unsigned char output[64]) { size_t last, padn; uint64_t high, low; unsigned char msglen[16]; - high = ( ctx->total[0] >> 61 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + high = (ctx->total[0] >> 61) + | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); - PUT_UINT64_BE( high, msglen, 0 ); - PUT_UINT64_BE( low, msglen, 8 ); + PUT_UINT64_BE (high, msglen, 0); + PUT_UINT64_BE (low, msglen, 8); - last = (size_t)( ctx->total[0] & 0x7F ); - padn = ( last < 112 ) ? ( 112 - last ) : ( 240 - last ); + last = (size_t) (ctx->total[0] & 0x7F); + padn = (last < 112) ? (112 - last) : (240 - last); - sha512_update( ctx, sha512_padding, padn ); - sha512_update( ctx, msglen, 16 ); + sha512_update (ctx, sha512_padding, padn); + sha512_update (ctx, msglen, 16); - PUT_UINT64_BE( ctx->state[0], output, 0 ); - PUT_UINT64_BE( ctx->state[1], output, 8 ); - PUT_UINT64_BE( ctx->state[2], output, 16 ); - PUT_UINT64_BE( ctx->state[3], output, 24 ); - PUT_UINT64_BE( ctx->state[4], output, 32 ); - PUT_UINT64_BE( ctx->state[5], output, 40 ); + PUT_UINT64_BE (ctx->state[0], output, 0); + PUT_UINT64_BE (ctx->state[1], output, 8); + PUT_UINT64_BE (ctx->state[2], output, 16); + PUT_UINT64_BE (ctx->state[3], output, 24); + PUT_UINT64_BE (ctx->state[4], output, 32); + PUT_UINT64_BE (ctx->state[5], output, 40); - if( ctx->is384 == 0 ) - { - PUT_UINT64_BE( ctx->state[6], output, 48 ); - PUT_UINT64_BE( ctx->state[7], output, 56 ); + if (ctx->is384 == 0) { + PUT_UINT64_BE (ctx->state[6], output, 48); + PUT_UINT64_BE (ctx->state[7], output, 56); } } @@ -333,49 +327,46 @@ void sha512_finish( sha512_context *ctx, unsigned char output[64] ) /* * output = SHA-512( input buffer ) */ -void sha512( const unsigned char *input, size_t ilen, - unsigned char output[64], int is384 ) -{ +void sha512 (const unsigned char *input, size_t ilen, + unsigned char output[64], int is384) { sha512_context ctx; - sha512_init( &ctx ); - sha512_starts( &ctx, is384 ); - sha512_update( &ctx, input, ilen ); - sha512_finish( &ctx, output ); - sha512_free( &ctx ); + sha512_init (&ctx); + sha512_starts (&ctx, is384); + sha512_update (&ctx, input, ilen); + sha512_finish (&ctx, output); + sha512_free (&ctx); } #if defined(POLARSSL_FS_IO) /* * output = SHA-512( file contents ) */ -int sha512_file( const char *path, unsigned char output[64], int is384 ) -{ +int sha512_file (const char *path, unsigned char output[64], int is384) { FILE *f; size_t n; sha512_context ctx; unsigned char buf[1024]; - if( ( f = fopen( path, "rb" ) ) == NULL ) - return( POLARSSL_ERR_SHA512_FILE_IO_ERROR ); + if ((f = fopen (path, "rb")) == NULL) + return (POLARSSL_ERR_SHA512_FILE_IO_ERROR); - sha512_init( &ctx ); - sha512_starts( &ctx, is384 ); + sha512_init (&ctx); + sha512_starts (&ctx, is384); - while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) - sha512_update( &ctx, buf, n ); + while ((n = fread (buf, 1, sizeof (buf), f)) > 0) + sha512_update (&ctx, buf, n); - sha512_finish( &ctx, output ); - sha512_free( &ctx ); + sha512_finish (&ctx, output); + sha512_free (&ctx); - if( ferror( f ) != 0 ) - { - fclose( f ); - return( POLARSSL_ERR_SHA512_FILE_IO_ERROR ); + if (ferror (f) != 0) { + fclose (f); + return (POLARSSL_ERR_SHA512_FILE_IO_ERROR); } - fclose( f ); - return( 0 ); + fclose (f); + return (0); } #endif /* POLARSSL_FS_IO */ @@ -383,86 +374,79 @@ int sha512_file( const char *path, unsigned char output[64], int is384 ) /* * SHA-512 HMAC context setup */ -void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key, - size_t keylen, int is384 ) -{ +void sha512_hmac_starts (sha512_context * ctx, const unsigned char *key, + size_t keylen, int is384) { size_t i; unsigned char sum[64]; - if( keylen > 128 ) - { - sha512( key, keylen, sum, is384 ); - keylen = ( is384 ) ? 48 : 64; + if (keylen > 128) { + sha512 (key, keylen, sum, is384); + keylen = (is384) ? 48 : 64; key = sum; } - memset( ctx->ipad, 0x36, 128 ); - memset( ctx->opad, 0x5C, 128 ); + memset (ctx->ipad, 0x36, 128); + memset (ctx->opad, 0x5C, 128); - for( i = 0; i < keylen; i++ ) - { - ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); - ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); + for (i = 0; i < keylen; i++) { + ctx->ipad[i] = (unsigned char) (ctx->ipad[i] ^ key[i]); + ctx->opad[i] = (unsigned char) (ctx->opad[i] ^ key[i]); } - sha512_starts( ctx, is384 ); - sha512_update( ctx, ctx->ipad, 128 ); + sha512_starts (ctx, is384); + sha512_update (ctx, ctx->ipad, 128); - polarssl_zeroize( sum, sizeof( sum ) ); + polarssl_zeroize (sum, sizeof (sum)); } /* * SHA-512 HMAC process buffer */ -void sha512_hmac_update( sha512_context *ctx, - const unsigned char *input, size_t ilen ) -{ - sha512_update( ctx, input, ilen ); +void sha512_hmac_update (sha512_context * ctx, + const unsigned char *input, size_t ilen) { + sha512_update (ctx, input, ilen); } /* * SHA-512 HMAC final digest */ -void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] ) -{ +void sha512_hmac_finish (sha512_context * ctx, unsigned char output[64]) { int is384, hlen; unsigned char tmpbuf[64]; is384 = ctx->is384; - hlen = ( is384 == 0 ) ? 64 : 48; + hlen = (is384 == 0) ? 64 : 48; - sha512_finish( ctx, tmpbuf ); - sha512_starts( ctx, is384 ); - sha512_update( ctx, ctx->opad, 128 ); - sha512_update( ctx, tmpbuf, hlen ); - sha512_finish( ctx, output ); + sha512_finish (ctx, tmpbuf); + sha512_starts (ctx, is384); + sha512_update (ctx, ctx->opad, 128); + sha512_update (ctx, tmpbuf, hlen); + sha512_finish (ctx, output); - polarssl_zeroize( tmpbuf, sizeof( tmpbuf ) ); + polarssl_zeroize (tmpbuf, sizeof (tmpbuf)); } /* * SHA-512 HMAC context reset */ -void sha512_hmac_reset( sha512_context *ctx ) -{ - sha512_starts( ctx, ctx->is384 ); - sha512_update( ctx, ctx->ipad, 128 ); +void sha512_hmac_reset (sha512_context * ctx) { + sha512_starts (ctx, ctx->is384); + sha512_update (ctx, ctx->ipad, 128); } /* * output = HMAC-SHA-512( hmac key, input buffer ) */ -void sha512_hmac( const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char output[64], int is384 ) -{ +void sha512_hmac (const unsigned char *key, size_t keylen, + const unsigned char *input, size_t ilen, + unsigned char output[64], int is384) { sha512_context ctx; - sha512_init( &ctx ); - sha512_hmac_starts( &ctx, key, keylen, is384 ); - sha512_hmac_update( &ctx, input, ilen ); - sha512_hmac_finish( &ctx, output ); - sha512_free( &ctx ); + sha512_init (&ctx); + sha512_hmac_starts (&ctx, key, keylen, is384); + sha512_hmac_update (&ctx, input, ilen); + sha512_hmac_finish (&ctx, output); + sha512_free (&ctx); } #endif // POLARSSL_SHA512_HMAC #ifdef POLARSSL_SELF_TEST @@ -470,316 +454,300 @@ void sha512_hmac( const unsigned char *key, size_t keylen, /* * FIPS-180-2 test vectors */ -static unsigned char sha512_test_buf[3][113] = -{ - { "abc" }, - { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" }, - { "" } +static unsigned char sha512_test_buf[3][113] = { + {"abc"}, + {"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"}, + {""} }; -static const int sha512_test_buflen[3] = -{ +static const int sha512_test_buflen[3] = { 3, 112, 1000 }; -static const unsigned char sha512_test_sum[6][64] = -{ +static const unsigned char sha512_test_sum[6][64] = { /* * SHA-384 test vectors */ - { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, - 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, - 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, - 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, - 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, - 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 }, - { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, - 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, - 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, - 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, - 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, - 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 }, - { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, - 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, - 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, - 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, - 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, - 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }, + {0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, + 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, + 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, + 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, + 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, + 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7}, + {0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, + 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, + 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, + 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, + 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, + 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39}, + {0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, + 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, + 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, + 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, + 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, + 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85}, /* * SHA-512 test vectors */ - { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, - 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, - 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, - 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, - 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, - 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, - 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, - 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F }, - { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, - 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, - 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, - 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, - 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, - 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, - 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, - 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 }, - { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, - 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, - 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, - 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, - 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, - 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, - 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, - 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B } + {0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, + 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, + 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, + 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, + 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, + 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, + 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, + 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F}, + {0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, + 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, + 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, + 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, + 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, + 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, + 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, + 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09}, + {0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, + 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, + 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, + 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, + 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, + 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, + 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, + 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B} }; #ifdef POLARSSL_SHA512_HMAC /* * RFC 4231 test vectors */ -static unsigned char sha512_hmac_test_key[7][26] = -{ - { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" - "\x0B\x0B\x0B\x0B" }, - { "Jefe" }, - { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" - "\xAA\xAA\xAA\xAA" }, - { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" - "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, - { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" - "\x0C\x0C\x0C\x0C" }, - { "" }, /* 0xAA 131 times */ - { "" } +static unsigned char sha512_hmac_test_key[7][26] = { + {"\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" + "\x0B\x0B\x0B\x0B"}, + {"Jefe"}, + {"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" + "\xAA\xAA\xAA\xAA"}, + {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" + "\x11\x12\x13\x14\x15\x16\x17\x18\x19"}, + {"\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" + "\x0C\x0C\x0C\x0C"}, + {""}, /* 0xAA 131 times */ + {""} }; -static const int sha512_hmac_test_keylen[7] = -{ +static const int sha512_hmac_test_keylen[7] = { 20, 4, 20, 25, 20, 131, 131 }; -static unsigned char sha512_hmac_test_buf[7][153] = -{ - { "Hi There" }, - { "what do ya want for nothing?" }, - { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, - { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, - { "Test With Truncation" }, - { "Test Using Larger Than Block-Size Key - Hash Key First" }, - { "This is a test using a larger than block-size key " - "and a larger than block-size data. The key needs to " - "be hashed before being used by the HMAC algorithm." } +static unsigned char sha512_hmac_test_buf[7][153] = { + {"Hi There"}, + {"what do ya want for nothing?"}, + {"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"}, + {"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"}, + {"Test With Truncation"}, + {"Test Using Larger Than Block-Size Key - Hash Key First"}, + {"This is a test using a larger than block-size key " + "and a larger than block-size data. The key needs to " + "be hashed before being used by the HMAC algorithm."} }; -static const int sha512_hmac_test_buflen[7] = -{ +static const int sha512_hmac_test_buflen[7] = { 8, 28, 50, 50, 20, 54, 152 }; -static const unsigned char sha512_hmac_test_sum[14][64] = -{ +static const unsigned char sha512_hmac_test_sum[14][64] = { /* * HMAC-SHA-384 test vectors */ - { 0xAF, 0xD0, 0x39, 0x44, 0xD8, 0x48, 0x95, 0x62, - 0x6B, 0x08, 0x25, 0xF4, 0xAB, 0x46, 0x90, 0x7F, - 0x15, 0xF9, 0xDA, 0xDB, 0xE4, 0x10, 0x1E, 0xC6, - 0x82, 0xAA, 0x03, 0x4C, 0x7C, 0xEB, 0xC5, 0x9C, - 0xFA, 0xEA, 0x9E, 0xA9, 0x07, 0x6E, 0xDE, 0x7F, - 0x4A, 0xF1, 0x52, 0xE8, 0xB2, 0xFA, 0x9C, 0xB6 }, - { 0xAF, 0x45, 0xD2, 0xE3, 0x76, 0x48, 0x40, 0x31, - 0x61, 0x7F, 0x78, 0xD2, 0xB5, 0x8A, 0x6B, 0x1B, - 0x9C, 0x7E, 0xF4, 0x64, 0xF5, 0xA0, 0x1B, 0x47, - 0xE4, 0x2E, 0xC3, 0x73, 0x63, 0x22, 0x44, 0x5E, - 0x8E, 0x22, 0x40, 0xCA, 0x5E, 0x69, 0xE2, 0xC7, - 0x8B, 0x32, 0x39, 0xEC, 0xFA, 0xB2, 0x16, 0x49 }, - { 0x88, 0x06, 0x26, 0x08, 0xD3, 0xE6, 0xAD, 0x8A, - 0x0A, 0xA2, 0xAC, 0xE0, 0x14, 0xC8, 0xA8, 0x6F, - 0x0A, 0xA6, 0x35, 0xD9, 0x47, 0xAC, 0x9F, 0xEB, - 0xE8, 0x3E, 0xF4, 0xE5, 0x59, 0x66, 0x14, 0x4B, - 0x2A, 0x5A, 0xB3, 0x9D, 0xC1, 0x38, 0x14, 0xB9, - 0x4E, 0x3A, 0xB6, 0xE1, 0x01, 0xA3, 0x4F, 0x27 }, - { 0x3E, 0x8A, 0x69, 0xB7, 0x78, 0x3C, 0x25, 0x85, - 0x19, 0x33, 0xAB, 0x62, 0x90, 0xAF, 0x6C, 0xA7, - 0x7A, 0x99, 0x81, 0x48, 0x08, 0x50, 0x00, 0x9C, - 0xC5, 0x57, 0x7C, 0x6E, 0x1F, 0x57, 0x3B, 0x4E, - 0x68, 0x01, 0xDD, 0x23, 0xC4, 0xA7, 0xD6, 0x79, - 0xCC, 0xF8, 0xA3, 0x86, 0xC6, 0x74, 0xCF, 0xFB }, - { 0x3A, 0xBF, 0x34, 0xC3, 0x50, 0x3B, 0x2A, 0x23, - 0xA4, 0x6E, 0xFC, 0x61, 0x9B, 0xAE, 0xF8, 0x97 }, - { 0x4E, 0xCE, 0x08, 0x44, 0x85, 0x81, 0x3E, 0x90, - 0x88, 0xD2, 0xC6, 0x3A, 0x04, 0x1B, 0xC5, 0xB4, - 0x4F, 0x9E, 0xF1, 0x01, 0x2A, 0x2B, 0x58, 0x8F, - 0x3C, 0xD1, 0x1F, 0x05, 0x03, 0x3A, 0xC4, 0xC6, - 0x0C, 0x2E, 0xF6, 0xAB, 0x40, 0x30, 0xFE, 0x82, - 0x96, 0x24, 0x8D, 0xF1, 0x63, 0xF4, 0x49, 0x52 }, - { 0x66, 0x17, 0x17, 0x8E, 0x94, 0x1F, 0x02, 0x0D, - 0x35, 0x1E, 0x2F, 0x25, 0x4E, 0x8F, 0xD3, 0x2C, - 0x60, 0x24, 0x20, 0xFE, 0xB0, 0xB8, 0xFB, 0x9A, - 0xDC, 0xCE, 0xBB, 0x82, 0x46, 0x1E, 0x99, 0xC5, - 0xA6, 0x78, 0xCC, 0x31, 0xE7, 0x99, 0x17, 0x6D, - 0x38, 0x60, 0xE6, 0x11, 0x0C, 0x46, 0x52, 0x3E }, + {0xAF, 0xD0, 0x39, 0x44, 0xD8, 0x48, 0x95, 0x62, + 0x6B, 0x08, 0x25, 0xF4, 0xAB, 0x46, 0x90, 0x7F, + 0x15, 0xF9, 0xDA, 0xDB, 0xE4, 0x10, 0x1E, 0xC6, + 0x82, 0xAA, 0x03, 0x4C, 0x7C, 0xEB, 0xC5, 0x9C, + 0xFA, 0xEA, 0x9E, 0xA9, 0x07, 0x6E, 0xDE, 0x7F, + 0x4A, 0xF1, 0x52, 0xE8, 0xB2, 0xFA, 0x9C, 0xB6}, + {0xAF, 0x45, 0xD2, 0xE3, 0x76, 0x48, 0x40, 0x31, + 0x61, 0x7F, 0x78, 0xD2, 0xB5, 0x8A, 0x6B, 0x1B, + 0x9C, 0x7E, 0xF4, 0x64, 0xF5, 0xA0, 0x1B, 0x47, + 0xE4, 0x2E, 0xC3, 0x73, 0x63, 0x22, 0x44, 0x5E, + 0x8E, 0x22, 0x40, 0xCA, 0x5E, 0x69, 0xE2, 0xC7, + 0x8B, 0x32, 0x39, 0xEC, 0xFA, 0xB2, 0x16, 0x49}, + {0x88, 0x06, 0x26, 0x08, 0xD3, 0xE6, 0xAD, 0x8A, + 0x0A, 0xA2, 0xAC, 0xE0, 0x14, 0xC8, 0xA8, 0x6F, + 0x0A, 0xA6, 0x35, 0xD9, 0x47, 0xAC, 0x9F, 0xEB, + 0xE8, 0x3E, 0xF4, 0xE5, 0x59, 0x66, 0x14, 0x4B, + 0x2A, 0x5A, 0xB3, 0x9D, 0xC1, 0x38, 0x14, 0xB9, + 0x4E, 0x3A, 0xB6, 0xE1, 0x01, 0xA3, 0x4F, 0x27}, + {0x3E, 0x8A, 0x69, 0xB7, 0x78, 0x3C, 0x25, 0x85, + 0x19, 0x33, 0xAB, 0x62, 0x90, 0xAF, 0x6C, 0xA7, + 0x7A, 0x99, 0x81, 0x48, 0x08, 0x50, 0x00, 0x9C, + 0xC5, 0x57, 0x7C, 0x6E, 0x1F, 0x57, 0x3B, 0x4E, + 0x68, 0x01, 0xDD, 0x23, 0xC4, 0xA7, 0xD6, 0x79, + 0xCC, 0xF8, 0xA3, 0x86, 0xC6, 0x74, 0xCF, 0xFB}, + {0x3A, 0xBF, 0x34, 0xC3, 0x50, 0x3B, 0x2A, 0x23, + 0xA4, 0x6E, 0xFC, 0x61, 0x9B, 0xAE, 0xF8, 0x97}, + {0x4E, 0xCE, 0x08, 0x44, 0x85, 0x81, 0x3E, 0x90, + 0x88, 0xD2, 0xC6, 0x3A, 0x04, 0x1B, 0xC5, 0xB4, + 0x4F, 0x9E, 0xF1, 0x01, 0x2A, 0x2B, 0x58, 0x8F, + 0x3C, 0xD1, 0x1F, 0x05, 0x03, 0x3A, 0xC4, 0xC6, + 0x0C, 0x2E, 0xF6, 0xAB, 0x40, 0x30, 0xFE, 0x82, + 0x96, 0x24, 0x8D, 0xF1, 0x63, 0xF4, 0x49, 0x52}, + {0x66, 0x17, 0x17, 0x8E, 0x94, 0x1F, 0x02, 0x0D, + 0x35, 0x1E, 0x2F, 0x25, 0x4E, 0x8F, 0xD3, 0x2C, + 0x60, 0x24, 0x20, 0xFE, 0xB0, 0xB8, 0xFB, 0x9A, + 0xDC, 0xCE, 0xBB, 0x82, 0x46, 0x1E, 0x99, 0xC5, + 0xA6, 0x78, 0xCC, 0x31, 0xE7, 0x99, 0x17, 0x6D, + 0x38, 0x60, 0xE6, 0x11, 0x0C, 0x46, 0x52, 0x3E}, /* * HMAC-SHA-512 test vectors */ - { 0x87, 0xAA, 0x7C, 0xDE, 0xA5, 0xEF, 0x61, 0x9D, - 0x4F, 0xF0, 0xB4, 0x24, 0x1A, 0x1D, 0x6C, 0xB0, - 0x23, 0x79, 0xF4, 0xE2, 0xCE, 0x4E, 0xC2, 0x78, - 0x7A, 0xD0, 0xB3, 0x05, 0x45, 0xE1, 0x7C, 0xDE, - 0xDA, 0xA8, 0x33, 0xB7, 0xD6, 0xB8, 0xA7, 0x02, - 0x03, 0x8B, 0x27, 0x4E, 0xAE, 0xA3, 0xF4, 0xE4, - 0xBE, 0x9D, 0x91, 0x4E, 0xEB, 0x61, 0xF1, 0x70, - 0x2E, 0x69, 0x6C, 0x20, 0x3A, 0x12, 0x68, 0x54 }, - { 0x16, 0x4B, 0x7A, 0x7B, 0xFC, 0xF8, 0x19, 0xE2, - 0xE3, 0x95, 0xFB, 0xE7, 0x3B, 0x56, 0xE0, 0xA3, - 0x87, 0xBD, 0x64, 0x22, 0x2E, 0x83, 0x1F, 0xD6, - 0x10, 0x27, 0x0C, 0xD7, 0xEA, 0x25, 0x05, 0x54, - 0x97, 0x58, 0xBF, 0x75, 0xC0, 0x5A, 0x99, 0x4A, - 0x6D, 0x03, 0x4F, 0x65, 0xF8, 0xF0, 0xE6, 0xFD, - 0xCA, 0xEA, 0xB1, 0xA3, 0x4D, 0x4A, 0x6B, 0x4B, - 0x63, 0x6E, 0x07, 0x0A, 0x38, 0xBC, 0xE7, 0x37 }, - { 0xFA, 0x73, 0xB0, 0x08, 0x9D, 0x56, 0xA2, 0x84, - 0xEF, 0xB0, 0xF0, 0x75, 0x6C, 0x89, 0x0B, 0xE9, - 0xB1, 0xB5, 0xDB, 0xDD, 0x8E, 0xE8, 0x1A, 0x36, - 0x55, 0xF8, 0x3E, 0x33, 0xB2, 0x27, 0x9D, 0x39, - 0xBF, 0x3E, 0x84, 0x82, 0x79, 0xA7, 0x22, 0xC8, - 0x06, 0xB4, 0x85, 0xA4, 0x7E, 0x67, 0xC8, 0x07, - 0xB9, 0x46, 0xA3, 0x37, 0xBE, 0xE8, 0x94, 0x26, - 0x74, 0x27, 0x88, 0x59, 0xE1, 0x32, 0x92, 0xFB }, - { 0xB0, 0xBA, 0x46, 0x56, 0x37, 0x45, 0x8C, 0x69, - 0x90, 0xE5, 0xA8, 0xC5, 0xF6, 0x1D, 0x4A, 0xF7, - 0xE5, 0x76, 0xD9, 0x7F, 0xF9, 0x4B, 0x87, 0x2D, - 0xE7, 0x6F, 0x80, 0x50, 0x36, 0x1E, 0xE3, 0xDB, - 0xA9, 0x1C, 0xA5, 0xC1, 0x1A, 0xA2, 0x5E, 0xB4, - 0xD6, 0x79, 0x27, 0x5C, 0xC5, 0x78, 0x80, 0x63, - 0xA5, 0xF1, 0x97, 0x41, 0x12, 0x0C, 0x4F, 0x2D, - 0xE2, 0xAD, 0xEB, 0xEB, 0x10, 0xA2, 0x98, 0xDD }, - { 0x41, 0x5F, 0xAD, 0x62, 0x71, 0x58, 0x0A, 0x53, - 0x1D, 0x41, 0x79, 0xBC, 0x89, 0x1D, 0x87, 0xA6 }, - { 0x80, 0xB2, 0x42, 0x63, 0xC7, 0xC1, 0xA3, 0xEB, - 0xB7, 0x14, 0x93, 0xC1, 0xDD, 0x7B, 0xE8, 0xB4, - 0x9B, 0x46, 0xD1, 0xF4, 0x1B, 0x4A, 0xEE, 0xC1, - 0x12, 0x1B, 0x01, 0x37, 0x83, 0xF8, 0xF3, 0x52, - 0x6B, 0x56, 0xD0, 0x37, 0xE0, 0x5F, 0x25, 0x98, - 0xBD, 0x0F, 0xD2, 0x21, 0x5D, 0x6A, 0x1E, 0x52, - 0x95, 0xE6, 0x4F, 0x73, 0xF6, 0x3F, 0x0A, 0xEC, - 0x8B, 0x91, 0x5A, 0x98, 0x5D, 0x78, 0x65, 0x98 }, - { 0xE3, 0x7B, 0x6A, 0x77, 0x5D, 0xC8, 0x7D, 0xBA, - 0xA4, 0xDF, 0xA9, 0xF9, 0x6E, 0x5E, 0x3F, 0xFD, - 0xDE, 0xBD, 0x71, 0xF8, 0x86, 0x72, 0x89, 0x86, - 0x5D, 0xF5, 0xA3, 0x2D, 0x20, 0xCD, 0xC9, 0x44, - 0xB6, 0x02, 0x2C, 0xAC, 0x3C, 0x49, 0x82, 0xB1, - 0x0D, 0x5E, 0xEB, 0x55, 0xC3, 0xE4, 0xDE, 0x15, - 0x13, 0x46, 0x76, 0xFB, 0x6D, 0xE0, 0x44, 0x60, - 0x65, 0xC9, 0x74, 0x40, 0xFA, 0x8C, 0x6A, 0x58 } + {0x87, 0xAA, 0x7C, 0xDE, 0xA5, 0xEF, 0x61, 0x9D, + 0x4F, 0xF0, 0xB4, 0x24, 0x1A, 0x1D, 0x6C, 0xB0, + 0x23, 0x79, 0xF4, 0xE2, 0xCE, 0x4E, 0xC2, 0x78, + 0x7A, 0xD0, 0xB3, 0x05, 0x45, 0xE1, 0x7C, 0xDE, + 0xDA, 0xA8, 0x33, 0xB7, 0xD6, 0xB8, 0xA7, 0x02, + 0x03, 0x8B, 0x27, 0x4E, 0xAE, 0xA3, 0xF4, 0xE4, + 0xBE, 0x9D, 0x91, 0x4E, 0xEB, 0x61, 0xF1, 0x70, + 0x2E, 0x69, 0x6C, 0x20, 0x3A, 0x12, 0x68, 0x54}, + {0x16, 0x4B, 0x7A, 0x7B, 0xFC, 0xF8, 0x19, 0xE2, + 0xE3, 0x95, 0xFB, 0xE7, 0x3B, 0x56, 0xE0, 0xA3, + 0x87, 0xBD, 0x64, 0x22, 0x2E, 0x83, 0x1F, 0xD6, + 0x10, 0x27, 0x0C, 0xD7, 0xEA, 0x25, 0x05, 0x54, + 0x97, 0x58, 0xBF, 0x75, 0xC0, 0x5A, 0x99, 0x4A, + 0x6D, 0x03, 0x4F, 0x65, 0xF8, 0xF0, 0xE6, 0xFD, + 0xCA, 0xEA, 0xB1, 0xA3, 0x4D, 0x4A, 0x6B, 0x4B, + 0x63, 0x6E, 0x07, 0x0A, 0x38, 0xBC, 0xE7, 0x37}, + {0xFA, 0x73, 0xB0, 0x08, 0x9D, 0x56, 0xA2, 0x84, + 0xEF, 0xB0, 0xF0, 0x75, 0x6C, 0x89, 0x0B, 0xE9, + 0xB1, 0xB5, 0xDB, 0xDD, 0x8E, 0xE8, 0x1A, 0x36, + 0x55, 0xF8, 0x3E, 0x33, 0xB2, 0x27, 0x9D, 0x39, + 0xBF, 0x3E, 0x84, 0x82, 0x79, 0xA7, 0x22, 0xC8, + 0x06, 0xB4, 0x85, 0xA4, 0x7E, 0x67, 0xC8, 0x07, + 0xB9, 0x46, 0xA3, 0x37, 0xBE, 0xE8, 0x94, 0x26, + 0x74, 0x27, 0x88, 0x59, 0xE1, 0x32, 0x92, 0xFB}, + {0xB0, 0xBA, 0x46, 0x56, 0x37, 0x45, 0x8C, 0x69, + 0x90, 0xE5, 0xA8, 0xC5, 0xF6, 0x1D, 0x4A, 0xF7, + 0xE5, 0x76, 0xD9, 0x7F, 0xF9, 0x4B, 0x87, 0x2D, + 0xE7, 0x6F, 0x80, 0x50, 0x36, 0x1E, 0xE3, 0xDB, + 0xA9, 0x1C, 0xA5, 0xC1, 0x1A, 0xA2, 0x5E, 0xB4, + 0xD6, 0x79, 0x27, 0x5C, 0xC5, 0x78, 0x80, 0x63, + 0xA5, 0xF1, 0x97, 0x41, 0x12, 0x0C, 0x4F, 0x2D, + 0xE2, 0xAD, 0xEB, 0xEB, 0x10, 0xA2, 0x98, 0xDD}, + {0x41, 0x5F, 0xAD, 0x62, 0x71, 0x58, 0x0A, 0x53, + 0x1D, 0x41, 0x79, 0xBC, 0x89, 0x1D, 0x87, 0xA6}, + {0x80, 0xB2, 0x42, 0x63, 0xC7, 0xC1, 0xA3, 0xEB, + 0xB7, 0x14, 0x93, 0xC1, 0xDD, 0x7B, 0xE8, 0xB4, + 0x9B, 0x46, 0xD1, 0xF4, 0x1B, 0x4A, 0xEE, 0xC1, + 0x12, 0x1B, 0x01, 0x37, 0x83, 0xF8, 0xF3, 0x52, + 0x6B, 0x56, 0xD0, 0x37, 0xE0, 0x5F, 0x25, 0x98, + 0xBD, 0x0F, 0xD2, 0x21, 0x5D, 0x6A, 0x1E, 0x52, + 0x95, 0xE6, 0x4F, 0x73, 0xF6, 0x3F, 0x0A, 0xEC, + 0x8B, 0x91, 0x5A, 0x98, 0x5D, 0x78, 0x65, 0x98}, + {0xE3, 0x7B, 0x6A, 0x77, 0x5D, 0xC8, 0x7D, 0xBA, + 0xA4, 0xDF, 0xA9, 0xF9, 0x6E, 0x5E, 0x3F, 0xFD, + 0xDE, 0xBD, 0x71, 0xF8, 0x86, 0x72, 0x89, 0x86, + 0x5D, 0xF5, 0xA3, 0x2D, 0x20, 0xCD, 0xC9, 0x44, + 0xB6, 0x02, 0x2C, 0xAC, 0x3C, 0x49, 0x82, 0xB1, + 0x0D, 0x5E, 0xEB, 0x55, 0xC3, 0xE4, 0xDE, 0x15, + 0x13, 0x46, 0x76, 0xFB, 0x6D, 0xE0, 0x44, 0x60, + 0x65, 0xC9, 0x74, 0x40, 0xFA, 0x8C, 0x6A, 0x58} }; #endif // POLARSSL_SHA512_HMAC /* * Checkup routine */ -int sha512_self_test( int verbose ) -{ +int sha512_self_test (int verbose) { int i, j, k, buflen, ret = 0; unsigned char buf[1024]; unsigned char sha512sum[64]; sha512_context ctx; - sha512_init( &ctx ); + sha512_init (&ctx); - for( i = 0; i < 6; i++ ) - { + for (i = 0; i < 6; i++) { j = i % 3; k = i < 3; - if( verbose != 0 ) - polarssl_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); + if (verbose != 0) + polarssl_printf (" SHA-%d test #%d: ", 512 - k * 128, j + 1); - sha512_starts( &ctx, k ); + sha512_starts (&ctx, k); - if( j == 2 ) - { - memset( buf, 'a', buflen = 1000 ); + if (j == 2) { + memset (buf, 'a', buflen = 1000); - for( j = 0; j < 1000; j++ ) - sha512_update( &ctx, buf, buflen ); + for (j = 0; j < 1000; j++) + sha512_update (&ctx, buf, buflen); } else - sha512_update( &ctx, sha512_test_buf[j], - sha512_test_buflen[j] ); + sha512_update (&ctx, sha512_test_buf[j], sha512_test_buflen[j]); - sha512_finish( &ctx, sha512sum ); + sha512_finish (&ctx, sha512sum); - if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) - { - if( verbose != 0 ) - polarssl_printf( "failed\n" ); + if (memcmp (sha512sum, sha512_test_sum[i], 64 - k * 16) != 0) { + if (verbose != 0) + polarssl_printf ("failed\n"); ret = 1; goto exit; } - if( verbose != 0 ) - polarssl_printf( "passed\n" ); + if (verbose != 0) + polarssl_printf ("passed\n"); } - if( verbose != 0 ) - polarssl_printf( "\n" ); + if (verbose != 0) + polarssl_printf ("\n"); #ifdef POLARSSL_SHA512_HMAC - for( i = 0; i < 14; i++ ) - { + for (i = 0; i < 14; i++) { j = i % 7; k = i < 7; - if( verbose != 0 ) - polarssl_printf( " HMAC-SHA-%d test #%d: ", 512 - k * 128, j + 1 ); + if (verbose != 0) + polarssl_printf (" HMAC-SHA-%d test #%d: ", 512 - k * 128, j + 1); - if( j == 5 || j == 6 ) - { - memset( buf, '\xAA', buflen = 131 ); - sha512_hmac_starts( &ctx, buf, buflen, k ); + if (j == 5 || j == 6) { + memset (buf, '\xAA', buflen = 131); + sha512_hmac_starts (&ctx, buf, buflen, k); } else - sha512_hmac_starts( &ctx, sha512_hmac_test_key[j], - sha512_hmac_test_keylen[j], k ); + sha512_hmac_starts (&ctx, sha512_hmac_test_key[j], + sha512_hmac_test_keylen[j], k); - sha512_hmac_update( &ctx, sha512_hmac_test_buf[j], - sha512_hmac_test_buflen[j] ); + sha512_hmac_update (&ctx, sha512_hmac_test_buf[j], + sha512_hmac_test_buflen[j]); - sha512_hmac_finish( &ctx, sha512sum ); + sha512_hmac_finish (&ctx, sha512sum); - buflen = ( j == 4 ) ? 16 : 64 - k * 16; + buflen = (j == 4) ? 16 : 64 - k * 16; - if( memcmp( sha512sum, sha512_hmac_test_sum[i], buflen ) != 0 ) - { - if( verbose != 0 ) - polarssl_printf( "failed\n" ); + if (memcmp (sha512sum, sha512_hmac_test_sum[i], buflen) != 0) { + if (verbose != 0) + polarssl_printf ("failed\n"); ret = 1; goto exit; } - if( verbose != 0 ) - polarssl_printf( "passed\n" ); + if (verbose != 0) + polarssl_printf ("passed\n"); } - if( verbose != 0 ) - polarssl_printf( "\n" ); + if (verbose != 0) + polarssl_printf ("\n"); #endif // POLARSSL_SHA512_HMAC -exit: - sha512_free( &ctx ); + exit: + sha512_free (&ctx); - return( ret ); + return (ret); } #endif /* POLARSSL_SELF_TEST */ diff --git a/lib/src/bitpunch/crypto/hash/sha512.c b/lib/src/bitpunch/crypto/hash/sha512.c index 30580e3..8ae6cbd 100644 --- a/lib/src/bitpunch/crypto/hash/sha512.c +++ b/lib/src/bitpunch/crypto/hash/sha512.c @@ -23,24 +23,25 @@ along with this program. If not, see . #include #include -int BPU_gf2VecHash(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in) { - uint8_t md[BPU_HASH_LEN]; - - if (out->len != BPU_HASH_LEN * 8) { - BPU_printError("Wrong vector len %d", out->len); - - return -1; - } - // if input len is not divisible by 4, then it is not complete vector - if (in->len % 8) { -// BPU_printError("Wrong input vector len %d, should be divisible by 8", in->len); - BPU_printWarning("input vector len %d, should be divisible by 8", in->len); - } - // hash vector - sha512((uint8_t *)in->elements, in->len / 8, md, 0); - // copy digest to vector - memcpy(out->elements, md, BPU_HASH_LEN); - - return 0; +int BPU_gf2VecHash (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in) { + uint8_t md[BPU_HASH_LEN]; + + if (out->len != BPU_HASH_LEN * 8) { + BPU_printError ("Wrong vector len %d", out->len); + + return -1; + } + // if input len is not divisible by 4, then it is not complete vector + if (in->len % 8) { +// BPU_printError("Wrong input vector len %d, should be divisible by 8", in->len); + BPU_printWarning ("input vector len %d, should be divisible by 8", + in->len); + } + // hash vector + sha512 ((uint8_t *) in->elements, in->len / 8, md, 0); + // copy digest to vector + memcpy (out->elements, md, BPU_HASH_LEN); + + return 0; } #endif diff --git a/lib/src/bitpunch/crypto/hash/sha512.h b/lib/src/bitpunch/crypto/hash/sha512.h index f720719..9bfd23f 100644 --- a/lib/src/bitpunch/crypto/hash/sha512.h +++ b/lib/src/bitpunch/crypto/hash/sha512.h @@ -27,9 +27,8 @@ along with this program. If not, see . #define BPU_HASH_LEN 64 -int BPU_gf2VecHash(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in); +int BPU_gf2VecHash (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in); #endif // BPU_CONF_SHA_512 #endif // SHA512_H - diff --git a/lib/src/bitpunch/crypto/mecs.c b/lib/src/bitpunch/crypto/mecs.c index b7fa913..7b11f2b 100644 --- a/lib/src/bitpunch/crypto/mecs.c +++ b/lib/src/bitpunch/crypto/mecs.c @@ -30,8 +30,9 @@ along with this program. If not, see . * @return 0 - succes, else error */ /// Encrypt plaintext (pt) and save it to cipher text. -int BPU_mecsEncrypt(BPU_T_GF2_Vector *ct, const BPU_T_GF2_Vector *pt, const BPU_T_Mecs_Ctx *ctx) { - return ctx->_encrypt(ct, pt, ctx); +int BPU_mecsEncrypt (BPU_T_GF2_Vector * ct, const BPU_T_GF2_Vector * pt, + const BPU_T_Mecs_Ctx * ctx) { + return ctx->_encrypt (ct, pt, ctx); } /** @@ -42,8 +43,9 @@ int BPU_mecsEncrypt(BPU_T_GF2_Vector *ct, const BPU_T_GF2_Vector *pt, const BPU_ * @return 0 - succes, else error */ /// Decrypt cipher text (ct) and save it to plain text. -int BPU_mecsDecrypt(BPU_T_GF2_Vector *pt, BPU_T_GF2_Vector *ct, const BPU_T_Mecs_Ctx *ctx) { - return ctx->_decrypt(pt, ct, ctx); +int BPU_mecsDecrypt (BPU_T_GF2_Vector * pt, BPU_T_GF2_Vector * ct, + const BPU_T_Mecs_Ctx * ctx) { + return ctx->_decrypt (pt, ct, ctx); } /** @@ -52,6 +54,6 @@ int BPU_mecsDecrypt(BPU_T_GF2_Vector *pt, BPU_T_GF2_Vector *ct, const BPU_T_Mecs * @return 0 - success, else error */ /// Key generation, first must be initialized context using BPU_mecsInitCtx(). -int BPU_mecsGenKeyPair(BPU_T_Mecs_Ctx *ctx) { - return ctx->_genKeyPair(ctx->code_ctx); +int BPU_mecsGenKeyPair (BPU_T_Mecs_Ctx * ctx) { + return ctx->_genKeyPair (ctx->code_ctx); } diff --git a/lib/src/bitpunch/crypto/mecs.h b/lib/src/bitpunch/crypto/mecs.h index af523fd..78795bb 100644 --- a/lib/src/bitpunch/crypto/mecs.h +++ b/lib/src/bitpunch/crypto/mecs.h @@ -32,17 +32,19 @@ along with this program. If not, see . /******************************************************************/ #ifdef BPU_CONF_ENCRYPTION -int BPU_mecsEncrypt(BPU_T_GF2_Vector *ct, const BPU_T_GF2_Vector *pt, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsEncrypt (BPU_T_GF2_Vector * ct, const BPU_T_GF2_Vector * pt, + const BPU_T_Mecs_Ctx * ctx); #endif #ifdef BPU_CONF_DECRYPTION -int BPU_mecsDecrypt(BPU_T_GF2_Vector *pt, BPU_T_GF2_Vector *ct, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsDecrypt (BPU_T_GF2_Vector * pt, BPU_T_GF2_Vector * ct, + const BPU_T_Mecs_Ctx * ctx); #endif #ifdef BPU_CONF_KEY_GEN -int BPU_mecsGenKeyPair(BPU_T_Mecs_Ctx *ctx); +int BPU_mecsGenKeyPair (BPU_T_Mecs_Ctx * ctx); #endif #endif // BPU_MECS_H diff --git a/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.c b/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.c index f093ad4..8bf203c 100644 --- a/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.c +++ b/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.c @@ -21,53 +21,57 @@ along with this program. If not, see . #include #ifdef BPU_CONF_ENCRYPTION -int BPU_mecsBasicEncrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx) { - int rc; - BPU_T_GF2_Vector *e = ctx->code_ctx->e; - BPU_gf2VecNull(out); - - // test the size of message and g_m - if (in->len != ctx->code_ctx->msg_len) { - BPU_printError("message length has to be of length %d", ctx->code_ctx->msg_len); - - return -1; - } - - rc = ctx->code_ctx->_encode(out, in, ctx->code_ctx); - if (rc) { - BPU_printError("can not encode"); - return rc; - } - - // generate random error vector e - rc += BPU_gf2VecRand(e, ctx->code_ctx->t); - if (rc) { - BPU_printError("can not init rand vector"); - return rc; - } - - // z' XOR e - rc = BPU_gf2VecXor(out, e); - if (rc) { - BPU_printError("can not add error vector"); - return rc; - } - return rc; +int BPU_mecsBasicEncrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx) { + int rc; + BPU_T_GF2_Vector *e = ctx->code_ctx->e; + + BPU_gf2VecNull (out); + + // test the size of message and g_m + if (in->len != ctx->code_ctx->msg_len) { + BPU_printError ("message length has to be of length %d", + ctx->code_ctx->msg_len); + + return -1; + } + + rc = ctx->code_ctx->_encode (out, in, ctx->code_ctx); + if (rc) { + BPU_printError ("can not encode"); + return rc; + } + + // generate random error vector e + rc += BPU_gf2VecRand (e, ctx->code_ctx->t); + if (rc) { + BPU_printError ("can not init rand vector"); + return rc; + } + + // z' XOR e + rc = BPU_gf2VecXor (out, e); + if (rc) { + BPU_printError ("can not add error vector"); + return rc; + } + return rc; } #endif #ifdef BPU_CONF_DECRYPTION -int BPU_mecsBasicDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx) { - int rc = 0; +int BPU_mecsBasicDecrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx) { + int rc = 0; BPU_T_GF2_Vector *temp; - BPU_gf2VecMalloc(&temp, in->len); - BPU_gf2VecCopy(temp, in); + BPU_gf2VecMalloc (&temp, in->len); + BPU_gf2VecCopy (temp, in); - rc = ctx->code_ctx->_decode(out, temp, ctx->code_ctx); + rc = ctx->code_ctx->_decode (out, temp, ctx->code_ctx); - BPU_gf2VecFree(&temp); + BPU_gf2VecFree (&temp); - return rc; + return rc; } #endif diff --git a/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.h b/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.h index 4d68c95..59fa444 100644 --- a/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.h +++ b/lib/src/bitpunch/crypto/mecsbasic/mecsbasic.h @@ -29,11 +29,13 @@ along with this program. If not, see . * @param ctx McEliece context * @return 0 - succes, else error */ -int BPU_mecsBasicEncrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsBasicEncrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx); #endif #ifdef BPU_CONF_DECRYPTION -int BPU_mecsBasicDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx); +int BPU_mecsBasicDecrypt (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const BPU_T_Mecs_Ctx * ctx); #endif #endif // MECSBASIC_H diff --git a/lib/src/bitpunch/crypto/mecsctx.c b/lib/src/bitpunch/crypto/mecsctx.c index ce4b5d2..1541e37 100644 --- a/lib/src/bitpunch/crypto/mecsctx.c +++ b/lib/src/bitpunch/crypto/mecsctx.c @@ -34,20 +34,21 @@ along with this program. If not, see . #include #endif -int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, const BPU_T_EN_Mecs_Types type) { +int BPU_mecsInitCtx (BPU_T_Mecs_Ctx ** ctx, const BPU_T_UN_Mecs_Params * params, + const BPU_T_EN_Mecs_Types type) { int rc = 0; BPU_T_Mecs_Ctx *ctx_p; if (!*ctx) { - *ctx = (BPU_T_Mecs_Ctx *) calloc(sizeof(BPU_T_Mecs_Ctx), 1); + *ctx = (BPU_T_Mecs_Ctx *) calloc (sizeof (BPU_T_Mecs_Ctx), 1); if (!*ctx) { - BPU_printError("can not alloc Mecs ctx"); + BPU_printError ("can not alloc Mecs ctx"); return -1; } } else { - BPU_printDebug("Already initialized"); + BPU_printDebug ("Already initialized"); return 0; } ctx_p = *ctx; @@ -64,7 +65,8 @@ int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, co #ifdef BPU_CONF_KEY_GEN ctx_p->_genKeyPair = BPU_goppaGenCode; #endif - rc = BPU_codeInitCtx(&ctx_p->code_ctx, (BPU_T_UN_Code_Params *)params, BPU_EN_CODE_GOPPA); + rc = BPU_codeInitCtx (&ctx_p->code_ctx, (BPU_T_UN_Code_Params *) params, + BPU_EN_CODE_GOPPA); if (rc) { return rc; } @@ -75,19 +77,23 @@ int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, co #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA case BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA: #ifdef BPU_CONF_ENCRYPTION - ctx_p->_encrypt = BPU_mecsPointchevalCCA2Encrypt; + ctx_p->_encrypt = BPU_mecsPointchevalCCA2Encrypt; #endif #ifdef BPU_CONF_DECRYPTION - ctx_p->_decrypt = BPU_mecsPointchevalCCA2Decrypt; + ctx_p->_decrypt = BPU_mecsPointchevalCCA2Decrypt; #endif #ifdef BPU_CONF_KEY_GEN - ctx_p->_genKeyPair = BPU_goppaGenCode; + ctx_p->_genKeyPair = BPU_goppaGenCode; #endif - rc = BPU_codeInitCtx(&ctx_p->code_ctx, (BPU_T_UN_Code_Params *)params, BPU_EN_CODE_GOPPA); + rc = BPU_codeInitCtx (&ctx_p->code_ctx, (BPU_T_UN_Code_Params *) params, + BPU_EN_CODE_GOPPA); if (rc) { return rc; } - ctx_p->pt_len = BPU_HASH_LEN * 8 < ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * 8 : ctx_p->code_ctx->msg_len; + ctx_p->pt_len = + BPU_HASH_LEN * 8 < + ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * + 8 : ctx_p->code_ctx->msg_len; ctx_p->ct_len = ctx_p->code_ctx->code_len + 2 * ctx_p->ct_len; break; #endif @@ -102,7 +108,8 @@ int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, co #ifdef BPU_CONF_KEY_GEN ctx_p->_genKeyPair = BPU_mecsQcmdpcGenKeys; #endif - rc = BPU_codeInitCtx(&ctx_p->code_ctx, (BPU_T_UN_Code_Params *)params, BPU_EN_CODE_QCMDPC); + rc = BPU_codeInitCtx (&ctx_p->code_ctx, (BPU_T_UN_Code_Params *) params, + BPU_EN_CODE_QCMDPC); if (rc) { return rc; } @@ -121,55 +128,59 @@ int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, co #ifdef BPU_CONF_KEY_GEN ctx_p->_genKeyPair = BPU_mecsQcmdpcGenKeys; #endif - rc = BPU_codeInitCtx(&ctx_p->code_ctx, (BPU_T_UN_Code_Params *)params, BPU_EN_CODE_QCMDPC); + rc = BPU_codeInitCtx (&ctx_p->code_ctx, (BPU_T_UN_Code_Params *) params, + BPU_EN_CODE_QCMDPC); if (rc) { return rc; } - ctx_p->pt_len = BPU_HASH_LEN * 8 < ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * 8 : ctx_p->code_ctx->msg_len; + ctx_p->pt_len = + BPU_HASH_LEN * 8 < + ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * + 8 : ctx_p->code_ctx->msg_len; ctx_p->ct_len = ctx_p->code_ctx->code_len + 2 * ctx_p->ct_len; break; #endif - /* EXAMPLE please DO NOT REMOVE - case BPU_EN_MECS_*****: -#ifdef BPU_CONF_ENCRYPTION - ctx_p->_encrypt = FUNC_FROM_YOUR_FILE; -#endif -#ifdef BPU_CONF_DECRYPTION - ctx_p->_decrypt = FUNC_FROM_YOUR_FILE; -#endif -#ifdef BPU_CONF_KEY_GEN - ctx_p->_genKeyPair = FUNC_FROM_YOUR_FILE; -#endif - rc = BPU_codeInitCtx(&ctx_p->code_ctx, m, t, BPU_EN_CODE_GOPPA, mod); - if (rc) { - return rc; - } - ctx->pt_len = PT_LEN; - ctx->ct_len = CT_LEN; - break; - */ + /* EXAMPLE please DO NOT REMOVE + case BPU_EN_MECS_*****: + #ifdef BPU_CONF_ENCRYPTION + ctx_p->_encrypt = FUNC_FROM_YOUR_FILE; + #endif + #ifdef BPU_CONF_DECRYPTION + ctx_p->_decrypt = FUNC_FROM_YOUR_FILE; + #endif + #ifdef BPU_CONF_KEY_GEN + ctx_p->_genKeyPair = FUNC_FROM_YOUR_FILE; + #endif + rc = BPU_codeInitCtx(&ctx_p->code_ctx, m, t, BPU_EN_CODE_GOPPA, mod); + if (rc) { + return rc; + } + ctx->pt_len = PT_LEN; + ctx->ct_len = CT_LEN; + break; + */ default: - BPU_printError("MECS type not supported: %d", type); + BPU_printError ("MECS type not supported: %d", type); return BPU_EC_MECS_TYPE_NOT_SUPPORTED; } return rc; } -int BPU_mecsFreeCtx(BPU_T_Mecs_Ctx **ctx) { +int BPU_mecsFreeCtx (BPU_T_Mecs_Ctx ** ctx) { BPU_T_Mecs_Ctx *ctx_p = *ctx; if (!ctx_p) { return 0; } switch (ctx_p->type) { - case BPU_EN_MECS_BASIC_GOPPA: + case BPU_EN_MECS_BASIC_GOPPA: #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - case BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA: + case BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA: #endif ctx_p->_encrypt = NULL; ctx_p->_decrypt = NULL; ctx_p->_genKeyPair = NULL; - break; + break; case BPU_EN_MECS_BASIC_QCMDPC: #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC case BPU_EN_MECS_CCA2_POINTCHEVAL_QCMDPC: @@ -178,29 +189,33 @@ int BPU_mecsFreeCtx(BPU_T_Mecs_Ctx **ctx) { ctx_p->_decrypt = NULL; ctx_p->_genKeyPair = NULL; break; - default: - BPU_printError("MECS type not supported: %d", ctx_p->type); - return BPU_EC_MECS_TYPE_NOT_SUPPORTED; - } - BPU_codeFreeCtx(&ctx_p->code_ctx); - free(ctx_p); + default: + BPU_printError ("MECS type not supported: %d", ctx_p->type); + return BPU_EC_MECS_TYPE_NOT_SUPPORTED; + } + BPU_codeFreeCtx (&ctx_p->code_ctx); + free (ctx_p); *ctx = NULL; - return 0; + return 0; } -int BPU_mecsInitParamsGoppa(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) { - return BPU_codeInitParamsGoppa((BPU_T_UN_Code_Params *)params, m, t, mod); +int BPU_mecsInitParamsGoppa (BPU_T_UN_Mecs_Params * params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod) { + return BPU_codeInitParamsGoppa ((BPU_T_UN_Code_Params *) params, m, t, mod); } -void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params) { - BPU_codeFreeParamsGoppa((BPU_T_UN_Code_Params *)params); +void BPU_mecsFreeParamsGoppa (BPU_T_UN_Mecs_Params * params) { + BPU_codeFreeParamsGoppa ((BPU_T_UN_Code_Params *) params); } -int BPU_mecsInitParamsQcmdpc(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t) { - return BPU_codeInitParamsQcmdpc((BPU_T_UN_Code_Params *)params, m, n0, w, t); +int BPU_mecsInitParamsQcmdpc (BPU_T_UN_Mecs_Params * params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t) { + return BPU_codeInitParamsQcmdpc ((BPU_T_UN_Code_Params *) params, m, n0, w, + t); } -void BPU_mecsFreeParamsQcmdpc(BPU_T_UN_Mecs_Params *params) { - BPU_codeFreeParamsQcmdpc((BPU_T_UN_Code_Params *)params); +void BPU_mecsFreeParamsQcmdpc (BPU_T_UN_Mecs_Params * params) { + BPU_codeFreeParamsQcmdpc ((BPU_T_UN_Code_Params *) params); } diff --git a/lib/src/bitpunch/crypto/mecsctx.h b/lib/src/bitpunch/crypto/mecsctx.h index 0498441..217d16c 100644 --- a/lib/src/bitpunch/crypto/mecsctx.h +++ b/lib/src/bitpunch/crypto/mecsctx.h @@ -25,38 +25,42 @@ along with this program. If not, see . /// Possible types of MECS typedef enum _BPU_T_EN_Mecs_Types { - BPU_EN_MECS_BASIC_GOPPA = 1, ///< basic mecs, without any conversion + BPU_EN_MECS_BASIC_GOPPA = 1, ///< basic mecs, without any conversion #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA, ///< adapted Pointcheval's cca2 conversion + BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA, ///< adapted Pointcheval's cca2 conversion #endif - BPU_EN_MECS_BASIC_QCMDPC, ///< basic qc-mdpc mecs, without any conversion + BPU_EN_MECS_BASIC_QCMDPC, ///< basic qc-mdpc mecs, without any conversion #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC - BPU_EN_MECS_CCA2_POINTCHEVAL_QCMDPC, ///< adapted Pointcheval's cca2 conversion for qcmdpc + BPU_EN_MECS_CCA2_POINTCHEVAL_QCMDPC, ///< adapted Pointcheval's cca2 conversion for qcmdpc #endif -}BPU_T_EN_Mecs_Types; +} BPU_T_EN_Mecs_Types; typedef struct _BPU_T_Mecs_Ctx { - BPU_T_EN_Mecs_Types type; - int (* _encrypt)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Mecs_Ctx *ctx); - int (* _decrypt)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Mecs_Ctx *ctx); - int (* _genKeyPair)(struct _BPU_T_Code_Ctx *ctx); - - BPU_T_Code_Ctx *code_ctx; - uint16_t pt_len; ///< PT len in bits - uint16_t ct_len; ///< CT len in bits -}BPU_T_Mecs_Ctx; + BPU_T_EN_Mecs_Types type; + int (*_encrypt) (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Mecs_Ctx * ctx); + int (*_decrypt) (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const struct _BPU_T_Mecs_Ctx * ctx); + int (*_genKeyPair) (struct _BPU_T_Code_Ctx * ctx); + + BPU_T_Code_Ctx *code_ctx; + uint16_t pt_len; ///< PT len in bits + uint16_t ct_len; ///< CT len in bits +} BPU_T_Mecs_Ctx; typedef union _BPU_T_UN_Code_Params BPU_T_UN_Mecs_Params; /// Initialize (register) mecs functions for encryption, decryption and key gen based on type. -int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, const BPU_T_EN_Mecs_Types type); +int BPU_mecsInitCtx (BPU_T_Mecs_Ctx ** ctx, const BPU_T_UN_Mecs_Params * params, + const BPU_T_EN_Mecs_Types type); -int BPU_mecsFreeCtx(BPU_T_Mecs_Ctx **ctx); +int BPU_mecsFreeCtx (BPU_T_Mecs_Ctx ** ctx); // Available init params for all codes, it is only wrapper for code params init -int BPU_mecsInitParamsGoppa(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod); +int BPU_mecsInitParamsGoppa (BPU_T_UN_Mecs_Params * params, const uint16_t m, + const uint16_t t, const BPU_T_GF2_16x mod); -void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params); +void BPU_mecsFreeParamsGoppa (BPU_T_UN_Mecs_Params * params); /** * Wrapper for param initialization. After work you have to free memory using call BPU_mecsFreeParamsQcmdpc @@ -67,13 +71,15 @@ void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params); * @param t count of errors * @return [description] */ -int BPU_mecsInitParamsQcmdpc(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t); +int BPU_mecsInitParamsQcmdpc (BPU_T_UN_Mecs_Params * params, const uint16_t m, + const uint16_t n0, const uint16_t w, + const uint16_t t); /** * Free memory for params. * @param params pointer to structure */ -void BPU_mecsFreeParamsQcmdpc(BPU_T_UN_Mecs_Params *params); +void BPU_mecsFreeParamsQcmdpc (BPU_T_UN_Mecs_Params * params); // add yours additional params if needed diff --git a/lib/src/bitpunch/crypto/padding/padding.c b/lib/src/bitpunch/crypto/padding/padding.c index e286338..e0e9dd4 100644 --- a/lib/src/bitpunch/crypto/padding/padding.c +++ b/lib/src/bitpunch/crypto/padding/padding.c @@ -24,56 +24,60 @@ along with this program. If not, see . #include #include -int BPU_padAdd(BPU_T_GF2_Vector *padded_message, const BPU_T_GF2_Vector *message, const uint16_t padding_len) { - int i; +int BPU_padAdd (BPU_T_GF2_Vector * padded_message, + const BPU_T_GF2_Vector * message, const uint16_t padding_len) { + int i; - if (message->len + padding_len != padded_message->len) { - BPU_printError("Wrong message len"); - return -1; - } - // copy message into padded message - for (i = 0; i < message->array_length; i++){ - padded_message->elements[i] = message->elements[i]; - } - // add padding - first padded bit set to 1, rest keep 0 - BPU_gf2VecSetBit(padded_message, message->len, 1); + if (message->len + padding_len != padded_message->len) { + BPU_printError ("Wrong message len"); + return -1; + } + // copy message into padded message + for (i = 0; i < message->array_length; i++) { + padded_message->elements[i] = message->elements[i]; + } + // add padding - first padded bit set to 1, rest keep 0 + BPU_gf2VecSetBit (padded_message, message->len, 1); - return 0; + return 0; } -int BPU_padDel(BPU_T_GF2_Vector *message, const BPU_T_GF2_Vector *padded_message) { - int i, message_size = 0; +int BPU_padDel (BPU_T_GF2_Vector * message, + const BPU_T_GF2_Vector * padded_message) { + int i, message_size = 0; - // count the message size - for (i = padded_message->len-1; i >= 0; i--) { - // nula - padding - if (BPU_gf2VecGetBit(padded_message, i) == 1) { + // count the message size + for (i = padded_message->len - 1; i >= 0; i--) { + // nula - padding + if (BPU_gf2VecGetBit (padded_message, i) == 1) { - // ci bola aspon jedna 0 pred 1 - //if (i <= padded_message->len-3) { - message_size = i; - break; - //} - // inak zly padding - /*else { - BPU_printError("del_padding: message padding is incorrect"); - return -1; - }*/ - } - } - if (message->len < message_size) { - BPU_printError("Wrong message size."); - return -1; - } - message->len = message_size; + // ci bola aspon jedna 0 pred 1 + //if (i <= padded_message->len-3) { + message_size = i; + break; + //} + // inak zly padding + /*else { + BPU_printError("del_padding: message padding is incorrect"); + return -1; + } */ + } + } + if (message->len < message_size) { + BPU_printError ("Wrong message size."); + return -1; + } + message->len = message_size; - // copy n-1 elements of padded message into message - for (i = 0; i < padded_message->array_length - 1; i++){ - message->elements[i] = padded_message->elements[i]; - } - // copy the rest of message - for (i = (padded_message->array_length - 1) * padded_message->element_bit_size; i < message->len; i++){ - BPU_gf2VecSetBit(message, i, BPU_gf2VecGetBit(padded_message, i)); - } - return 0; + // copy n-1 elements of padded message into message + for (i = 0; i < padded_message->array_length - 1; i++) { + message->elements[i] = padded_message->elements[i]; + } + // copy the rest of message + for (i = + (padded_message->array_length - 1) * padded_message->element_bit_size; + i < message->len; i++) { + BPU_gf2VecSetBit (message, i, BPU_gf2VecGetBit (padded_message, i)); + } + return 0; } diff --git a/lib/src/bitpunch/crypto/padding/padding.h b/lib/src/bitpunch/crypto/padding/padding.h index 8f18155..450a599 100644 --- a/lib/src/bitpunch/crypto/padding/padding.h +++ b/lib/src/bitpunch/crypto/padding/padding.h @@ -32,7 +32,8 @@ along with this program. If not, see . * @return 0 on succes else, non-zero value */ /// Add padding to message. -int BPU_padAdd(BPU_T_GF2_Vector *padded_message, const BPU_T_GF2_Vector *message, const uint16_t padding_len); +int BPU_padAdd (BPU_T_GF2_Vector * padded_message, + const BPU_T_GF2_Vector * message, const uint16_t padding_len); /** * Delete padding from message. Padding begins with 1. @@ -41,6 +42,7 @@ int BPU_padAdd(BPU_T_GF2_Vector *padded_message, const BPU_T_GF2_Vector *message * @return 0 on succes else, non-zero value */ /// Delete padding from message. -int BPU_padDel(BPU_T_GF2_Vector *message, const BPU_T_GF2_Vector *padded_message); +int BPU_padDel (BPU_T_GF2_Vector * message, + const BPU_T_GF2_Vector * padded_message); #endif // PADDING_H diff --git a/lib/src/bitpunch/debugio.h b/lib/src/bitpunch/debugio.h index 2585c3b..b581dcb 100644 --- a/lib/src/bitpunch/debugio.h +++ b/lib/src/bitpunch/debugio.h @@ -28,39 +28,39 @@ #include #ifndef _WIN32 - #include +#include /// print macro for 64bit data types - #define BPU_PRINT_U_64 PRIu64 +#define BPU_PRINT_U_64 PRIu64 #else - #define BPU_PRINT_U_64 "I64u" +#define BPU_PRINT_U_64 "I64u" #endif #if defined(DEBUG_L) || defined(WARNING_L) || defined(ERROR_L) - #include - #include +#include +#include /// print error message with filename, line #define BPU_printError(fmt, ...) fprintf(stderr, "ERROR::%s::%s::%d: "fmt"\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); if (errno) {perror("ERROR MSG:"); errno = 0;} #else /// print error message with filename, line - #define BPU_printError(fmt, ...) +#define BPU_printError(fmt, ...) #endif #if defined(DEBUG_L) || defined(WARNING_L) /// print warning message with filename, line - #define BPU_printWarning(fmt, ...) fprintf(stderr, "WARNING::%s::%s::%d: "fmt"\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); if (errno) {perror("ERROR MSG:"); errno = 0;} +#define BPU_printWarning(fmt, ...) fprintf(stderr, "WARNING::%s::%s::%d: "fmt"\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); if (errno) {perror("ERROR MSG:"); errno = 0;} #else /// print warning message with filename, line - #define BPU_printWarning(fmt, ...) +#define BPU_printWarning(fmt, ...) #endif #if defined(DEBUG_L) /// print debug message with filename, line - #define BPU_printDebug(fmt, ...) fprintf(stderr, "DEBUG::%s::%s::%d: "fmt"\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__) +#define BPU_printDebug(fmt, ...) fprintf(stderr, "DEBUG::%s::%s::%d: "fmt"\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__) #else /// print debug message with filename, line - #define BPU_printDebug(fmt, ...) +#define BPU_printDebug(fmt, ...) #endif #endif // DEBUGIO_H diff --git a/lib/src/bitpunch/math/bigint.c b/lib/src/bitpunch/math/bigint.c index 2c2a409..91a950c 100644 --- a/lib/src/bitpunch/math/bigint.c +++ b/lib/src/bitpunch/math/bigint.c @@ -19,50 +19,51 @@ along with this program. If not, see . #include "bigint.h" #include -int BPU_bigintMultiply(BPU_T_Bigint *out, BPU_T_Bigint *a, BPU_T_Bigint *b) { +int BPU_bigintMultiply (BPU_T_Bigint * out, BPU_T_Bigint * a, BPU_T_Bigint * b) { uint32_t tmp, carry; uint32_t i, j; if ((a->len + b->len) > out->len) { - BPU_printDebug("Resizing big int output to %d", a->len + b->len); + BPU_printDebug ("Resizing big int output to %d", a->len + b->len); - BPU_bigintResize(out, a->len + b->len); + BPU_bigintResize (out, a->len + b->len); } else { - BPU_bigintNull(out); + BPU_bigintNull (out); } carry = 0; - for(i = 0; i < b->array_length; i++) { - for(j = 0; j < a->array_length; j++) { + for (i = 0; i < b->array_length; i++) { + for (j = 0; j < a->array_length; j++) { tmp = (a->elements[j] * b->elements[i]) + out->elements[i + j]; carry = tmp >> a->element_bit_size; - out->elements[i + j] = (BPU_T_Element)tmp; - out->elements[i + j + 1] += (BPU_T_Element)carry; + out->elements[i + j] = (BPU_T_Element) tmp; + out->elements[i + j + 1] += (BPU_T_Element) carry; } } return 0; } -int BPU_bigintAdd(BPU_T_Bigint *out, BPU_T_Bigint *a, BPU_T_Bigint *b) { +int BPU_bigintAdd (BPU_T_Bigint * out, BPU_T_Bigint * a, BPU_T_Bigint * b) { uint32_t tmp, carry; uint32_t i; uint32_t tmplen = (a->len > b->len) ? a->len : b->len; - uint16_t array_len = (a->array_length > b->array_length) ? a->array_length : b->array_length; + uint16_t array_len = + (a->array_length > b->array_length) ? a->array_length : b->array_length; tmplen++; if (tmplen > out->len) { - BPU_printDebug("Resizing big int output to %d", tmplen); + BPU_printDebug ("Resizing big int output to %d", tmplen); - BPU_bigintResize(out, tmplen); + BPU_bigintResize (out, tmplen); } else { - BPU_bigintNull(out); + BPU_bigintNull (out); } carry = 0; - for(i = 0; i < array_len; i++) { + for (i = 0; i < array_len; i++) { tmp = carry; if (i < a->array_length) { diff --git a/lib/src/bitpunch/math/bigint.h b/lib/src/bitpunch/math/bigint.h index a972802..902138a 100644 --- a/lib/src/bitpunch/math/bigint.h +++ b/lib/src/bitpunch/math/bigint.h @@ -20,9 +20,9 @@ along with this program. If not, see . typedef BPU_T_Element_Array BPU_T_Bigint; -int BPU_bigintMultiply(BPU_T_Bigint *out, BPU_T_Bigint *a, BPU_T_Bigint *b); +int BPU_bigintMultiply (BPU_T_Bigint * out, BPU_T_Bigint * a, BPU_T_Bigint * b); -int BPU_bigintAdd(BPU_T_Bigint *out, BPU_T_Bigint *a, BPU_T_Bigint *b); +int BPU_bigintAdd (BPU_T_Bigint * out, BPU_T_Bigint * a, BPU_T_Bigint * b); #define BPU_bigintNull(b_pointer) BPU_elementArrayNull(b_pointer) diff --git a/lib/src/bitpunch/math/gf2.c b/lib/src/bitpunch/math/gf2.c index 6a2d29e..8d5a77d 100644 --- a/lib/src/bitpunch/math/gf2.c +++ b/lib/src/bitpunch/math/gf2.c @@ -29,119 +29,124 @@ along with this program. If not, see . #ifdef BPU_CONF_PRINT /* ==================================== Print functions ==================================== */ -void BPU_printGf2Mat(const BPU_T_GF2_Matrix* m) { - int i, j, bits_to_print; - - fprintf(stderr, "Matrix size: %dx%d\n", m->k, m->n); - - for (i = 0; i < m->k; i++) { - fprintf(stderr, "%4d: ",i); - - for (j = 0; j <= m->elements_in_row - 1; j++) { - if (j == m->elements_in_row-1) { - if (m->n%(m->element_bit_size) != 0) { - bits_to_print = m->n % m->element_bit_size; - } - else { - bits_to_print = m->element_bit_size; - } - } - else { - bits_to_print = m->element_bit_size; - } - BPU_printBinaryLsb(m->elements[i][j], bits_to_print); - // fprintf(stderr, " "); // medzera medzi elementami - } - fprintf(stderr, "\n"); - } +void BPU_printGf2Mat (const BPU_T_GF2_Matrix * m) { + int i, j, bits_to_print; + + fprintf (stderr, "Matrix size: %dx%d\n", m->k, m->n); + + for (i = 0; i < m->k; i++) { + fprintf (stderr, "%4d: ", i); + + for (j = 0; j <= m->elements_in_row - 1; j++) { + if (j == m->elements_in_row - 1) { + if (m->n % (m->element_bit_size) != 0) { + bits_to_print = m->n % m->element_bit_size; + } + else { + bits_to_print = m->element_bit_size; + } + } + else { + bits_to_print = m->element_bit_size; + } + BPU_printBinaryLsb (m->elements[i][j], bits_to_print); + // fprintf(stderr, " "); // medzera medzi elementami + } + fprintf (stderr, "\n"); + } } -void BPU_printGf2SparsePoly (const BPU_T_GF2_Sparse_Poly *v) { - int i; +void BPU_printGf2SparsePoly (const BPU_T_GF2_Sparse_Poly * v) { + int i; - fprintf(stderr, "Sparse poly (%i): ", v->weight); - for (i = 0; i < v->weight; i++) { - fprintf(stderr, "%3i ", v->index[i]); - } - fprintf(stderr, "\n"); + fprintf (stderr, "Sparse poly (%i): ", v->weight); + for (i = 0; i < v->weight; i++) { + fprintf (stderr, "%3i ", v->index[i]); + } + fprintf (stderr, "\n"); } -void BPU_printGf2PolyForMatrix(const BPU_T_GF2_Poly* v) { - int j, bits_to_print; - - for (j = 0; j < v->array_length; j++) { - if (j == v->array_length-1) { - if (v->len % (v->element_bit_size) != 0) { - bits_to_print = v->len % v->element_bit_size; - } - else { - bits_to_print = v->element_bit_size; - } - } - else { - bits_to_print = v->element_bit_size; +void BPU_printGf2PolyForMatrix (const BPU_T_GF2_Poly * v) { + int j, bits_to_print; + + for (j = 0; j < v->array_length; j++) { + if (j == v->array_length - 1) { + if (v->len % (v->element_bit_size) != 0) { + bits_to_print = v->len % v->element_bit_size; + } + else { + bits_to_print = v->element_bit_size; + } + } + else { + bits_to_print = v->element_bit_size; + } + BPU_printBinaryLsb (v->elements[j], bits_to_print); } - BPU_printBinaryLsb(v->elements[j], bits_to_print); - } } -void BPU_printGf2Poly(const BPU_T_GF2_Poly* v) { - int j, bits_to_print; - - fprintf(stderr, "Poly (%4d): ", v->len-1); - for (j = v->array_length - 1; j >= 0; j--) { - if (j == v->array_length-1) { - if (v->len % (v->element_bit_size) != 0) { - bits_to_print = v->len % v->element_bit_size; - } - else { - bits_to_print = v->element_bit_size; - } - } - else { - bits_to_print = v->element_bit_size; +void BPU_printGf2Poly (const BPU_T_GF2_Poly * v) { + int j, bits_to_print; + + fprintf (stderr, "Poly (%4d): ", v->len - 1); + for (j = v->array_length - 1; j >= 0; j--) { + if (j == v->array_length - 1) { + if (v->len % (v->element_bit_size) != 0) { + bits_to_print = v->len % v->element_bit_size; + } + else { + bits_to_print = v->element_bit_size; + } + } + else { + bits_to_print = v->element_bit_size; + } + BPU_printBinaryMsb (v->elements[j], bits_to_print); } - BPU_printBinaryMsb(v->elements[j], bits_to_print); - } - fprintf(stderr, "\n"); + fprintf (stderr, "\n"); } -void BPU_printGf2QcMatrix(const BPU_T_GF2_QC_Matrix *v) { - int ele, i; - BPU_T_GF2_Poly temp; - - fprintf(stderr, "%s QC Matrix(%i x %i) with %i elements", v->isVertical ? "VERTICAL" : "HORIZONTAL", (v->is_I_appended ? v->k : 0) + v->n, v->k, v->element_count); - if (v->is_I_appended) - fprintf(stderr, " and Identity matrix"); - fprintf(stderr, "\n"); - - for (ele = 0; ele < v->element_count; ele++) { - BPU_gf2PolyCopy(&temp, &v->matrices[ele]); - for (i = 0; i < v->element_size; i++) { - BPU_printGf2PolyForMatrix(&temp); - fprintf(stderr, "\n"); - BPU_gf2PolyMulX(&temp); +void BPU_printGf2QcMatrix (const BPU_T_GF2_QC_Matrix * v) { + int ele, i; + BPU_T_GF2_Poly temp; + + fprintf (stderr, "%s QC Matrix(%i x %i) with %i elements", + v->isVertical ? "VERTICAL" : "HORIZONTAL", + (v->is_I_appended ? v->k : 0) + v->n, v->k, v->element_count); + if (v->is_I_appended) + fprintf (stderr, " and Identity matrix"); + fprintf (stderr, "\n"); + + for (ele = 0; ele < v->element_count; ele++) { + BPU_gf2PolyCopy (&temp, &v->matrices[ele]); + for (i = 0; i < v->element_size; i++) { + BPU_printGf2PolyForMatrix (&temp); + fprintf (stderr, "\n"); + BPU_gf2PolyMulX (&temp); + } + BPU_gf2PolyFree (&temp, 0); } - BPU_gf2PolyFree(&temp, 0); - } } -void BPU_printGf2SparseQcMatrix(const BPU_T_GF2_Sparse_Qc_Matrix *v) { - int i; - BPU_T_GF2_Sparse_Poly row; +void BPU_printGf2SparseQcMatrix (const BPU_T_GF2_Sparse_Qc_Matrix * v) { + int i; + BPU_T_GF2_Sparse_Poly row; - fprintf(stderr, "%s QC Matrix(%i x %i) with %i elements\n", v->isVertical ? "VERTICAL" : "HORIZONTAL", v->n, v->k, v->element_count); + fprintf (stderr, "%s QC Matrix(%i x %i) with %i elements\n", + v->isVertical ? "VERTICAL" : "HORIZONTAL", v->n, v->k, + v->element_count); - for (i = 0; i < v->k; i++) { - BPU_gf2SparseQcMatrixGetRow(&row, v, i); - BPU_printGf2SparsePoly(&row); - BPU_gf2SparsePolyFree(&row, 0); - } + for (i = 0; i < v->k; i++) { + BPU_gf2SparseQcMatrixGetRow (&row, v, i); + BPU_printGf2SparsePoly (&row); + BPU_gf2SparsePolyFree (&row, 0); + } } + /* ------------------------------------ Print functions ------------------------------------ */ #endif // BPU_CONF_PRINT -void BPU_gf2MatFree(BPU_T_GF2_Matrix **m) { +void BPU_gf2MatFree (BPU_T_GF2_Matrix ** m) { int i; if (!*m) { @@ -149,25 +154,25 @@ void BPU_gf2MatFree(BPU_T_GF2_Matrix **m) { } // first free cols for (i = 0; i < (*m)->k; i++) { - free((*m)->elements[i]); + free ((*m)->elements[i]); } // then free rows - free((*m)->elements); - free((*m)); + free ((*m)->elements); + free ((*m)); *m = NULL; } -int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols) { +int BPU_gf2MatMalloc (BPU_T_GF2_Matrix ** m, int rows, int cols) { int i; - *m = (BPU_T_GF2_Matrix *) calloc(sizeof(BPU_T_GF2_Matrix), 1); + *m = (BPU_T_GF2_Matrix *) calloc (sizeof (BPU_T_GF2_Matrix), 1); if (!*m) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } // element size - (*m)->element_bit_size = sizeof(BPU_T_GF2)*8; + (*m)->element_bit_size = sizeof (BPU_T_GF2) * 8; // rows (*m)->k = rows; @@ -176,1015 +181,1086 @@ int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols) { // calc how many elements of set size will be in one row int modul = 0; - if ( cols % (*m)->element_bit_size > 0) { + + if (cols % (*m)->element_bit_size > 0) { modul = 1; } - (*m)->elements_in_row = cols/(*m)->element_bit_size + modul; + (*m)->elements_in_row = cols / (*m)->element_bit_size + modul; // allocate rows - (*m)->elements = (BPU_T_GF2**) malloc(sizeof(BPU_T_GF2*) * (*m)->k); + (*m)->elements = (BPU_T_GF2 **) malloc (sizeof (BPU_T_GF2 *) * (*m)->k); if (!(*m)->elements) { - BPU_printError("can not allocate memory for matrix rows"); + BPU_printError ("can not allocate memory for matrix rows"); return -1; } // allocate cols for (i = 0; i < (*m)->k; i++) { - (*m)->elements[i] = (BPU_T_GF2*) calloc(1, sizeof(BPU_T_GF2) * (*m)->elements_in_row); + (*m)->elements[i] = + (BPU_T_GF2 *) calloc (1, + sizeof (BPU_T_GF2) * (*m)->elements_in_row); if (!(*m)->elements[i]) { - BPU_printError("can not allocate memory for matrix cols"); + BPU_printError ("can not allocate memory for matrix cols"); return -2; } } return 0; } -void BPU_gf2SparsePolyMalloc(BPU_T_GF2_Sparse_Poly *p, int weight) { - // allocate indexes - p->index = (uint32_t*) malloc(weight*sizeof(uint32_t)); - // set weight - p->weight = weight; +void BPU_gf2SparsePolyMalloc (BPU_T_GF2_Sparse_Poly * p, int weight) { + // allocate indexes + p->index = (uint32_t *) malloc (weight * sizeof (uint32_t)); + // set weight + p->weight = weight; } -void BPU_gf2SparsePolyFree(BPU_T_GF2_Sparse_Poly *p, int is_dyn) { - free(p->index); +void BPU_gf2SparsePolyFree (BPU_T_GF2_Sparse_Poly * p, int is_dyn) { + free (p->index); - if (is_dyn) - free(p); + if (is_dyn) + free (p); } -void BPU_gf2SparseQcMatrixMalloc(BPU_T_GF2_Sparse_Qc_Matrix *v, int element_count, int element_size, int isVertical) { - // allocate matrices - v->matrices = (BPU_T_GF2_Sparse_Poly*) malloc(element_count*sizeof(BPU_T_GF2_Sparse_Poly)); - - // set sizes depended on orientation - if (isVertical) { - v->k = element_count * element_size; - v->n = element_size; - } - else { - v->k = element_size; - v->n = element_count * element_size; - } - - // set others - v->element_count = element_count; - v->element_size = element_size; - v->isVertical = isVertical; +void BPU_gf2SparseQcMatrixMalloc (BPU_T_GF2_Sparse_Qc_Matrix * v, + int element_count, int element_size, + int isVertical) { + // allocate matrices + v->matrices = + (BPU_T_GF2_Sparse_Poly *) malloc (element_count * + sizeof (BPU_T_GF2_Sparse_Poly)); + + // set sizes depended on orientation + if (isVertical) { + v->k = element_count * element_size; + v->n = element_size; + } + else { + v->k = element_size; + v->n = element_count * element_size; + } + + // set others + v->element_count = element_count; + v->element_size = element_size; + v->isVertical = isVertical; } -void BPU_gf2SparseQcMatrixFree(BPU_T_GF2_Sparse_Qc_Matrix *v, int is_dyn) { - int i; +void BPU_gf2SparseQcMatrixFree (BPU_T_GF2_Sparse_Qc_Matrix * v, int is_dyn) { + int i; - // free matrices - for (i = 0; i < v->element_count; i++) - BPU_gf2SparsePolyFree(&v->matrices[i], is_dyn); + // free matrices + for (i = 0; i < v->element_count; i++) + BPU_gf2SparsePolyFree (&v->matrices[i], is_dyn); - free(v->matrices); + free (v->matrices); - if (is_dyn) - free (v); + if (is_dyn) + free (v); } -void BPU_gf2PolyFree(BPU_T_GF2_Poly *p, int is_dyn) { - free(p->elements); +void BPU_gf2PolyFree (BPU_T_GF2_Poly * p, int is_dyn) { + free (p->elements); - if (is_dyn) { - free(p); - } + if (is_dyn) { + free (p); + } } -int BPU_gf2PolyMalloc(BPU_T_GF2_Poly *p, int len) { - // element size in bits - p->element_bit_size = sizeof(BPU_T_GF2) * 8; +int BPU_gf2PolyMalloc (BPU_T_GF2_Poly * p, int len) { + // element size in bits + p->element_bit_size = sizeof (BPU_T_GF2) * 8; - // len - p->len = len; + // len + p->len = len; - // calc how many elements of set size will be in one row - int modul = 0; + // calc how many elements of set size will be in one row + int modul = 0; - if ( len % p->element_bit_size > 0) { - modul = 1; - } - p->array_length = len / p->element_bit_size + modul; + if (len % p->element_bit_size > 0) { + modul = 1; + } + p->array_length = len / p->element_bit_size + modul; - // allocate elemtens - p->elements = (BPU_T_GF2*) calloc(1, sizeof(BPU_T_GF2) * p->array_length); + // allocate elemtens + p->elements = + (BPU_T_GF2 *) calloc (1, sizeof (BPU_T_GF2) * p->array_length); - if (!p->elements) { - BPU_printError("can not allocate memory for vector of len %d", len); - return 1; - } - return 0; + if (!p->elements) { + BPU_printError ("can not allocate memory for vector of len %d", len); + return 1; + } + return 0; } -int BPU_gf2QcMatrixMalloc(BPU_T_GF2_QC_Matrix *v, int element_count, int element_size, int isVertical, int is_I_appended) { - int err = 0; +int BPU_gf2QcMatrixMalloc (BPU_T_GF2_QC_Matrix * v, int element_count, + int element_size, int isVertical, + int is_I_appended) { + int err = 0; - // check isVertical - if (isVertical != 0 && isVertical != 1) { - return -1; - } - - // allocate matrices - v->matrices = (BPU_T_GF2_Poly*) malloc(element_count*sizeof(BPU_T_GF2_Poly)); - - // set sizes depended on orientation - if (isVertical) { - v->k = element_count * element_size; - v->n = element_size; - } - else { - v->k = element_size; - v->n = element_count * element_size; - } - - // set others - v->element_count = element_count; - v->element_size = element_size; - v->is_I_appended = is_I_appended; - v->isVertical = isVertical; - - return err; + // check isVertical + if (isVertical != 0 && isVertical != 1) { + return -1; + } + + // allocate matrices + v->matrices = + (BPU_T_GF2_Poly *) malloc (element_count * sizeof (BPU_T_GF2_Poly)); + + // set sizes depended on orientation + if (isVertical) { + v->k = element_count * element_size; + v->n = element_size; + } + else { + v->k = element_size; + v->n = element_count * element_size; + } + + // set others + v->element_count = element_count; + v->element_size = element_size; + v->is_I_appended = is_I_appended; + v->isVertical = isVertical; + + return err; } // free QC binary matrix -void BPU_gf2QcMatrixFree(BPU_T_GF2_QC_Matrix *v, int is_dyn) { - int i; +void BPU_gf2QcMatrixFree (BPU_T_GF2_QC_Matrix * v, int is_dyn) { + int i; - // free matrices - for (i = 0; i < v->element_count; i++) { - BPU_gf2PolyFree(&v->matrices[i], 0); - } + // free matrices + for (i = 0; i < v->element_count; i++) { + BPU_gf2PolyFree (&v->matrices[i], 0); + } - free(v->matrices); + free (v->matrices); - if (is_dyn) - free (v); + if (is_dyn) + free (v); } -int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w) { - int i, j; - - if (w > out->len) { - BPU_printError("weight error w > l"); - return -2; - } - //vector of random weight - if(w == 0) { - for(i = 0; i < out->len; i++) { - BPU_gf2VecSetBit(out, i, BPU_prngGetRand(0, 2)); - } - } - //vector of required weight - else { - BPU_gf2VecNull(out); - - for(i = 0; i < w; i++) { - j = BPU_prngGetRand(0, out->len); - - if(BPU_gf2VecGetBit(out, j) == 0) { - BPU_gf2VecSetBit(out, j, 1); - } - else{ - i--; - } - } - } - return 0; +int BPU_gf2VecRand (BPU_T_GF2_Vector * out, int w) { + int i, j; + + if (w > out->len) { + BPU_printError ("weight error w > l"); + return -2; + } + //vector of random weight + if (w == 0) { + for (i = 0; i < out->len; i++) { + BPU_gf2VecSetBit (out, i, BPU_prngGetRand (0, 2)); + } + } + //vector of required weight + else { + BPU_gf2VecNull (out); + + for (i = 0; i < w; i++) { + j = BPU_prngGetRand (0, out->len); + + if (BPU_gf2VecGetBit (out, j) == 0) { + BPU_gf2VecSetBit (out, j, 1); + } + else { + i--; + } + } + } + return 0; } -int BPU_gf2MatCopy(BPU_T_GF2_Matrix *out, const BPU_T_GF2_Matrix *in) { - int i, j; +int BPU_gf2MatCopy (BPU_T_GF2_Matrix * out, const BPU_T_GF2_Matrix * in) { + int i, j; - if (out->k != in->k || out->n != in->n) { - BPU_printError("BPU_gf2MatCopy: wrong matrix size"); + if (out->k != in->k || out->n != in->n) { + BPU_printError ("BPU_gf2MatCopy: wrong matrix size"); - return -1; - } + return -1; + } - // copy the matrix - for (i = 0; i < in->k; i++) { - for (j = 0; j < in->elements_in_row; j++) { - out->elements[i][j] = in->elements[i][j]; - } - } - return 0; + // copy the matrix + for (i = 0; i < in->k; i++) { + for (j = 0; j < in->elements_in_row; j++) { + out->elements[i][j] = in->elements[i][j]; + } + } + return 0; } -int BPU_gf2VecPermute(BPU_T_GF2_Vector *vec, const BPU_T_Perm_Vector *permutation) { - int i; +int BPU_gf2VecPermute (BPU_T_GF2_Vector * vec, + const BPU_T_Perm_Vector * permutation) { + int i; BPU_T_GF2_Vector *tmp; - BPU_gf2VecMalloc(&tmp, vec->len); + BPU_gf2VecMalloc (&tmp, vec->len); - for (i = 0; i < permutation->size; i++) { - BPU_gf2VecSetBit(tmp, i, BPU_gf2VecGetBit(vec, permutation->elements[i])); - } - BPU_gf2VecCopy(vec, tmp); + for (i = 0; i < permutation->size; i++) { + BPU_gf2VecSetBit (tmp, i, + BPU_gf2VecGetBit (vec, permutation->elements[i])); + } + BPU_gf2VecCopy (vec, tmp); - BPU_gf2VecFree(&tmp); + BPU_gf2VecFree (&tmp); - return 0; + return 0; } -BPU_T_GF2 BPU_gf2MatGetMaskedBit(const BPU_T_GF2_Matrix *m, uint32_t row, uint32_t bit) { - int segment, bit_in_segment; +BPU_T_GF2 BPU_gf2MatGetMaskedBit (const BPU_T_GF2_Matrix * m, uint32_t row, + uint32_t bit) { + int segment, bit_in_segment; - segment = bit / (m->element_bit_size); - bit_in_segment = bit % (m->element_bit_size); - // TODO: consider repleacing di literal 1u - return m->elements[row][segment] & ((uint32_t)1 << bit_in_segment); + segment = bit / (m->element_bit_size); + bit_in_segment = bit % (m->element_bit_size); + // TODO: consider repleacing di literal 1u + return m->elements[row][segment] & ((uint32_t) 1 << bit_in_segment); } -BPU_T_GF2 BPU_gf2VecGetMaskedBit(const BPU_T_GF2_Vector *vec, uint32_t bit) { - int segment, bit_in_segment; +BPU_T_GF2 BPU_gf2VecGetMaskedBit (const BPU_T_GF2_Vector * vec, uint32_t bit) { + int segment, bit_in_segment; - segment = bit / (vec->element_bit_size); - bit_in_segment = bit % (vec->element_bit_size); - // TODO: consider repleacing di literal 1u - return vec->elements[segment] & ((uint32_t)1 << bit_in_segment); + segment = bit / (vec->element_bit_size); + bit_in_segment = bit % (vec->element_bit_size); + // TODO: consider repleacing di literal 1u + return vec->elements[segment] & ((uint32_t) 1 << bit_in_segment); } -int BPU_gf2MatTransp(BPU_T_GF2_Matrix *out, const BPU_T_GF2_Matrix *in) { - int i, j; +int BPU_gf2MatTransp (BPU_T_GF2_Matrix * out, const BPU_T_GF2_Matrix * in) { + int i, j; - if (out->k != in->n || out->n != in->k) { - BPU_printError("Wrong matrix dimenzion"); + if (out->k != in->n || out->n != in->k) { + BPU_printError ("Wrong matrix dimenzion"); - return -1; - } - for (i = 0; i < in->k; i++) { - for (j = 0; j < in->n; j++) { - BPU_gf2MatSetBit(out, j, i, BPU_gf2MatGetBit(in, i, j)); - } // col loop - } // row loop + return -1; + } + for (i = 0; i < in->k; i++) { + for (j = 0; j < in->n; j++) { + BPU_gf2MatSetBit (out, j, i, BPU_gf2MatGetBit (in, i, j)); + } // col loop + } // row loop - return 0; + return 0; } -void BPU_gf2Swap(BPU_T_GF2 *a, BPU_T_GF2 *b) { - BPU_T_GF2 tmp; - tmp = *a; - *a = *b; - *b = tmp; +void BPU_gf2Swap (BPU_T_GF2 * a, BPU_T_GF2 * b) { + BPU_T_GF2 tmp; + + tmp = *a; + *a = *b; + *b = tmp; } -void BPU_gf2MatSwapRows(BPU_T_GF2_Matrix *mat, int i, int j) { - int k; - for (k = 0; k < mat->elements_in_row; k++) { - BPU_gf2Swap(&(mat->elements[i][k]), &(mat->elements[j][k])); - } +void BPU_gf2MatSwapRows (BPU_T_GF2_Matrix * mat, int i, int j) { + int k; + + for (k = 0; k < mat->elements_in_row; k++) { + BPU_gf2Swap (&(mat->elements[i][k]), &(mat->elements[j][k])); + } } -int BPU_gf2MatFindRow(const BPU_T_GF2_Matrix *mat, int i, int start_index) { - int k; - for (k = start_index; k < mat->k; k++) { - if ( BPU_gf2MatGetMaskedBit(mat, k, i) ){ - return k; - } - } - return -1; +int BPU_gf2MatFindRow (const BPU_T_GF2_Matrix * mat, int i, int start_index) { + int k; + + for (k = start_index; k < mat->k; k++) { + if (BPU_gf2MatGetMaskedBit (mat, k, i)) { + return k; + } + } + return -1; } -int BPU_gf2MatFindCol(const BPU_T_GF2_Matrix *mat, int i, int start_index) { - int k; - for (k = start_index; k < mat->n; k++) { - if ( BPU_gf2MatGetMaskedBit(mat, i, k) ){ - return k; - } - } - return -1; +int BPU_gf2MatFindCol (const BPU_T_GF2_Matrix * mat, int i, int start_index) { + int k; + + for (k = start_index; k < mat->n; k++) { + if (BPU_gf2MatGetMaskedBit (mat, i, k)) { + return k; + } + } + return -1; } -int BPU_gf2MatMakeSystematic(BPU_T_GF2_Matrix *inout) { - int act_position = 0; - int i; - int row; - - for (act_position = 0; act_position < inout->k; act_position++) { - row = BPU_gf2MatFindRow(inout, act_position, act_position); - if (row == -1){ - BPU_printWarning("Not systematic"); - return -1; - } - BPU_gf2MatSwapRows(inout, act_position, row); - - // xor with the rest of rows if needed - for (i = 0; i < inout->k; i++) { - if ( BPU_gf2MatGetMaskedBit(inout, i, act_position) && act_position != i) { - BPU_gf2MatXorRows(inout, i, act_position); - } - } - } - return 0; +int BPU_gf2MatMakeSystematic (BPU_T_GF2_Matrix * inout) { + int act_position = 0; + int i; + int row; + + for (act_position = 0; act_position < inout->k; act_position++) { + row = BPU_gf2MatFindRow (inout, act_position, act_position); + if (row == -1) { + BPU_printWarning ("Not systematic"); + return -1; + } + BPU_gf2MatSwapRows (inout, act_position, row); + + // xor with the rest of rows if needed + for (i = 0; i < inout->k; i++) { + if (BPU_gf2MatGetMaskedBit (inout, i, act_position) + && act_position != i) { + BPU_gf2MatXorRows (inout, i, act_position); + } + } + } + return 0; } -int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const BPU_T_GF2_Vector *vec2) { - int len = vec1->len + vec2->len; - int i; +int BPU_gf2VecConcat (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * vec1, + const BPU_T_GF2_Vector * vec2) { + int len = vec1->len + vec2->len; + int i; - if (out->len != len) { - if (BPU_gf2VecResize(out, len)) { - BPU_printError("resize error"); + if (out->len != len) { + if (BPU_gf2VecResize (out, len)) { + BPU_printError ("resize error"); return -1; } - } - else { - BPU_gf2VecNull(out); - } - // copy first vector - BPU_gf2VecCopy(out, vec1); - - // copy second vector - for (i = 0; i < vec2->len; i++) { - BPU_gf2VecSetBit(out, i + vec1->len, BPU_gf2VecGetBit(vec2, i)); - } - out->len = len; - - return 0; + } + else { + BPU_gf2VecNull (out); + } + // copy first vector + BPU_gf2VecCopy (out, vec1); + + // copy second vector + for (i = 0; i < vec2->len; i++) { + BPU_gf2VecSetBit (out, i + vec1->len, BPU_gf2VecGetBit (vec2, i)); + } + out->len = len; + + return 0; } -int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int start, const int length) { - int i; - int counter = 0; - - //input test - if (start < 0 || (length+start) > in->len) { - BPU_printError("cropVector: bad params"); - return -1; - } - if (out->len < length) { - BPU_printError("cropVector: out vector is smaller then needed"); - return -1; - } - for (i = start; i < start+length; i++) { - BPU_gf2VecSetBit(out, counter, BPU_gf2VecGetBit(in, i)); - counter++; - } - return 0; +int BPU_gf2VecCrop (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const int start, const int length) { + int i; + int counter = 0; + + //input test + if (start < 0 || (length + start) > in->len) { + BPU_printError ("cropVector: bad params"); + return -1; + } + if (out->len < length) { + BPU_printError ("cropVector: out vector is smaller then needed"); + return -1; + } + for (i = start; i < start + length; i++) { + BPU_gf2VecSetBit (out, counter, BPU_gf2VecGetBit (in, i)); + counter++; + } + return 0; } -int BPU_gf2MatGetRowAsGf2Vec(BPU_T_GF2_Vector *out, const BPU_T_GF2_Matrix *in, int row) { +int BPU_gf2MatGetRowAsGf2Vec (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Matrix * in, int row) { if (out->len != in->n) { - BPU_printError("dimension is wrong out->len %d != in->n %d", out->len, in->n); - return -1; - } - BPU_gf2MatCopyRowToVec(out, in, row); + BPU_printError ("dimension is wrong out->len %d != in->n %d", out->len, + in->n); + return -1; + } + BPU_gf2MatCopyRowToVec (out, in, row); - return 0; + return 0; } -void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src) { - // if there is not enough space resize +void BPU_gf2VecCopy (BPU_T_GF2_Vector * dest, const BPU_T_GF2_Vector * src) { + // if there is not enough space resize if (dest->array_length < src->array_length) { - BPU_gf2VecResize(dest, src->array_length * src->element_bit_size * sizeof(BPU_T_GF2)); - } - else { - BPU_gf2VecNull(dest); - } - memcpy((void *) (dest->elements), (void *) (src->elements), sizeof(BPU_T_GF2) * (src->array_length)); - - dest->len = src->len; + BPU_gf2VecResize (dest, + src->array_length * src->element_bit_size * + sizeof (BPU_T_GF2)); + } + else { + BPU_gf2VecNull (dest); + } + memcpy ((void *) (dest->elements), (void *) (src->elements), + sizeof (BPU_T_GF2) * (src->array_length)); + + dest->len = src->len; } -int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2) { - int i; +int BPU_gf2VecCmp (const BPU_T_GF2_Vector * v1, const BPU_T_GF2_Vector * v2) { + int i; - if (v1->len != v2->len) { + if (v1->len != v2->len) { return -1; - } - for (i = 0; i < v1->len; i++) { - if (BPU_gf2VecGetBit(v1, i) != BPU_gf2VecGetBit(v2, i)) { + } + for (i = 0; i < v1->len; i++) { + if (BPU_gf2VecGetBit (v1, i) != BPU_gf2VecGetBit (v2, i)) { return i + 1; - } - } - return 0; + } + } + return 0; } -int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in) { - int i; +int BPU_gf2VecXor (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in) { + int i; - if (out->len != in->len) { - BPU_printError("BPU_gf2VecXor: length error (el. in row) %d != %d, len %d != %d", out->array_length, in->array_length, out->len, in->len); + if (out->len != in->len) { + BPU_printError + ("BPU_gf2VecXor: length error (el. in row) %d != %d, len %d != %d", + out->array_length, in->array_length, out->len, in->len); - return -1; - } + return -1; + } for (i = 0; i < out->array_length; i++) { - out->elements[i] ^= in->elements[i]; - } - return 0; + out->elements[i] ^= in->elements[i]; + } + return 0; } -int BPU_gf2VecMulMat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *v, const BPU_T_GF2_Matrix *b) { - int i, j; +int BPU_gf2VecMulMat (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * v, + const BPU_T_GF2_Matrix * b) { + int i, j; - if ((v->len != b->k) || (out->len != b->n)) { - BPU_printError("wrong vector and matrix dimension v->len = %d, b->k = %d", v->len, b->k); - BPU_printError("wrong vector and matrix dimension out->len = %d, b->n = %d", out->len, b->n); + if ((v->len != b->k) || (out->len != b->n)) { + BPU_printError + ("wrong vector and matrix dimension v->len = %d, b->k = %d", v->len, + b->k); + BPU_printError + ("wrong vector and matrix dimension out->len = %d, b->n = %d", + out->len, b->n); - return -1; - } - // null elements - BPU_gf2VecNull(out); + return -1; + } + // null elements + BPU_gf2VecNull (out); - for (i = 0; i < v->len; i++) { - if (BPU_gf2VecGetBit(v, i)) { - // xor rows + for (i = 0; i < v->len; i++) { + if (BPU_gf2VecGetBit (v, i)) { + // xor rows for (j = 0; j < out->array_length; j++) { - out->elements[j] ^= b->elements[i][j]; - } - } - } - return 0; + out->elements[j] ^= b->elements[i][j]; + } + } + } + return 0; } -void BPU_gf2MatXorRows(BPU_T_GF2_Matrix *mat, int i, int j) { - int k; - - for (k = 0; k < mat->elements_in_row; k++) { - mat->elements[i][k] ^= mat->elements[j][k]; - } +void BPU_gf2MatXorRows (BPU_T_GF2_Matrix * mat, int i, int j) { + int k; + + for (k = 0; k < mat->elements_in_row; k++) { + mat->elements[i][k] ^= mat->elements[j][k]; + } } -int BPU_gf2MatPermute(BPU_T_GF2_Matrix *inout, BPU_T_Perm_Vector *permutation) { - int i, j; - int bit = 0; - BPU_T_GF2_Vector *vector; - int length; - - if (inout->n != permutation->size) { - BPU_printError("permutation size not correct m->n = %d, p->size = %d", inout->n, permutation->size); - - return -1; - } - length = inout->elements_in_row * (inout->element_bit_size / 8); - - BPU_gf2VecMalloc(&vector, permutation->size); - - for (i = 0; i < inout->k; i++) { - memcpy((vector->elements), inout->elements[i], length); - for (j = 0; j < permutation->size; j++) { - bit = BPU_gf2VecGetBit(vector, permutation->elements[j]); - BPU_gf2MatSetBit(inout, i, j, bit); - } - } - BPU_gf2VecFree(&vector); - return 0; +int BPU_gf2MatPermute (BPU_T_GF2_Matrix * inout, + BPU_T_Perm_Vector * permutation) { + int i, j; + int bit = 0; + BPU_T_GF2_Vector *vector; + int length; + + if (inout->n != permutation->size) { + BPU_printError ("permutation size not correct m->n = %d, p->size = %d", + inout->n, permutation->size); + + return -1; + } + length = inout->elements_in_row * (inout->element_bit_size / 8); + + BPU_gf2VecMalloc (&vector, permutation->size); + + for (i = 0; i < inout->k; i++) { + memcpy ((vector->elements), inout->elements[i], length); + for (j = 0; j < permutation->size; j++) { + bit = BPU_gf2VecGetBit (vector, permutation->elements[j]); + BPU_gf2MatSetBit (inout, i, j, bit); + } + } + BPU_gf2VecFree (&vector); + return 0; } -int BPU_gf2MatCrop(BPU_T_GF2_Matrix *m, uint16_t width) { - BPU_T_GF2_Vector *row, *cropped_row; - int length, i, new_length; +int BPU_gf2MatCrop (BPU_T_GF2_Matrix * m, uint16_t width) { + BPU_T_GF2_Vector *row, *cropped_row; + int length, i, new_length; - length = m->elements_in_row * (m->element_bit_size / 8); - BPU_gf2VecMalloc(&row, m->n); - BPU_gf2VecMalloc(&cropped_row, width); + length = m->elements_in_row * (m->element_bit_size / 8); + BPU_gf2VecMalloc (&row, m->n); + BPU_gf2VecMalloc (&cropped_row, width); new_length = cropped_row->array_length * (m->element_bit_size / 8); - for (i = 0; i < m->k; i++) { - memcpy(row->elements, m->elements[i], length); - BPU_gf2VecCrop(cropped_row, row, m->k, width); - free(m->elements[i]); - m->elements[i] = (BPU_T_GF2*) malloc(new_length); - memcpy(m->elements[i], cropped_row->elements, new_length); - } - m->n = cropped_row->len; + for (i = 0; i < m->k; i++) { + memcpy (row->elements, m->elements[i], length); + BPU_gf2VecCrop (cropped_row, row, m->k, width); + free (m->elements[i]); + m->elements[i] = (BPU_T_GF2 *) malloc (new_length); + memcpy (m->elements[i], cropped_row->elements, new_length); + } + m->n = cropped_row->len; m->elements_in_row = cropped_row->array_length; - BPU_gf2VecFree(&row); - BPU_gf2VecFree(&cropped_row); + BPU_gf2VecFree (&row); + BPU_gf2VecFree (&cropped_row); - return 0; + return 0; } -uint8_t BPU_getParity(BPU_T_GF2 dword) { - uint32_t tmp = dword; - tmp = (tmp >> 16) ^ tmp; - tmp = (tmp >> 8 ) ^ tmp; - tmp = (tmp >> 4 ) ^ tmp; - tmp = (tmp >> 2 ) ^ tmp; - tmp = (tmp >> 1 ) ^ tmp; - return tmp & 1; +uint8_t BPU_getParity (BPU_T_GF2 dword) { + uint32_t tmp = dword; + + tmp = (tmp >> 16) ^ tmp; + tmp = (tmp >> 8) ^ tmp; + tmp = (tmp >> 4) ^ tmp; + tmp = (tmp >> 2) ^ tmp; + tmp = (tmp >> 1) ^ tmp; + return tmp & 1; } -void BPU_gf2PolyCopy(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in) { +void BPU_gf2PolyCopy (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in) { - int i; - // allocate output poly - BPU_gf2PolyMalloc(out, in->len); - - // copy all elements - if (in->len != 0) - for (i = 0; i < in->array_length; i++) { - out->elements[i] = in->elements[i]; - } + int i; + + // allocate output poly + BPU_gf2PolyMalloc (out, in->len); + + // copy all elements + if (in->len != 0) + for (i = 0; i < in->array_length; i++) { + out->elements[i] = in->elements[i]; + } } -int BPU_gf2QcMatrixToSparse(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_QC_Matrix *in, const int wi[]) { - int i, counter, bit; - - // allocate output matrix - BPU_gf2SparseQcMatrixMalloc(out, in->element_count, in->element_size, 1); - - // transpose polynoms - for (i = 0; i < in->element_count; i++) { - counter = 0; - // allocate sparse poly - BPU_gf2SparsePolyMalloc(&out->matrices[i], wi[i]); - // set bits - for (bit = 0; bit < in->matrices[i].len; bit++) { - if (BPU_gf2VecGetBit(&in->matrices[i], bit) == 1ul) { - out->matrices[i].index[counter] = (uint32_t)(bit); - counter++; - } - } - // weight error - if (counter != wi[i]) { - BPU_printError("weight error. Weight should be %i, but is %i.\n", wi[i], counter); - return -1; +int BPU_gf2QcMatrixToSparse (BPU_T_GF2_Sparse_Qc_Matrix * out, + const BPU_T_GF2_QC_Matrix * in, const int wi[]) { + int i, counter, bit; + + // allocate output matrix + BPU_gf2SparseQcMatrixMalloc (out, in->element_count, in->element_size, 1); + + // transpose polynoms + for (i = 0; i < in->element_count; i++) { + counter = 0; + // allocate sparse poly + BPU_gf2SparsePolyMalloc (&out->matrices[i], wi[i]); + // set bits + for (bit = 0; bit < in->matrices[i].len; bit++) { + if (BPU_gf2VecGetBit (&in->matrices[i], bit) == 1ul) { + out->matrices[i].index[counter] = (uint32_t) (bit); + counter++; + } + } + // weight error + if (counter != wi[i]) { + BPU_printError ("weight error. Weight should be %i, but is %i.\n", + wi[i], counter); + return -1; + } } - } - return 0; + return 0; } -int BPU_gf2PolyInitRand(BPU_T_GF2_Poly *out, int l, int w, int set_deg) { - int ret; +int BPU_gf2PolyInitRand (BPU_T_GF2_Poly * out, int l, int w, int set_deg) { + int ret; - // allocate output poly - ret = BPU_gf2PolyMalloc(out, l); + // allocate output poly + ret = BPU_gf2PolyMalloc (out, l); - // set random bits - if (w >= 0) - ret = BPU_gf2VecRand((BPU_T_GF2_Vector*) out, w); + // set random bits + if (w >= 0) + ret = BPU_gf2VecRand ((BPU_T_GF2_Vector *) out, w); - // set poly deg - if (set_deg) BPU_gf2PolySetDeg(out, -1); + // set poly deg + if (set_deg) + BPU_gf2PolySetDeg (out, -1); - return ret; + return ret; } -void BPU_gf2SparsePolyCopy(BPU_T_GF2_Sparse_Poly *out, const BPU_T_GF2_Sparse_Poly *in) { - int i; - // allocate output poly - BPU_gf2SparsePolyMalloc(out, in->weight); - // copy all indexes - for (i = 0; i < in->weight; i++) { - out->index[i] = in->index[i]; - } +void BPU_gf2SparsePolyCopy (BPU_T_GF2_Sparse_Poly * out, + const BPU_T_GF2_Sparse_Poly * in) { + int i; + + // allocate output poly + BPU_gf2SparsePolyMalloc (out, in->weight); + // copy all indexes + for (i = 0; i < in->weight; i++) { + out->index[i] = in->index[i]; + } } -void BPU_gf2SparseQcMatrixTransp(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_Sparse_Qc_Matrix *in) { - int counter, coeff, ele, zero_coeff_is_set; - - // allocate memory for output matrix - BPU_gf2SparseQcMatrixMalloc(out, in->element_count, in->element_size, 1); - - // for all polynoms - for (ele = 0; ele < in->element_count; ele++) { - counter = 0; zero_coeff_is_set = 0; - - // alloc new matrix of same weight - BPU_gf2SparsePolyMalloc(&out->matrices[ele], in->matrices[ele].weight); - - // is zero coeff set? - zero_coeff_is_set = (in->matrices[ele].index[0] == 0ul); - // set zero coeff - if (zero_coeff_is_set) { - out->matrices[ele].index[counter] = 0ul; - counter++; - } - - // for other coeffs - for (coeff = in->matrices[ele].weight-1; coeff >= (1-!zero_coeff_is_set); coeff--) { - out->matrices[ele].index[counter] = in->element_size - in->matrices[ele].index[coeff]; - counter++; - } - } +void BPU_gf2SparseQcMatrixTransp (BPU_T_GF2_Sparse_Qc_Matrix * out, + const BPU_T_GF2_Sparse_Qc_Matrix * in) { + int counter, coeff, ele, zero_coeff_is_set; + + // allocate memory for output matrix + BPU_gf2SparseQcMatrixMalloc (out, in->element_count, in->element_size, 1); + + // for all polynoms + for (ele = 0; ele < in->element_count; ele++) { + counter = 0; + zero_coeff_is_set = 0; + + // alloc new matrix of same weight + BPU_gf2SparsePolyMalloc (&out->matrices[ele], in->matrices[ele].weight); + + // is zero coeff set? + zero_coeff_is_set = (in->matrices[ele].index[0] == 0ul); + // set zero coeff + if (zero_coeff_is_set) { + out->matrices[ele].index[counter] = 0ul; + counter++; + } + + // for other coeffs + for (coeff = in->matrices[ele].weight - 1; + coeff >= (1 - !zero_coeff_is_set); coeff--) { + out->matrices[ele].index[counter] = + in->element_size - in->matrices[ele].index[coeff]; + counter++; + } + } } -void BPU_gf2PolyShiftLeft(BPU_T_GF2_Poly *a, int shift_count) { - int i; - int diff, bit_shift, start, shift_right, shift_left; - uint32_t ele1, ele2; - - // allocate new poly, with additional bits - // BPU_gf2PolyMalloc(&poly, a->len+shift_count); - BPU_gf2PolySetDeg(a, a->len+shift_count); - - // calc element shift and bit shift - diff = shift_count / a->element_bit_size; - bit_shift = shift_count % a->element_bit_size; - - start = diff; - // shift elements - for (i = a->array_length-1; i >= start; i--) { - // not the first element, concat two elements - if (i-start != 0) { - - // get first element - if ((a->element_bit_size - bit_shift) >= a->element_bit_size) { - ele1 = 0ul; - shift_right = 0; - } - else { - ele1 = a->elements[i-start-1]; - shift_right = a->element_bit_size - bit_shift; - } - - // get second element - if ((i-start) >= a->array_length) { - ele2 = 0ul; - shift_left = 0; - } - else { - ele2 = a->elements[i-start]; - shift_left = bit_shift; - } - - // set element - a->elements[i] = (ele1 >> shift_right) ^ (ele2 << shift_left); - } - // first element, just shift - else - a->elements[i] = a->elements[i-start] << bit_shift; - } - - // set zeros in the beginning - for (i = 0; i < diff; i++) - a->elements[i] = 0ul; +void BPU_gf2PolyShiftLeft (BPU_T_GF2_Poly * a, int shift_count) { + int i; + int diff, bit_shift, start, shift_right, shift_left; + uint32_t ele1, ele2; + + // allocate new poly, with additional bits + // BPU_gf2PolyMalloc(&poly, a->len+shift_count); + BPU_gf2PolySetDeg (a, a->len + shift_count); + + // calc element shift and bit shift + diff = shift_count / a->element_bit_size; + bit_shift = shift_count % a->element_bit_size; + + start = diff; + // shift elements + for (i = a->array_length - 1; i >= start; i--) { + // not the first element, concat two elements + if (i - start != 0) { + + // get first element + if ((a->element_bit_size - bit_shift) >= a->element_bit_size) { + ele1 = 0ul; + shift_right = 0; + } + else { + ele1 = a->elements[i - start - 1]; + shift_right = a->element_bit_size - bit_shift; + } + + // get second element + if ((i - start) >= a->array_length) { + ele2 = 0ul; + shift_left = 0; + } + else { + ele2 = a->elements[i - start]; + shift_left = bit_shift; + } + + // set element + a->elements[i] = (ele1 >> shift_right) ^ (ele2 << shift_left); + } + // first element, just shift + else + a->elements[i] = a->elements[i - start] << bit_shift; + } + + // set zeros in the beginning + for (i = 0; i < diff; i++) + a->elements[i] = 0ul; } -int BPU_gf2PolyGetHighestBitPos(BPU_T_GF2_Poly *a) { - int ele; +int BPU_gf2PolyGetHighestBitPos (BPU_T_GF2_Poly * a) { + int ele; - // scan all elements and found highest non zero element - for (ele = a->array_length-1; ele >= 0; ele--) { - if (a->elements[ele] != 0ul) - // find highest bit in highest non zero element - return msb32(a->elements[ele], 1, a->element_bit_size, a->element_bit_size) + ele*a->element_bit_size; - } + // scan all elements and found highest non zero element + for (ele = a->array_length - 1; ele >= 0; ele--) { + if (a->elements[ele] != 0ul) + // find highest bit in highest non zero element + return msb32 (a->elements[ele], 1, a->element_bit_size, + a->element_bit_size) + ele * a->element_bit_size; + } - // poly is zero - return -1; + // poly is zero + return -1; } -void BPU_gf2PolySetDeg(BPU_T_GF2_Poly *a, int deg) { - int j, orig_elements_in_row = a->array_length; - - // find max degree - if (deg == -1) { - deg = BPU_gf2PolyGetHighestBitPos(a); - } - - if (deg != -1) { - // set degree and element count - a->len = deg; - a->array_length = a->len / a->element_bit_size + ((a->len % a->element_bit_size) != 0 ? 1 : 0); - // reallocate elements - a->elements = (BPU_T_GF2*) realloc(a->elements, sizeof(BPU_T_GF2) * a->array_length); - // null new elements - for (j = orig_elements_in_row; j < a->array_length; j++) - a->elements[j] = 0ul; - } - // poly is zero - else { - a->len = 0; - a->array_length = 0; - } +void BPU_gf2PolySetDeg (BPU_T_GF2_Poly * a, int deg) { + int j, orig_elements_in_row = a->array_length; + + // find max degree + if (deg == -1) { + deg = BPU_gf2PolyGetHighestBitPos (a); + } + + if (deg != -1) { + // set degree and element count + a->len = deg; + a->array_length = + a->len / a->element_bit_size + ((a->len % a->element_bit_size) != + 0 ? 1 : 0); + // reallocate elements + a->elements = + (BPU_T_GF2 *) realloc (a->elements, + sizeof (BPU_T_GF2) * a->array_length); + // null new elements + for (j = orig_elements_in_row; j < a->array_length; j++) + a->elements[j] = 0ul; + } + // poly is zero + else { + a->len = 0; + a->array_length = 0; + } } -void BPU_gf2PolyMulX(BPU_T_GF2_Poly *a) { - int ele; - uint8_t shift = a->element_bit_size-1; +void BPU_gf2PolyMulX (BPU_T_GF2_Poly * a) { + int ele; + uint8_t shift = a->element_bit_size - 1; + + // save highest bit + uint32_t msb = BPU_gf2VecGetBit (a, a->len - 1); - // save highest bit - uint32_t msb = BPU_gf2VecGetBit(a, a->len-1); - // null highest bit - BPU_gf2VecSetBit(a, a->len-1, 0ul); + // null highest bit + BPU_gf2VecSetBit (a, a->len - 1, 0ul); - // for all elements - for (ele = a->array_length-1; ele >= 1; ele--) { - a->elements[ele] = (a->elements[ele] << 1) ^ (a->elements[ele-1] >> shift); - } + // for all elements + for (ele = a->array_length - 1; ele >= 1; ele--) { + a->elements[ele] = + (a->elements[ele] << 1) ^ (a->elements[ele - 1] >> shift); + } - // last element just shift - a->elements[0] <<= 1; - // and set lowest bit - a->elements[0] ^= msb; + // last element just shift + a->elements[0] <<= 1; + // and set lowest bit + a->elements[0] ^= msb; } -void BPU_gf2PolyShiftRightOne(BPU_T_GF2_Poly *a) { - int i; +void BPU_gf2PolyShiftRightOne (BPU_T_GF2_Poly * a) { + int i; - // for all elements - for (i = 0; i < a->array_length-1; i++) { - // shift right by one and add lowest bit from next element - a->elements[i] = (a->elements[i] >> 1) ^ ((a->elements[i+1] & 1ul) << (a->element_bit_size-1)); - } + // for all elements + for (i = 0; i < a->array_length - 1; i++) { + // shift right by one and add lowest bit from next element + a->elements[i] = + (a-> + elements[i] >> 1) ^ ((a->elements[i + 1] & 1ul) << (a-> + element_bit_size + - 1)); + } - // last element just shift - a->elements[a->array_length-1] >>= 1; + // last element just shift + a->elements[a->array_length - 1] >>= 1; } -void BPU_gf2PolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in, int crop) { - int i; - - // if out poly is zero, just copy in into out - if (out->len == 0) { - BPU_gf2PolyFree(out, 0); - BPU_gf2PolyCopy(out, in); - } - // if in is non zero - else if (in->len != 0) { - // if deg(in) > deg(out) - if (in->len > out->len) - // set degree - BPU_gf2PolySetDeg(out, in->len); - // make add - for (i = 0; i < in->array_length; i++) - out->elements[i] ^= in->elements[i]; - } - - // if set new degree - if (crop) BPU_gf2PolySetDeg(out, -1); +void BPU_gf2PolyAdd (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in, int crop) { + int i; + + // if out poly is zero, just copy in into out + if (out->len == 0) { + BPU_gf2PolyFree (out, 0); + BPU_gf2PolyCopy (out, in); + } + // if in is non zero + else if (in->len != 0) { + // if deg(in) > deg(out) + if (in->len > out->len) + // set degree + BPU_gf2PolySetDeg (out, in->len); + // make add + for (i = 0; i < in->array_length; i++) + out->elements[i] ^= in->elements[i]; + } + + // if set new degree + if (crop) + BPU_gf2PolySetDeg (out, -1); } // operation add with binary polynomials with high degree -void BPU_gf2SparsePolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Sparse_Poly *in) { - int i; - uint8_t bit; - - // for all coefficients - for (i = 0; i < in->weight; i++) - { - bit = BPU_gf2VecGetBit(out, in->index[i]) ^ 1ul; - BPU_gf2VecSetBit(out, in->index[i], bit); - } +void BPU_gf2SparsePolyAdd (BPU_T_GF2_Poly * out, + const BPU_T_GF2_Sparse_Poly * in) { + int i; + uint8_t bit; + + // for all coefficients + for (i = 0; i < in->weight; i++) { + bit = BPU_gf2VecGetBit (out, in->index[i]) ^ 1ul; + BPU_gf2VecSetBit (out, in->index[i], bit); + } } -int BPU_gf2SparsePolyAndHW(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Sparse_Poly *b) { - int i, hw = 0; +int BPU_gf2SparsePolyAndHW (const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Sparse_Poly * b) { + int i, hw = 0; - // for all coefficients - for (i = 0; i < b->weight; i++) - // if both poly has set coeff - if (BPU_gf2VecGetBit(a, b->index[i]) == 1ul) - // increase hamming weight - hw++; + // for all coefficients + for (i = 0; i < b->weight; i++) + // if both poly has set coeff + if (BPU_gf2VecGetBit (a, b->index[i]) == 1ul) + // increase hamming weight + hw++; - return hw; + return hw; } -void BPU_gf2SparseQcMatrixGetRow(BPU_T_GF2_Sparse_Poly *p, const BPU_T_GF2_Sparse_Qc_Matrix *m, int row_num) { - int i; - - // if rownum is in matrix - if (row_num < m->k) { - // allocate output polynom - BPU_gf2SparsePolyCopy(p, &m->matrices[row_num / m->element_size]); - // VERTICAL QC matrix - if (m->isVertical) - // for all coefficients - for (i = 0; i < p->weight; i++) - // shift coefficients - p->index[i] = ((p->index[i]) + row_num) % m->element_size; - // horizontal matrix is not supported +void BPU_gf2SparseQcMatrixGetRow (BPU_T_GF2_Sparse_Poly * p, + const BPU_T_GF2_Sparse_Qc_Matrix * m, + int row_num) { + int i; + + // if rownum is in matrix + if (row_num < m->k) { + // allocate output polynom + BPU_gf2SparsePolyCopy (p, &m->matrices[row_num / m->element_size]); + // VERTICAL QC matrix + if (m->isVertical) + // for all coefficients + for (i = 0; i < p->weight; i++) + // shift coefficients + p->index[i] = ((p->index[i]) + row_num) % m->element_size; + // horizontal matrix is not supported + else { + BPU_printError + ("BPU_QcMatrixGetRow: HORIZONTAL matrix not supported\n"); + } + } + // row num is out of the matrix else { - BPU_printError("BPU_QcMatrixGetRow: HORIZONTAL matrix not supported\n"); + BPU_printError + ("BPU_QcMatrixGetRow: row with index %i does not exist\n", row_num); } - } - // row num is out of the matrix - else { - BPU_printError("BPU_QcMatrixGetRow: row with index %i does not exist\n", row_num); - } } -void BPU_gf2PolyMulMod(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, BPU_T_GF2_Poly *c, const BPU_T_GF2_Poly *m, int crop) { - int i; - BPU_T_GF2_Poly temp_b; +void BPU_gf2PolyMulMod (const BPU_T_GF2_Poly * a, const BPU_T_GF2_Poly * b, + BPU_T_GF2_Poly * c, const BPU_T_GF2_Poly * m, + int crop) { + int i; + BPU_T_GF2_Poly temp_b; - // if one of factors is zero, product will also be zero - if (a->len == 0 || b->len == 0) { - BPU_gf2PolyMalloc(c, 0); - return; - } + // if one of factors is zero, product will also be zero + if (a->len == 0 || b->len == 0) { + BPU_gf2PolyMalloc (c, 0); + return; + } - // copy b to temp_b and prolong to modulo lenght - 1 - BPU_gf2PolyCopy(&temp_b, b); - BPU_gf2PolySetDeg(&temp_b, m->len-1); + // copy b to temp_b and prolong to modulo lenght - 1 + BPU_gf2PolyCopy (&temp_b, b); + BPU_gf2PolySetDeg (&temp_b, m->len - 1); - // malloc multiplication product - BPU_gf2PolyMalloc(c, m->len); + // malloc multiplication product + BPU_gf2PolyMalloc (c, m->len); - // for length of a - for (i = 0; i < a->len; i++) { - // 1 in a -> multiply - if (BPU_gf2VecGetBit(a, i) == 1ul) - BPU_gf2PolyAdd(c, &temp_b, 0); + // for length of a + for (i = 0; i < a->len; i++) { + // 1 in a -> multiply + if (BPU_gf2VecGetBit (a, i) == 1ul) + BPU_gf2PolyAdd (c, &temp_b, 0); - // multiply b by 2 - BPU_gf2PolyMulX(&temp_b); - } + // multiply b by 2 + BPU_gf2PolyMulX (&temp_b); + } - // if do not crop the result length - if (!crop) - BPU_gf2PolySetDeg(c, m->len-1); - else - BPU_gf2PolySetDeg(c, -1); + // if do not crop the result length + if (!crop) + BPU_gf2PolySetDeg (c, m->len - 1); + else + BPU_gf2PolySetDeg (c, -1); - BPU_gf2PolyFree(&temp_b, 0); + BPU_gf2PolyFree (&temp_b, 0); } -void BPU_gf2PolyDiv(BPU_T_GF2_Poly *q, BPU_T_GF2_Poly *r, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b) { - BPU_T_GF2_Poly divisor, dividend; - int limit_deg = a->len - b->len; - int i = 0; +void BPU_gf2PolyDiv (BPU_T_GF2_Poly * q, BPU_T_GF2_Poly * r, + const BPU_T_GF2_Poly * a, const BPU_T_GF2_Poly * b) { + BPU_T_GF2_Poly divisor, dividend; + int limit_deg = a->len - b->len; + int i = 0; - // copy a, b - BPU_gf2PolyCopy(&divisor, b); - BPU_gf2PolyCopy(÷nd, a); + // copy a, b + BPU_gf2PolyCopy (&divisor, b); + BPU_gf2PolyCopy (÷nd, a); - // allocate quotient - BPU_gf2PolyMalloc(q, a->len); + // allocate quotient + BPU_gf2PolyMalloc (q, a->len); - // divisor shift to deg of dividend - BPU_gf2PolyShiftLeft(&divisor, a->len - b->len); + // divisor shift to deg of dividend + BPU_gf2PolyShiftLeft (&divisor, a->len - b->len); - while (dividend.len >= b->len) { - if (dividend.len == divisor.len) { - BPU_gf2VecSetBit(q, limit_deg - i, 1ul); + while (dividend.len >= b->len) { + if (dividend.len == divisor.len) { + BPU_gf2VecSetBit (q, limit_deg - i, 1ul); - // dividend - divisor - BPU_gf2PolyAdd(÷nd, &divisor, 1); - } + // dividend - divisor + BPU_gf2PolyAdd (÷nd, &divisor, 1); + } - // divisor degree decreased by 1 - BPU_gf2PolyShiftRightOne(&divisor); - // set actual degree - BPU_gf2PolySetDeg(&divisor, -1); - i++; - } + // divisor degree decreased by 1 + BPU_gf2PolyShiftRightOne (&divisor); + // set actual degree + BPU_gf2PolySetDeg (&divisor, -1); + i++; + } - BPU_gf2PolyCopy(r, ÷nd); - BPU_gf2PolySetDeg(q, -1); + BPU_gf2PolyCopy (r, ÷nd); + BPU_gf2PolySetDeg (q, -1); - // free - BPU_gf2PolyFree(÷nd, 0); - BPU_gf2PolyFree(&divisor, 0); + // free + BPU_gf2PolyFree (÷nd, 0); + BPU_gf2PolyFree (&divisor, 0); } // XGCD with binary polynomial with high degree -void BPU_gf2PolyExtEuclidA(BPU_T_GF2_Poly *d, BPU_T_GF2_Poly *s, BPU_T_GF2_Poly *t, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, const BPU_T_GF2_Poly *m) { - BPU_T_GF2_Poly tmp, tmp_2, old_s, old_t, old_r, r, q; - int deg = (a->len > b->len) ? a->len : b->len; - - // allocate Bezout coeffitients - BPU_gf2PolyMalloc(s, 0); - BPU_gf2PolyMalloc(t, deg); - BPU_gf2PolyMalloc(&old_s, deg); - BPU_gf2PolyMalloc(&old_t, 0); - - // set initial values - BPU_gf2PolyCopy(&r, a); - BPU_gf2PolyCopy(&old_r, b); - - if (a->len == 0) { - old_t.elements[0] = 1ul; - BPU_gf2PolySetDeg(&old_t, -1); - } - else if (b->len == 0) { - BPU_gf2PolyFree(&old_r, 0); - BPU_gf2PolyCopy(&old_r, a); - old_s.elements[0] = 1ul; - BPU_gf2PolySetDeg(&old_s, -1); - } - // run algoritm, if everything is OK - else { +void BPU_gf2PolyExtEuclidA (BPU_T_GF2_Poly * d, BPU_T_GF2_Poly * s, + BPU_T_GF2_Poly * t, const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Poly * b, + const BPU_T_GF2_Poly * m) { + BPU_T_GF2_Poly tmp, tmp_2, old_s, old_t, old_r, r, q; + int deg = (a->len > b->len) ? a->len : b->len; + + // allocate Bezout coeffitients + BPU_gf2PolyMalloc (s, 0); + BPU_gf2PolyMalloc (t, deg); + BPU_gf2PolyMalloc (&old_s, deg); + BPU_gf2PolyMalloc (&old_t, 0); + // set initial values - old_s.elements[0] = 1ul; - BPU_gf2PolySetDeg(&old_s, -1); - t->elements[0] = 1ul; - BPU_gf2PolySetDeg(t, -1); - - // while loop until r is not zero - while (r.len > 0) { - // divide - BPU_gf2PolyDiv(&q, &tmp, &old_r, &r); - - // save old reminder - BPU_gf2PolyFree(&old_r, 0); - BPU_gf2PolyCopy(&old_r, &r); - - // save current reminder - BPU_gf2PolyFree(&r, 0); - BPU_gf2PolyCopy(&r, &tmp); - - // free - BPU_gf2PolyFree(&tmp, 0); - - // save s quocient - BPU_gf2PolyCopy(&tmp, &old_s); - BPU_gf2PolyFree(&old_s, 0); - BPU_gf2PolyCopy(&old_s, s); - - BPU_gf2PolyMulMod(&q, s, &tmp_2, m, 1); - BPU_gf2PolyAdd(&tmp, &tmp_2, 1); - BPU_gf2PolyFree(s, 0); - BPU_gf2PolyCopy(s, &tmp); - - // free - BPU_gf2PolyFree(&tmp, 0); - BPU_gf2PolyFree(&tmp_2, 0); - - // save t quocient - BPU_gf2PolyCopy(&tmp, &old_t); - BPU_gf2PolyFree(&old_t, 0); - BPU_gf2PolyCopy(&old_t, t); - BPU_gf2PolyMulMod(&q, t, &tmp_2, m, 1); - BPU_gf2PolyAdd(&tmp, &tmp_2, 1); - BPU_gf2PolyFree(t, 0); - BPU_gf2PolyCopy(t, &tmp); - - // free - BPU_gf2PolyFree(&tmp_2, 0); - BPU_gf2PolyFree(&tmp, 0); - BPU_gf2PolyFree(&q, 0); - } - } - // prepare return values - BPU_gf2PolyFree(t, 0); - BPU_gf2PolyFree(s, 0); - BPU_gf2PolyCopy(d, &old_r); - BPU_gf2PolyCopy(s, &old_s); - BPU_gf2PolyCopy(t, &old_t); - - // free - BPU_gf2PolyFree(&old_s, 0); - BPU_gf2PolyFree(&old_t, 0); - BPU_gf2PolyFree(&old_r, 0); - BPU_gf2PolyFree(&r, 0); + BPU_gf2PolyCopy (&r, a); + BPU_gf2PolyCopy (&old_r, b); + + if (a->len == 0) { + old_t.elements[0] = 1ul; + BPU_gf2PolySetDeg (&old_t, -1); + } + else if (b->len == 0) { + BPU_gf2PolyFree (&old_r, 0); + BPU_gf2PolyCopy (&old_r, a); + old_s.elements[0] = 1ul; + BPU_gf2PolySetDeg (&old_s, -1); + } + // run algoritm, if everything is OK + else { + // set initial values + old_s.elements[0] = 1ul; + BPU_gf2PolySetDeg (&old_s, -1); + t->elements[0] = 1ul; + BPU_gf2PolySetDeg (t, -1); + + // while loop until r is not zero + while (r.len > 0) { + // divide + BPU_gf2PolyDiv (&q, &tmp, &old_r, &r); + + // save old reminder + BPU_gf2PolyFree (&old_r, 0); + BPU_gf2PolyCopy (&old_r, &r); + + // save current reminder + BPU_gf2PolyFree (&r, 0); + BPU_gf2PolyCopy (&r, &tmp); + + // free + BPU_gf2PolyFree (&tmp, 0); + + // save s quocient + BPU_gf2PolyCopy (&tmp, &old_s); + BPU_gf2PolyFree (&old_s, 0); + BPU_gf2PolyCopy (&old_s, s); + + BPU_gf2PolyMulMod (&q, s, &tmp_2, m, 1); + BPU_gf2PolyAdd (&tmp, &tmp_2, 1); + BPU_gf2PolyFree (s, 0); + BPU_gf2PolyCopy (s, &tmp); + + // free + BPU_gf2PolyFree (&tmp, 0); + BPU_gf2PolyFree (&tmp_2, 0); + + // save t quocient + BPU_gf2PolyCopy (&tmp, &old_t); + BPU_gf2PolyFree (&old_t, 0); + BPU_gf2PolyCopy (&old_t, t); + BPU_gf2PolyMulMod (&q, t, &tmp_2, m, 1); + BPU_gf2PolyAdd (&tmp, &tmp_2, 1); + BPU_gf2PolyFree (t, 0); + BPU_gf2PolyCopy (t, &tmp); + + // free + BPU_gf2PolyFree (&tmp_2, 0); + BPU_gf2PolyFree (&tmp, 0); + BPU_gf2PolyFree (&q, 0); + } + } + // prepare return values + BPU_gf2PolyFree (t, 0); + BPU_gf2PolyFree (s, 0); + BPU_gf2PolyCopy (d, &old_r); + BPU_gf2PolyCopy (s, &old_s); + BPU_gf2PolyCopy (t, &old_t); + + // free + BPU_gf2PolyFree (&old_s, 0); + BPU_gf2PolyFree (&old_t, 0); + BPU_gf2PolyFree (&old_r, 0); + BPU_gf2PolyFree (&r, 0); } -int BPU_gf2PolyInv(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *m) { - BPU_T_GF2_Poly d, s; - int ret = 1; +int BPU_gf2PolyInv (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Poly * m) { + BPU_T_GF2_Poly d, s; + int ret = 1; - // call XGCD - BPU_gf2PolyExtEuclidA(&d, &s, out, a, m, m); + // call XGCD + BPU_gf2PolyExtEuclidA (&d, &s, out, a, m, m); - // if GCD (a,m) is not 1 - if (d.len != 1 || d.elements[0] != 1ul) { - BPU_printDebug("inverse polynomial NOT found"); - ret = 0; - } + // if GCD (a,m) is not 1 + if (d.len != 1 || d.elements[0] != 1ul) { + BPU_printDebug ("inverse polynomial NOT found"); + ret = 0; + } - // free - BPU_gf2PolyFree(&d, 0); - BPU_gf2PolyFree(&s, 0); - return ret; + // free + BPU_gf2PolyFree (&d, 0); + BPU_gf2PolyFree (&s, 0); + return ret; } -void BPU_gf2PolyTransp(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in) { - int i; +void BPU_gf2PolyTransp (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in) { + int i; - // allocate output poly - BPU_gf2PolyMalloc(out, in->len); + // allocate output poly + BPU_gf2PolyMalloc (out, in->len); - // copy zero coefficient - BPU_gf2VecSetBit(out, 0, BPU_gf2VecGetBit(in, 0)); - - // swap other coefficients - for (i = 1; i < out->len; i++) { - BPU_gf2VecSetBit(out, out->len-i, BPU_gf2VecGetBit(in, i)); - } + // copy zero coefficient + BPU_gf2VecSetBit (out, 0, BPU_gf2VecGetBit (in, 0)); + + // swap other coefficients + for (i = 1; i < out->len; i++) { + BPU_gf2VecSetBit (out, out->len - i, BPU_gf2VecGetBit (in, i)); + } } -void BPU_gf2QcMatrixTransp(BPU_T_GF2_QC_Matrix *out, const BPU_T_GF2_QC_Matrix *in) { - int i; +void BPU_gf2QcMatrixTransp (BPU_T_GF2_QC_Matrix * out, + const BPU_T_GF2_QC_Matrix * in) { + int i; - // allocate output matrix - BPU_gf2QcMatrixMalloc(out, in->element_count, in->element_size, !in->isVertical, 0); + // allocate output matrix + BPU_gf2QcMatrixMalloc (out, in->element_count, in->element_size, + !in->isVertical, 0); - // transpose all polynoms - for (i = 0; i < in->element_count; i++) - BPU_gf2PolyTransp(&out->matrices[i], &in->matrices[i]); + // transpose all polynoms + for (i = 0; i < in->element_count; i++) + BPU_gf2PolyTransp (&out->matrices[i], &in->matrices[i]); } -int BPU_gf2PolyIsZero(const BPU_T_GF2_Poly *a) { - int i; +int BPU_gf2PolyIsZero (const BPU_T_GF2_Poly * a) { + int i; - // scan all elements - for (i = 0; i < a->array_length; i++) - // if there is non zero element, poly is not zero - if (a->elements[i] != 0ul) - return 0; + // scan all elements + for (i = 0; i < a->array_length; i++) + // if there is non zero element, poly is not zero + if (a->elements[i] != 0ul) + return 0; - // all elements are zero, so also poly is zero - return 1; + // all elements are zero, so also poly is zero + return 1; } - diff --git a/lib/src/bitpunch/math/gf2.h b/lib/src/bitpunch/math/gf2.h index c456261..b384bd6 100644 --- a/lib/src/bitpunch/math/gf2.h +++ b/lib/src/bitpunch/math/gf2.h @@ -48,12 +48,12 @@ typedef BPU_T_Element_Array BPU_T_GF2_Vector; * Every element is one bit. */ typedef struct _BPU_T_GF2_Matrix { - BPU_T_GF2 **elements; ///< all element of matrix - uint8_t element_bit_size; ///< element size, is sizeof(BPU_T_GF2) i.e. 64 bits - uint16_t elements_in_row; ///< number of elements in one row - uint32_t k; ///< rows - uint32_t n; ///< cols -}BPU_T_GF2_Matrix; + BPU_T_GF2 **elements; ///< all element of matrix + uint8_t element_bit_size; ///< element size, is sizeof(BPU_T_GF2) i.e. 64 bits + uint16_t elements_in_row; ///< number of elements in one row + uint32_t k; ///< rows + uint32_t n; ///< cols +} BPU_T_GF2_Matrix; /** * Polynomial over GF2, represented as Vector. @@ -67,9 +67,9 @@ typedef BPU_T_GF2_Vector BPU_T_GF2_Poly; * Every set coefficient is stored in array of indexes. */ typedef struct _BPU_T_GF2_Sparse_Poly { - uint32_t *index; /// with I on the left side. After work, return value should be freed, it is allocated dynamically. @@ -458,7 +463,7 @@ int BPU_gf2MatFindCol(const BPU_T_GF2_Matrix *mat, int i, int start_index); * @return on success pointer to BPU_T_Perm_Vector */ /// It brings Matrix GF2 into the systematic form -> with I on the left side. -int BPU_gf2MatMakeSystematic(BPU_T_GF2_Matrix *inout); +int BPU_gf2MatMakeSystematic (BPU_T_GF2_Matrix * inout); /** * Concats two vectors without allocation ouput. @@ -468,7 +473,8 @@ int BPU_gf2MatMakeSystematic(BPU_T_GF2_Matrix *inout); * @return 0 - succes, else error */ /// Concats two vectors without allocation ouput. -int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const BPU_T_GF2_Vector *vec2); +int BPU_gf2VecConcat (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * vec1, + const BPU_T_GF2_Vector * vec2); /** * Crop the vector @@ -478,7 +484,8 @@ int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const * @param length count of bits of cropped vector * @return 0 - succes, else error */ -int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int start, const int length); +int BPU_gf2VecCrop (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in, + const int start, const int length); /** * Get matrix GF2 row as vector gf2. You can set alloc param, to allocate it dynamically inside function, or use allocated vector yet. When set alloc, after work call BPU_freeVecGF2 @@ -488,7 +495,8 @@ int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int * @param alloc if set to 0, jsut copy row to allocated vector yet, or also allocate output vector * @return 0 - success, else error */ -int BPU_gf2MatGetRowAsGf2Vec(BPU_T_GF2_Vector *out, const BPU_T_GF2_Matrix *in, int row); +int BPU_gf2MatGetRowAsGf2Vec (BPU_T_GF2_Vector * out, + const BPU_T_GF2_Matrix * in, int row); /** * Copy VectorGF2. @@ -496,7 +504,7 @@ int BPU_gf2MatGetRowAsGf2Vec(BPU_T_GF2_Vector *out, const BPU_T_GF2_Matrix *in, * @param src[in] pointer to VectorGF2 */ /// Copy VectorGF2. -void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src); +void BPU_gf2VecCopy (BPU_T_GF2_Vector * dest, const BPU_T_GF2_Vector * src); /** * @brief BPU_gf2VecCmp Compare two vectors. @@ -504,7 +512,7 @@ void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src); * @param v2 * @return 0 - same, -1 - len differs, > 0 (index + 1) of first difference */ -int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2); +int BPU_gf2VecCmp (const BPU_T_GF2_Vector * v1, const BPU_T_GF2_Vector * v2); /** * XOR operation on rows of Matrix GF2. row[i] = row[i] ^ row[j] @@ -513,7 +521,7 @@ int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2); * @param int j * @return */ -void BPU_gf2MatXorRows(BPU_T_GF2_Matrix *mat, int i, int j); +void BPU_gf2MatXorRows (BPU_T_GF2_Matrix * mat, int i, int j); /** * Xor two Vectors GF2 and store result in first vector. @@ -522,7 +530,7 @@ void BPU_gf2MatXorRows(BPU_T_GF2_Matrix *mat, int i, int j); * @return 0 - successm else error */ /// Xor two Vectors GF2 and store result in first vector. -int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in); +int BPU_gf2VecXor (BPU_T_GF2_Vector * out, const BPU_T_GF2_Vector * in); /** * Create a binary vector of required length and weight. After work call BPU_freeVecGF2 @@ -530,7 +538,7 @@ int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in); * @param w weight of vector, if 0 weight is random * @return 0 - succes, else error */ -int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w); +int BPU_gf2VecRand (BPU_T_GF2_Vector * out, int w); /** * Vector and matrix multiplication over GF2. @@ -540,7 +548,8 @@ int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w); * @param[in] b matrix GF2 * @return on succes 0, on size error -1, on allocation error -2 */ -int BPU_gf2VecMulMat(BPU_T_GF2_Vector *x, const BPU_T_GF2_Vector *v, const BPU_T_GF2_Matrix *b); +int BPU_gf2VecMulMat (BPU_T_GF2_Vector * x, const BPU_T_GF2_Vector * v, + const BPU_T_GF2_Matrix * b); /** * Crop matrix GF2 from left. @@ -550,14 +559,14 @@ int BPU_gf2VecMulMat(BPU_T_GF2_Vector *x, const BPU_T_GF2_Vector *v, const BPU_T * @return on succes 0, on higher or equal width as actual width of in matrix -1, on allocation of new matrix error -2 */ /// Crop matrix GF2 from left. -int BPU_gf2MatCrop(BPU_T_GF2_Matrix *m, uint16_t width); +int BPU_gf2MatCrop (BPU_T_GF2_Matrix * m, uint16_t width); /** * @brief BPU_getParity Get parity of word. * @param dword * @return */ -uint8_t BPU_getParity(BPU_T_GF2 dword); +uint8_t BPU_getParity (BPU_T_GF2 dword); /************************************************ POLYNOMIAL UTILS @@ -568,7 +577,7 @@ POLYNOMIAL UTILS * @param a poly * @return if 1, poly is zero, else 0 */ -int BPU_gf2PolyIsZero(const BPU_T_GF2_Poly *a); +int BPU_gf2PolyIsZero (const BPU_T_GF2_Poly * a); /** * Find highest coefficient in polynomial and set degree. @@ -576,34 +585,34 @@ int BPU_gf2PolyIsZero(const BPU_T_GF2_Poly *a); * @param a poly * @param deg if -1, set degree = max poly degree, else set given degree */ -void BPU_gf2PolySetDeg(BPU_T_GF2_Poly *a, int deg); +void BPU_gf2PolySetDeg (BPU_T_GF2_Poly * a, int deg); /** * Returns highest set coefficient in polynomial. * @param a poly * @return Index of highest set coefficient in polynomial. */ -int BPU_gf2PolyGetHighestBitPos(BPU_T_GF2_Poly *a); +int BPU_gf2PolyGetHighestBitPos (BPU_T_GF2_Poly * a); /** * Shift polynomial left by given positions. * @param a polynomial to be shifted * @param shift_count count of shiftes to do */ -void BPU_gf2PolyShiftLeft(BPU_T_GF2_Poly *a, int shift_count); +void BPU_gf2PolyShiftLeft (BPU_T_GF2_Poly * a, int shift_count); /** * Shift polynomial right by one. * @param a polynomial to be shifted */ -void BPU_gf2PolyShiftRightOne(BPU_T_GF2_Poly *a); +void BPU_gf2PolyShiftRightOne (BPU_T_GF2_Poly * a); /** * Transpose polynomial. * @param out transposed output polynomial * @param in input polynomial */ -void BPU_gf2PolyTransp(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in); +void BPU_gf2PolyTransp (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in); /************************************************ @@ -614,7 +623,7 @@ POLYNOMIAL MATH * Multiplicate polynomial by x. * @param a input / output polynomial */ -void BPU_gf2PolyMulX(BPU_T_GF2_Poly *a); +void BPU_gf2PolyMulX (BPU_T_GF2_Poly * a); /** * Polynomial add. @@ -625,7 +634,7 @@ void BPU_gf2PolyMulX(BPU_T_GF2_Poly *a); * @param in intput polynomial * @param crop boolean, if set actual degree of output */ -void BPU_gf2PolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in, int crop); +void BPU_gf2PolyAdd (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in, int crop); /** * Polynomial multiplication modulo. @@ -638,7 +647,8 @@ void BPU_gf2PolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in, int crop); * @param m modulo * @param crop boolean, if set actual degree of output */ -void BPU_gf2PolyMulMod(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, BPU_T_GF2_Poly *c, const BPU_T_GF2_Poly *m, int crop); +void BPU_gf2PolyMulMod (const BPU_T_GF2_Poly * a, const BPU_T_GF2_Poly * b, + BPU_T_GF2_Poly * c, const BPU_T_GF2_Poly * m, int crop); /** * Polynomial division. @@ -649,7 +659,8 @@ void BPU_gf2PolyMulMod(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, BPU_T_G * @param a dividend * @param b divisor */ -void BPU_gf2PolyDiv(BPU_T_GF2_Poly *q, BPU_T_GF2_Poly *r, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b); +void BPU_gf2PolyDiv (BPU_T_GF2_Poly * q, BPU_T_GF2_Poly * r, + const BPU_T_GF2_Poly * a, const BPU_T_GF2_Poly * b); /** * Extended Euclidean algorithm (XGCD). @@ -661,7 +672,9 @@ void BPU_gf2PolyDiv(BPU_T_GF2_Poly *q, BPU_T_GF2_Poly *r, const BPU_T_GF2_Poly * * @param b poly b * @param m modulo */ -void BPU_gf2PolyExtEuclidA(BPU_T_GF2_Poly *d, BPU_T_GF2_Poly *s, BPU_T_GF2_Poly *t, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *b, const BPU_T_GF2_Poly *m); +void BPU_gf2PolyExtEuclidA (BPU_T_GF2_Poly * d, BPU_T_GF2_Poly * s, + BPU_T_GF2_Poly * t, const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Poly * b, const BPU_T_GF2_Poly * m); /** * Calc inversion polynomial. @@ -671,7 +684,8 @@ void BPU_gf2PolyExtEuclidA(BPU_T_GF2_Poly *d, BPU_T_GF2_Poly *s, BPU_T_GF2_Poly * @param m modulo * @return 1 if found, else not found */ -int BPU_gf2PolyInv(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *a, const BPU_T_GF2_Poly *mod); +int BPU_gf2PolyInv (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Poly * mod); /************************************************ @@ -683,14 +697,15 @@ SPARSE POLYNOMIAL UTILS * @param out output sparse polynomial * @param in input sparse polynomial */ -void BPU_gf2SparsePolyCopy(BPU_T_GF2_Sparse_Poly *out, const BPU_T_GF2_Sparse_Poly *in); +void BPU_gf2SparsePolyCopy (BPU_T_GF2_Sparse_Poly * out, + const BPU_T_GF2_Sparse_Poly * in); /** * Copy polynomial. After work you have to free memory using call BPU_gf2PolyFree. * @param out output polynomial * @param in input polynomial */ -void BPU_gf2PolyCopy(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in); +void BPU_gf2PolyCopy (BPU_T_GF2_Poly * out, const BPU_T_GF2_Poly * in); /** * Initialize random polynomial. After work you have to free memory using call BPU_gf2PolyFree. @@ -700,7 +715,7 @@ void BPU_gf2PolyCopy(BPU_T_GF2_Poly *out, const BPU_T_GF2_Poly *in); * @param set_deg boolean param, if 1, it will crop polynomial to the actual degree and reallocate it * @return 0 - succes, else error */ -int BPU_gf2PolyInitRand(BPU_T_GF2_Poly *out, int l, int w, int set_deg); +int BPU_gf2PolyInitRand (BPU_T_GF2_Poly * out, int l, int w, int set_deg); /************************************************ @@ -713,7 +728,8 @@ SPARSE POLYNOMIAL MATH * @param out input / output polynomial * @param in intput sparse polynomial */ -void BPU_gf2SparsePolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Sparse_Poly *in); +void BPU_gf2SparsePolyAdd (BPU_T_GF2_Poly * out, + const BPU_T_GF2_Sparse_Poly * in); /** * Polynomial and sparse polynomial, returns hamming weight of result. @@ -722,7 +738,8 @@ void BPU_gf2SparsePolyAdd(BPU_T_GF2_Poly *out, const BPU_T_GF2_Sparse_Poly *in); * @param b sparse polynomial * @return hamming weight of result of and operation */ -int BPU_gf2SparsePolyAndHW(const BPU_T_GF2_Poly *a, const BPU_T_GF2_Sparse_Poly *b); +int BPU_gf2SparsePolyAndHW (const BPU_T_GF2_Poly * a, + const BPU_T_GF2_Sparse_Poly * b); /************************************************ @@ -734,7 +751,8 @@ QUASI-CYCLIC MATRIX UTILS * @param out output transposed matrix * @param in input matrix */ -void BPU_gf2QcMatrixTransp(BPU_T_GF2_QC_Matrix *out, const BPU_T_GF2_QC_Matrix *in); +void BPU_gf2QcMatrixTransp (BPU_T_GF2_QC_Matrix * out, + const BPU_T_GF2_QC_Matrix * in); /** * Convert quasi-cyclic matrix into sparse quasi-cyclic matrix. Allocate memory, so after work it has to be freed by using call BPU_gf2SparseQcMatrixFree. @@ -743,7 +761,8 @@ void BPU_gf2QcMatrixTransp(BPU_T_GF2_QC_Matrix *out, const BPU_T_GF2_QC_Matrix * * @param wi weights of cyclic elements in input matrix * @return */ -int BPU_gf2QcMatrixToSparse(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_QC_Matrix *in, const int wi[]); +int BPU_gf2QcMatrixToSparse (BPU_T_GF2_Sparse_Qc_Matrix * out, + const BPU_T_GF2_QC_Matrix * in, const int wi[]); /************************************************ @@ -756,13 +775,16 @@ SPARSE QUASI-CYCLIC MATRIX UTILS * @param p output row * @param row_num index of row to get */ -void BPU_gf2SparseQcMatrixGetRow(BPU_T_GF2_Sparse_Poly *p, const BPU_T_GF2_Sparse_Qc_Matrix *m, int row_num); +void BPU_gf2SparseQcMatrixGetRow (BPU_T_GF2_Sparse_Poly * p, + const BPU_T_GF2_Sparse_Qc_Matrix * m, + int row_num); /** * Transpose sparse quasi-cyclic matrix. * @param out output transposed matrix * @param in input matrix */ -void BPU_gf2SparseQcMatrixTransp(BPU_T_GF2_Sparse_Qc_Matrix *out, const BPU_T_GF2_Sparse_Qc_Matrix *in); +void BPU_gf2SparseQcMatrixTransp (BPU_T_GF2_Sparse_Qc_Matrix * out, + const BPU_T_GF2_Sparse_Qc_Matrix * in); #endif // BPU_GF2_H diff --git a/lib/src/bitpunch/math/gf2x.c b/lib/src/bitpunch/math/gf2x.c index a8b96b7..1b6be4c 100644 --- a/lib/src/bitpunch/math/gf2x.c +++ b/lib/src/bitpunch/math/gf2x.c @@ -29,57 +29,63 @@ along with this program. If not, see . #ifdef BPU_CONF_PRINT /* ==================================== Print functions ==================================== */ -void BPU_printGf2xMat(const BPU_T_GF2_16x_Matrix* in) { - uint32_t i; - uint32_t j; - fprintf(stderr, "Matrix size: %dx%d\n", in->k, in->n); - - for(i = 0; i < in->k; i++) { - fprintf(stderr, "%3d: ",i); - for(j = 0; j < in->n; j++) { - fprintf(stderr, "%x ", in->elements[i][j]);//BPU_printBinary(in->elements[i][j], 4); - } - fprintf(stderr, "\n"); - } +void BPU_printGf2xMat (const BPU_T_GF2_16x_Matrix * in) { + uint32_t i; + uint32_t j; + + fprintf (stderr, "Matrix size: %dx%d\n", in->k, in->n); + + for (i = 0; i < in->k; i++) { + fprintf (stderr, "%3d: ", i); + for (j = 0; j < in->n; j++) { + fprintf (stderr, "%x ", in->elements[i][j]); //BPU_printBinary(in->elements[i][j], 4); + } + fprintf (stderr, "\n"); + } } -void BPU_printGf2xPoly(const BPU_T_GF2_16x_Poly *p, const BPU_T_Math_Ctx *math_ctx) { - int i; - - fprintf(stderr, "Poly (deg = %d): ", p->deg); - - if (p->deg == -1) { - fprintf(stderr, "0\n"); - - return; - } - for (i = p->deg; i >= 0; i--) { - if(p->coef[i] == 0) - continue; - if (i != p->deg) { - fprintf(stderr, "+ "); - } - fprintf(stderr, "alpha^(%d).x^%d ", math_ctx->log_table[p->coef[i]], i); - } - fprintf(stderr, "\n"); +void BPU_printGf2xPoly (const BPU_T_GF2_16x_Poly * p, + const BPU_T_Math_Ctx * math_ctx) { + int i; + + fprintf (stderr, "Poly (deg = %d): ", p->deg); + + if (p->deg == -1) { + fprintf (stderr, "0\n"); + + return; + } + for (i = p->deg; i >= 0; i--) { + if (p->coef[i] == 0) + continue; + if (i != p->deg) { + fprintf (stderr, "+ "); + } + fprintf (stderr, "alpha^(%d).x^%d ", math_ctx->log_table[p->coef[i]], + i); + } + fprintf (stderr, "\n"); } -void BPU_printGf2xVec(const BPU_T_GF2_16x_Vector *v) { - int i; - for(i = 0; i < v->len; i++) { - fprintf(stderr, "%x ", v->elements[i]); - } - fprintf(stderr, "\n"); +void BPU_printGf2xVec (const BPU_T_GF2_16x_Vector * v) { + int i; + + for (i = 0; i < v->len; i++) { + fprintf (stderr, "%x ", v->elements[i]); + } + fprintf (stderr, "\n"); } + /* ------------------------------------ Print functions ------------------------------------ */ #endif -int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols) { +int BPU_gf2xMatMalloc (BPU_T_GF2_16x_Matrix ** m, int rows, int cols) { int i; - *m = (BPU_T_GF2_16x_Matrix *) calloc(sizeof(BPU_T_GF2_16x_Matrix), 1); + + *m = (BPU_T_GF2_16x_Matrix *) calloc (sizeof (BPU_T_GF2_16x_Matrix), 1); if (!*m) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } // rows @@ -87,52 +93,55 @@ int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols) { // cols (*m)->n = cols; // allocate rows - (*m)->elements = (BPU_T_GF2_16x **) malloc(sizeof(BPU_T_GF2_16x*) * (*m)->k); + (*m)->elements = + (BPU_T_GF2_16x **) malloc (sizeof (BPU_T_GF2_16x *) * (*m)->k); if (!(*m)->elements) { - BPU_printError("BPU_mallocMatrix: can not allocate memory for matrix rows"); + BPU_printError + ("BPU_mallocMatrix: can not allocate memory for matrix rows"); return 1; } // allocate cols for (i = 0; i < (*m)->k; i++) { - (*m)->elements[i] = (BPU_T_GF2_16x*) calloc(1, sizeof(BPU_T_GF2_16x) * (*m)->n); + (*m)->elements[i] = + (BPU_T_GF2_16x *) calloc (1, sizeof (BPU_T_GF2_16x) * (*m)->n); if (!(*m)->elements[i]) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -2; } } return 0; } -int BPU_gf2xVecMalloc(BPU_T_GF2_16x_Vector **vec, int size) { - *vec = (BPU_T_GF2_16x_Vector *) calloc(sizeof(BPU_T_GF2_16x_Vector), 1); +int BPU_gf2xVecMalloc (BPU_T_GF2_16x_Vector ** vec, int size) { + *vec = (BPU_T_GF2_16x_Vector *) calloc (sizeof (BPU_T_GF2_16x_Vector), 1); if (!*vec) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } (*vec)->len = size; - (*vec)->elements = (BPU_T_GF2_16x*)calloc(size, sizeof(BPU_T_GF2_16x)); + (*vec)->elements = (BPU_T_GF2_16x *) calloc (size, sizeof (BPU_T_GF2_16x)); if (!(*vec)->elements) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -2; } return 0; } -void BPU_gf2xVecFree(BPU_T_GF2_16x_Vector **vec) { +void BPU_gf2xVecFree (BPU_T_GF2_16x_Vector ** vec) { if (!*vec) { return; } - free((*vec)->elements); - free(*vec); + free ((*vec)->elements); + free (*vec); *vec = NULL; } -void BPU_gf2xMatFree(BPU_T_GF2_16x_Matrix **m) { +void BPU_gf2xMatFree (BPU_T_GF2_16x_Matrix ** m) { int i; if (!*m) { @@ -140,62 +149,63 @@ void BPU_gf2xMatFree(BPU_T_GF2_16x_Matrix **m) { } // first free cols for (i = 0; i < (*m)->k; i++) { - free((*m)->elements[i]); + free ((*m)->elements[i]); } // then free rows - free((*m)->elements); - free((*m)); + free ((*m)->elements); + free ((*m)); *m = NULL; } -void BPU_gf2xPolyFree(BPU_T_GF2_16x_Poly **p) { +void BPU_gf2xPolyFree (BPU_T_GF2_16x_Poly ** p) { if (!*p) { return; } - free((*p)->coef); - free(*p); + free ((*p)->coef); + free (*p); *p = NULL; } -int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg) { - *p = (BPU_T_GF2_16x_Poly *) calloc(sizeof(BPU_T_GF2_16x_Poly), 1); +int BPU_gf2xPolyMalloc (BPU_T_GF2_16x_Poly ** p, int16_t max_deg) { + *p = (BPU_T_GF2_16x_Poly *) calloc (sizeof (BPU_T_GF2_16x_Poly), 1); if (!*p) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } - return BPU_gf2xPolyMallocCoef(*p, max_deg); + return BPU_gf2xPolyMallocCoef (*p, max_deg); } -int BPU_gf2xPolyResize(BPU_T_GF2_16x_Poly *p, int16_t max_deg) { +int BPU_gf2xPolyResize (BPU_T_GF2_16x_Poly * p, int16_t max_deg) { if (p->coef) { - free(p->coef); + free (p->coef); } - return BPU_gf2xPolyMallocCoef(p, max_deg); + return BPU_gf2xPolyMallocCoef (p, max_deg); } -int BPU_gf2xPolyMallocCoef(BPU_T_GF2_16x_Poly *p, int16_t max_deg) { +int BPU_gf2xPolyMallocCoef (BPU_T_GF2_16x_Poly * p, int16_t max_deg) { // allocate memory p->deg = -1; p->max_deg = max_deg; if (p->max_deg < 0) { - BPU_printError("BPU_mallocPoly: max_deg must be at least 0"); + BPU_printError ("BPU_mallocPoly: max_deg must be at least 0"); return -1; } - p->coef = (BPU_T_GF2_16x*) calloc(max_deg + 1, sizeof(BPU_T_GF2_16x)); + p->coef = (BPU_T_GF2_16x *) calloc (max_deg + 1, sizeof (BPU_T_GF2_16x)); if (!p->coef) { - BPU_printError("BPU_mallocPoly: can not allocate polynomial"); + BPU_printError ("BPU_mallocPoly: can not allocate polynomial"); return -1; } return 0; } -void BPU_gf2xMatNull(BPU_T_GF2_16x_Matrix *mat) { +void BPU_gf2xMatNull (BPU_T_GF2_16x_Matrix * mat) { int i, j; + for (i = 0; i < mat->k; i++) { for (j = 0; j < mat->n; j++) { mat->elements[i][j] = 0; @@ -203,37 +213,42 @@ void BPU_gf2xMatNull(BPU_T_GF2_16x_Matrix *mat) { } } -BPU_T_GF2_16x BPU_gf2xMulMod(BPU_T_GF2_16x a, BPU_T_GF2_16x b, BPU_T_GF2_16x mod) { - BPU_T_GF2_16x tmp, tmp2; +BPU_T_GF2_16x BPU_gf2xMulMod (BPU_T_GF2_16x a, BPU_T_GF2_16x b, + BPU_T_GF2_16x mod) { + BPU_T_GF2_16x tmp, tmp2; - if (a == 0) { - return 0; - } - // (a/x * b) - tmp = BPU_gf2xMulMod(a >> 1, b, mod); + if (a == 0) { + return 0; + } + // (a/x * b) + tmp = BPU_gf2xMulMod (a >> 1, b, mod); - // tmp * x - tmp = (tmp << 1); + // tmp * x + tmp = (tmp << 1); - // tmp + mod - tmp2 = tmp ^ mod; + // tmp + mod + tmp2 = tmp ^ mod; - if (tmp2 < tmp) { - tmp = tmp2; - } - if ((a & 1) == 1) { - tmp ^= b; - } - return tmp; + if (tmp2 < tmp) { + tmp = tmp2; + } + if ((a & 1) == 1) { + tmp ^= b; + } + return tmp; } -BPU_T_GF2_16x BPU_gf2xMulModT(const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, const BPU_T_Math_Ctx *math_ctx) { - BPU_T_GF2_32x condition; - BPU_T_GF2_16x candidate; - candidate = math_ctx->exp_table[(math_ctx->log_table[a] + math_ctx->log_table[b]) % math_ctx->ord]; - if ((condition = (a * b))) - return candidate; - return condition; +BPU_T_GF2_16x BPU_gf2xMulModT (const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, + const BPU_T_Math_Ctx * math_ctx) { + BPU_T_GF2_32x condition; + BPU_T_GF2_16x candidate; + + candidate = + math_ctx->exp_table[(math_ctx->log_table[a] + math_ctx->log_table[b]) % + math_ctx->ord]; + if ((condition = (a * b))) + return candidate; + return condition; } @@ -253,839 +268,921 @@ BPU_T_GF2_16x BPU_gf2xMulModT(const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, cons // return condition; //} -BPU_T_GF2_16x BPU_gf2xPowerModT(BPU_T_GF2_16x a, int e, const BPU_T_Math_Ctx *math_ctx) { - if (e == 0) { - return 1; - } - if (a == 0) { - return 0; - } - // look into log table to find i (b^i) = a - e = e * math_ctx->log_table[a]; - e = e % math_ctx->ord; - - if (e < 0) { - e = e + math_ctx->ord; - } - // look into exp table - return math_ctx->exp_table[e]; +BPU_T_GF2_16x BPU_gf2xPowerModT (BPU_T_GF2_16x a, int e, + const BPU_T_Math_Ctx * math_ctx) { + if (e == 0) { + return 1; + } + if (a == 0) { + return 0; + } + // look into log table to find i (b^i) = a + e = e * math_ctx->log_table[a]; + e = e % math_ctx->ord; + + if (e < 0) { + e = e + math_ctx->ord; + } + // look into exp table + return math_ctx->exp_table[e]; } /*** PZ: speedup critical instructions ***/ -int BPU_gf2xMatMul(BPU_T_GF2_16x_Matrix *x, const BPU_T_GF2_16x_Matrix *a, const BPU_T_GF2_16x_Matrix *b, const BPU_T_Math_Ctx *math_ctx) { - uint32_t i, j, k; - int loga; - - if (a->n != b->k || x->k != a->k || x->n != b->n){ - BPU_printError("Wrong mat dimension."); - - return -1; - } - BPU_gf2xMatNull(x); - - for (i = 0; i < a->k; i++) { - for (j = 0; j < b->n; j++) { - x->elements[i][j] = 0; - } - } - - for (i = 0; i < a->k; i++) { - for (k = 0; k < a->n; k++) { - if (a->elements[i][k] == 0) - continue; - loga = math_ctx->log_table[a->elements[i][k]]; - for (j = 0; j < b->n; j++) { - if (b->elements[k][j] == 0) - continue; - x->elements[i][j] ^= math_ctx->exp_table[(loga + math_ctx->log_table[b->elements[k][j]]) % math_ctx->ord]; - } - } - } - return 0; +int BPU_gf2xMatMul (BPU_T_GF2_16x_Matrix * x, const BPU_T_GF2_16x_Matrix * a, + const BPU_T_GF2_16x_Matrix * b, + const BPU_T_Math_Ctx * math_ctx) { + uint32_t i, j, k; + int loga; + + if (a->n != b->k || x->k != a->k || x->n != b->n) { + BPU_printError ("Wrong mat dimension."); + + return -1; + } + BPU_gf2xMatNull (x); + + for (i = 0; i < a->k; i++) { + for (j = 0; j < b->n; j++) { + x->elements[i][j] = 0; + } + } + + for (i = 0; i < a->k; i++) { + for (k = 0; k < a->n; k++) { + if (a->elements[i][k] == 0) + continue; + loga = math_ctx->log_table[a->elements[i][k]]; + for (j = 0; j < b->n; j++) { + if (b->elements[k][j] == 0) + continue; + x->elements[i][j] ^= + math_ctx-> + exp_table[(loga + + math_ctx->log_table[b->elements[k][j]]) % + math_ctx->ord]; + } + } + } + return 0; } -void BPU_gf2xPolyAdd(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b) { - int16_t out_deg; - int i = 0; - - out_deg = a->deg > b->deg ? a->deg : b->deg; - - if (out->max_deg < out_deg) { - BPU_gf2xPolyResize(out, out_deg); - } - else { - BPU_gf2xPolyNull(out); - } - for (i = 0; i <= out_deg; i++) { - if (i <= a->deg) { - out->coef[i] ^= a->coef[i]; - } - if (i <= b->deg) { - out->coef[i] ^= b->coef[i]; - } - } - out->deg = BPU_gf2xPolyGetDeg(out); +void BPU_gf2xPolyAdd (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b) { + int16_t out_deg; + int i = 0; + + out_deg = a->deg > b->deg ? a->deg : b->deg; + + if (out->max_deg < out_deg) { + BPU_gf2xPolyResize (out, out_deg); + } + else { + BPU_gf2xPolyNull (out); + } + for (i = 0; i <= out_deg; i++) { + if (i <= a->deg) { + out->coef[i] ^= a->coef[i]; + } + if (i <= b->deg) { + out->coef[i] ^= b->coef[i]; + } + } + out->deg = BPU_gf2xPolyGetDeg (out); } -void BPU_gf2xPolyDiv(BPU_T_GF2_16x_Poly *q, BPU_T_GF2_16x_Poly *r, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, const BPU_T_Math_Ctx *math_ctx) { - // a:b = q+r +void BPU_gf2xPolyDiv (BPU_T_GF2_16x_Poly * q, BPU_T_GF2_16x_Poly * r, + const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, + const BPU_T_Math_Ctx * math_ctx) { + // a:b = q+r BPU_T_GF2_16x_Poly *tmp; - BPU_T_GF2_16x leader; + BPU_T_GF2_16x leader; BPU_T_GF2_16x_Poly *dividend; - const BPU_T_GF2_16x_Poly *divider = b; - int exponent; - int i; - int max_deg_q; - - BPU_gf2xPolyMalloc(÷nd, a->max_deg); - BPU_gf2xPolyCopy(dividend, a); - - max_deg_q = a->deg - b->deg; - - // check size of outputs - if (q->max_deg < max_deg_q) { - BPU_gf2xPolyResize(q, max_deg_q); - } - else { - BPU_gf2xPolyNull(q); - } - if (r->max_deg < (b->max_deg - 1)) { - BPU_gf2xPolyResize(r, b->max_deg - 1); - } - else { - BPU_gf2xPolyNull(r); - } - BPU_gf2xPolyMalloc(&tmp, a->max_deg); - - for (i = a->deg; i >= 0; i--) { + const BPU_T_GF2_16x_Poly *divider = b; + int exponent; + int i; + int max_deg_q; + + BPU_gf2xPolyMalloc (÷nd, a->max_deg); + BPU_gf2xPolyCopy (dividend, a); + + max_deg_q = a->deg - b->deg; + + // check size of outputs + if (q->max_deg < max_deg_q) { + BPU_gf2xPolyResize (q, max_deg_q); + } + else { + BPU_gf2xPolyNull (q); + } + if (r->max_deg < (b->max_deg - 1)) { + BPU_gf2xPolyResize (r, b->max_deg - 1); + } + else { + BPU_gf2xPolyNull (r); + } + BPU_gf2xPolyMalloc (&tmp, a->max_deg); + + for (i = a->deg; i >= 0; i--) { if (dividend->deg < divider->deg) { - BPU_gf2xPolyCopy(r, dividend); - break; - } - BPU_gf2xPolyNull(tmp); - leader = BPU_gf2xMulModT(dividend->coef[i], BPU_gf2xPowerModT(divider->coef[divider->deg], -1, math_ctx), math_ctx); + BPU_gf2xPolyCopy (r, dividend); + break; + } + BPU_gf2xPolyNull (tmp); + leader = + BPU_gf2xMulModT (dividend->coef[i], + BPU_gf2xPowerModT (divider->coef[divider->deg], -1, + math_ctx), math_ctx); exponent = dividend->deg - divider->deg; - q->coef[exponent] = leader; + q->coef[exponent] = leader; - if(q->deg == -1) { - q->deg = BPU_gf2xPolyGetDeg(q); - } - BPU_gf2xPolyMul(tmp, divider, q, math_ctx); + if (q->deg == -1) { + q->deg = BPU_gf2xPolyGetDeg (q); + } + BPU_gf2xPolyMul (tmp, divider, q, math_ctx); - BPU_gf2xPolyAdd(dividend, a, tmp); - } - BPU_gf2xPolyFree(÷nd); - BPU_gf2xPolyFree(&tmp); + BPU_gf2xPolyAdd (dividend, a, tmp); + } + BPU_gf2xPolyFree (÷nd); + BPU_gf2xPolyFree (&tmp); } -void BPU_gf2xPolyMul(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, const BPU_T_Math_Ctx *math_ctx) { - int i; - int j; - int max_deg = a->deg + b->deg; - - if (out->max_deg < max_deg) { - BPU_gf2xPolyResize(out, max_deg); - } - else { - BPU_gf2xPolyNull(out); - } - - for (i = a->deg; i >= 0; i--) { - for (j = b->deg; j >= 0; j--) { - out->coef[i+j] ^= BPU_gf2xMulModT(a->coef[i], b->coef[j], math_ctx); - } - } - out->deg = BPU_gf2xPolyGetDeg(out); +void BPU_gf2xPolyMul (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, + const BPU_T_Math_Ctx * math_ctx) { + int i; + int j; + int max_deg = a->deg + b->deg; + + if (out->max_deg < max_deg) { + BPU_gf2xPolyResize (out, max_deg); + } + else { + BPU_gf2xPolyNull (out); + } + + for (i = a->deg; i >= 0; i--) { + for (j = b->deg; j >= 0; j--) { + out->coef[i + j] ^= + BPU_gf2xMulModT (a->coef[i], b->coef[j], math_ctx); + } + } + out->deg = BPU_gf2xPolyGetDeg (out); } -void BPU_gf2xPolyShr(BPU_T_GF2_16x_Poly *a, int n) { +void BPU_gf2xPolyShr (BPU_T_GF2_16x_Poly * a, int n) { BPU_T_GF2_16x_Poly *tmp; - // if there is nothing to shift, return - if (a->deg == -1 || n <= 0) { - return; - } - BPU_gf2xPolyMalloc(&tmp, a->deg); - BPU_gf2xPolyCopy(tmp, a); - BPU_gf2xPolyNull(a); + // if there is nothing to shift, return + if (a->deg == -1 || n <= 0) { + return; + } + BPU_gf2xPolyMalloc (&tmp, a->deg); + BPU_gf2xPolyCopy (tmp, a); + BPU_gf2xPolyNull (a); if (n < tmp->deg + 1) { - memcpy((void *)(a->coef), (void *)(tmp->coef + n), (tmp->deg + 1 - n) * sizeof(BPU_T_GF2_16x)); + memcpy ((void *) (a->coef), (void *) (tmp->coef + n), + (tmp->deg + 1 - n) * sizeof (BPU_T_GF2_16x)); - a->deg = BPU_gf2xPolyGetDeg(a); - } - BPU_gf2xPolyFree(&tmp); + a->deg = BPU_gf2xPolyGetDeg (a); + } + BPU_gf2xPolyFree (&tmp); } -void BPU_gf2xPolyShl(BPU_T_GF2_16x_Poly *a, int n) { +void BPU_gf2xPolyShl (BPU_T_GF2_16x_Poly * a, int n) { BPU_T_GF2_16x_Poly *tmp; - // if there is nothing to shift, return - if (a->deg == -1 || n <= 0) { - return; - } - BPU_gf2xPolyMalloc(&tmp, a->deg); - BPU_gf2xPolyCopy(tmp, a); - - if (a->max_deg < a->deg + n) { - BPU_gf2xPolyResize(a, a->deg + n); - } - else { - BPU_gf2xPolyNull(a); - } - memcpy((void *)(a->coef + n), (void *)tmp->coef, (tmp->deg + 1) * sizeof(BPU_T_GF2_16x)); - a->deg = BPU_gf2xPolyGetDeg(a); - - BPU_gf2xPolyFree(&tmp); + // if there is nothing to shift, return + if (a->deg == -1 || n <= 0) { + return; + } + BPU_gf2xPolyMalloc (&tmp, a->deg); + BPU_gf2xPolyCopy (tmp, a); + + if (a->max_deg < a->deg + n) { + BPU_gf2xPolyResize (a, a->deg + n); + } + else { + BPU_gf2xPolyNull (a); + } + memcpy ((void *) (a->coef + n), (void *) tmp->coef, + (tmp->deg + 1) * sizeof (BPU_T_GF2_16x)); + a->deg = BPU_gf2xPolyGetDeg (a); + + BPU_gf2xPolyFree (&tmp); } -void BPU_gf2xPolyPower(BPU_T_GF2_16x_Poly *a, int e, const BPU_T_Math_Ctx *math_ctx) { - int i; +void BPU_gf2xPolyPower (BPU_T_GF2_16x_Poly * a, int e, + const BPU_T_Math_Ctx * math_ctx) { + int i; BPU_T_GF2_16x_Poly *tmp, *tmp_2; - if (e < 0) { - BPU_printError("gf2xPolyPower: e < 0, NOT IMPLEMENTED YET"); - - return; - } - if (e == 0) { - BPU_gf2xPolyNull(a); - a->coef[0] = 1; - a->deg = 0; - } - else if (e == 1 || a->deg < 0) { - return; - } - else { - BPU_gf2xPolyMalloc(&tmp, a->deg * e); - BPU_gf2xPolyMalloc(&tmp_2, a->deg * e); - - BPU_gf2xPolyCopy(tmp, a); - - for (i = 1; i < e; i++) { - BPU_gf2xPolyCopy(tmp_2, tmp); - - BPU_gf2xPolyMul(tmp, tmp_2, a, math_ctx); - } - BPU_gf2xPolyCopy(a, tmp); - - BPU_gf2xPolyFree(&tmp); - BPU_gf2xPolyFree(&tmp_2); - } + if (e < 0) { + BPU_printError ("gf2xPolyPower: e < 0, NOT IMPLEMENTED YET"); + + return; + } + if (e == 0) { + BPU_gf2xPolyNull (a); + a->coef[0] = 1; + a->deg = 0; + } + else if (e == 1 || a->deg < 0) { + return; + } + else { + BPU_gf2xPolyMalloc (&tmp, a->deg * e); + BPU_gf2xPolyMalloc (&tmp_2, a->deg * e); + + BPU_gf2xPolyCopy (tmp, a); + + for (i = 1; i < e; i++) { + BPU_gf2xPolyCopy (tmp_2, tmp); + + BPU_gf2xPolyMul (tmp, tmp_2, a, math_ctx); + } + BPU_gf2xPolyCopy (a, tmp); + + BPU_gf2xPolyFree (&tmp); + BPU_gf2xPolyFree (&tmp_2); + } } -void BPU_gf2xPolyMulEl(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x el, const BPU_T_Math_Ctx *math_ctx) { - int i; +void BPU_gf2xPolyMulEl (BPU_T_GF2_16x_Poly * a, BPU_T_GF2_16x el, + const BPU_T_Math_Ctx * math_ctx) { + int i; - for (i = a->deg; i >= 0; i--) { - a->coef[i] = BPU_gf2xMulModT(a->coef[i], el, math_ctx); - } - a->deg = BPU_gf2xPolyGetDeg(a); + for (i = a->deg; i >= 0; i--) { + a->coef[i] = BPU_gf2xMulModT (a->coef[i], el, math_ctx); + } + a->deg = BPU_gf2xPolyGetDeg (a); } -void BPU_gf2xPolyMod(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx) { - int i; +void BPU_gf2xPolyMod (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx) { + int i; BPU_T_GF2_16x_Poly *tmp_out, *tmp_mod; - BPU_T_GF2_16x lead; - - if (mod->deg < 0) { - return; - } - if (out->max_deg < a->deg) { - BPU_gf2xPolyResize(out, a->deg); - } - else { - BPU_gf2xPolyNull(out); - } - // if there is nothing to do - if (a->deg < mod->deg) { - BPU_gf2xPolyCopy(out, a); - - return; - } - // prepare tmp variables - BPU_gf2xPolyMalloc(&tmp_mod, a->deg); - BPU_gf2xPolyMalloc(&tmp_out, a->deg); - BPU_gf2xPolyCopy(tmp_out, a); - - for (i = a->deg; i >= mod->deg; i--) { - BPU_gf2xPolyCopy(tmp_mod, mod); - - lead = BPU_gf2xGetPseudoInv(BPU_gf2xPolyLeadCoef(mod), tmp_out->coef[i], math_ctx); - - BPU_gf2xPolyMulEl(tmp_mod, lead, math_ctx); - BPU_gf2xPolyShl(tmp_mod, tmp_out->deg - mod->deg); - BPU_gf2xPolyAdd(out, tmp_out, tmp_mod); - - if (i > mod->deg) { - BPU_gf2xPolyCopy(tmp_out, out); - } - } - BPU_gf2xPolyFree(&tmp_mod); - BPU_gf2xPolyFree(&tmp_out); + BPU_T_GF2_16x lead; + + if (mod->deg < 0) { + return; + } + if (out->max_deg < a->deg) { + BPU_gf2xPolyResize (out, a->deg); + } + else { + BPU_gf2xPolyNull (out); + } + // if there is nothing to do + if (a->deg < mod->deg) { + BPU_gf2xPolyCopy (out, a); + + return; + } + // prepare tmp variables + BPU_gf2xPolyMalloc (&tmp_mod, a->deg); + BPU_gf2xPolyMalloc (&tmp_out, a->deg); + BPU_gf2xPolyCopy (tmp_out, a); + + for (i = a->deg; i >= mod->deg; i--) { + BPU_gf2xPolyCopy (tmp_mod, mod); + + lead = + BPU_gf2xGetPseudoInv (BPU_gf2xPolyLeadCoef (mod), tmp_out->coef[i], + math_ctx); + + BPU_gf2xPolyMulEl (tmp_mod, lead, math_ctx); + BPU_gf2xPolyShl (tmp_mod, tmp_out->deg - mod->deg); + BPU_gf2xPolyAdd (out, tmp_out, tmp_mod); + + if (i > mod->deg) { + BPU_gf2xPolyCopy (tmp_out, out); + } + } + BPU_gf2xPolyFree (&tmp_mod); + BPU_gf2xPolyFree (&tmp_out); } -void BPU_gf2xMatRoot(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx) { - int i, j; +void BPU_gf2xMatRoot (BPU_T_GF2_16x_Matrix * out, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx) { + int i, j; BPU_T_GF2_16x_Poly *row, *tmp; - BPU_T_GF2_16x_Matrix *bigMat;//, test;//, test_out; // matrix (S | I) - - // create square matrix - BPU_gf2xMatNull(out); - BPU_gf2xMatMalloc(&bigMat, mod->deg, mod->deg * 2); - BPU_gf2xPolyMalloc(&tmp, 0); - BPU_gf2xPolyMalloc(&row, (2 * out->k)); - - for (i = 0; i < out->k; i++) { - BPU_gf2xPolyNull(row); - row->coef[2*i] = 1; - row->deg = 2*i; - // compute line - BPU_gf2xPolyMod(tmp, row, mod, math_ctx); - // copy elements from polynomial into matrix - BPU_gf2xMatInsertPoly(out, tmp, i); - } - BPU_gf2xPolyFree(&tmp); - BPU_gf2xPolyFree(&row); - - for (i = 0; i < out->k; i++) { - for (j = 0; j < out->k; j++) { + BPU_T_GF2_16x_Matrix *bigMat; //, test;//, test_out; // matrix (S | I) + + // create square matrix + BPU_gf2xMatNull (out); + BPU_gf2xMatMalloc (&bigMat, mod->deg, mod->deg * 2); + BPU_gf2xPolyMalloc (&tmp, 0); + BPU_gf2xPolyMalloc (&row, (2 * out->k)); + + for (i = 0; i < out->k; i++) { + BPU_gf2xPolyNull (row); + row->coef[2 * i] = 1; + row->deg = 2 * i; + // compute line + BPU_gf2xPolyMod (tmp, row, mod, math_ctx); + // copy elements from polynomial into matrix + BPU_gf2xMatInsertPoly (out, tmp, i); + } + BPU_gf2xPolyFree (&tmp); + BPU_gf2xPolyFree (&row); + + for (i = 0; i < out->k; i++) { + for (j = 0; j < out->k; j++) { bigMat->elements[i][j] = out->elements[i][j]; - } + } bigMat->elements[i][out->n + i] = 1; - } - BPU_gf2xMatGEM(bigMat, math_ctx); - - for (i = 0; i < out->k; i++) { - for (j = 0; j < out->k; j++) { - out->elements[i][j] = BPU_gf2xRoot(bigMat->elements[i][out->k + j], math_ctx); - } - } - BPU_gf2xMatFree(&bigMat); + } + BPU_gf2xMatGEM (bigMat, math_ctx); + + for (i = 0; i < out->k; i++) { + for (j = 0; j < out->k; j++) { + out->elements[i][j] = + BPU_gf2xRoot (bigMat->elements[i][out->k + j], math_ctx); + } + } + BPU_gf2xMatFree (&bigMat); } -void BPU_gf2xVecMulMat(BPU_T_GF2_16x_Vector *out, const BPU_T_GF2_16x_Vector *x, const BPU_T_GF2_16x_Matrix *mat, const BPU_T_Math_Ctx *math_ctx) { - int i, j; - BPU_T_GF2_16x element; - - for (i = 0; i < x->len; i++) { - element = 0; - for (j = 0; j < mat->n; j++) { - element = element ^ BPU_gf2xMulModT(x->elements[j], mat->elements[j][i], math_ctx); - } - out->elements[i] = element; - } +void BPU_gf2xVecMulMat (BPU_T_GF2_16x_Vector * out, + const BPU_T_GF2_16x_Vector * x, + const BPU_T_GF2_16x_Matrix * mat, + const BPU_T_Math_Ctx * math_ctx) { + int i, j; + BPU_T_GF2_16x element; + + for (i = 0; i < x->len; i++) { + element = 0; + for (j = 0; j < mat->n; j++) { + element = + element ^ BPU_gf2xMulModT (x->elements[j], mat->elements[j][i], + math_ctx); + } + out->elements[i] = element; + } } -BPU_T_GF2_16x BPU_gf2xRoot(BPU_T_GF2_16x element, const BPU_T_Math_Ctx *math_ctx) { - BPU_T_GF2_16x sqr; - BPU_T_GF2_16x sqr_alpha; - int exponent; - if (element == 0) { - return 0; - } - exponent = math_ctx->log_table[element]; - sqr = math_ctx->exp_table[exponent >> 1]; - if ((exponent & 1) == 1) { - sqr_alpha = math_ctx->exp_table[(math_ctx->ord + 1) / 2]; - sqr = BPU_gf2xMulModT(sqr, sqr_alpha, math_ctx); - } - return sqr; +BPU_T_GF2_16x BPU_gf2xRoot (BPU_T_GF2_16x element, + const BPU_T_Math_Ctx * math_ctx) { + BPU_T_GF2_16x sqr; + BPU_T_GF2_16x sqr_alpha; + int exponent; + + if (element == 0) { + return 0; + } + exponent = math_ctx->log_table[element]; + sqr = math_ctx->exp_table[exponent >> 1]; + if ((exponent & 1) == 1) { + sqr_alpha = math_ctx->exp_table[(math_ctx->ord + 1) / 2]; + sqr = BPU_gf2xMulModT (sqr, sqr_alpha, math_ctx); + } + return sqr; } -void BPU_gf2xPolyRoot(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx) { +void BPU_gf2xPolyRoot (BPU_T_GF2_16x_Poly * out, + const BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx) { BPU_T_GF2_16x_Vector *tmp, *tmp_out; BPU_T_GF2_16x_Matrix *squareMat; - int i; + int i; - BPU_gf2xVecMalloc(&tmp_out, mod->deg); + BPU_gf2xVecMalloc (&tmp_out, mod->deg); - if (out->deg < mod->deg) { - BPU_gf2xPolyResize(out, mod->deg); - } - BPU_gf2xVecMalloc(&tmp, mod->deg); - BPU_gf2xPolyToVec(tmp, poly, mod->deg); - BPU_gf2xMatMalloc(&squareMat, mod->deg, mod->deg); - BPU_gf2xMatRoot(squareMat, mod, math_ctx); + if (out->deg < mod->deg) { + BPU_gf2xPolyResize (out, mod->deg); + } + BPU_gf2xVecMalloc (&tmp, mod->deg); + BPU_gf2xPolyToVec (tmp, poly, mod->deg); + BPU_gf2xMatMalloc (&squareMat, mod->deg, mod->deg); + BPU_gf2xMatRoot (squareMat, mod, math_ctx); for (i = 0; i < tmp->len; i++) { - tmp->elements[i] = BPU_gf2xRoot(tmp->elements[i], math_ctx); - } - BPU_gf2xVecMulMat(tmp_out, tmp, squareMat, math_ctx); - BPU_gf2xPolyNull(out); - BPU_gf2xVecToPoly(out, tmp_out); - - BPU_gf2xMatFree(&squareMat); - BPU_gf2xVecFree(&tmp); - BPU_gf2xVecFree(&tmp_out); + tmp->elements[i] = BPU_gf2xRoot (tmp->elements[i], math_ctx); + } + BPU_gf2xVecMulMat (tmp_out, tmp, squareMat, math_ctx); + BPU_gf2xPolyNull (out); + BPU_gf2xVecToPoly (out, tmp_out); + + BPU_gf2xMatFree (&squareMat); + BPU_gf2xVecFree (&tmp); + BPU_gf2xVecFree (&tmp_out); } -int BPU_gf2xGetDeg(BPU_T_GF2_32x poly) { - int i = 31; +int BPU_gf2xGetDeg (BPU_T_GF2_32x poly) { + int i = 31; - while (i >= 0) { - if (BPU_getBit(poly, i)) { - return i; - } - i--; - } - return -1; + while (i >= 0) { + if (BPU_getBit (poly, i)) { + return i; + } + i--; + } + return -1; } -int BPU_gf2xPolyGetDeg(BPU_T_GF2_16x_Poly *poly) { - int i = poly->max_deg; +int BPU_gf2xPolyGetDeg (BPU_T_GF2_16x_Poly * poly) { + int i = poly->max_deg; - while (i >= 0) { - if (poly->coef[i] != 0) { - return i; - } - i--; - } - return -1; + while (i >= 0) { + if (poly->coef[i] != 0) { + return i; + } + i--; + } + return -1; } -int BPU_gf2xMatPermute(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Matrix *m, const BPU_T_Perm_Vector *permutation) { - int i, j; - - // check if the size is correct - if (m->n != permutation->size) { - BPU_printError("BPU_gf2MatPermute: permutation size not correct m->n = %d, p->size = %d", m->n, permutation->size); - - return -1; - } - // permute - for (j = 0; j < m->n; j++) { // column loop - for (i = 0; i < m->k; i++) { // row loop - out->elements[i][j] = m->elements[i][permutation->elements[j]]; // permute the columns - } - } - return 0; +int BPU_gf2xMatPermute (BPU_T_GF2_16x_Matrix * out, + const BPU_T_GF2_16x_Matrix * m, + const BPU_T_Perm_Vector * permutation) { + int i, j; + + // check if the size is correct + if (m->n != permutation->size) { + BPU_printError + ("BPU_gf2MatPermute: permutation size not correct m->n = %d, p->size = %d", + m->n, permutation->size); + + return -1; + } + // permute + for (j = 0; j < m->n; j++) { // column loop + for (i = 0; i < m->k; i++) { // row loop + out->elements[i][j] = m->elements[i][permutation->elements[j]]; // permute the columns + } + } + return 0; } -int BPU_gf2xMatConvertToGf2Mat(BPU_T_GF2_Matrix *out, const BPU_T_GF2_16x_Matrix *m, int element_bit_size) { - int i, j, bit, bit_in_element = -1, act_element = 0; - - if (out->k != m->k * element_bit_size || out->n != m->n) { - BPU_printError("Wrong matrix dimension."); - - return -1; - } - // converting - for (j = 0; j < m->n; j++) { // column loop - // check if there is shift through elements - if ((j - act_element * out->element_bit_size) >= out->element_bit_size) { // next elemenet, first bit - act_element++; - bit_in_element = 0; - } - else // same element, next bit - bit_in_element++; - for (i = 0; i < m->k; i++) { // row loop - for (bit = 0; bit < element_bit_size; bit++) { // bit loop through element of matrix - out->elements[i*element_bit_size + bit][act_element] ^= BPU_getBit(m->elements[i][j], bit) << (bit_in_element); // get bit from element and shift it - } - } - } - return 0; +int BPU_gf2xMatConvertToGf2Mat (BPU_T_GF2_Matrix * out, + const BPU_T_GF2_16x_Matrix * m, + int element_bit_size) { + int i, j, bit, bit_in_element = -1, act_element = 0; + + if (out->k != m->k * element_bit_size || out->n != m->n) { + BPU_printError ("Wrong matrix dimension."); + + return -1; + } + // converting + for (j = 0; j < m->n; j++) { // column loop + // check if there is shift through elements + if ((j - act_element * out->element_bit_size) >= out->element_bit_size) { // next elemenet, first bit + act_element++; + bit_in_element = 0; + } + else // same element, next bit + bit_in_element++; + for (i = 0; i < m->k; i++) { // row loop + for (bit = 0; bit < element_bit_size; bit++) { // bit loop through element of matrix + out->elements[i * element_bit_size + bit][act_element] ^= BPU_getBit (m->elements[i][j], bit) << (bit_in_element); // get bit from element and shift it + } + } + } + return 0; } -int BPU_gf2xPolyExtEuclid(BPU_T_GF2_16x_Poly *d, BPU_T_GF2_16x_Poly *s, BPU_T_GF2_16x_Poly *t, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, const int end_deg, const BPU_T_Math_Ctx *math_ctx) { +int BPU_gf2xPolyExtEuclid (BPU_T_GF2_16x_Poly * d, BPU_T_GF2_16x_Poly * s, + BPU_T_GF2_16x_Poly * t, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, const int end_deg, + const BPU_T_Math_Ctx * math_ctx) { BPU_T_GF2_16x_Poly *tmp, *tmp_2, *old_s, *old_t, *old_r, *r, *q; - BPU_T_GF2_16x inv_lead; - int deg; - - deg = (a->deg > b->deg) ? a->deg : b->deg; - - // check GCD qoutient size - if (d->max_deg < deg) { - BPU_gf2xPolyResize(d, deg); - } - if (s->max_deg < deg) { - BPU_gf2xPolyResize(s, deg); - } - if (t->max_deg < deg) { - BPU_gf2xPolyResize(t, deg); - } - BPU_gf2xPolyMalloc(&tmp, deg); - BPU_gf2xPolyMalloc(&tmp_2, deg); - BPU_gf2xPolyMalloc(&old_s, deg); - BPU_gf2xPolyMalloc(&old_t, deg); - BPU_gf2xPolyMalloc(&old_r, deg); - BPU_gf2xPolyMalloc(&r, deg); - BPU_gf2xPolyMalloc(&q, deg); - - BPU_gf2xPolyCopy(r, b); - BPU_gf2xPolyCopy(old_r, a); - - if (a->deg == -1) { - BPU_gf2xPolyCopy(old_r, b); + BPU_T_GF2_16x inv_lead; + int deg; + + deg = (a->deg > b->deg) ? a->deg : b->deg; + + // check GCD qoutient size + if (d->max_deg < deg) { + BPU_gf2xPolyResize (d, deg); + } + if (s->max_deg < deg) { + BPU_gf2xPolyResize (s, deg); + } + if (t->max_deg < deg) { + BPU_gf2xPolyResize (t, deg); + } + BPU_gf2xPolyMalloc (&tmp, deg); + BPU_gf2xPolyMalloc (&tmp_2, deg); + BPU_gf2xPolyMalloc (&old_s, deg); + BPU_gf2xPolyMalloc (&old_t, deg); + BPU_gf2xPolyMalloc (&old_r, deg); + BPU_gf2xPolyMalloc (&r, deg); + BPU_gf2xPolyMalloc (&q, deg); + + BPU_gf2xPolyCopy (r, b); + BPU_gf2xPolyCopy (old_r, a); + + if (a->deg == -1) { + BPU_gf2xPolyCopy (old_r, b); old_t->coef[0] = 1; old_t->deg = 0; - } - else if (b->deg == -1) { - BPU_gf2xPolyCopy(old_r, a); + } + else if (b->deg == -1) { + BPU_gf2xPolyCopy (old_r, a); old_s->coef[0] = 1; old_s->deg = 0; - } - else { + } + else { old_s->coef[0] = 1; old_s->deg = 0; - t->coef[0] = 1; - t->deg = 0; + t->coef[0] = 1; + t->deg = 0; while (old_r->deg > end_deg && r->deg > -1) { - BPU_gf2xPolyDiv(q, tmp, old_r, r, math_ctx); - - // save old reminder - BPU_gf2xPolyCopy(old_r, r); - // save current reminder - BPU_gf2xPolyCopy(r, tmp); - - // save s quocient - BPU_gf2xPolyCopy(tmp, old_s); - BPU_gf2xPolyCopy(old_s, s); - BPU_gf2xPolyMul(tmp_2, q, s, math_ctx); - BPU_gf2xPolyAdd(s, tmp, tmp_2); - - // save t quocient - BPU_gf2xPolyCopy(tmp, old_t); - BPU_gf2xPolyCopy(old_t, t); - BPU_gf2xPolyMul(tmp_2, q, t, math_ctx); - BPU_gf2xPolyAdd(t, tmp, tmp_2); - } - } - // prepare return values - BPU_gf2xPolyCopy(d, old_r); - BPU_gf2xPolyCopy(s, old_s); - BPU_gf2xPolyCopy(t, old_t); - - // make monic, if it is not - inv_lead = BPU_gf2xPolyMakeMonic(d, math_ctx); - - if (inv_lead != 0) { - BPU_gf2xPolyMulEl(s, inv_lead, math_ctx); - BPU_gf2xPolyMulEl(t, inv_lead, math_ctx); - } - BPU_gf2xPolyFree(&tmp); - BPU_gf2xPolyFree(&tmp_2); - BPU_gf2xPolyFree(&old_s); - BPU_gf2xPolyFree(&old_t); - BPU_gf2xPolyFree(&old_r); - BPU_gf2xPolyFree(&r); - BPU_gf2xPolyFree(&q); - - return 0; + BPU_gf2xPolyDiv (q, tmp, old_r, r, math_ctx); + + // save old reminder + BPU_gf2xPolyCopy (old_r, r); + // save current reminder + BPU_gf2xPolyCopy (r, tmp); + + // save s quocient + BPU_gf2xPolyCopy (tmp, old_s); + BPU_gf2xPolyCopy (old_s, s); + BPU_gf2xPolyMul (tmp_2, q, s, math_ctx); + BPU_gf2xPolyAdd (s, tmp, tmp_2); + + // save t quocient + BPU_gf2xPolyCopy (tmp, old_t); + BPU_gf2xPolyCopy (old_t, t); + BPU_gf2xPolyMul (tmp_2, q, t, math_ctx); + BPU_gf2xPolyAdd (t, tmp, tmp_2); + } + } + // prepare return values + BPU_gf2xPolyCopy (d, old_r); + BPU_gf2xPolyCopy (s, old_s); + BPU_gf2xPolyCopy (t, old_t); + + // make monic, if it is not + inv_lead = BPU_gf2xPolyMakeMonic (d, math_ctx); + + if (inv_lead != 0) { + BPU_gf2xPolyMulEl (s, inv_lead, math_ctx); + BPU_gf2xPolyMulEl (t, inv_lead, math_ctx); + } + BPU_gf2xPolyFree (&tmp); + BPU_gf2xPolyFree (&tmp_2); + BPU_gf2xPolyFree (&old_s); + BPU_gf2xPolyFree (&old_t); + BPU_gf2xPolyFree (&old_r); + BPU_gf2xPolyFree (&r); + BPU_gf2xPolyFree (&q); + + return 0; } -BPU_T_GF2_16x BPU_gf2xPolyEval(const BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x x, const BPU_T_Math_Ctx *math_ctx) { - int i; - BPU_T_GF2_16x ret = poly->coef[poly->deg]; +BPU_T_GF2_16x BPU_gf2xPolyEval (const BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x x, + const BPU_T_Math_Ctx * math_ctx) { + int i; + BPU_T_GF2_16x ret = poly->coef[poly->deg]; - for (i = poly->deg; i > 0; i--) { - ret = BPU_gf2xMulModT(ret, x, math_ctx) ^ poly->coef[i-1]; - } - return ret; + for (i = poly->deg; i > 0; i--) { + ret = BPU_gf2xMulModT (ret, x, math_ctx) ^ poly->coef[i - 1]; + } + return ret; } -int BPU_gf2xPolyCmp(const BPU_T_GF2_16x_Poly *p1, const BPU_T_GF2_16x_Poly *p2) { - int i; - if (p1->deg != p2->deg) { +int BPU_gf2xPolyCmp (const BPU_T_GF2_16x_Poly * p1, + const BPU_T_GF2_16x_Poly * p2) { + int i; + + if (p1->deg != p2->deg) { return -1; - } - for (i = 0; i <= p1->deg; i++) { - if (p1->coef[i] != p2->coef[i]) { + } + for (i = 0; i <= p1->deg; i++) { + if (p1->coef[i] != p2->coef[i]) { return i + 1; - } - } + } + } return 0; } -int BPU_gf2xPolyIrredTest(const BPU_T_GF2_16x_Poly *p, const BPU_T_Math_Ctx *math_ctx) { - // x^(q^n) = x^((2^m)^n) = x^(2^(m*n)) - // q = 2^m - // check if gcd(x^(q^n) - x, f)==f. if not, f is reducible - // find all prime factors of n = deg(f) - // for every prime factor t of n check if gcd(x^(q^(n/t)), f) == 1. if not f is reducible - // otherwise f is ireducible - int i, j; - int is_irred = 1; - int m = math_ctx->mod_deg; - int n = p->deg; - int exponent = m*n; +int BPU_gf2xPolyIrredTest (const BPU_T_GF2_16x_Poly * p, + const BPU_T_Math_Ctx * math_ctx) { + // x^(q^n) = x^((2^m)^n) = x^(2^(m*n)) + // q = 2^m + // check if gcd(x^(q^n) - x, f)==f. if not, f is reducible + // find all prime factors of n = deg(f) + // for every prime factor t of n check if gcd(x^(q^(n/t)), f) == 1. if not f is reducible + // otherwise f is ireducible + int i, j; + int is_irred = 1; + int m = math_ctx->mod_deg; + int n = p->deg; + int exponent = m * n; BPU_T_GF2_16x_Poly *tmp, *out, *qr, *x, *gcd, *s, *t, *one; - // test if some alpha is root - for (i = 0; i < math_ctx->ord; i++) { - if (BPU_gf2xPolyEval(p, math_ctx->exp_table[i], math_ctx) == 0) { - return 0; - } - } - BPU_gf2xPolyMalloc(&out, 0); - BPU_gf2xPolyMalloc(&one, 0); - BPU_gf2xPolyMalloc(&qr, 0); - BPU_gf2xPolyMalloc(&tmp, 1); - BPU_gf2xPolyMalloc(&x, 1); - // gcd, s, t, will be allocated inside gf2xPolyExtEuclidA - - // set tmp polynomial tmp(x) = x + // test if some alpha is root + for (i = 0; i < math_ctx->ord; i++) { + if (BPU_gf2xPolyEval (p, math_ctx->exp_table[i], math_ctx) == 0) { + return 0; + } + } + BPU_gf2xPolyMalloc (&out, 0); + BPU_gf2xPolyMalloc (&one, 0); + BPU_gf2xPolyMalloc (&qr, 0); + BPU_gf2xPolyMalloc (&tmp, 1); + BPU_gf2xPolyMalloc (&x, 1); + // gcd, s, t, will be allocated inside gf2xPolyExtEuclidA + + // set tmp polynomial tmp(x) = x tmp->coef[0] = 0; tmp->coef[1] = 1; - tmp->deg = BPU_gf2xPolyGetDeg(tmp); + tmp->deg = BPU_gf2xPolyGetDeg (tmp); x->coef[0] = 0; x->coef[1] = 1; - x->deg = BPU_gf2xPolyGetDeg(x); + x->deg = BPU_gf2xPolyGetDeg (x); one->coef[0] = 1; one->deg = 0; - // simplify polynomial with big coeffitient - for (i = 0; i < exponent; i++) { - BPU_gf2xPolyMul(out, tmp, tmp, math_ctx); - BPU_gf2xPolyMod(tmp, out, p, math_ctx); - } - BPU_gf2xPolyAdd(qr, tmp, x); + // simplify polynomial with big coeffitient + for (i = 0; i < exponent; i++) { + BPU_gf2xPolyMul (out, tmp, tmp, math_ctx); + BPU_gf2xPolyMod (tmp, out, p, math_ctx); + } + BPU_gf2xPolyAdd (qr, tmp, x); - BPU_gf2xPolyMalloc(&gcd, (qr->deg > p->deg) ? qr->deg : p->deg); - BPU_gf2xPolyMalloc(&s, gcd->max_deg); - BPU_gf2xPolyMalloc(&t, gcd->max_deg); + BPU_gf2xPolyMalloc (&gcd, (qr->deg > p->deg) ? qr->deg : p->deg); + BPU_gf2xPolyMalloc (&s, gcd->max_deg); + BPU_gf2xPolyMalloc (&t, gcd->max_deg); - BPU_gf2xPolyExtEuclid(gcd, s, t, qr, p, -1, math_ctx); + BPU_gf2xPolyExtEuclid (gcd, s, t, qr, p, -1, math_ctx); // if differs - if (BPU_gf2xPolyCmp(p, gcd)) { - BPU_gf2xPolyFree(&out); - BPU_gf2xPolyFree(&one); - BPU_gf2xPolyFree(&qr); - BPU_gf2xPolyFree(&tmp); - BPU_gf2xPolyFree(&x); - BPU_gf2xPolyFree(&gcd); - BPU_gf2xPolyFree(&s); - BPU_gf2xPolyFree(&t); - return 0; - } - - for (j = 2; j <= p->deg; j++) { - if (p->deg % j != 0 || BPU_isPrime(j) == 0){ - continue; - } - BPU_gf2xPolyNull(tmp); + if (BPU_gf2xPolyCmp (p, gcd)) { + BPU_gf2xPolyFree (&out); + BPU_gf2xPolyFree (&one); + BPU_gf2xPolyFree (&qr); + BPU_gf2xPolyFree (&tmp); + BPU_gf2xPolyFree (&x); + BPU_gf2xPolyFree (&gcd); + BPU_gf2xPolyFree (&s); + BPU_gf2xPolyFree (&t); + return 0; + } + + for (j = 2; j <= p->deg; j++) { + if (p->deg % j != 0 || BPU_isPrime (j) == 0) { + continue; + } + BPU_gf2xPolyNull (tmp); tmp->coef[0] = 0; tmp->coef[1] = 1; - tmp->deg = BPU_gf2xPolyGetDeg(tmp); - - exponent = m*(n/j); - - for (i = 0; i < exponent; i++) { - BPU_gf2xPolyMul(out, tmp, tmp, math_ctx); - BPU_gf2xPolyMod(tmp, out, p, math_ctx); - } - BPU_gf2xPolyExtEuclid(gcd, s, t, tmp, p, -1, math_ctx); + tmp->deg = BPU_gf2xPolyGetDeg (tmp); + + exponent = m * (n / j); + + for (i = 0; i < exponent; i++) { + BPU_gf2xPolyMul (out, tmp, tmp, math_ctx); + BPU_gf2xPolyMod (tmp, out, p, math_ctx); + } + BPU_gf2xPolyExtEuclid (gcd, s, t, tmp, p, -1, math_ctx); // if differs - if (BPU_gf2xPolyCmp(one, gcd)) { - is_irred = 0; - break; - } - } - BPU_gf2xPolyFree(&out); - BPU_gf2xPolyFree(&one); - BPU_gf2xPolyFree(&qr); - BPU_gf2xPolyFree(&tmp); - BPU_gf2xPolyFree(&x); - BPU_gf2xPolyFree(&gcd); - BPU_gf2xPolyFree(&s); - BPU_gf2xPolyFree(&t); - - return is_irred; + if (BPU_gf2xPolyCmp (one, gcd)) { + is_irred = 0; + break; + } + } + BPU_gf2xPolyFree (&out); + BPU_gf2xPolyFree (&one); + BPU_gf2xPolyFree (&qr); + BPU_gf2xPolyFree (&tmp); + BPU_gf2xPolyFree (&x); + BPU_gf2xPolyFree (&gcd); + BPU_gf2xPolyFree (&s); + BPU_gf2xPolyFree (&t); + + return is_irred; } -void BPU_gf2xPolyCopy(BPU_T_GF2_16x_Poly *dest, const BPU_T_GF2_16x_Poly *src) { - // if there is not enough space resize - if (dest->max_deg < src->max_deg) { - BPU_gf2xPolyResize(dest, src->max_deg); - } - else { - BPU_gf2xPolyNull(dest); - } - memcpy((void *) (dest->coef), (void *) (src->coef), sizeof(BPU_T_GF2_16x) * (src->max_deg + 1)); - - dest->deg = src->deg; +void BPU_gf2xPolyCopy (BPU_T_GF2_16x_Poly * dest, + const BPU_T_GF2_16x_Poly * src) { + // if there is not enough space resize + if (dest->max_deg < src->max_deg) { + BPU_gf2xPolyResize (dest, src->max_deg); + } + else { + BPU_gf2xPolyNull (dest); + } + memcpy ((void *) (dest->coef), (void *) (src->coef), + sizeof (BPU_T_GF2_16x) * (src->max_deg + 1)); + + dest->deg = src->deg; } -void BPU_gf2xPolyInv(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx) { +void BPU_gf2xPolyInv (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx) { BPU_T_GF2_16x_Poly *d, *t; - - BPU_gf2xPolyMalloc(&d, (a->deg > mod->deg) ? a->deg : mod->deg); - BPU_gf2xPolyMalloc(&t, d->max_deg); - BPU_gf2xPolyExtEuclid(d, out, t, a, mod, 0, math_ctx); + BPU_gf2xPolyMalloc (&d, (a->deg > mod->deg) ? a->deg : mod->deg); + BPU_gf2xPolyMalloc (&t, d->max_deg); + + BPU_gf2xPolyExtEuclid (d, out, t, a, mod, 0, math_ctx); if (d->deg != 0 || d->coef[0] != 1) { - BPU_printDebug("inverse polynomial NOT found"); - BPU_printError("degree: %d\nelement: %d", d->deg, d->coef[0]); + BPU_printDebug ("inverse polynomial NOT found"); + BPU_printError ("degree: %d\nelement: %d", d->deg, d->coef[0]); #ifdef BPU_CONF_PRINT - BPU_printGf2xPoly(out, math_ctx); + BPU_printGf2xPoly (out, math_ctx); #endif - BPU_gf2xPolyNull(out); - } - BPU_gf2xPolyFree(&d); - BPU_gf2xPolyFree(&t); + BPU_gf2xPolyNull (out); + } + BPU_gf2xPolyFree (&d); + BPU_gf2xPolyFree (&t); } -BPU_T_GF2_16x BPU_gf2xPolyMakeMonic(BPU_T_GF2_16x_Poly *a, const BPU_T_Math_Ctx *math_ctx) { - BPU_T_GF2_16x inv_lead = 0; +BPU_T_GF2_16x BPU_gf2xPolyMakeMonic (BPU_T_GF2_16x_Poly * a, + const BPU_T_Math_Ctx * math_ctx) { + BPU_T_GF2_16x inv_lead = 0; - // if it is already monic do nothing - if ((a->deg > -1 && a->coef[a->deg] == 1) || a->deg == -1) { - return inv_lead; - } - inv_lead = BPU_gf2xInvElement(BPU_gf2xPolyLeadCoef(a), math_ctx); - BPU_gf2xPolyMulEl(a, inv_lead, math_ctx); + // if it is already monic do nothing + if ((a->deg > -1 && a->coef[a->deg] == 1) || a->deg == -1) { + return inv_lead; + } + inv_lead = BPU_gf2xInvElement (BPU_gf2xPolyLeadCoef (a), math_ctx); + BPU_gf2xPolyMulEl (a, inv_lead, math_ctx); - return inv_lead; + return inv_lead; } -BPU_T_GF2_16x BPU_gf2xGetPseudoInv(const BPU_T_GF2_16x a, const BPU_T_GF2_16x res, const BPU_T_Math_Ctx *math_ctx) { - return BPU_gf2xMulModT(BPU_gf2xInvElement(a, math_ctx), res, math_ctx); +BPU_T_GF2_16x BPU_gf2xGetPseudoInv (const BPU_T_GF2_16x a, + const BPU_T_GF2_16x res, + const BPU_T_Math_Ctx * math_ctx) { + return BPU_gf2xMulModT (BPU_gf2xInvElement (a, math_ctx), res, math_ctx); } -void BPU_gf2xMatInsertPoly(BPU_T_GF2_16x_Matrix *mat, const BPU_T_GF2_16x_Poly *poly, int i) { - int j; - for (j = 0; j <= poly->deg; j++) { - mat->elements[i][j] = poly->coef[j]; - } +void BPU_gf2xMatInsertPoly (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_GF2_16x_Poly * poly, int i) { + int j; + + for (j = 0; j <= poly->deg; j++) { + mat->elements[i][j] = poly->coef[j]; + } } -void BPU_gf2xPolyToVec(BPU_T_GF2_16x_Vector *vec, const BPU_T_GF2_16x_Poly *poly, int len) { - int i; +void BPU_gf2xPolyToVec (BPU_T_GF2_16x_Vector * vec, + const BPU_T_GF2_16x_Poly * poly, int len) { + int i; - if (poly->deg >= len) { - BPU_printError("dimension missmatch"); + if (poly->deg >= len) { + BPU_printError ("dimension missmatch"); - exit(-1); - } - vec->len = len; + exit (-1); + } + vec->len = len; - for (i = 0; i <= poly->deg; i++) { - vec->elements[i] = poly->coef[i]; - } + for (i = 0; i <= poly->deg; i++) { + vec->elements[i] = poly->coef[i]; + } } -void BPU_gf2xVecToPoly(BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x_Vector *vec) { - int i; - - for (i = 0; i < vec->len; i++) { - poly->coef[i] = vec->elements[i]; - } - poly->deg = BPU_gf2xPolyGetDeg(poly); +void BPU_gf2xVecToPoly (BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x_Vector * vec) { + int i; + + for (i = 0; i < vec->len; i++) { + poly->coef[i] = vec->elements[i]; + } + poly->deg = BPU_gf2xPolyGetDeg (poly); } -void BPU_gf2xSwap(BPU_T_GF2_16x *a, BPU_T_GF2_16x *b) { - BPU_T_GF2_16x tmp; - tmp = *a; - *a = *b; - *b = tmp; +void BPU_gf2xSwap (BPU_T_GF2_16x * a, BPU_T_GF2_16x * b) { + BPU_T_GF2_16x tmp; + + tmp = *a; + *a = *b; + *b = tmp; } -void BPU_gf2xMatSwapRows(BPU_T_GF2_16x_Matrix *mat, int i, int j) { - int k; - for (k = 0; k < mat->n; k++) { - BPU_gf2xSwap(&(mat->elements[i][k]), &(mat->elements[j][k])); - } +void BPU_gf2xMatSwapRows (BPU_T_GF2_16x_Matrix * mat, int i, int j) { + int k; + + for (k = 0; k < mat->n; k++) { + BPU_gf2xSwap (&(mat->elements[i][k]), &(mat->elements[j][k])); + } } -void BPU_gf2xMatMulElRow(BPU_T_GF2_16x_Matrix *mat, const BPU_T_GF2_16x element, int row, const BPU_T_Math_Ctx *math_ctx) { - int i; - for (i = 0; i < mat->n; i++) { - mat->elements[row][i] = BPU_gf2xMulModT(element, mat->elements[row][i], math_ctx); - } +void BPU_gf2xMatMulElRow (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_GF2_16x element, int row, + const BPU_T_Math_Ctx * math_ctx) { + int i; + + for (i = 0; i < mat->n; i++) { + mat->elements[row][i] = + BPU_gf2xMulModT (element, mat->elements[row][i], math_ctx); + } } -int BPU_gf2xMatFindPivot(const BPU_T_GF2_16x_Matrix *mat, int index) { - int i; - for (i = index; i < mat->k; i++) { - if(mat->elements[i][index] != 0) { - return i; - } - } - return -1; +int BPU_gf2xMatFindPivot (const BPU_T_GF2_16x_Matrix * mat, int index) { + int i; + + for (i = index; i < mat->k; i++) { + if (mat->elements[i][index] != 0) { + return i; + } + } + return -1; } -void BPU_gf2xVecMulEl(BPU_T_GF2_16x_Vector *vec, BPU_T_GF2_16x element, const BPU_T_Math_Ctx *math_ctx) { - int i; - for (i = 0; i < vec->len; i++) { - vec->elements[i] = BPU_gf2xMulModT(element, vec->elements[i], math_ctx); - } +void BPU_gf2xVecMulEl (BPU_T_GF2_16x_Vector * vec, BPU_T_GF2_16x element, + const BPU_T_Math_Ctx * math_ctx) { + int i; + + for (i = 0; i < vec->len; i++) { + vec->elements[i] = + BPU_gf2xMulModT (element, vec->elements[i], math_ctx); + } } -void BPU_gf2xMatXorRows(BPU_T_GF2_16x_Matrix *mat, int index, int j, const BPU_T_Math_Ctx *math_ctx) { - int k; - BPU_T_GF2_16x element; +void BPU_gf2xMatXorRows (BPU_T_GF2_16x_Matrix * mat, int index, int j, + const BPU_T_Math_Ctx * math_ctx) { + int k; + BPU_T_GF2_16x element; BPU_T_GF2_16x_Vector *tmp; - BPU_gf2xVecMalloc(&tmp, mat->n); + BPU_gf2xVecMalloc (&tmp, mat->n); for (k = 0; k < tmp->len; k++) { tmp->elements[k] = mat->elements[index][k]; - } - element = mat->elements[j][index]; + } + element = mat->elements[j][index]; - BPU_gf2xVecMulEl(tmp, element, math_ctx); + BPU_gf2xVecMulEl (tmp, element, math_ctx); for (k = 0; k < tmp->len; k++) { mat->elements[j][k] = mat->elements[j][k] ^ tmp->elements[k]; - } - BPU_gf2xVecFree(&tmp); + } + BPU_gf2xVecFree (&tmp); } -void BPU_gf2xMatClearCol(BPU_T_GF2_16x_Matrix *mat, int index, const BPU_T_Math_Ctx *math_ctx) { - int i; - for (i = 0; i < mat->k; i++) { - if (i == index) { - continue; - } - BPU_gf2xMatXorRows(mat, index, i, math_ctx); - } +void BPU_gf2xMatClearCol (BPU_T_GF2_16x_Matrix * mat, int index, + const BPU_T_Math_Ctx * math_ctx) { + int i; + + for (i = 0; i < mat->k; i++) { + if (i == index) { + continue; + } + BPU_gf2xMatXorRows (mat, index, i, math_ctx); + } } -void BPU_gf2xMatGEM(BPU_T_GF2_16x_Matrix *mat, const BPU_T_Math_Ctx *math_ctx) { - int i, pivot; - BPU_T_GF2_16x element; - for (i = 0; i < mat->k; i++) { - pivot = BPU_gf2xMatFindPivot(mat, i); - if (pivot == -1) - BPU_printError("################# unbeliviable 'badness' ###########"); - if (pivot != i) { - BPU_gf2xMatSwapRows(mat, i, pivot); - } - if (mat->elements[i][i] != 1) { - element = BPU_gf2xPowerModT(mat->elements[i][i], -1, math_ctx); - BPU_gf2xMatMulElRow(mat, element, i, math_ctx); - } - BPU_gf2xMatClearCol(mat, i, math_ctx); - } +void BPU_gf2xMatGEM (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_Math_Ctx * math_ctx) { + int i, pivot; + BPU_T_GF2_16x element; + + for (i = 0; i < mat->k; i++) { + pivot = BPU_gf2xMatFindPivot (mat, i); + if (pivot == -1) + BPU_printError + ("################# unbeliviable 'badness' ###########"); + if (pivot != i) { + BPU_gf2xMatSwapRows (mat, i, pivot); + } + if (mat->elements[i][i] != 1) { + element = BPU_gf2xPowerModT (mat->elements[i][i], -1, math_ctx); + BPU_gf2xMatMulElRow (mat, element, i, math_ctx); + } + BPU_gf2xMatClearCol (mat, i, math_ctx); + } } -void BPU_gf2xPolyGenRandom(BPU_T_GF2_16x_Poly *p, int t, const BPU_T_Math_Ctx *math_ctx) { - int i; - p->coef[t] = 1; - p->coef[0] = BPU_prngGetRand(1, math_ctx->ord); - - for (i = 1; i < t; i++) { - p->coef[i] = BPU_prngGetRand(0, math_ctx->ord); - } - p->deg = t; +void BPU_gf2xPolyGenRandom (BPU_T_GF2_16x_Poly * p, int t, + const BPU_T_Math_Ctx * math_ctx) { + int i; + + p->coef[t] = 1; + p->coef[0] = BPU_prngGetRand (1, math_ctx->ord); + + for (i = 1; i < t; i++) { + p->coef[i] = BPU_prngGetRand (0, math_ctx->ord); + } + p->deg = t; } -void BPU_gf2xPolyGenGoppa(BPU_T_GF2_16x_Poly *p, int t, const BPU_T_Math_Ctx *math_ctx) { +void BPU_gf2xPolyGenGoppa (BPU_T_GF2_16x_Poly * p, int t, + const BPU_T_Math_Ctx * math_ctx) { #if defined(DEBUG_L) || defined(WARNING_L) - int i = 1; + int i = 1; #endif - while(1) { - BPU_gf2xPolyGenRandom(p, t, math_ctx); - - if (BPU_gf2xPolyIrredTest(p, math_ctx) == 1) { - break; - } - BPU_printWarning("new irreducibility test #%d", i++); - } + while (1) { + BPU_gf2xPolyGenRandom (p, t, math_ctx); + + if (BPU_gf2xPolyIrredTest (p, math_ctx) == 1) { + break; + } + BPU_printWarning ("new irreducibility test #%d", i++); + } } diff --git a/lib/src/bitpunch/math/gf2x.h b/lib/src/bitpunch/math/gf2x.h index 11a23a6..3db105c 100644 --- a/lib/src/bitpunch/math/gf2x.h +++ b/lib/src/bitpunch/math/gf2x.h @@ -53,12 +53,12 @@ typedef uint32_t BPU_T_GF2_32x; * Representation of aritmetics data. */ typedef struct _BPU_T_Math_Ctx { - BPU_T_GF2_16x *exp_table; ///< there are all elements referenced by i, so at i-th index is g^i element, g - generator - BPU_T_GF2_16x *log_table; ///< there are all indexes referenced by element, so alpha elemnet (g^i) -> i - BPU_T_GF2_16x mod; ///< polynomial modulus - uint8_t mod_deg; ///< modulo degree, galois finite field GF(2^m) - int ord; ///< group ord, number of elements -}BPU_T_Math_Ctx; + BPU_T_GF2_16x *exp_table; ///< there are all elements referenced by i, so at i-th index is g^i element, g - generator + BPU_T_GF2_16x *log_table; ///< there are all indexes referenced by element, so alpha elemnet (g^i) -> i + BPU_T_GF2_16x mod; ///< polynomial modulus + uint8_t mod_deg; ///< modulo degree, galois finite field GF(2^m) + int ord; ///< group ord, number of elements +} BPU_T_Math_Ctx; /** * Precalculate logaritmic and exponencial tables and initialize structure Aritmetic_Data @@ -67,7 +67,8 @@ typedef struct _BPU_T_Math_Ctx { * @return number of elements or < 0 means error */ /// Precalculate logaritmic and exponencial tables and initialize structure Aritmetic_Data -int BPU_mathInitCtx(BPU_T_Math_Ctx **ctx, const BPU_T_GF2_16x g, const BPU_T_GF2_16x mod); +int BPU_mathInitCtx (BPU_T_Math_Ctx ** ctx, const BPU_T_GF2_16x g, + const BPU_T_GF2_16x mod); /** * Free dynamiccaly or statically allocated Aritmetic_Data structure. @@ -75,33 +76,34 @@ int BPU_mathInitCtx(BPU_T_Math_Ctx **ctx, const BPU_T_GF2_16x g, const BPU_T_GF2 * @param is_dyn 0 - staticaly allocated Aritmetic_Data object or 1 when dynamically */ /// Free dynamiccaly or statically allocated Aritmetic_Data structure. -void BPU_mathFreeCtx(BPU_T_Math_Ctx **ctx); +void BPU_mathFreeCtx (BPU_T_Math_Ctx ** ctx); /** * GF2_16x Vector representation */ typedef struct _BPU_T_GF2_16x_Vector { - BPU_T_GF2_16x *elements; - uint8_t len; ///< number of elements -}BPU_T_GF2_16x_Vector; + BPU_T_GF2_16x *elements; + uint8_t len; ///< number of elements +} BPU_T_GF2_16x_Vector; /** * Matrix representation over GF2_16x. */ typedef struct _BPU_T_GF2_16x_Matrix { - BPU_T_GF2_16x **elements; ///< all element of matrix - uint16_t k; ///< rows - uint16_t n; ///< cols -}BPU_T_GF2_16x_Matrix; + BPU_T_GF2_16x **elements; ///< all element of matrix + uint16_t k; ///< rows + uint16_t n; ///< cols +} BPU_T_GF2_16x_Matrix; /** * Representation of polynomial. */ -typedef struct _BPU_T_GF2_16x_Poly{ - BPU_T_GF2_16x *coef; ///< Polynomial over GF2m - int16_t deg; ///< degree - int16_t max_deg; ///< degree -}BPU_T_GF2_16x_Poly; +typedef struct _BPU_T_GF2_16x_Poly { + BPU_T_GF2_16x *coef; ///< Polynomial over GF2m + int16_t deg; ///< degree + int16_t max_deg; ///< degree +} BPU_T_GF2_16x_Poly; + #ifdef BPU_CONF_PRINT /* ==================================== Print functions ==================================== */ /** @@ -109,20 +111,22 @@ typedef struct _BPU_T_GF2_16x_Poly{ * It will be padded 64 bits. * @param in input of max 64 bits */ -void BPU_printGf2xMat(const BPU_T_GF2_16x_Matrix *in); +void BPU_printGf2xMat (const BPU_T_GF2_16x_Matrix * in); /** * @brief BPU_printGf2xPoly print GF2x polynomial. * @param p * @param math_ctx */ -void BPU_printGf2xPoly(const BPU_T_GF2_16x_Poly *p, const BPU_T_Math_Ctx *math_ctx); +void BPU_printGf2xPoly (const BPU_T_GF2_16x_Poly * p, + const BPU_T_Math_Ctx * math_ctx); /** * @brief BPU_printGf2xVec print GF2x vector. * @param v */ -void BPU_printGf2xVec(const BPU_T_GF2_16x_Vector *v); +void BPU_printGf2xVec (const BPU_T_GF2_16x_Vector * v); + /* ------------------------------------ Print functions ------------------------------------ */ #endif // BPU_CONF_PRINT @@ -134,7 +138,7 @@ void BPU_printGf2xVec(const BPU_T_GF2_16x_Vector *v); #define BPU_gf2xPolyNull(d_pointer) memset((void *) ((d_pointer)->coef), 0, sizeof(BPU_T_GF2_16x)*((d_pointer)->max_deg + 1));\ (d_pointer)->deg = -1 -void BPU_gf2xMatNull(BPU_T_GF2_16x_Matrix *mat); +void BPU_gf2xMatNull (BPU_T_GF2_16x_Matrix * mat); /** * Allocate memory for matrix. After work you have to free memory using call BPU_freeMat. @@ -144,7 +148,7 @@ void BPU_gf2xMatNull(BPU_T_GF2_16x_Matrix *mat); * @return on succes 0, else error */ /// Allocate memory for matrix. -int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols); +int BPU_gf2xMatMalloc (BPU_T_GF2_16x_Matrix ** m, int rows, int cols); /** * @brief BPU_gf2xVecMalloc Malloc vector structure. @@ -152,20 +156,20 @@ int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols); * @param size * @return */ -int BPU_gf2xVecMalloc(BPU_T_GF2_16x_Vector **vec, int size); +int BPU_gf2xVecMalloc (BPU_T_GF2_16x_Vector ** vec, int size); /** * @brief BPU_gf2xVecFree Free vector structure. * @param vec */ -void BPU_gf2xVecFree(BPU_T_GF2_16x_Vector **vec); +void BPU_gf2xVecFree (BPU_T_GF2_16x_Vector ** vec); /** * Free dynamically or statically allocated matrix. * @param[out] *m address of matrix object */ /// Free dynamically or statically allocated matrix -void BPU_gf2xMatFree(BPU_T_GF2_16x_Matrix **m); +void BPU_gf2xMatFree (BPU_T_GF2_16x_Matrix ** m); /** * Malloc memory for polynomial and zero-initialize @@ -173,7 +177,7 @@ void BPU_gf2xMatFree(BPU_T_GF2_16x_Matrix **m); * @param max_deg max degree of polynomial * @return */ -int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg); +int BPU_gf2xPolyMalloc (BPU_T_GF2_16x_Poly ** p, int16_t max_deg); /** * @brief BPU_gf2xPolyResize Resize polynomial, increase max deg. @@ -181,7 +185,7 @@ int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg); * @param max_deg * @return */ -int BPU_gf2xPolyResize(BPU_T_GF2_16x_Poly *p, int16_t max_deg); +int BPU_gf2xPolyResize (BPU_T_GF2_16x_Poly * p, int16_t max_deg); /** * @brief BPU_gf2xPolyMallocCoef Malloc internal coeficients for polynomial. @@ -189,13 +193,13 @@ int BPU_gf2xPolyResize(BPU_T_GF2_16x_Poly *p, int16_t max_deg); * @param max_deg * @return */ -int BPU_gf2xPolyMallocCoef(BPU_T_GF2_16x_Poly *p, int16_t max_deg); +int BPU_gf2xPolyMallocCoef (BPU_T_GF2_16x_Poly * p, int16_t max_deg); /** * dealloc memory * @param p */ -void BPU_gf2xPolyFree(BPU_T_GF2_16x_Poly **p); +void BPU_gf2xPolyFree (BPU_T_GF2_16x_Poly ** p); /** * Get inverse element of galois field. @@ -222,7 +226,8 @@ void BPU_gf2xPolyFree(BPU_T_GF2_16x_Poly **p); * @return a*b mod mod */ /// Multiplication over Galois field, modulus mod. -BPU_T_GF2_16x BPU_gf2xMulMod(BPU_T_GF2_16x a, BPU_T_GF2_16x b, BPU_T_GF2_16x mod); +BPU_T_GF2_16x BPU_gf2xMulMod (BPU_T_GF2_16x a, BPU_T_GF2_16x b, + BPU_T_GF2_16x mod); /** * Multiplication over Galois field, modulus mod. @@ -233,7 +238,8 @@ BPU_T_GF2_16x BPU_gf2xMulMod(BPU_T_GF2_16x a, BPU_T_GF2_16x b, BPU_T_GF2_16x mod * @return result */ /// Multiplication over Galois field, modulus mod. -BPU_T_GF2_16x BPU_gf2xMulModT(const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xMulModT (const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, + const BPU_T_Math_Ctx * math_ctx); //side channel //#define BPU_gf2xMulModT(a, b, math_ctx) (((a) == 0 || (b) == 0) ? 0 : (math_ctx)->exp_table[((math_ctx)->log_table[(a)] + (math_ctx)->log_table[(b)]) % (math_ctx)->ord]) @@ -246,7 +252,8 @@ BPU_T_GF2_16x BPU_gf2xMulModT(const BPU_T_GF2_16x a, const BPU_T_GF2_16x b, cons * @return result */ /// E-th power of a. It uses precomputed log and exp tables -BPU_T_GF2_16x BPU_gf2xPowerModT(BPU_T_GF2_16x a, int e, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xPowerModT (BPU_T_GF2_16x a, int e, + const BPU_T_Math_Ctx * math_ctx); /** * Matrix multiplication over finite field.. @@ -256,7 +263,9 @@ BPU_T_GF2_16x BPU_gf2xPowerModT(BPU_T_GF2_16x a, int e, const BPU_T_Math_Ctx *ma * @param[in] b matrix GF2^m * @return on succes 0, on size error -1, on allocation error -2 */ -int BPU_gf2xMatMul(BPU_T_GF2_16x_Matrix *x, const BPU_T_GF2_16x_Matrix *a, const BPU_T_GF2_16x_Matrix *b, const BPU_T_Math_Ctx *math_ctx); +int BPU_gf2xMatMul (BPU_T_GF2_16x_Matrix * x, const BPU_T_GF2_16x_Matrix * a, + const BPU_T_GF2_16x_Matrix * b, + const BPU_T_Math_Ctx * math_ctx); /** * out = x * M over finite field @@ -264,7 +273,10 @@ int BPU_gf2xMatMul(BPU_T_GF2_16x_Matrix *x, const BPU_T_GF2_16x_Matrix *a, const * @param mat [description] * @param out [description] */ -void BPU_gf2xVecMulMat(BPU_T_GF2_16x_Vector *out, const BPU_T_GF2_16x_Vector *x, const BPU_T_GF2_16x_Matrix *mat, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xVecMulMat (BPU_T_GF2_16x_Vector * out, + const BPU_T_GF2_16x_Vector * x, + const BPU_T_GF2_16x_Matrix * mat, + const BPU_T_Math_Ctx * math_ctx); /** * Plus operation on polynomials (out = a + b). All argumets must be allocated before using BPU_mallocPoly(). @@ -272,7 +284,8 @@ void BPU_gf2xVecMulMat(BPU_T_GF2_16x_Vector *out, const BPU_T_GF2_16x_Vector *x, * @param a [description] * @param b [description] */ -void BPU_gf2xPolyAdd(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b); +void BPU_gf2xPolyAdd (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b); /** * Divide two polynomials. All argumets must be allocated before using BPU_mallocPoly(). @@ -281,7 +294,10 @@ void BPU_gf2xPolyAdd(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const * @param a [in] input polynomial * @param b [in] input polynomial */ -void BPU_gf2xPolyDiv(BPU_T_GF2_16x_Poly *q, BPU_T_GF2_16x_Poly *r, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyDiv (BPU_T_GF2_16x_Poly * q, BPU_T_GF2_16x_Poly * r, + const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, + const BPU_T_Math_Ctx * math_ctx); /** * Multiplicate two polynomials. All argumets must be allocated before using BPU_mallocPoly(). @@ -290,7 +306,9 @@ void BPU_gf2xPolyDiv(BPU_T_GF2_16x_Poly *q, BPU_T_GF2_16x_Poly *r, const BPU_T_G * @param out [out] output polynomial * @param math_ctx log and exp table needed for multiplication of elements in GF2^m */ -void BPU_gf2xPolyMul(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyMul (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, + const BPU_T_Math_Ctx * math_ctx); /** * Shift polynomial right, it is like a div x^n. @@ -298,7 +316,7 @@ void BPU_gf2xPolyMul(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const * @param n[in] shift */ /// Shift polynomial right, it is like a div x^n. -void BPU_gf2xPolyShr(BPU_T_GF2_16x_Poly *a, int n); +void BPU_gf2xPolyShr (BPU_T_GF2_16x_Poly * a, int n); /** * Shift polynomial left, it is like a mul 1/x^n. @@ -306,7 +324,7 @@ void BPU_gf2xPolyShr(BPU_T_GF2_16x_Poly *a, int n); * @param n[in] shift */ /// Shift polynomial left, it is like a mul 1/x^n. -void BPU_gf2xPolyShl(BPU_T_GF2_16x_Poly *a, int n); +void BPU_gf2xPolyShl (BPU_T_GF2_16x_Poly * a, int n); /** * Calculate power of polynomial. @@ -315,7 +333,8 @@ void BPU_gf2xPolyShl(BPU_T_GF2_16x_Poly *a, int n); * @param math_ctx aitmetic data structure. */ /// Calculate power of polynomial. -void BPU_gf2xPolyPower(BPU_T_GF2_16x_Poly *a, int e, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyPower (BPU_T_GF2_16x_Poly * a, int e, + const BPU_T_Math_Ctx * math_ctx); /** * Multiplication polynomial over GF2_16x and element from GF2_16x. It is like multiplication of polynomials @@ -324,7 +343,8 @@ void BPU_gf2xPolyPower(BPU_T_GF2_16x_Poly *a, int e, const BPU_T_Math_Ctx *math_ * @param math_ctx arithmetic data structure */ /// Multiplication polynomial over GF2_16x and element from GF2_16x. -void BPU_gf2xPolyMulEl(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x el, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyMulEl (BPU_T_GF2_16x_Poly * a, BPU_T_GF2_16x el, + const BPU_T_Math_Ctx * math_ctx); /** * Calculate reminder of a. Example a mod b = reminder. ALl arguments must be allocated before use. @@ -334,7 +354,9 @@ void BPU_gf2xPolyMulEl(BPU_T_GF2_16x_Poly *a, BPU_T_GF2_16x el, const BPU_T_Math * @param math_ctx[in] aritmetics data structure */ /// Calculate reminder of a. Example a mod b = reminder. -void BPU_gf2xPolyMod(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyMod (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx); /** * Compute root of polynomial poly modulo polynomial mod and save result to polynomial out. @@ -344,7 +366,9 @@ void BPU_gf2xPolyMod(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const * @param out [description] * @param math_ctx [description] */ -void BPU_gf2xMatRoot(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xMatRoot (BPU_T_GF2_16x_Matrix * out, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx); /** * Function returns sqrt(element) @@ -352,7 +376,8 @@ void BPU_gf2xMatRoot(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Poly *mod, c * @param math_ctx [description] * @return [description] */ -BPU_T_GF2_16x BPU_gf2xRoot(BPU_T_GF2_16x element, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xRoot (BPU_T_GF2_16x element, + const BPU_T_Math_Ctx * math_ctx); /** * @brief BPU_gf2xPolyRoot calculate root of given polynomial over GF2x. @@ -361,7 +386,10 @@ BPU_T_GF2_16x BPU_gf2xRoot(BPU_T_GF2_16x element, const BPU_T_Math_Ctx *math_ctx * @param mod * @param math_ctx */ -void BPU_gf2xPolyRoot(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyRoot (BPU_T_GF2_16x_Poly * out, + const BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx); /** * Copy Polynomial. @@ -369,7 +397,8 @@ void BPU_gf2xPolyRoot(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *poly, c * @param src[in] pointer to GF2_16x polynomial */ /// Copy Polynomial. -void BPU_gf2xPolyCopy(BPU_T_GF2_16x_Poly *dest, const BPU_T_GF2_16x_Poly *src); +void BPU_gf2xPolyCopy (BPU_T_GF2_16x_Poly * dest, + const BPU_T_GF2_16x_Poly * src); /** * Get degree of polynomial over GF2. @@ -377,7 +406,7 @@ void BPU_gf2xPolyCopy(BPU_T_GF2_16x_Poly *dest, const BPU_T_GF2_16x_Poly *src); * @return degree of pol or -1 if it is pol = 0 */ /// Get degree of polynomial over GF2 -int BPU_gf2xGetDeg(BPU_T_GF2_32x pol); +int BPU_gf2xGetDeg (BPU_T_GF2_32x pol); /** * Get degree of polynomial over GF2x. @@ -385,7 +414,7 @@ int BPU_gf2xGetDeg(BPU_T_GF2_32x pol); * @return degree of pol or -1 if it is pol = 0 */ /// Get degree of polynomial over GF2x -int BPU_gf2xPolyGetDeg(BPU_T_GF2_16x_Poly *poly); +int BPU_gf2xPolyGetDeg (BPU_T_GF2_16x_Poly * poly); /** * Permute matrix GF2^m with permutation. @@ -393,7 +422,9 @@ int BPU_gf2xPolyGetDeg(BPU_T_GF2_16x_Poly *poly); * @param permutation permutation vector * @return on succes 0, on size error -1, on allocation error -2 */ -int BPU_gf2xMatPermute(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Matrix *m, const BPU_T_Perm_Vector *permutation); +int BPU_gf2xMatPermute (BPU_T_GF2_16x_Matrix * out, + const BPU_T_GF2_16x_Matrix * m, + const BPU_T_Perm_Vector * permutation); /** * Converts matrix GF2m to matrix GF2. @@ -402,7 +433,8 @@ int BPU_gf2xMatPermute(BPU_T_GF2_16x_Matrix *out, const BPU_T_GF2_16x_Matrix *m, * @param out output matrix GF2 * @return on succes 0, else error */ -int BPU_gf2xMatConvertToGf2Mat(BPU_T_GF2_Matrix *out, const BPU_T_GF2_16x_Matrix *m, int deg); +int BPU_gf2xMatConvertToGf2Mat (BPU_T_GF2_Matrix * out, + const BPU_T_GF2_16x_Matrix * m, int deg); /** * Evaluate polynomial over GF2^m with x. @@ -412,7 +444,9 @@ int BPU_gf2xMatConvertToGf2Mat(BPU_T_GF2_Matrix *out, const BPU_T_GF2_16x_Matrix * @return evaluated polynomial, GF2x element */ /// Evaluate polynomial over GF2^m with x. -BPU_T_GF2_16x BPU_gf2xPolyEval(const BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x x, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xPolyEval (const BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x x, + const BPU_T_Math_Ctx * math_ctx); /** * Extended euclidian to find greatest common divisor and Bézout coefficients s, t, where gcd(a, b) = d = a*s + b*t. @@ -427,7 +461,10 @@ BPU_T_GF2_16x BPU_gf2xPolyEval(const BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_1 * @return 0 */ /// Extended Euclidean to find greatest common divisor and Bézout coefficients s, t, where gcd(a, b) = d = a*s + b*t. -int BPU_gf2xPolyExtEuclid(BPU_T_GF2_16x_Poly *d, BPU_T_GF2_16x_Poly *s, BPU_T_GF2_16x_Poly *t, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *b, int end_deg, const BPU_T_Math_Ctx *math_ctx); +int BPU_gf2xPolyExtEuclid (BPU_T_GF2_16x_Poly * d, BPU_T_GF2_16x_Poly * s, + BPU_T_GF2_16x_Poly * t, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * b, int end_deg, + const BPU_T_Math_Ctx * math_ctx); /** * Function compares two polynomials @@ -435,7 +472,8 @@ int BPU_gf2xPolyExtEuclid(BPU_T_GF2_16x_Poly *d, BPU_T_GF2_16x_Poly *s, BPU_T_GF * @param p2 [description] * @return 0 if are same, -1 when deg differs, >0 index of first diff result is index + 1 */ -int BPU_gf2xPolyCmp(const BPU_T_GF2_16x_Poly *p1, const BPU_T_GF2_16x_Poly *p2); +int BPU_gf2xPolyCmp (const BPU_T_GF2_16x_Poly * p1, + const BPU_T_GF2_16x_Poly * p2); /** * Function tests if polynomial is irreducible or not. @@ -443,7 +481,8 @@ int BPU_gf2xPolyCmp(const BPU_T_GF2_16x_Poly *p1, const BPU_T_GF2_16x_Poly *p2); * @param math_ctx [description] * @return [description] */ -int BPU_gf2xPolyIrredTest(const BPU_T_GF2_16x_Poly *p, const BPU_T_Math_Ctx *math_ctx); +int BPU_gf2xPolyIrredTest (const BPU_T_GF2_16x_Poly * p, + const BPU_T_Math_Ctx * math_ctx); /** * Get inverse polynomial over GF2_16x. It is using Extended Euclidean alg. * @param a[in] input polynomial @@ -452,7 +491,9 @@ int BPU_gf2xPolyIrredTest(const BPU_T_GF2_16x_Poly *p, const BPU_T_Math_Ctx *mat * @param math_ctx[in] arithmetic data structure */ /// Get inverse polynomial over GF2_16x. -void BPU_gf2xPolyInv(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const BPU_T_GF2_16x_Poly *mod, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyInv (BPU_T_GF2_16x_Poly * out, const BPU_T_GF2_16x_Poly * a, + const BPU_T_GF2_16x_Poly * mod, + const BPU_T_Math_Ctx * math_ctx); /** * Make from polynomial monic polynomial. It means that it will be multiplicated with (leading coef)^-1. It will change source polynomial. @@ -461,7 +502,8 @@ void BPU_gf2xPolyInv(BPU_T_GF2_16x_Poly *out, const BPU_T_GF2_16x_Poly *a, const * @return inverse leading coefitient or 0 */ /// Make from polynomial monic polynomial. -BPU_T_GF2_16x BPU_gf2xPolyMakeMonic(BPU_T_GF2_16x_Poly *a, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xPolyMakeMonic (BPU_T_GF2_16x_Poly * a, + const BPU_T_Math_Ctx * math_ctx); /** * Insert coefitients from poly into matrix mat to i-th row @@ -469,28 +511,31 @@ BPU_T_GF2_16x BPU_gf2xPolyMakeMonic(BPU_T_GF2_16x_Poly *a, const BPU_T_Math_Ctx * @param mat [description] * @param i [description] */ -void BPU_gf2xMatInsertPoly(BPU_T_GF2_16x_Matrix *mat, const BPU_T_GF2_16x_Poly *poly, int i); +void BPU_gf2xMatInsertPoly (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_GF2_16x_Poly * poly, int i); /** * save poly coefitients to vector, it allocates vec inside function. After use use BPU_freeVectorGF2_16x * @param poly [description] * @param vec [description] */ -void BPU_gf2xPolyToVec(BPU_T_GF2_16x_Vector *vec, const BPU_T_GF2_16x_Poly *poly, int len); +void BPU_gf2xPolyToVec (BPU_T_GF2_16x_Vector * vec, + const BPU_T_GF2_16x_Poly * poly, int len); /** * GEM applied on matrix with coefitients from GF2^m * @param mat [description] * @param math_ctx [description] */ -void BPU_gf2xMatGEM(BPU_T_GF2_16x_Matrix *mat, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xMatGEM (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_Math_Ctx * math_ctx); /** * Swap * @param a [description] * @param b [description] */ -void BPU_gf2xSwap(BPU_T_GF2_16x *a, BPU_T_GF2_16x *b); +void BPU_gf2xSwap (BPU_T_GF2_16x * a, BPU_T_GF2_16x * b); /** * in matrix mat swap rows i and j @@ -498,7 +543,7 @@ void BPU_gf2xSwap(BPU_T_GF2_16x *a, BPU_T_GF2_16x *b); * @param i [description] * @param j [description] */ -void BPU_gf2xMatSwapRows(BPU_T_GF2_16x_Matrix *mat, int i, int j); +void BPU_gf2xMatSwapRows (BPU_T_GF2_16x_Matrix * mat, int i, int j); /** * Multiply whole row of matrix mat by element from finite field @@ -507,7 +552,9 @@ void BPU_gf2xMatSwapRows(BPU_T_GF2_16x_Matrix *mat, int i, int j); * @param i [description] * @param math_ctx [description] */ -void BPU_gf2xMatMulElRow(BPU_T_GF2_16x_Matrix *mat, const BPU_T_GF2_16x element, int i, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xMatMulElRow (BPU_T_GF2_16x_Matrix * mat, + const BPU_T_GF2_16x element, int i, + const BPU_T_Math_Ctx * math_ctx); /** * find row where pivot is at index index @@ -515,7 +562,7 @@ void BPU_gf2xMatMulElRow(BPU_T_GF2_16x_Matrix *mat, const BPU_T_GF2_16x element, * @param index [description] * @return [description] */ -int BPU_gf2xMatFindPivot(const BPU_T_GF2_16x_Matrix *mat, int index); +int BPU_gf2xMatFindPivot (const BPU_T_GF2_16x_Matrix * mat, int index); /** * Get GF2_16x element which is a * return = res. @@ -524,7 +571,9 @@ int BPU_gf2xMatFindPivot(const BPU_T_GF2_16x_Matrix *mat, int index); * @return element a * return = res */ /// Get GF2_16x element which is a * return = res. -BPU_T_GF2_16x BPU_gf2xGetPseudoInv(const BPU_T_GF2_16x a, const BPU_T_GF2_16x res, const BPU_T_Math_Ctx *math_ctx); +BPU_T_GF2_16x BPU_gf2xGetPseudoInv (const BPU_T_GF2_16x a, + const BPU_T_GF2_16x res, + const BPU_T_Math_Ctx * math_ctx); /** * If at mat->elements[j][index] is not zero, then @@ -533,7 +582,8 @@ BPU_T_GF2_16x BPU_gf2xGetPseudoInv(const BPU_T_GF2_16x a, const BPU_T_GF2_16x re * @param index [description] * @param j [description] */ -void BPU_gf2xMatXorRows(BPU_T_GF2_16x_Matrix *mat, int index, int j, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xMatXorRows (BPU_T_GF2_16x_Matrix * mat, int index, int j, + const BPU_T_Math_Ctx * math_ctx); /** * vec = element*vec @@ -541,11 +591,14 @@ void BPU_gf2xMatXorRows(BPU_T_GF2_16x_Matrix *mat, int index, int j, const BPU_T * @param element [description] * @param math_ctx [description] */ -void BPU_gf2xVecMulEl(BPU_T_GF2_16x_Vector *vec, BPU_T_GF2_16x element, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xVecMulEl (BPU_T_GF2_16x_Vector * vec, BPU_T_GF2_16x element, + const BPU_T_Math_Ctx * math_ctx); -void BPU_gf2xVecToPoly(BPU_T_GF2_16x_Poly *poly, const BPU_T_GF2_16x_Vector *vec); +void BPU_gf2xVecToPoly (BPU_T_GF2_16x_Poly * poly, + const BPU_T_GF2_16x_Vector * vec); -void BPU_gf2xMatClearCol(BPU_T_GF2_16x_Matrix *mat, int index, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xMatClearCol (BPU_T_GF2_16x_Matrix * mat, int index, + const BPU_T_Math_Ctx * math_ctx); /** * Generate monic polynomial of degree t and with non-zero coefficient a_0 @@ -553,7 +606,8 @@ void BPU_gf2xMatClearCol(BPU_T_GF2_16x_Matrix *mat, int index, const BPU_T_Math_ * @param t [description] * @param math_ctx [description] */ -void BPU_gf2xPolyGenRandom(BPU_T_GF2_16x_Poly *p, int t, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyGenRandom (BPU_T_GF2_16x_Poly * p, int t, + const BPU_T_Math_Ctx * math_ctx); /** * Generate monic irreducible polynopmial of degree t. @@ -561,6 +615,7 @@ void BPU_gf2xPolyGenRandom(BPU_T_GF2_16x_Poly *p, int t, const BPU_T_Math_Ctx *m * @param t [description] * @param math_ctx [description] */ -void BPU_gf2xPolyGenGoppa(BPU_T_GF2_16x_Poly *p, int t, const BPU_T_Math_Ctx *math_ctx); +void BPU_gf2xPolyGenGoppa (BPU_T_GF2_16x_Poly * p, int t, + const BPU_T_Math_Ctx * math_ctx); #endif // BPU_GF2X_H diff --git a/lib/src/bitpunch/math/int.c b/lib/src/bitpunch/math/int.c index 47a4c1e..e653441 100644 --- a/lib/src/bitpunch/math/int.c +++ b/lib/src/bitpunch/math/int.c @@ -22,42 +22,42 @@ along with this program. If not, see . #include #include "int.h" -int BPU_isPrime(int n) { - int i; - - if (n == 2) +int BPU_isPrime (int n) { + int i; + + if (n == 2) + return 1; + for (i = 2; i <= sqrt ((double) n); i++) { + if (n % i == 0) + return 0; + } return 1; - for (i = 2; i <= sqrt((double)n); i++) { - if (n % i == 0) - return 0; - } - return 1; } -int msb32(uint32_t x, int start, int len, int ele_size) { - // crop lenght to half - len /= 2; - - // last recursion - if (len == 1) { - // return position of highest bit - if ((x >> len) == 1ul) - return start+1; - else - return start; - } - - // right half of integer - if ((x >> len) != 0ul) { - start += len; - x >>= len; - } - // left half of integer - else { - x <<= ele_size - len; - x >>= ele_size - len; - } - - // recursion - return msb32(x, start, len, ele_size); +int msb32 (uint32_t x, int start, int len, int ele_size) { + // crop lenght to half + len /= 2; + + // last recursion + if (len == 1) { + // return position of highest bit + if ((x >> len) == 1ul) + return start + 1; + else + return start; + } + + // right half of integer + if ((x >> len) != 0ul) { + start += len; + x >>= len; + } + // left half of integer + else { + x <<= ele_size - len; + x >>= ele_size - len; + } + + // recursion + return msb32 (x, start, len, ele_size); } diff --git a/lib/src/bitpunch/math/int.h b/lib/src/bitpunch/math/int.h index 961e97e..3b82b8b 100644 --- a/lib/src/bitpunch/math/int.h +++ b/lib/src/bitpunch/math/int.h @@ -29,7 +29,7 @@ along with this program. If not, see . * @param n [description] * @return [description] */ -int BPU_isPrime(int n); +int BPU_isPrime (int n); /** * Get recursively index of highest set bit in unsigned integer. @@ -39,6 +39,6 @@ int BPU_isPrime(int n); * @param ele_size size of integer * @return index of highest set bit */ -int msb32(uint32_t x, int start, int len, int ele_size); +int msb32 (uint32_t x, int start, int len, int ele_size); #endif // BPU_INT_H diff --git a/lib/src/bitpunch/math/mathctx.c b/lib/src/bitpunch/math/mathctx.c index ebb12c0..d25bee5 100644 --- a/lib/src/bitpunch/math/mathctx.c +++ b/lib/src/bitpunch/math/mathctx.c @@ -20,59 +20,62 @@ along with this program. If not, see . #include #include -int BPU_mathInitCtx(BPU_T_Math_Ctx **ctx, const BPU_T_GF2_16x g, const BPU_T_GF2_16x mod) { - BPU_T_GF2_16x b = 1; - int i = 0; - BPU_T_Math_Ctx *math_ctx; +int BPU_mathInitCtx (BPU_T_Math_Ctx ** ctx, const BPU_T_GF2_16x g, + const BPU_T_GF2_16x mod) { + BPU_T_GF2_16x b = 1; + int i = 0; + BPU_T_Math_Ctx *math_ctx; - *ctx = (BPU_T_Math_Ctx *) calloc(1, sizeof(BPU_T_Math_Ctx)); - if (!*ctx) { - BPU_printError("Can not malloc BPU_T_Math_Ctx"); + *ctx = (BPU_T_Math_Ctx *) calloc (1, sizeof (BPU_T_Math_Ctx)); + if (!*ctx) { + BPU_printError ("Can not malloc BPU_T_Math_Ctx"); - return -1; - } - math_ctx = *ctx; - // get group ord, number of elements - BPU_T_GF2_16x ord = ((1 << BPU_gf2xGetDeg(mod)) - 1); + return -1; + } + math_ctx = *ctx; + // get group ord, number of elements + BPU_T_GF2_16x ord = ((1 << BPU_gf2xGetDeg (mod)) - 1); - // alocate memory for tables - math_ctx->mod = mod; - math_ctx->mod_deg = BPU_gf2xGetDeg(mod); - math_ctx->log_table = (BPU_T_GF2_16x*) malloc(sizeof(BPU_T_GF2_16x) * (ord + 1)); - math_ctx->exp_table = (BPU_T_GF2_16x*) malloc(sizeof(BPU_T_GF2_16x) * (ord + 1)); + // alocate memory for tables + math_ctx->mod = mod; + math_ctx->mod_deg = BPU_gf2xGetDeg (mod); + math_ctx->log_table = + (BPU_T_GF2_16x *) malloc (sizeof (BPU_T_GF2_16x) * (ord + 1)); + math_ctx->exp_table = + (BPU_T_GF2_16x *) malloc (sizeof (BPU_T_GF2_16x) * (ord + 1)); - // set ord - math_ctx->ord = ord; + // set ord + math_ctx->ord = ord; - do { - math_ctx->exp_table[i] = b; - math_ctx->log_table[b] = i; + do { + math_ctx->exp_table[i] = b; + math_ctx->log_table[b] = i; - b = BPU_gf2xMulMod(b, g, mod); - i++; - } while (b != 1); + b = BPU_gf2xMulMod (b, g, mod); + i++; + } while (b != 1); - math_ctx->exp_table[ord] = 0; - math_ctx->log_table[0] = ord; + math_ctx->exp_table[ord] = 0; + math_ctx->log_table[0] = ord; - if (i != ord) { - BPU_printError("element 0x%x is not generator", g); + if (i != ord) { + BPU_printError ("element 0x%x is not generator", g); - return 1; - } - return 0; + return 1; + } + return 0; } -void BPU_mathFreeCtx(BPU_T_Math_Ctx **ctx) { - if (!*ctx) { - return; - } - if ((*ctx)->exp_table) { - free((*ctx)->exp_table); - } - if ((*ctx)->log_table) { - free((*ctx)->log_table); - } - free(*ctx); - *ctx = NULL; +void BPU_mathFreeCtx (BPU_T_Math_Ctx ** ctx) { + if (!*ctx) { + return; + } + if ((*ctx)->exp_table) { + free ((*ctx)->exp_table); + } + if ((*ctx)->log_table) { + free ((*ctx)->log_table); + } + free (*ctx); + *ctx = NULL; } diff --git a/lib/src/bitpunch/math/perm.c b/lib/src/bitpunch/math/perm.c index 8e0811b..c8e4db1 100644 --- a/lib/src/bitpunch/math/perm.c +++ b/lib/src/bitpunch/math/perm.c @@ -29,40 +29,42 @@ along with this program. If not, see . #include #ifdef BPU_CONF_PRINT -void BPU_printPerm(const BPU_T_Perm_Vector *permutation) { - int i; - - fprintf(stderr, "Perm (%4d): ", permutation->size); - for (i = 0; i < permutation->size; i++) { - fprintf(stderr, "%i ",permutation->elements[i]); - } - fprintf(stderr, "\n"); +void BPU_printPerm (const BPU_T_Perm_Vector * permutation) { + int i; + + fprintf (stderr, "Perm (%4d): ", permutation->size); + for (i = 0; i < permutation->size; i++) { + fprintf (stderr, "%i ", permutation->elements[i]); + } + fprintf (stderr, "\n"); } #endif // BPU_CONF_PRINT -void BPU_permFree(BPU_T_Perm_Vector **p) { +void BPU_permFree (BPU_T_Perm_Vector ** p) { if (!*p) { return; } - free((*p)->elements); - free(*p); + free ((*p)->elements); + free (*p); } -int BPU_permMalloc(BPU_T_Perm_Vector **p, int size) { +int BPU_permMalloc (BPU_T_Perm_Vector ** p, int size) { // allocate memory int i; - *p = (BPU_T_Perm_Vector *) calloc(sizeof(BPU_T_Perm_Vector), 1); + + *p = (BPU_T_Perm_Vector *) calloc (sizeof (BPU_T_Perm_Vector), 1); if (!*p) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } (*p)->size = size; - (*p)->elements = (BPU_T_Perm_Element*) malloc(sizeof(BPU_T_Perm_Element) * size); + (*p)->elements = + (BPU_T_Perm_Element *) malloc (sizeof (BPU_T_Perm_Element) * size); if (!(*p)->elements) { - BPU_printError("BPU_mallocPerm: can not allocate permutation vector"); + BPU_printError ("BPU_mallocPerm: can not allocate permutation vector"); return -1; } @@ -72,80 +74,84 @@ int BPU_permMalloc(BPU_T_Perm_Vector **p, int size) { return 0; } -int BPU_permRandomize(BPU_T_Perm_Vector* permutation) { - int i; - uint32_t rand_value; - - for (i = 0; i < permutation->size; i++) { - permutation->elements[i] = i; - } - for (i = 0; i < permutation->size; i++) { - rand_value = BPU_prngGetRand(i, permutation->size); - BPU_permSwap(&permutation->elements[i], &permutation->elements[rand_value]); - } - return 0; +int BPU_permRandomize (BPU_T_Perm_Vector * permutation) { + int i; + uint32_t rand_value; + + for (i = 0; i < permutation->size; i++) { + permutation->elements[i] = i; + } + for (i = 0; i < permutation->size; i++) { + rand_value = BPU_prngGetRand (i, permutation->size); + BPU_permSwap (&permutation->elements[i], + &permutation->elements[rand_value]); + } + return 0; } -void BPU_permSwap(BPU_T_Perm_Element *a, BPU_T_Perm_Element *b) { +void BPU_permSwap (BPU_T_Perm_Element * a, BPU_T_Perm_Element * b) { BPU_T_Perm_Element tmp; + tmp = *a; *a = *b; *b = tmp; } -int BPU_permGetInv(BPU_T_Perm_Vector *out, const BPU_T_Perm_Vector *in) { - int i; +int BPU_permGetInv (BPU_T_Perm_Vector * out, const BPU_T_Perm_Vector * in) { + int i; - if (out->size != in->size) { - BPU_printError("permutation size error"); + if (out->size != in->size) { + BPU_printError ("permutation size error"); - return -1; - } - for (i = 0; i < in->size; i++) { - out->elements[in->elements[i]] = i; - } - return 0; + return -1; + } + for (i = 0; i < in->size; i++) { + out->elements[in->elements[i]] = i; + } + return 0; } -int BPU_permPermute(BPU_T_Perm_Vector *to_permute, const BPU_T_Perm_Vector *permutation) { - int i; +int BPU_permPermute (BPU_T_Perm_Vector * to_permute, + const BPU_T_Perm_Vector * permutation) { + int i; BPU_T_Perm_Vector *new_permutation; - // check if the size is correct - if (to_permute->size != permutation->size){ - return -1; - } - // allocate new permutation - if (BPU_permMalloc(&new_permutation, to_permute->size) != 0){ - return -2; - } - // copy the permutation - for (i = 0; i < to_permute->size; i++) { + // check if the size is correct + if (to_permute->size != permutation->size) { + return -1; + } + // allocate new permutation + if (BPU_permMalloc (&new_permutation, to_permute->size) != 0) { + return -2; + } + // copy the permutation + for (i = 0; i < to_permute->size; i++) { new_permutation->elements[i] = to_permute->elements[i]; - } - // permute - for (i = 0; i < permutation->size; i++) { // row loop - to_permute->elements[i] = new_permutation->elements[permutation->elements[i]]; - } - BPU_permFree(&new_permutation); - - return 0; + } + // permute + for (i = 0; i < permutation->size; i++) { // row loop + to_permute->elements[i] = + new_permutation->elements[permutation->elements[i]]; + } + BPU_permFree (&new_permutation); + + return 0; } -int BPU_permIsValid(const BPU_T_Perm_Vector *p) { - int i, j; +int BPU_permIsValid (const BPU_T_Perm_Vector * p) { + int i, j; - for (i = 0; i < p->size; i++) { - for (j = 0; j < p->size; j++) { - if (i != j && p->elements[i] == p->elements[j]) { - BPU_printError("permutation is not valid"); + for (i = 0; i < p->size; i++) { + for (j = 0; j < p->size; j++) { + if (i != j && p->elements[i] == p->elements[j]) { + BPU_printError ("permutation is not valid"); #ifdef BPU_CONF_PRINT - BPU_printPerm(p); + BPU_printPerm (p); #endif - return 1; - } - } - } - return 0; + return 1; + } + } + } + return 0; } diff --git a/lib/src/bitpunch/math/perm.h b/lib/src/bitpunch/math/perm.h index b6c3f66..725809d 100644 --- a/lib/src/bitpunch/math/perm.h +++ b/lib/src/bitpunch/math/perm.h @@ -35,16 +35,16 @@ typedef uint16_t BPU_T_Perm_Element; * Size corresponds to count of columns in matrix. */ typedef struct _BPU_T_Perm_Vector { - BPU_T_Perm_Element *elements; ///< permutation vector - BPU_T_Perm_Element size; ///< permutation size -}BPU_T_Perm_Vector; + BPU_T_Perm_Element *elements; ///< permutation vector + BPU_T_Perm_Element size; ///< permutation size +} BPU_T_Perm_Vector; #ifdef BPU_CONF_PRINT /** * Print permutation. * @param permutation permutation */ -void BPU_printPerm(const BPU_T_Perm_Vector *permutation); +void BPU_printPerm (const BPU_T_Perm_Vector * permutation); #endif // BPU_CONF_PRINT /** @@ -53,7 +53,7 @@ void BPU_printPerm(const BPU_T_Perm_Vector *permutation); * @param is_dyn 0 - staticaly allocated BPU_T_Perm_Vector object or 1 when dynamically */ /// Free dynamically or statically alocated permutation vector. -void BPU_permFree(BPU_T_Perm_Vector **p); +void BPU_permFree (BPU_T_Perm_Vector ** p); /** * Allocate permutation vector elements of size size using malloc. After work you have to free memory using call BPU_freePerm(p) @@ -62,7 +62,7 @@ void BPU_permFree(BPU_T_Perm_Vector **p); * @return on succes 0 else error */ /// Allocate permutation vector elements of size size. -int BPU_permMalloc(BPU_T_Perm_Vector **p, int size); +int BPU_permMalloc (BPU_T_Perm_Vector ** p, int size); /** * Generate random permutation. @@ -70,14 +70,14 @@ int BPU_permMalloc(BPU_T_Perm_Vector **p, int size); * @param size size of permutation vector * @return on succes 0, else error */ -int BPU_permRandomize(BPU_T_Perm_Vector* permutation); +int BPU_permRandomize (BPU_T_Perm_Vector * permutation); /** * @brief BPU_swap * @param a * @param b */ -void BPU_permSwap(BPU_T_Perm_Element *a, BPU_T_Perm_Element *b); +void BPU_permSwap (BPU_T_Perm_Element * a, BPU_T_Perm_Element * b); /** * Get inverse permutation to permutation. @@ -85,7 +85,7 @@ void BPU_permSwap(BPU_T_Perm_Element *a, BPU_T_Perm_Element *b); * @param in permutation * @return on succes 0, else error */ -int BPU_permGetInv(BPU_T_Perm_Vector *out, const BPU_T_Perm_Vector *in); +int BPU_permGetInv (BPU_T_Perm_Vector * out, const BPU_T_Perm_Vector * in); /** * Compose permutations. @@ -93,13 +93,14 @@ int BPU_permGetInv(BPU_T_Perm_Vector *out, const BPU_T_Perm_Vector *in); * @param permutation * @return 0 success, -1 when size of vectors is not the same, -2 on memory allocation error */ -int BPU_permPermute(BPU_T_Perm_Vector *to_permute, const BPU_T_Perm_Vector *permutation); +int BPU_permPermute (BPU_T_Perm_Vector * to_permute, + const BPU_T_Perm_Vector * permutation); /** * @brief BPU_permIsValid Check wheter is permutaion valid. * @param p * @return on succes 0, else error */ -int BPU_permIsValid(const BPU_T_Perm_Vector *p); +int BPU_permIsValid (const BPU_T_Perm_Vector * p); #endif // BPU_PERM_H diff --git a/lib/src/bitpunch/math/uni.c b/lib/src/bitpunch/math/uni.c index 8d9c5dd..c8bbc49 100644 --- a/lib/src/bitpunch/math/uni.c +++ b/lib/src/bitpunch/math/uni.c @@ -22,54 +22,54 @@ along with this program. If not, see . #ifdef BPU_CONF_PRINT /* ==================================== Print functions ==================================== */ -void BPU_printBinaryMsb(uint32_t in, int len) { +void BPU_printBinaryMsb (uint32_t in, int len) { if (len > 0) { - BPU_printBinaryMsb(in >> 1, len - 1); + BPU_printBinaryMsb (in >> 1, len - 1); - fprintf(stderr, "%d", (int) (in & (0x1u))); + fprintf (stderr, "%d", (int) (in & (0x1u))); } } -void BPU_printBinaryMsbLn(uint32_t in, int len) { - BPU_printBinaryMsb(in, len); - fprintf(stderr, "\n"); +void BPU_printBinaryMsbLn (uint32_t in, int len) { + BPU_printBinaryMsb (in, len); + fprintf (stderr, "\n"); } -void BPU_printBinaryMsb32(uint32_t in) { - BPU_printBinaryMsb(in, 32); +void BPU_printBinaryMsb32 (uint32_t in) { + BPU_printBinaryMsb (in, 32); } -void BPU_printBinaryMsb32Ln(uint32_t in) { - BPU_printBinaryMsbLn(in, 32); +void BPU_printBinaryMsb32Ln (uint32_t in) { + BPU_printBinaryMsbLn (in, 32); } -void BPU_printBinaryLsb(uint32_t in, int len) { +void BPU_printBinaryLsb (uint32_t in, int len) { if (len > 0) { - fprintf(stderr, "%d", (int) (in & (0x1u))); + fprintf (stderr, "%d", (int) (in & (0x1u))); - BPU_printBinaryLsb(in >> 1, len - 1); + BPU_printBinaryLsb (in >> 1, len - 1); } } -void BPU_printBinaryLsbLn(uint32_t in, int len) { - BPU_printBinaryLsb(in, len); - fprintf(stderr, "\n"); +void BPU_printBinaryLsbLn (uint32_t in, int len) { + BPU_printBinaryLsb (in, len); + fprintf (stderr, "\n"); } -void BPU_printBinaryLsb32(uint32_t in) { - BPU_printBinaryLsb(in, 32); +void BPU_printBinaryLsb32 (uint32_t in) { + BPU_printBinaryLsb (in, 32); } -void BPU_printBinary32LsbLn(uint32_t in) { - BPU_printBinaryLsbLn(in, 32); +void BPU_printBinary32LsbLn (uint32_t in) { + BPU_printBinaryLsbLn (in, 32); } -void BPU_printElementArray(const BPU_T_Element_Array* a) { +void BPU_printElementArray (const BPU_T_Element_Array * a) { int j, bits_to_print; - fprintf(stderr, "Vec (%4d): ", a->len); + fprintf (stderr, "Vec (%4d): ", a->len); for (j = 0; j <= a->array_length - 1; j++) { - if (j == a->array_length-1) { + if (j == a->array_length - 1) { if (a->len % (a->element_bit_size) != 0) { bits_to_print = a->len % a->element_bit_size; } @@ -80,18 +80,18 @@ void BPU_printElementArray(const BPU_T_Element_Array* a) { else { bits_to_print = a->element_bit_size; } - BPU_printBinaryLsb(a->elements[j], bits_to_print); - fprintf(stderr, " "); + BPU_printBinaryLsb (a->elements[j], bits_to_print); + fprintf (stderr, " "); } - fprintf(stderr, "\n"); + fprintf (stderr, "\n"); } -void BPU_printElementArrayMsb(const BPU_T_Element_Array* a) { +void BPU_printElementArrayMsb (const BPU_T_Element_Array * a) { int j, bits_to_print; - fprintf(stderr, "Vec (%4d): ", a->len); + fprintf (stderr, "Vec (%4d): ", a->len); for (j = 0; j <= a->array_length - 1; j++) { - if (j == a->array_length-1) { + if (j == a->array_length - 1) { if (a->len % (a->element_bit_size) != 0) { bits_to_print = a->len % a->element_bit_size; } @@ -102,54 +102,55 @@ void BPU_printElementArrayMsb(const BPU_T_Element_Array* a) { else { bits_to_print = a->element_bit_size; } - BPU_printBinaryMsbLn(a->elements[j], bits_to_print); - fprintf(stderr, " "); + BPU_printBinaryMsbLn (a->elements[j], bits_to_print); + fprintf (stderr, " "); } - fprintf(stderr, "\n"); + fprintf (stderr, "\n"); } -void BPU_printElementArrayOnes(const BPU_T_Element_Array *a) { +void BPU_printElementArrayOnes (const BPU_T_Element_Array * a) { int i; - for (i = 0; i < a->len; ++i) - { - if (BPU_elementArrayGetBit(a, i)) { - fprintf(stderr, "%d ", i); + + for (i = 0; i < a->len; ++i) { + if (BPU_elementArrayGetBit (a, i)) { + fprintf (stderr, "%d ", i); } } - fprintf(stderr, "\n"); + fprintf (stderr, "\n"); } + /* ------------------------------------ Print functions ------------------------------------ */ #endif // BPU_CONF_PRINT -void BPU_elementArrayFree(BPU_T_Element_Array **a) { +void BPU_elementArrayFree (BPU_T_Element_Array ** a) { if (!*a) { return; } - free((*a)->elements); - free(*a); + free ((*a)->elements); + free (*a); *a = NULL; } -int BPU_elementArrayMalloc(BPU_T_Element_Array **a, int len) { - *a = (BPU_T_Element_Array *) calloc(sizeof(BPU_T_Element_Array), 1); +int BPU_elementArrayMalloc (BPU_T_Element_Array ** a, int len) { + *a = (BPU_T_Element_Array *) calloc (sizeof (BPU_T_Element_Array), 1); if (!*a) { - BPU_printError("allocation error"); + BPU_printError ("allocation error"); return -1; } - return BPU_elementArrayMallocElements(*a, len); + return BPU_elementArrayMallocElements (*a, len); } -int BPU_elementArrayResize(BPU_T_Element_Array *a, int len) { +int BPU_elementArrayResize (BPU_T_Element_Array * a, int len) { if (a->elements) { - free(a->elements); + free (a->elements); } - return BPU_elementArrayMallocElements(a, len); + return BPU_elementArrayMallocElements (a, len); } -int BPU_elementArrayMallocElements(BPU_T_Element_Array *a, int len) { +int BPU_elementArrayMallocElements (BPU_T_Element_Array * a, int len) { // element size in bits - a->element_bit_size = sizeof(BPU_T_Element) * 8; + a->element_bit_size = sizeof (BPU_T_Element) * 8; // len a->len = len; @@ -163,10 +164,11 @@ int BPU_elementArrayMallocElements(BPU_T_Element_Array *a, int len) { a->array_length = len / a->element_bit_size + modul; // allocate elemtens - a->elements = (BPU_T_Element*) calloc(1, sizeof(BPU_T_Element) * a->array_length); + a->elements = + (BPU_T_Element *) calloc (1, sizeof (BPU_T_Element) * a->array_length); if (!a->elements) { - BPU_printError("can not allocate memory for vector of len %d", len); + BPU_printError ("can not allocate memory for vector of len %d", len); return -1; } diff --git a/lib/src/bitpunch/math/uni.h b/lib/src/bitpunch/math/uni.h index 9719798..e4e8eec 100644 --- a/lib/src/bitpunch/math/uni.h +++ b/lib/src/bitpunch/math/uni.h @@ -30,11 +30,11 @@ typedef uint16_t BPU_T_Element; * Represents elements bigger than standard C types. */ typedef struct _BPU_T_Element_Array { - BPU_T_Element *elements; ///< element stored in the array - uint8_t element_bit_size; ///< element size e.g., 16 bits - uint16_t array_length; ///< number of elements in the array - uint32_t len; ///< bit length of the element -}BPU_T_Element_Array; + BPU_T_Element *elements; ///< element stored in the array + uint8_t element_bit_size; ///< element size e.g., 16 bits + uint16_t array_length; ///< number of elements in the array + uint32_t len; ///< bit length of the element +} BPU_T_Element_Array; #ifdef BPU_CONF_PRINT /* ==================================== Print functions ==================================== */ @@ -46,7 +46,7 @@ typedef struct _BPU_T_Element_Array { * @param len print len */ /// Print number as binary string in big endian so msb is first printed. -void BPU_printBinaryMsb(uint32_t in, int len); +void BPU_printBinaryMsb (uint32_t in, int len); /** * Print number as binary string in big endian so msb is first printed. Print also new line at the end. @@ -55,7 +55,7 @@ void BPU_printBinaryMsb(uint32_t in, int len); * @param len print len */ /// Print number as binary string in big endian so msb is first printed. -void BPU_printBinaryMsbLn(uint32_t in, int len); +void BPU_printBinaryMsbLn (uint32_t in, int len); /** * Print number as binary string in big endian so msb is first printed. Do not print new line at the end. @@ -64,7 +64,7 @@ void BPU_printBinaryMsbLn(uint32_t in, int len); * @param in input of max 64 bits */ /// Print number as binary string in big endian so msb is first printed. -void BPU_printBinaryMsb32(uint32_t in); +void BPU_printBinaryMsb32 (uint32_t in); /** * Print number as binary string in big endian so msb is first printed. Print also new line at the end. @@ -73,7 +73,7 @@ void BPU_printBinaryMsb32(uint32_t in); * @param in input of max 64 bits */ /// Print number as binary string in big endian so msb is first printed. -void BPU_printBinaryMsb32Ln(uint32_t in); +void BPU_printBinaryMsb32Ln (uint32_t in); /** * Print number as binary string in little endian so lsb is first printed. Do not print new line at the end. @@ -82,7 +82,7 @@ void BPU_printBinaryMsb32Ln(uint32_t in); * @param len print len */ /// Print number as binary string in little endian so lsb is first printed. -void BPU_printBinaryLsb(uint32_t in, int len); +void BPU_printBinaryLsb (uint32_t in, int len); /** * Print number as binary string in little endian so lsb is first printed. Print also new line at the end. @@ -91,7 +91,7 @@ void BPU_printBinaryLsb(uint32_t in, int len); * @param len print len */ /// Print number as binary string in little endian so lsb is first printed. -void BPU_printBinaryLsbLn(uint32_t in, int len); +void BPU_printBinaryLsbLn (uint32_t in, int len); /** * Print number as binary string in little endian so lsb is first printed. Do not print new line at the end. @@ -100,7 +100,7 @@ void BPU_printBinaryLsbLn(uint32_t in, int len); * @param in input of max 64 bits */ /// Print number as binary string in little endian so lsb is first printed. -void BPU_printBinaryLsb32(uint32_t in); +void BPU_printBinaryLsb32 (uint32_t in); /** * Print number as binary string in little endian so lsb is first printed. Print also new line at the end. @@ -109,25 +109,25 @@ void BPU_printBinaryLsb32(uint32_t in); * @param in input of max 64 bits */ /// Print number as binary string in little endian so lsb is first printed. -void BPU_printBinary32LsbLn(uint32_t in); +void BPU_printBinary32LsbLn (uint32_t in); /** * Print vector GF2 with new line. * @param v vector */ -void BPU_printElementArray(const BPU_T_Element_Array *a); +void BPU_printElementArray (const BPU_T_Element_Array * a); /** * @brief BPU_printElementArrayMsb Most significant bit is printed first. * @param v */ -void BPU_printElementArrayMsb(const BPU_T_Element_Array* a); +void BPU_printElementArrayMsb (const BPU_T_Element_Array * a); /** * @brief BPU_printElementArrayOnes Print only ones. * @param vec */ -void BPU_printElementArrayOnes(const BPU_T_Element_Array *a); +void BPU_printElementArrayOnes (const BPU_T_Element_Array * a); /* ------------------------------------ Print functions ------------------------------------ */ #endif // BPU_CONF_PRINT @@ -176,14 +176,14 @@ void BPU_printElementArrayOnes(const BPU_T_Element_Array *a); * @param[out] *m address of vector object */ /// Free dynamically or statically allocated element array -void BPU_elementArrayFree(BPU_T_Element_Array **a); +void BPU_elementArrayFree (BPU_T_Element_Array ** a); /** * Allocate memory for element array. It also nulls array. * @param len len of vector * @return on succes 0, else error */ -int BPU_elementArrayMalloc(BPU_T_Element_Array **a, int len); +int BPU_elementArrayMalloc (BPU_T_Element_Array ** a, int len); /** * @brief Resize element array. @@ -191,7 +191,7 @@ int BPU_elementArrayMalloc(BPU_T_Element_Array **a, int len); * @param len * @return */ -int BPU_elementArrayResize(BPU_T_Element_Array *a, int len); +int BPU_elementArrayResize (BPU_T_Element_Array * a, int len); /** * @brief Malloc array elements and set to zero. @@ -199,6 +199,6 @@ int BPU_elementArrayResize(BPU_T_Element_Array *a, int len); * @param len * @return */ -int BPU_elementArrayMallocElements(BPU_T_Element_Array *a, int len); +int BPU_elementArrayMallocElements (BPU_T_Element_Array * a, int len); #endif // BPU_UNI_H diff --git a/lib/src/bitpunch/prng/prng.c b/lib/src/bitpunch/prng/prng.c index f5073e8..c8c8b24 100644 --- a/lib/src/bitpunch/prng/prng.c +++ b/lib/src/bitpunch/prng/prng.c @@ -24,27 +24,27 @@ along with this program. If not, see . #include #include #include - -uint32_t BPU_prngGetRand(int from, int to) { -// uint32_t tmp; -// FILE *fp; -// if (from <= to){ -// fp = fopen("/dev/urandom", "rb"); +uint32_t BPU_prngGetRand (int from, int to) { +// uint32_t tmp; +// FILE *fp; -// if (!fp) { -// fprintf(stderr, "dsadsa\n"); -// tmp = rand(); -// } -// else { -// fread(&tmp, sizeof(uint32_t), 1, fp); -// fclose(fp); -// } -// return (uint32_t) (tmp % (to - from) + from); -// } -// return 0; - if (from <= to){ - return rand() % (to - from) + from; - } +// if (from <= to){ +// fp = fopen("/dev/urandom", "rb"); + +// if (!fp) { +// fprintf(stderr, "dsadsa\n"); +// tmp = rand(); +// } +// else { +// fread(&tmp, sizeof(uint32_t), 1, fp); +// fclose(fp); +// } +// return (uint32_t) (tmp % (to - from) + from); +// } +// return 0; + if (from <= to) { + return rand () % (to - from) + from; + } return 0; } diff --git a/lib/src/bitpunch/prng/prng.h b/lib/src/bitpunch/prng/prng.h index ddd650b..8161f5b 100644 --- a/lib/src/bitpunch/prng/prng.h +++ b/lib/src/bitpunch/prng/prng.h @@ -31,6 +31,6 @@ along with this program. If not, see . * @return random value */ /// Get random unsigned int 32 value from given range (from <= return <= to) -uint32_t BPU_prngGetRand(int from, int to); +uint32_t BPU_prngGetRand (int from, int to); #endif // BPU_PRNG_H diff --git a/lib/src/main.c b/lib/src/main.c index 023dc06..e441d3a 100644 --- a/lib/src/main.c +++ b/lib/src/main.c @@ -27,81 +27,95 @@ #include #include -int testCmpMecsCtx(const BPU_T_Mecs_Ctx *ctx1, const BPU_T_Mecs_Ctx *ctx2) { +int testCmpMecsCtx (const BPU_T_Mecs_Ctx * ctx1, const BPU_T_Mecs_Ctx * ctx2) { int i, j, rc = 0; + if (ctx1->type != ctx2->type) { - BPU_printError("type"); + BPU_printError ("type"); } if (ctx1->ct_len != ctx2->ct_len) { - BPU_printError("ct_len"); + BPU_printError ("ct_len"); } if (ctx1->pt_len != ctx2->pt_len) { - BPU_printError("pt_len"); + BPU_printError ("pt_len"); } if (ctx1->_decrypt != ctx2->_decrypt) { - BPU_printError("_decrypt"); + BPU_printError ("_decrypt"); } if (ctx1->_encrypt != ctx2->_encrypt) { - BPU_printError("_encrypt"); + BPU_printError ("_encrypt"); } if (ctx1->code_ctx->code_len != ctx2->code_ctx->code_len) { - BPU_printError("code_len"); + BPU_printError ("code_len"); } if (ctx1->code_ctx->msg_len != ctx2->code_ctx->msg_len) { - BPU_printError("msg_len"); + BPU_printError ("msg_len"); } if (ctx1->code_ctx->t != ctx2->code_ctx->t) { - BPU_printError("t"); + BPU_printError ("t"); } if (ctx1->code_ctx->type != ctx2->code_ctx->type) { - BPU_printError("code type"); + BPU_printError ("code type"); } if (ctx1->code_ctx->_decode != ctx2->code_ctx->_decode) { - BPU_printError("_decode"); + BPU_printError ("_decode"); } if (ctx1->code_ctx->_encode != ctx2->code_ctx->_encode) { - BPU_printError("_encode"); + BPU_printError ("_encode"); } if (ctx1->code_ctx->e->len != ctx2->code_ctx->e->len) { - BPU_printError("e.len"); + BPU_printError ("e.len"); } - if (BPU_gf2xPolyCmp(ctx1->code_ctx->code_spec->goppa->g, ctx2->code_ctx->code_spec->goppa->g)) { - BPU_printError("g poly"); + if (BPU_gf2xPolyCmp + (ctx1->code_ctx->code_spec->goppa->g, + ctx2->code_ctx->code_spec->goppa->g)) { + BPU_printError ("g poly"); } - if (ctx1->code_ctx->code_spec->goppa->support_len != ctx2->code_ctx->code_spec->goppa->support_len) { - BPU_printError("support len"); + if (ctx1->code_ctx->code_spec->goppa->support_len != + ctx2->code_ctx->code_spec->goppa->support_len) { + BPU_printError ("support len"); } - if (ctx1->code_ctx->code_spec->goppa->permutation->size != ctx2->code_ctx->code_spec->goppa->permutation->size) { - BPU_printError("perm size"); + if (ctx1->code_ctx->code_spec->goppa->permutation->size != + ctx2->code_ctx->code_spec->goppa->permutation->size) { + BPU_printError ("perm size"); } - if (ctx1->code_ctx->code_spec->goppa->g_mat->elements_in_row != ctx2->code_ctx->code_spec->goppa->g_mat->elements_in_row) { - BPU_printError("g_mat elements_in_row"); + if (ctx1->code_ctx->code_spec->goppa->g_mat->elements_in_row != + ctx2->code_ctx->code_spec->goppa->g_mat->elements_in_row) { + BPU_printError ("g_mat elements_in_row"); } - if (ctx1->code_ctx->code_spec->goppa->g_mat->element_bit_size != ctx2->code_ctx->code_spec->goppa->g_mat->element_bit_size) { - BPU_printError("g_mat element_bit_size"); + if (ctx1->code_ctx->code_spec->goppa->g_mat->element_bit_size != + ctx2->code_ctx->code_spec->goppa->g_mat->element_bit_size) { + BPU_printError ("g_mat element_bit_size"); } - if (ctx1->code_ctx->code_spec->goppa->g_mat->k != ctx2->code_ctx->code_spec->goppa->g_mat->k) { - BPU_printError("g_mat k"); + if (ctx1->code_ctx->code_spec->goppa->g_mat->k != + ctx2->code_ctx->code_spec->goppa->g_mat->k) { + BPU_printError ("g_mat k"); } - if (ctx1->code_ctx->code_spec->goppa->g_mat->n != ctx2->code_ctx->code_spec->goppa->g_mat->n) { - BPU_printError("g_mat n"); + if (ctx1->code_ctx->code_spec->goppa->g_mat->n != + ctx2->code_ctx->code_spec->goppa->g_mat->n) { + BPU_printError ("g_mat n"); } for (i = 0; i < ctx1->code_ctx->code_spec->goppa->permutation->size; i++) { - if (ctx1->code_ctx->code_spec->goppa->permutation->elements[i] != ctx2->code_ctx->code_spec->goppa->permutation->elements[i]) { - BPU_printError("perm diff"); + if (ctx1->code_ctx->code_spec->goppa->permutation->elements[i] != + ctx2->code_ctx->code_spec->goppa->permutation->elements[i]) { + BPU_printError ("perm diff"); break; } } - if (ctx1->code_ctx->code_spec->goppa->h_mat->k != ctx2->code_ctx->code_spec->goppa->h_mat->k) { - BPU_printError("h_mat k"); + if (ctx1->code_ctx->code_spec->goppa->h_mat->k != + ctx2->code_ctx->code_spec->goppa->h_mat->k) { + BPU_printError ("h_mat k"); } - if (ctx1->code_ctx->code_spec->goppa->h_mat->n != ctx2->code_ctx->code_spec->goppa->h_mat->n) { - BPU_printError("h_mat n"); + if (ctx1->code_ctx->code_spec->goppa->h_mat->n != + ctx2->code_ctx->code_spec->goppa->h_mat->n) { + BPU_printError ("h_mat n"); } - for (i = 0; i < ctx1->code_ctx->code_spec->goppa->g_mat->elements_in_row; i++) { + for (i = 0; i < ctx1->code_ctx->code_spec->goppa->g_mat->elements_in_row; + i++) { for (j = 0; j < ctx1->code_ctx->code_spec->goppa->g_mat->k; j++) { - if (ctx1->code_ctx->code_spec->goppa->g_mat->elements[j][i] != ctx2->code_ctx->code_spec->goppa->g_mat->elements[j][i]) { - BPU_printError("g_mat diff"); + if (ctx1->code_ctx->code_spec->goppa->g_mat->elements[j][i] != + ctx2->code_ctx->code_spec->goppa->g_mat->elements[j][i]) { + BPU_printError ("g_mat diff"); j = -1; break; } @@ -112,8 +126,9 @@ int testCmpMecsCtx(const BPU_T_Mecs_Ctx *ctx1, const BPU_T_Mecs_Ctx *ctx2) { } for (i = 0; i < ctx1->code_ctx->code_spec->goppa->h_mat->n; i++) { for (j = 0; j < ctx1->code_ctx->code_spec->goppa->h_mat->k; j++) { - if (ctx1->code_ctx->code_spec->goppa->h_mat->elements[j][i] != ctx2->code_ctx->code_spec->goppa->h_mat->elements[j][i]) { - BPU_printError("h_mat diff"); + if (ctx1->code_ctx->code_spec->goppa->h_mat->elements[j][i] != + ctx2->code_ctx->code_spec->goppa->h_mat->elements[j][i]) { + BPU_printError ("h_mat diff"); j = -1; break; } @@ -125,212 +140,216 @@ int testCmpMecsCtx(const BPU_T_Mecs_Ctx *ctx1, const BPU_T_Mecs_Ctx *ctx2) { return rc; } -int testKeyGenEncDec(BPU_T_Mecs_Ctx *ctx) { +int testKeyGenEncDec (BPU_T_Mecs_Ctx * ctx) { // BPU_T_Mecs_Ctx *ctx = NULL; BPU_T_GF2_Vector *ct, *pt_in, *pt_out; - int rc = 0; + int rc = 0; - /***************************************/ - fprintf(stderr, "Key generation...\n"); - // key pair generation - if (BPU_mecsGenKeyPair(ctx)) { + /***************************************/ + fprintf (stderr, "Key generation...\n"); + // key pair generation + if (BPU_mecsGenKeyPair (ctx)) { // if (BPU_asn1LoadKeyPair(&ctx, "prikey.der", "pubkey.der")) { - BPU_printError("Key generation error"); - - return 1; - } - /***************************************/ - // prepare plain text, allocate memory and init random plaintext - if (BPU_gf2VecMalloc(&pt_in, ctx->pt_len)) { - BPU_printError("PT initialisation error"); - - return 1; - } - BPU_gf2VecRand(pt_in, 0); - - // alocate cipher text vector - if (BPU_gf2VecMalloc(&ct, ctx->ct_len)) { - BPU_printError("CT vector allocation error"); - - BPU_gf2VecFree(&pt_in); - return 1; - } - // prepare plain text, allocate memory and init random plaintext - if (BPU_gf2VecMalloc(&pt_out, ctx->pt_len)) { - BPU_printError("PT out initialisation error"); - - return 1; - } - BPU_gf2VecRand(pt_out, 0); - /***************************************/ - fprintf(stderr, "Encryption...\n"); - // BPU_encrypt plain text - if (BPU_mecsEncrypt(ct, pt_in, ctx)) { - BPU_printError("Encryption error"); - - BPU_gf2VecFree(&ct); - BPU_gf2VecFree(&pt_in); - BPU_gf2VecFree(&pt_out); - return 1; - } - // exit(0); - /***************************************/ - fprintf(stderr, "Decryption...\n"); - // decrypt cipher text - if (BPU_mecsDecrypt(pt_out, ct, ctx)) { - BPU_printError("Decryption error"); - - BPU_gf2VecFree(&ct); - BPU_gf2VecFree(&pt_in); - BPU_gf2VecFree(&pt_out); + BPU_printError ("Key generation error"); + + return 1; + } + /***************************************/ + // prepare plain text, allocate memory and init random plaintext + if (BPU_gf2VecMalloc (&pt_in, ctx->pt_len)) { + BPU_printError ("PT initialisation error"); + + return 1; + } + BPU_gf2VecRand (pt_in, 0); + + // alocate cipher text vector + if (BPU_gf2VecMalloc (&ct, ctx->ct_len)) { + BPU_printError ("CT vector allocation error"); + + BPU_gf2VecFree (&pt_in); + return 1; + } + // prepare plain text, allocate memory and init random plaintext + if (BPU_gf2VecMalloc (&pt_out, ctx->pt_len)) { + BPU_printError ("PT out initialisation error"); + + return 1; + } + BPU_gf2VecRand (pt_out, 0); + /***************************************/ + fprintf (stderr, "Encryption...\n"); + // BPU_encrypt plain text + if (BPU_mecsEncrypt (ct, pt_in, ctx)) { + BPU_printError ("Encryption error"); + + BPU_gf2VecFree (&ct); + BPU_gf2VecFree (&pt_in); + BPU_gf2VecFree (&pt_out); + return 1; + } + // exit(0); + /***************************************/ + fprintf (stderr, "Decryption...\n"); + // decrypt cipher text + if (BPU_mecsDecrypt (pt_out, ct, ctx)) { + BPU_printError ("Decryption error"); + + BPU_gf2VecFree (&ct); + BPU_gf2VecFree (&pt_in); + BPU_gf2VecFree (&pt_out); return 1; } - /***************************************/ - - // check for correct decryption - if (BPU_gf2VecCmp(pt_in, pt_out)) { - BPU_printError("\nOutput plain text differs from input"); - - rc = 2; - } - else { - fprintf(stderr, "\nSUCCESS: Input plain text is equal to output plain text.\n"); - } - // clean up - /***************************************/ - fprintf(stderr, "\nCleaning up...\n"); - BPU_gf2VecFree(&pt_in); - BPU_gf2VecFree(&pt_out); - BPU_gf2VecFree(&ct); - return rc; + /***************************************/ + + // check for correct decryption + if (BPU_gf2VecCmp (pt_in, pt_out)) { + BPU_printError ("\nOutput plain text differs from input"); + + rc = 2; + } + else { + fprintf (stderr, + "\nSUCCESS: Input plain text is equal to output plain text.\n"); + } + // clean up + /***************************************/ + fprintf (stderr, "\nCleaning up...\n"); + BPU_gf2VecFree (&pt_in); + BPU_gf2VecFree (&pt_out); + BPU_gf2VecFree (&ct); + return rc; } #ifdef BPU_CONF_ASN1 -int testKeyGenAsn1() { +int testKeyGenAsn1 () { int rc = 0; + // MUST BE NULL BPU_T_Mecs_Ctx *ctx = NULL; BPU_T_Mecs_Ctx *ctx_2 = NULL; - BPU_T_UN_Mecs_Params params; + BPU_T_UN_Mecs_Params params; /***************************************/ // mce initialisation t = 50, m = 11 - fprintf(stderr, "Basic GOPPA Initialisation...\n"); - if (BPU_mecsInitParamsGoppa(¶ms, 11, 50, 0)) { - return 1; - } + fprintf (stderr, "Basic GOPPA Initialisation...\n"); + if (BPU_mecsInitParamsGoppa (¶ms, 11, 50, 0)) { + return 1; + } - if (BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA)) { + if (BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA)) { // if (BPU_mecsInitCtx(&ctx, 11, 50, BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA)) { return 1; } /***************************************/ - fprintf(stderr, "Key generation...\n"); + fprintf (stderr, "Key generation...\n"); // key pair generation - if (BPU_mecsGenKeyPair(ctx)) { - BPU_printError("Key generation error"); + if (BPU_mecsGenKeyPair (ctx)) { + BPU_printError ("Key generation error"); return 1; } - rc = BPU_asn1SaveKeyPair(ctx, "prikey.der", "pubkey.der"); + rc = BPU_asn1SaveKeyPair (ctx, "prikey.der", "pubkey.der"); if (rc) { - asn1_perror(rc); + asn1_perror (rc); } - rc = BPU_asn1LoadKeyPair(&ctx_2, "prikey.der", "pubkey.der"); + rc = BPU_asn1LoadKeyPair (&ctx_2, "prikey.der", "pubkey.der"); if (rc) { - asn1_perror(rc); + asn1_perror (rc); } - testCmpMecsCtx(ctx, ctx_2); + testCmpMecsCtx (ctx, ctx_2); - BPU_mecsFreeCtx(&ctx); - BPU_mecsFreeCtx(&ctx_2); - BPU_mecsFreeParamsGoppa(¶ms); + BPU_mecsFreeCtx (&ctx); + BPU_mecsFreeCtx (&ctx_2); + BPU_mecsFreeParamsGoppa (¶ms); return rc; } #endif -int main(int argc, char **argv) { - int rc = 0; +int main (int argc, char **argv) { + int rc = 0; + // MUST BE NULL BPU_T_Mecs_Ctx *ctx = NULL; - BPU_T_UN_Mecs_Params params; + BPU_T_UN_Mecs_Params params; - srand(time(NULL)); + srand (time (NULL)); #if !defined(BPU_CONF_GOPPA_WO_H) && defined(BPU_CONF_ASN1) - testKeyGenAsn1(); + testKeyGenAsn1 (); #endif // /***************************************/ // // mce initialisation t = 50, m = 11 - fprintf(stderr, "Basic GOPPA Initialisation...\n"); - if (BPU_mecsInitParamsGoppa(¶ms, 11, 50, 0)) { + fprintf (stderr, "Basic GOPPA Initialisation...\n"); + if (BPU_mecsInitParamsGoppa (¶ms, 11, 50, 0)) { return 1; } - if (BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA)) { + if (BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA)) { return 1; } - rc += testKeyGenEncDec(ctx); - BPU_mecsFreeCtx(&ctx); + rc += testKeyGenEncDec (ctx); + BPU_mecsFreeCtx (&ctx); #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - fprintf(stderr, "\nCCA2 Pointcheval GOPPA Initialisation...\n"); - if (BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA)) { + fprintf (stderr, "\nCCA2 Pointcheval GOPPA Initialisation...\n"); + if (BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA)) { return 1; } - rc += testKeyGenEncDec(ctx); - BPU_mecsFreeCtx(&ctx); - BPU_mecsFreeParamsGoppa(¶ms); + rc += testKeyGenEncDec (ctx); + BPU_mecsFreeCtx (&ctx); + BPU_mecsFreeParamsGoppa (¶ms); #endif -// /***************************************/ +// /***************************************/ // mce initialisation of 80-bit security - fprintf(stderr, "Basic QC-MDPC Initialisation...\n"); - if (BPU_mecsInitParamsQcmdpc(¶ms, 4801, 2, 90, 84)) { + fprintf (stderr, "Basic QC-MDPC Initialisation...\n"); + if (BPU_mecsInitParamsQcmdpc (¶ms, 4801, 2, 90, 84)) { return 1; } - if (BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_BASIC_QCMDPC)) { + if (BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_BASIC_QCMDPC)) { return 1; } - rc += testKeyGenEncDec(ctx); - BPU_mecsFreeCtx(&ctx); - BPU_mecsFreeParamsQcmdpc(¶ms); + rc += testKeyGenEncDec (ctx); + BPU_mecsFreeCtx (&ctx); + BPU_mecsFreeParamsQcmdpc (¶ms); - #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA - fprintf(stderr, "\nCCA2 Pointcheval QC-MDPC Initialisation...\n"); - if (BPU_mecsInitParamsQcmdpc(¶ms, 4801, 2, 90, 84)) { +#ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA + fprintf (stderr, "\nCCA2 Pointcheval QC-MDPC Initialisation...\n"); + if (BPU_mecsInitParamsQcmdpc (¶ms, 4801, 2, 90, 84)) { return 1; } - if (BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_CCA2_POINTCHEVAL_QCMDPC)) { + if (BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_CCA2_POINTCHEVAL_QCMDPC)) { return 1; } - rc += testKeyGenEncDec(ctx); - BPU_mecsFreeCtx(&ctx); - BPU_mecsFreeParamsQcmdpc(¶ms); - #endif + rc += testKeyGenEncDec (ctx); + BPU_mecsFreeCtx (&ctx); + BPU_mecsFreeParamsQcmdpc (¶ms); +#endif BPU_T_Bigint *a, *b, *c; - BPU_bigintMalloc(&a, 32); - BPU_bigintMalloc(&b, 16); - BPU_bigintMalloc(&c, 32); + + BPU_bigintMalloc (&a, 32); + BPU_bigintMalloc (&b, 16); + BPU_bigintMalloc (&c, 32); a->elements[0] = 65535; a->elements[1] = 65535; b->elements[0] = 65535; - BPU_bigintMultiply(c, a, b); + BPU_bigintMultiply (c, a, b); #ifdef BPU_CONF_PRINT - BPU_printElementArray(a); - BPU_printElementArray(b); - BPU_printElementArray(c); + BPU_printElementArray (a); + BPU_printElementArray (b); + BPU_printElementArray (c); #endif - BPU_bigintAdd(c, a, b); + BPU_bigintAdd (c, a, b); #ifdef BPU_CONF_PRINT - BPU_printElementArray(a); - BPU_printElementArray(b); - BPU_printElementArray(c); + BPU_printElementArray (a); + BPU_printElementArray (b); + BPU_printElementArray (c); #endif - BPU_bigintFree(&a); - BPU_bigintFree(&b); - BPU_bigintFree(&c); + BPU_bigintFree (&a); + BPU_bigintFree (&b); + BPU_bigintFree (&c); - return rc; + return rc; } diff --git a/lib/src/test-speed.c b/lib/src/test-speed.c index e3df6e6..26a0948 100644 --- a/lib/src/test-speed.c +++ b/lib/src/test-speed.c @@ -27,51 +27,57 @@ #define BPU_TEST_ROUNDS 300 -int main(int argc, char **argv) { +int main (int argc, char **argv) { // MUST BE INITIALIZED TO NULL BPU_T_Mecs_Ctx *ctx = NULL; - BPU_T_UN_Mecs_Params params; + BPU_T_UN_Mecs_Params params; BPU_T_GF2_Vector *ct, *pt; - int i; - struct timeval tv, tv_end; - double res = 0; - double res_2 = 0; - double res_3 = 0; - fprintf(stderr, "====== SPEED TEST ======\n"); - srand(time(NULL)); + int i; + struct timeval tv, tv_end; + double res = 0; + double res_2 = 0; + double res_3 = 0; - for (i = 0; i < BPU_TEST_ROUNDS; i++){ - BPU_mecsInitParamsGoppa(¶ms, 11, 50, 0); - BPU_mecsInitCtx(&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA); - gettimeofday(&tv, NULL); + fprintf (stderr, "====== SPEED TEST ======\n"); + srand (time (NULL)); - BPU_mecsGenKeyPair(ctx); - gettimeofday(&tv_end, NULL); - res += (tv_end.tv_sec - tv.tv_sec + ((tv_end.tv_usec - tv.tv_usec) / (double)1000000)); + for (i = 0; i < BPU_TEST_ROUNDS; i++) { + BPU_mecsInitParamsGoppa (¶ms, 11, 50, 0); + BPU_mecsInitCtx (&ctx, ¶ms, BPU_EN_MECS_BASIC_GOPPA); + gettimeofday (&tv, NULL); - BPU_gf2VecMalloc(&ct, ctx->ct_len); - BPU_gf2VecMalloc(&pt, ctx->pt_len); - BPU_gf2VecRand(pt, 0); + BPU_mecsGenKeyPair (ctx); + gettimeofday (&tv_end, NULL); + res += + (tv_end.tv_sec - tv.tv_sec + + ((tv_end.tv_usec - tv.tv_usec) / (double) 1000000)); - gettimeofday(&tv, NULL); - BPU_mecsEncrypt(ct, pt, ctx); - gettimeofday(&tv_end, NULL); - res_2 += (tv_end.tv_sec - tv.tv_sec + ((tv_end.tv_usec - tv.tv_usec) / (double)1000000)); + BPU_gf2VecMalloc (&ct, ctx->ct_len); + BPU_gf2VecMalloc (&pt, ctx->pt_len); + BPU_gf2VecRand (pt, 0); - gettimeofday(&tv, NULL); - BPU_mecsDecrypt(pt, ct, ctx); - gettimeofday(&tv_end, NULL); - res_3 += (tv_end.tv_sec - tv.tv_sec + ((tv_end.tv_usec - tv.tv_usec) / (double)1000000)); + gettimeofday (&tv, NULL); + BPU_mecsEncrypt (ct, pt, ctx); + gettimeofday (&tv_end, NULL); + res_2 += + (tv_end.tv_sec - tv.tv_sec + + ((tv_end.tv_usec - tv.tv_usec) / (double) 1000000)); - BPU_gf2VecFree(&pt); - BPU_gf2VecFree(&ct); - BPU_mecsFreeCtx(&ctx); - BPU_mecsFreeParamsGoppa(¶ms); - } - fprintf(stderr, "%0.6lf\n", res / BPU_TEST_ROUNDS); - fprintf(stderr, "%0.6lf\n", res_2 / BPU_TEST_ROUNDS); - fprintf(stderr, "%0.6lf\n", res_3 / BPU_TEST_ROUNDS); + gettimeofday (&tv, NULL); + BPU_mecsDecrypt (pt, ct, ctx); + gettimeofday (&tv_end, NULL); + res_3 += + (tv_end.tv_sec - tv.tv_sec + + ((tv_end.tv_usec - tv.tv_usec) / (double) 1000000)); - return 0; -} + BPU_gf2VecFree (&pt); + BPU_gf2VecFree (&ct); + BPU_mecsFreeCtx (&ctx); + BPU_mecsFreeParamsGoppa (¶ms); + } + fprintf (stderr, "%0.6lf\n", res / BPU_TEST_ROUNDS); + fprintf (stderr, "%0.6lf\n", res_2 / BPU_TEST_ROUNDS); + fprintf (stderr, "%0.6lf\n", res_3 / BPU_TEST_ROUNDS); + return 0; +} diff --git a/tools/format_code.sh b/tools/format_code.sh new file mode 100755 index 0000000..4ff814b --- /dev/null +++ b/tools/format_code.sh @@ -0,0 +1,2 @@ +OPTIONS="-npsl -l80 -i4 -bad -bap -brs -br -brf -nut" +find ../lib -name '*.[ch]' -not -path ../lib/src/bitpunch/version.h -exec indent $OPTIONS {} \; diff --git a/tools/remove_backups.sh b/tools/remove_backups.sh new file mode 100755 index 0000000..890d415 --- /dev/null +++ b/tools/remove_backups.sh @@ -0,0 +1 @@ +find ../lib -name '*.[ch]~' -not -exec rm -v {} \;