int cp_psb_gen(bn_t r, bn_t s[], g2_t g, g2_t x, g2_t y[], int l) { bn_t n; int i, result = RLC_OK; bn_null(n); TRY { bn_new(n); g2_get_ord(n); bn_rand_mod(r, n); g2_rand(g); g2_mul(x, g, r); for (i = 0; i < l; i++) { bn_rand_mod(s[i], n); g2_mul(y[i], g, s[i]); } } CATCH_ANY { result = RLC_ERR; } FINALLY { bn_free(n); } return result; }
/* /////////////////////////////////////////////////////////////////////// * initializer */ g2_svg_element_t* g2_svg_element_init_svg(tb_handle_t reader) { // alloc g2_svg_element_svg_t* element = tb_malloc0(sizeof(g2_svg_element_svg_t)); tb_assert_and_check_return_val(element, tb_null); // init element->base.writ = g2_svg_element_svg_writ; element->base.exit = g2_svg_element_svg_exit; // init painter element->painter.painter = tb_null; element->painter.hash = tb_null; element->painter.pool = tb_null; element->painter.load = tb_false; // attributes tb_size_t pw = 0; tb_size_t ph = 0; tb_xml_node_t const* attr = tb_xml_reader_attributes(reader); for (; attr; attr = attr->next) { tb_char_t const* p = tb_pstring_cstr(&attr->data); if (!tb_pstring_cstricmp(&attr->name, "x")) g2_svg_parser_float(p, &element->x); else if (!tb_pstring_cstricmp(&attr->name, "y")) g2_svg_parser_float(p, &element->y); else if (!tb_pstring_cstricmp(&attr->name, "width")) { p = g2_svg_parser_float(p, &element->width); if (*p == '%') pw = 1; } else if (!tb_pstring_cstricmp(&attr->name, "height")) { p = g2_svg_parser_float(p, &element->height); if (*p == '%') ph = 1; } else if (!tb_pstring_cstricmp(&attr->name, "viewBox")) { p = g2_svg_parser_float(p, &element->viewbox.x); p = g2_svg_parser_separator_skip(p); p = g2_svg_parser_float(p, &element->viewbox.y); p = g2_svg_parser_separator_skip(p); p = g2_svg_parser_float(p, &element->viewbox.w); p = g2_svg_parser_separator_skip(p); p = g2_svg_parser_float(p, &element->viewbox.h); } } if (pw && element->viewbox.w) element->width = g2_mul(element->viewbox.w, element->width / 100); if (pw && element->viewbox.h) element->height = g2_mul(element->viewbox.h, element->height / 100); // ok return element; }
status_t element_pow_int(element_t c, element_t a, integer_t b) { GroupType type = a->type; // TODO: c (type) = a (type) ^ b (ZR) LEAVE_IF( c->isInitialized != TRUE || a->isInitialized != TRUE, "uninitialized argument."); EXIT_IF_NOT_SAME(c, a); LEAVE_IF(b == NULL, "uninitialized integer."); status_t result = ELEMENT_OK; LEAVE_IF( c->type != type, "result initialized but invalid type."); switch(type) { case ZR: bn_mxp(c->bn, a->bn, b, a->order); break; case G1: g1_mul(c->g1, a->g1, b); break; case G2: g2_mul(c->g2, a->g2, b); break; case GT: gt_exp(c->gt, a->gt, b); break; default: result = ELEMENT_INVALID_TYPES; break; } return result; }
status_t element_pow_zr(element_t c, element_t a, element_t b) { GroupType type = a->type; // c (type) = a (type) ^ b (ZR) LEAVE_IF( c->isInitialized != TRUE || a->isInitialized != TRUE, "uninitialized argument."); EXIT_IF_NOT_SAME(c, a); LEAVE_IF(a->isInitialized != TRUE, "invalid argument."); LEAVE_IF(b->isInitialized != TRUE || b->type != ZR, "invalid type."); if(type == ZR) { bn_mxp(c->bn, a->bn, b->bn, a->order); } else if(type == G1) { g1_mul(c->g1, a->g1, b->bn); } else if(type == G2) { g2_mul(c->g2, a->g2, b->bn); } else if(type == GT) { gt_exp(c->gt, a->gt, b->bn); } else { return ELEMENT_INVALID_TYPES; } return ELEMENT_OK; }
status_t element_mul_int(element_t c, element_t a, integer_t b) { GroupType type = a->type; // TODO: c (type) = a (type) * b (ZR) LEAVE_IF(a->isInitialized != TRUE, "invalid argument."); LEAVE_IF(c->isInitialized != TRUE || c->type != type, "result not initialized or invalid type."); if(type == ZR) { bn_mul(c->bn, a->bn, b); bn_mod(c->bn, c->bn, c->order); } else if(type == G1) { g1_mul(c->g1, a->g1, b); } else if(type == G2) { g2_mul(c->g2, a->g2, b); } else if(type == GT) { gt_exp(c->gt, a->gt, b); } else { return ELEMENT_INVALID_TYPES; } return ELEMENT_OK; }
int cp_pss_ver(g1_t a, g1_t b, uint8_t *msg, int len, g2_t g, g2_t x, g2_t y) { g1_t p[2]; g2_t r[2]; gt_t e; bn_t m, n; int result = 1; g1_null(p[0]); g1_null(p[1]); g2_null(r[0]); g2_null(r[1]); gt_null(e); bn_null(m); bn_null(n); TRY { g1_new(p[0]); g1_new(p[1]); g2_new(r[0]); g2_new(r[1]); gt_new(e); bn_new(m); bn_new(n); if (g1_is_infty(a)) { result = 0; } g1_copy(p[0], a); g1_copy(p[1], b); g2_copy(r[1], g); g2_neg(r[1], r[1]); g1_get_ord(n); bn_read_bin(m, msg, len); bn_mod(m, m, n); g2_mul(r[0], y, m); g2_add(r[0], r[0], x); g2_norm(r[0], r[0]); pc_map_sim(e, p, r, 2); if (!gt_is_unity(e)) { result = 0; } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { g1_free(p[0]); g1_free(p[1]); g2_free(r[0]); g2_free(r[1]); gt_free(e); bn_free(m); bn_free(n); } return result; }
int cp_bgn_dec2(dig_t *out, g2_t in[2], bgn_t prv) { bn_t r, n; g2_t s, t, u; int i, result = STS_ERR; bn_null(n); bn_null(r); g2_null(s); g2_null(t); g2_null(u); TRY { bn_new(n); bn_new(r); g2_new(s); g2_new(t); g2_new(u); g2_get_ord(n); /* Compute T = x(ym + r)G - (zm + xr)G = m(xy - z)G. */ g2_mul(t, in[0], prv->x); g2_sub(t, t, in[1]); g2_norm(t, t); /* Compute U = (xy - z)G and find m. */ bn_mul(r, prv->x, prv->y); bn_sub(r, r, prv->z); bn_mod(r, r, n); g2_mul_gen(s, r); g2_copy(u, s); if (g2_is_infty(t) == 1) { *out = 0; result = STS_OK; } else { for (i = 0; i < INT_MAX; i++) { if (g2_cmp(t, u) == CMP_EQ) { *out = i + 1; result = STS_OK; break; } g2_add(u, u, s); g2_norm(u, u); } } } CATCH_ANY { result = STS_ERR; } FINALLY { bn_free(n); bn_free(r); g2_free(s); g2_free(t); g2_free(u); } return result; }
int cp_sokaka_gen_prv(sokaka_t k, char *id, int len, bn_t master) { if (pc_map_is_type1()) { g1_map(k->s1, (uint8_t *)id, len); g1_mul(k->s1, k->s1, master); } else { g1_map(k->s1, (uint8_t *)id, len); g1_mul(k->s1, k->s1, master); g2_map(k->s2, (uint8_t *)id, len); g2_mul(k->s2, k->s2, master); } return STS_OK; }
int element_is_member(element_t e) { LEAVE_IF(e->isInitialized != TRUE, "uninitialized argument."); int result; if(e->type == ZR) { if(bn_cmp(e->bn, e->order) <= CMP_EQ) result = TRUE; else result = FALSE; } else if(e->type == G1) { g1_t r; g1_inits(r); g1_mul(r, e->g1, e->order); if(g1_is_infty(r) == 1) result = TRUE; else result = FALSE; g1_free(r); } else if(e->type == G2) { g2_t r; g2_inits(r); g2_mul(r, e->g2, e->order); if(g2_is_infty(r) == 1) result = TRUE; else result = FALSE; g2_free(r); } else if(e->type == GT) { gt_t r; gt_inits(r); gt_exp(r, e->gt, e->order); if(gt_is_unity(r) == 1) result = TRUE; else result = FALSE; gt_free(r); } else { result = ELEMENT_INVALID_ARG; } return result; }
int cp_pss_gen(bn_t r, bn_t s, g2_t g, g2_t x, g2_t y) { bn_t n; int result = RLC_OK; bn_null(n); TRY { bn_new(n); g2_get_ord(n); bn_rand_mod(r, n); bn_rand_mod(s, n); g2_rand(g); g2_mul(x, g, r); g2_mul(y, g, s); } CATCH_ANY { result = RLC_ERR; } FINALLY { bn_free(n); } return result; }
int cp_bgn_enc2(g2_t out[2], dig_t in, bgn_t pub) { bn_t r, n; g2_t t; int result = STS_OK; bn_null(n); bn_null(r); g2_null(t); TRY { bn_new(n); bn_new(r); g2_new(t); g2_get_ord(n); bn_rand_mod(r, n); /* Compute c0 = (ym + r)G. */ g2_mul_dig(out[0], pub->hy, in); g2_mul_gen(t, r); g2_add(out[0], out[0], t); g2_norm(out[0], out[0]); /* Compute c1 = (zm + xr)G. */ g2_mul_dig(out[1], pub->hz, in); g2_mul(t, pub->hx, r); g2_add(out[1], out[1], t); g2_norm(out[1], out[1]); } CATCH_ANY { result = STS_ERR; } FINALLY { bn_free(n); bn_free(r); g2_free(t); } return result; }
int cp_psb_ver(g1_t a, g1_t b, uint8_t *msgs[], int lens[], g2_t g, g2_t x, g2_t y[], int l) { g1_t p[2]; g2_t q[2]; gt_t e; bn_t m, n; int i, result = 1; g1_null(p[0]); g1_null(p[1]); g2_null(q[0]); g2_null(q[1]); gt_null(e); bn_null(m); bn_null(n); TRY { g1_new(p[0]); g1_new(p[1]); g2_new(q[0]); g2_new(q[1]); gt_new(e); bn_new(m); bn_new(n); if (g1_is_infty(a)) { result = 0; } /* Check that e(a, x \prod y_i^m_i) = e(b, g). */ g1_copy(p[0], a); g1_copy(p[1], b); g2_copy(q[0], x); g1_get_ord(n); for (i = 0; i < l; i++) { bn_read_bin(m, msgs[i], lens[i]); bn_mod(m, m, n); g2_mul(q[1], y[i], m); g2_add(q[0], q[0], q[1]); } g2_norm(q[0], q[0]); g2_copy(q[1], g); g2_neg(q[1], q[1]); pc_map_sim(e, p, q, 2); if (!gt_is_unity(e)) { result = 0; } } CATCH_ANY { THROW(ERR_CAUGHT); } FINALLY { g1_free(p[0]); g1_free(p[1]); g2_free(q[0]); g2_free(q[1]); gt_free(e); bn_free(m); bn_free(n); } return result; }