Example #1
0
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;
}
Example #2
0
status_t element_mul(element_t c, element_t a, element_t b)
{
	GroupType type = a->type;
	EXIT_IF_NOT_SAME(a, b);
	LEAVE_IF(a->isInitialized != TRUE || b->isInitialized != TRUE || c->isInitialized != TRUE, "uninitialized arguments.");
	LEAVE_IF( c->type != type, "result initialized but invalid type.");

	if(type == ZR) {
		bn_mul(c->bn, a->bn, b->bn);
		bn_mod(c->bn, c->bn, c->order);
	}
	else if(type == G1) {
		g1_add(c->g1, a->g1, b->g1);
		g1_norm(c->g1, c->g1);
	}
	else if(type == G2) {
		g2_add(c->g2, a->g2, b->g2);
		g2_norm(c->g2, c->g2);
	}
	else if(type == GT) {
		gt_mul(c->gt, a->gt, b->gt);
	}
	else {
		return ELEMENT_INVALID_TYPES;
	}

	return ELEMENT_OK;
}
Example #3
0
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;
}
Example #4
0
status_t element_div(element_t c, element_t a, element_t b)
{
	GroupType type = a->type;
	EXIT_IF_NOT_SAME(a, b);
	LEAVE_IF(a->isInitialized != TRUE || b->isInitialized != TRUE || c->isInitialized != TRUE, "uninitialized arguments.");
	LEAVE_IF( c->type != type, "result initialized but invalid type.");

	if(type == ZR) {
		if(bn_is_zero(b->bn)) return ELEMENT_DIV_ZERO;
		// c = (1 / b) mod order
		element_invert(c, b);
		if(bn_is_one(a->bn))  return ELEMENT_OK;
//		bn_div(c->bn, a->bn, b->bn);
//		bn_mod(c->bn, c->bn, c->order);
		// remainder of ((a * c) / order)
		integer_t s;
		bn_inits(s);
		// c = (a * c) / order (remainder only)
		bn_mul(s, a->bn, c->bn);
		bn_div_rem(s, c->bn, s, a->order);
//		if(bn_sign(c->bn) == BN_NEG) bn_add(c->bn, c->bn, a->order);
		bn_free(s);


	}
	else if(type == G1) {
		g1_sub(c->g1, a->g1, b->g1);
		g1_norm(c->g1, c->g1);
	}
	else if(type == G2) {
		g2_sub(c->g2, a->g2, b->g2);
		g2_norm(c->g2, c->g2);
	}
	else if(type == GT) {
		gt_t t;
		gt_inits(t);
		gt_inv(t, b->gt);
		gt_mul(c->gt, a->gt, t);
		gt_free(t);
	}
	else {
		return ELEMENT_INVALID_TYPES;
	}

	return ELEMENT_OK;
}
Example #5
0
int g1_add_norm(g1_t out, g1_t in1, g1_t in2)
{
	g1_add(out, in1, in2);
	g1_norm(out, out);
	return 0;
}