Skip to content

Commit

Permalink
Another refactor for speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Jan 18, 2025
1 parent fa39349 commit 38ecab8
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 296 deletions.
98 changes: 42 additions & 56 deletions bench/bench_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,83 +877,69 @@ static void pdpub(void) {

static void pdprv(void) {
bn_t r1, r2[3], ls[AGGS * AGGS], cs[AGGS], ks[AGGS];
g1_t fs[AGGS], p[AGGS * AGGS], u1[2], v1[3];
g2_t q[AGGS * AGGS], u2[2], v2[4], w2[4], ds[AGGS * AGGS], bs[AGGS], rs[AGGS * AGGS];
gt_t e[2], r, ts[AGGS + 1], g[3 * AGGS + 1];
g1_t fs[AGGS], p[AGGS * AGGS], u1[2], v1[3], rs[AGGS * AGGS], ds[AGGS * AGGS];
g2_t q[AGGS * AGGS], u2[2], v2[4], w2[4], bs[AGGS * AGGS];
gt_t e[2], r, ts[2 * AGGS + 1], g[AGGS * AGGS + 1];

bn_null(r1);
gt_null(r);
for (int i = 0; i < 2; i++) {
g1_null(u1[i]);
g2_null(u2[i]);
gt_null(e[i]);
}
for (int i = 0; i < 3; i++) {
g1_null(v1[i]);
bn_null(r2[i]);
}
for (int i = 0; i < 4; i++) {
g2_null(v2[i]);
g2_null(w2[i]);
}
for (int i = 0; i < RLC_MAX(4, AGGS + 1); i++) {
gt_null(g[i]);
}
for (int i = 0; i < AGGS; i++) {
g1_null(p[i]);
g2_null(q[i]);
bn_null(ls[i]);
g2_null(ds[i])
g2_null(rs[i])
}
gt_null(ts[AGGS]);

bn_new(r1);
gt_new(r);
for (int i = 0; i < 2; i++) {
g1_null(u1[i]);
g2_null(u2[i]);
gt_null(e[i]);
g1_new(u1[i]);
g2_new(u2[i]);
gt_new(e[i]);
}
for (int i = 0; i < 3; i++) {
g1_null(v1[i]);
bn_null(r2[i]);
g1_new(v1[i]);
bn_new(r2[i]);
}
for (int i = 0; i < 4; i++) {
g2_null(v2[i]);
g2_null(w2[i]);
g2_new(v2[i]);
g2_new(w2[i]);
}
for (size_t i = 0; i < 3 * AGGS + 1; i++) {
gt_null(g[i]);
gt_new(g[i]);
}
for (size_t i = 0; i < AGGS; i++) {
for (size_t j = 0; j < AGGS; j++) {
bn_null(ls[i * AGGS + j]);
g1_null(p[i * AGGS + j]);
g2_null(q[i * AGGS + j]);
g2_null(rs[i * AGGS + j]);
g2_null(ds[i * AGGS + j]);
g1_null(rs[i * AGGS + j]);
g1_null(ds[i * AGGS + j]);
g2_null(bs[i * AGGS + j]);
gt_null(g[i * AGGS + j]);
bn_new(ls[i * AGGS + j]);
g1_new(p[i * AGGS + j]);
g2_new(q[i * AGGS + j]);
g2_new(rs[i * AGGS + j]);
g2_new(ds[i * AGGS + j]);
g1_new(rs[i * AGGS + j]);
g1_new(ds[i * AGGS + j]);
g2_new(bs[i * AGGS + j]);
gt_new(g[i * AGGS + j]);
g1_rand(p[i * AGGS + j]);
g2_rand(q[i * AGGS + j]);
}
bn_null(ks[i]);
bn_null(cs[i]);
g1_null(fs[i]);
g2_null(bs[i]);
gt_null(ts[i]);
gt_null(ts[i + AGGS]);
bn_new(ks[i]);
bn_new(cs[i])
g1_new(fs[i]);
g2_new(bs[i]);
gt_new(ts[i]);
gt_new(ts[i + AGGS]);
}
gt_new(ts[AGGS]);
gt_null(ts[2 * AGGS]);
gt_new(ts[2 * AGGS]);
gt_null(g[AGGS * AGGS]);
gt_new(g[AGGS * AGGS]);

BENCH_RUN("cp_pdprv_gen") {
BENCH_ADD(cp_pdprv_gen(r1, r2, u1, u2, v2, e));
Expand Down Expand Up @@ -1020,49 +1006,50 @@ static void pdprv(void) {
} BENCH_END;

BENCH_RUN("cp_ambat_ask (AGGS)") {
BENCH_ADD(cp_ambat_ask(ls, rs, u1[1], u2[1], r1, p, q[0], u1[0], u2[0], e[0], 0, AGGS));
BENCH_ADD(cp_ambat_ask(ls, rs, u1[1], u2[1], w2[0], r1, p[0], q, u1[0], u2[0], e[0], AGGS));
} BENCH_END;

BENCH_RUN("cp_ambat_ans (AGGS)") {
BENCH_ADD(cp_ambat_ans(g, rs, u1[1], u2[1], p, AGGS));
BENCH_ADD(cp_ambat_ans(g, rs, u1[1], u2[1], w2[0], q, AGGS));
} BENCH_END;

BENCH_RUN("cp_ambat_ver (AGGS)") {
BENCH_ADD(cp_ambat_ver(g, g, ls, e[0], AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_gen (AGGS)") {
BENCH_ADD(cp_amprd_gen(ls, rs, w2[0], r1, u1[0], u2[0], e[0], 1, AGGS));
BENCH_ADD(cp_amprd_gen(fs[0], r1, u1[0], u2[0], e[0]));
} BENCH_END;

BENCH_RUN("cp_amprd_ask (AGGS)") {
BENCH_ADD(cp_amprd_ask(ks, ds, cs, fs, bs, v1[0], v2[0], ls, rs, w2[0], r1, p, q, u1[0], u2[0], e[0], 1, AGGS));
BENCH_ADD(cp_amprd_ask(ks, ds, ls, rs, v1[0], v2[0], w2[0], bs, fs[0], r1, u1[0], u2[0], e[0], p, q, 1, AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_ans (AGGS)") {
BENCH_ADD(cp_amprd_ans(g, ds, fs, bs, v1[0], v2[0], p, q, 1, AGGS));
BENCH_ADD(cp_amprd_ans(g, ts, ds, rs, v1[0], v2[0], w2[0], bs, p, q, 1, AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_ver (AGGS)") {
BENCH_ADD(cp_amprd_ver(ts, g, ks, cs, e[0], 1));
BENCH_ADD(cp_amprd_ver(g, ts, ks, ls, e[0], 1, AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_gen (AGGS²)") {
BENCH_ADD(cp_amprd_gen(ls, rs, w2[0], r1, u1[0], u2[0], e[0], AGGS, AGGS));
BENCH_ADD(cp_amprd_gen(fs[0], r1, u1[0], u2[0], e[0]));
} BENCH_END;

BENCH_RUN("cp_amprd_ask (AGGS²)") {
BENCH_ADD(cp_amprd_ask(ks, ds, cs, fs, bs, v1[0], v2[0], ls, rs, w2[0], r1, p, q, u1[0], u2[0], e[0], AGGS, AGGS));
BENCH_ADD(cp_amprd_ask(ks, ds, ls, rs, v1[0], v2[0], w2[0], bs, fs[0], r1, u1[0], u2[0], e[0], p, q, AGGS, AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_ans (AGGS²)") {
BENCH_ADD(cp_amprd_ans(g, ds, fs, bs, v1[0], v2[0], p, q, AGGS, AGGS));
BENCH_ADD(cp_amprd_ans(g, ts, ds, rs, v1[0], v2[0], w2[0], bs, p, q, AGGS, AGGS));
} BENCH_END;

BENCH_RUN("cp_amprd_ver (AGGS²)") {
BENCH_ADD(cp_amprd_ver(ts, g, ks, cs, e[0], AGGS));
BENCH_ADD(cp_amprd_ver(g, ts, ks, ls, e[0], AGGS, AGGS));
} BENCH_END;


bn_free(r1);
gt_free(r);
for (int i = 0; i < 2; i++) {
Expand All @@ -1078,26 +1065,25 @@ static void pdprv(void) {
g2_free(v2[i]);
g2_free(w2[i]);
}
for (size_t i = 0; i < 3 * AGGS + 1; i++) {
gt_free(g[i]);
}
for (size_t i = 0; i < AGGS; i++) {
for (size_t j = 0; j < AGGS; j++) {
bn_free(ls[i * AGGS + j]);
g1_free(p[i * AGGS + j]);
g2_free(q[i * AGGS + j]);
g2_free(rs[i * AGGS + j]);
g2_free(ds[i * AGGS + j]);
g1_free(rs[i * AGGS + j]);
g1_free(ds[i * AGGS + j]);
g2_free(bs[i * AGGS + j]);
gt_free(g[i * AGGS + j]);
}
bn_free(ls[i]);
bn_free(cs[i]);
bn_free(ks[i]);
g1_free(fs[i]);
g2_free(rs[i]);
g2_free(bs[i]);
gt_free(ts[i]);
gt_free(ts[i + AGGS]);
}
gt_free(ts[AGGS]);
gt_free(ts[2 * AGGS]);
gt_free(g[AGGS * AGGS]);
}

static void sokaka(void) {
Expand Down
97 changes: 45 additions & 52 deletions include/relic_cp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1498,19 +1498,19 @@ int cp_ambat_gen(bn_t r, g1_t u, g2_t v, gt_t e);
* @param[out] rs - the group elements computed by the client.
* @param[out] a - the element in G_1 computed by the client.
* @param[out] b - the element in G_2 computed by the client.
* @param[out] c - the element in G_2 computed by the client.
* @param[in] r - the randomness.
* @param[in] p - the first arguments of the pairing.
* @param[in] q - the second argument of the pairing.
* @param[in] p - the first argument of the pairing.
* @param[in] q - the second arguments of the pairing.
* @param[in] u - the U1 precomputed value in G_1.
* @param[in] v - the U2 precomputed value in G_2.
* @param[in] e - the precomputed value e(U1, U2).
* @param[in] longc - the flag to indicate if challenge is long.
* @param[in] m - the number of pairings delegated in the batch.
* @return RLC_OK if no errors occurred, RLC_ERR otherwise.
*/
int cp_ambat_ask(bn_t *ls, g2_t *rs, g1_t a, g2_t b, const bn_t r,
const g1_t *p, const g2_t q, const g1_t u, const g2_t v, const gt_t e,
uint_t longc, size_t m);
int cp_ambat_ask(bn_t *ls, g1_t *rs, g1_t a, g2_t b, g2_t c, const bn_t r,
const g1_t p, const g2_t *q, const g1_t u, const g2_t v, const gt_t e,
size_t m);

/**
* Executes the server-side response for the AMORE batch pairing delegation
Expand All @@ -1520,12 +1520,13 @@ int cp_ambat_ask(bn_t *ls, g2_t *rs, g1_t a, g2_t b, const bn_t r,
* @param[out] rs - the group elements sent by the client.
* @param[in] a - the element in G_1 computed by the client.
* @param[in] b - the element in G_2 computed by the client.
* @param[in] p - the first arguments of the delegated pairings.
* @param[in] c - the element in G_2 computed by the client.
* @param[in] q - the second arguments of the delegated pairings.
* @param[in] m - the number of pairings delegated in the batch.
* @return RLC_OK if no errors occurred, RLC_ERR otherwise.
*/
int cp_ambat_ans(gt_t *gs, const g2_t *rs, const g1_t a, const g2_t b,
const g1_t *p, size_t m);
int cp_ambat_ans(gt_t *gs, const g1_t *rs, const g1_t a, const g2_t b,
const g2_t c, const g2_t *q, size_t m);

/**
* Verifies the result of the AMORE batch pairing delegation protocol.
Expand All @@ -1544,80 +1545,72 @@ int cp_ambat_ver(gt_t *es, const gt_t *gs, const bn_t *ls, const gt_t e,
* Generates parameters for the AMORE delegation protocol to compute the
* product of m pairings using a pairing delegation protocol.
*
* @param[out] ls - the (m) scalars for the protocol.
* @param[out] rs - the (m) points for the protocol.
* @param[out] r - the additional random point for the protocol.
* @param[out] c - the challenge for the pairing delegation.
* @param[out] u - the mask in G_1 for the pairing delegation.
* @param[out] v - the mask in G_2 for the pairing delegation.
* @param[in,out] x - the secret key.
* @param[in,out] e - the precomputed value e(U1, U2).
* @param[in] l - the number of pairing products to compute.
* @param[in] m - the number of pairings per product to compute.
* @return RLC_OK if no errors occurred, RLC_ERR otherwise.
* @param[out] r - the random point in G_1.
* @param[out] c - the randomness for the batch AMORE protocol.
* @param[out] u - the mask in G_1 for batch AMORE.
* @param[out] v - the mask in G_2 for batch AMORE.
* @param[out] e - the precomputed value e(U1, U2).
*/
int cp_amprd_gen(bn_t *ls, g2_t *rs, g2_t r, bn_t c, g1_t u, g2_t v, gt_t e,
size_t l, size_t m);
int cp_amprd_gen(g1_t r, bn_t c, g1_t u, g2_t v, gt_t e);

/**
/*
* Executes the client-side request for the AMORE pairing product delegation
* protocol.
*
* @param[out] ks - the (l) keys for the protocol.
* @param[out] ds - the (l) points for the protocol.
* @param[out] cs - the (l) challenges for the batch protocol.
* @param[out] bs - the (l) points for the batch protocol.
* @param[out] a - the element in G_1.
* @param[out] b - the element in G_2.
* @param[in] ls - the (m) scalars for the protocol.
* @param[in] rs - the (m) points for the protocol.
* @param[in] r - the additional random point for the protocol.
* @param[in] p - the inputs to the batch protocol.
* @param[in] p - the first argument inputs for the pairings.
* @param[in] q - the second argument inputs for the pairings.
* @param[in] u - the U1 precomputed value in G_1.
* @param[in] v - the U2 precomputed value in G_2.
* @param[out] ks - the (l) keys for the batch AMORE protocol.
* @param[out] ds - the (l) points for the batch AMORE protocol.
* @param[out] ls - the (l * m) scalars for the protocol.
* @param[out] rs - the (l * m) points for the protocol.
* @param[out] a - the setup for the batch AMORE protocol.
* @param[out] b - the setup for the batch AMORE protocol.
* @param[out] d - the setup for the batch AMORE protocol.
* @param[out] bs - the row-wise addition of the second arguments.
* @param[out] r - the additional random point for the protocol.
* @param[out] c - the challenge for the pairing delegation.
* @param[out] u - the mask in G_1 for the pairing delegation.
* @param[out] v - the mask in G_2 for the pairing delegation.
* @param[in] l - the number of pairing products to compute.
* @param[in] m - the number of pairings per product to compute.
* @return RLC_OK if no errors occurred, RLC_ERR otherwise.
*/
int cp_amprd_ask(bn_t *ks, g2_t *ds, bn_t *cs, g1_t *fs, g2_t *bs, g1_t a,
g2_t b, const bn_t *ls, const g2_t *rs, const g2_t r, const bn_t c,
const g1_t *p, const g2_t *q, const g1_t u, const g2_t v, const gt_t e,
size_t l, size_t m);

int cp_amprd_ask(bn_t *ks, g1_t *ds, bn_t *ls, g1_t *rs, g1_t a, g2_t b, g2_t d,
g2_t *bs, const g1_t c, const bn_t r, const g1_t u, const g2_t v,
gt_t e, const g1_t *p, const g2_t *q, size_t l, size_t m);
/**
* Executes the server-side response for the AMORE pairing product delegation
* protocol.
*
* @param[out] g - the group elements computed by the server.
* @param[in] ds - the m points for the protocol.
* @param[out] bs - the (l) points for the batch protocol.
* @param[out] gs - the results computed by the server.
* @param[out] ts - the group elements computed by the server.
* @param[in] ds - the (l) points for the batch AMORE protocol.
* @param[in] rs - the (l * m) points for the protocol.
* @param[in] a - the first element in G_1.
* @param[in] b - the first element in G_2.
* @param[in] b - the second element in G_2.
* @param[in] p - the first argument inputs for the pairings.
* @param[in] q - the second argument inputs for the pairings.
* @param[in] l - the number of pairing products to compute.
* @param[in] m - the number of pairings per product to compute.
* @return RLC_OK if no errors occurred, RLC_ERR otherwise.
*/
int cp_amprd_ans(gt_t *gs, const g2_t *ds, const g1_t *fs, const g2_t *bs,
const g1_t a, const g2_t b, const g1_t *p, const g2_t *q,
size_t l, size_t m);
int cp_amprd_ans(gt_t *gs, gt_t *ts, const g1_t *ds, const g1_t *rs,
const g1_t a, const g2_t b, const g2_t d, const g2_t *bs, const g1_t *p,
const g2_t *q, size_t l, size_t m);

/**
* Verifies the result of the AMORE pairing delegation protocol.
*
* @param[out] ts - the results of the computation.
* @param[in] gs - the group elements returned by the server.
* @param[out] gs - the results of the computation.
* @param[in,out] ts - the group elements returned by the server.
* @param[in] ks - the pairing product keys.
* @param[in] cs - the challenges for the batch protocol.
* @param[in] e - the precomputed value e(U1, U2).
* @param[in] l - the number of pairing products to compute.
* @param[in] m - the number of pairings per product to compute.
* @return a boolean value indicating if the computation is correct.
*/
int cp_amprd_ver(gt_t *ts, const gt_t *gs, const bn_t *ks, const bn_t *cs,
const gt_t e, size_t l);
int cp_amprd_ver(gt_t *gs, gt_t *ts, const bn_t *ks, const bn_t *ls,
const gt_t e, size_t l, size_t m);

/**
* Generates a master key for the SOKAKA identity-based non-interactive
Expand Down
Loading

0 comments on commit 38ecab8

Please sign in to comment.