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; }
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_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: if(bn_is_zero(b)) gt_set_unity(c->gt); else gt_exp(c->gt, a->gt, b); break; default: result = ELEMENT_INVALID_TYPES; break; } return result; }
int cp_bgn_dec1(dig_t *out, g1_t in[2], bgn_t prv) { bn_t r, n; g1_t s, t, u; int i, result = STS_ERR; bn_null(n); bn_null(r); g1_null(s); g1_null(t); g1_null(u); TRY { bn_new(n); bn_new(r); g1_new(s); g1_new(t); g1_new(u); g1_get_ord(n); /* Compute T = x(ym + r)G - (zm + xr)G = m(xy - z)G. */ g1_mul(t, in[0], prv->x); g1_sub(t, t, in[1]); g1_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); g1_mul_gen(s, r); g1_copy(u, s); if (g1_is_infty(t) == 1){ *out = 0; result = STS_OK; } else { for (i = 0; i < INT_MAX; i++) { if (g1_cmp(t, u) == CMP_EQ) { *out = i + 1; result = STS_OK; break; } g1_add(u, u, s); g1_norm(u, u); } } } CATCH_ANY { result = STS_ERR; } FINALLY { bn_free(n); bn_free(r); g1_free(s); g1_free(t); g1_free(u); } return result; }
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_bls_sig(g1_t s, uint8_t *msg, int len, bn_t d) { g1_t p; int result = STS_OK; g1_null(p); TRY { g1_new(p); g1_map(p, msg, len); g1_mul(s, p, d); } CATCH_ANY { result = STS_ERR; } FINALLY { g1_free(p); } return result; }
int cp_bgn_enc1(g1_t out[2], dig_t in, bgn_t pub) { bn_t r, n; g1_t t; int result = STS_OK; bn_null(n); bn_null(r); g1_null(t); TRY { bn_new(n); bn_new(r); g1_new(t); g1_get_ord(n); bn_rand_mod(r, n); /* Compute c0 = (ym + r)G. */ g1_mul_dig(out[0], pub->gy, in); g1_mul_gen(t, r); g1_add(out[0], out[0], t); g1_norm(out[0], out[0]); /* Compute c1 = (zm + xr)G. */ g1_mul_dig(out[1], pub->gz, in); g1_mul(t, pub->gx, r); g1_add(out[1], out[1], t); g1_norm(out[1], out[1]); } CATCH_ANY { result = STS_ERR; } FINALLY { bn_free(n); bn_free(r); g1_free(t); } return result; }
int cp_psb_sig(g1_t a, g1_t b, uint8_t *msgs[], int lens[], bn_t r, bn_t s[], int l) { bn_t m, n, t; int i, result = RLC_OK; bn_null(m); bn_null(n); bn_null(t); TRY { bn_new(m); bn_new(n); bn_new(t); /* Choose random a in G1. */ g1_rand(a); /* Compute b = a^x+\sum y_im_i. */ g1_get_ord(n); bn_copy(t, r); for (i = 0; i < l; i++) { bn_read_bin(m, msgs[i], lens[i]); bn_mod(m, m, n); bn_mul(m, m, s[i]); bn_mod(m, m, n); bn_add(t, t, m); bn_mod(t, t, n); } g1_mul(b, a, t); } CATCH_ANY { result = RLC_ERR; } FINALLY { bn_free(m); bn_free(n); bn_free(t); } return result; }