Skip to content

Commit c41b9dc

Browse files
committed
allow compression of msk
1 parent af4ee85 commit c41b9dc

File tree

9 files changed

+434
-140
lines changed

9 files changed

+434
-140
lines changed

include/aggre.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "helper.hpp"
3+
#include "crypto.hpp"
44

55
struct AggrePP{
66
// Suppose by default the length is 1.
@@ -15,6 +15,11 @@ struct AggreMsk{
1515
FpVec r;
1616
FpVec b;
1717
FpVec bi;
18+
int d_int = 0;
19+
int r_int = 0;
20+
int b_int = 0;
21+
bool compress;
22+
std::unique_ptr<HMAC> hmac;
1823
};
1924

2025
class Aggre{
@@ -30,9 +35,11 @@ class Aggre{
3035
/**
3136
* Generate master secret key.
3237
* @param pp the public parameters.
38+
* @param key the HMAC key to use.
39+
* @param compress boolean to indicate whether to compress the private keys.
3340
* @return the generated master secret key.
3441
*/
35-
static AggreMsk msk_gen(const AggrePP& pp);
42+
static AggreMsk msk_gen(const AggrePP& pp, const CharVec& key = {}, const bool& compress = false);
3643

3744
/**
3845
* Perform the Aggre FE encryption.

include/filter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct FilterMsk{
2020
int d_int = 0;
2121
int r_int = 0;
2222
int b_int = 0;
23-
int vec_len = 0;
2423
bool compress;
2524
std::unique_ptr<HMAC> hmac;
2625
};
@@ -40,6 +39,7 @@ class Filter{
4039
* Generate master secret key.
4140
* @param pp the public parameters.
4241
* @param key the HMAC key to use.
42+
* @param compress boolean to indicate whether to compress the private keys.
4343
* @return the generated master secret key.
4444
*/
4545
static FilterMsk msk_gen(const FilterPP& pp, const CharVec& key = {}, const bool& compress = false);

include/join.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ struct JoinMsk{
1818
FpVec r;
1919
FpVec b;
2020
FpVec bi;
21+
int k_int = 0;
22+
int d_int = 0;
23+
int r_int = 0;
24+
int b_int = 0;
25+
bool compress;
2126
std::unique_ptr<HMAC> hmac;
2227
};
2328

@@ -36,9 +41,10 @@ class Join{
3641
* Generate master secret key.
3742
* @param pp the public parameters.
3843
* @param key the HMAC key to use.
44+
* @param compress boolean to indicate whether to compress the private keys.
3945
* @return the generated master secret key.
4046
*/
41-
static JoinMsk msk_gen(const JoinPP& pp, const CharVec& key = {});
47+
static JoinMsk msk_gen(const JoinPP& pp, const CharVec& key = {}, const bool& compress = false);
4248

4349
/**
4450
* Perform the Equal-Join encryption.

src/aggre.cpp

Lines changed: 106 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,30 @@ AggrePP Aggre::pp_gen(const int length, const bool pre){
1111
return pp;
1212
}
1313

14-
AggreMsk Aggre::msk_gen(const AggrePP& pp){
14+
AggreMsk Aggre::msk_gen(const AggrePP& pp, const CharVec& key, const bool& compress){
1515
// Create the msk instance.
1616
AggreMsk msk;
17-
18-
// Sample a random point and find its inverse.
19-
msk.d = pp.pairing_group->Zp->rand();
20-
msk.di = pp.pairing_group->Zp->inv(msk.d);
21-
22-
// Sample two random vectors and find one of the inverse.
23-
msk.r = pp.pairing_group->Zp->rand_vec(pp.l);
24-
msk.b = pp.pairing_group->Zp->rand_vec(pp.l);
25-
msk.bi = pp.pairing_group->Zp->vec_inv(msk.b);
26-
17+
// Save whether the values need to be compressed.
18+
msk.compress = compress;
19+
20+
if (compress){
21+
// Sample the starting point.
22+
msk.d_int = Helper::rand_int();
23+
msk.r_int = Helper::rand_int();
24+
msk.b_int = Helper::rand_int();
25+
// Get the unique point for HMAC.
26+
msk.hmac = std::make_unique<HMAC>(key);
27+
}
28+
else{
29+
// Sample a random point and find its inverse.
30+
msk.d = pp.pairing_group->Zp->rand();
31+
msk.di = pp.pairing_group->Zp->inv(msk.d);
32+
33+
// Sample two random vectors and find one of the inverse.
34+
msk.r = pp.pairing_group->Zp->rand_vec(pp.l);
35+
msk.b = pp.pairing_group->Zp->rand_vec(pp.l);
36+
msk.bi = pp.pairing_group->Zp->vec_inv(msk.b);
37+
}
2738
return msk;
2839
}
2940

@@ -34,38 +45,106 @@ G1Vec Aggre::enc(const AggrePP& pp, const AggreMsk& msk, const IntVec& x){
3445
// Sample the random point alpha.
3546
const Fp alpha = pp.pairing_group->Zp->rand();
3647

48+
// Create pointers for values that needs to be used.
49+
const Fp* d;
50+
const FpVec *r, *b;
51+
Fp temp_d;
52+
FpVec temp_r, temp_b;
53+
54+
// In this case, first figure out whether the msk values needs to be sampled.
55+
if (msk.compress){
56+
// Only one value is generated.
57+
temp_d = msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.d_int, 1)[0];
58+
// Sample r and b.
59+
temp_r = msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.r_int, pp.l);
60+
temp_b = msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.b_int, pp.l);
61+
// Assign the pointers with generated values.
62+
d = &temp_d;
63+
r = &temp_r;
64+
b = &temp_b;
65+
}
66+
else{
67+
// Assign the pointers with values from msk.
68+
d = &msk.d;
69+
r = &msk.r;
70+
b = &msk.b;
71+
}
72+
3773
// Compute x + r.
38-
const auto xr = pp.pairing_group->Zp->vec_add(x_vec, msk.r);
74+
const auto xr = pp.pairing_group->Zp->vec_add(x_vec, *r);
3975
// Compute b * (x + r).
40-
const auto bxr = pp.pairing_group->Zp->vec_mul(xr, msk.b);
76+
const auto bxr = pp.pairing_group->Zp->vec_mul(xr, *b);
4177
// Compute alpha * b * (x + r).
4278
auto abxr = pp.pairing_group->Zp->vec_mul(bxr, alpha);
4379
// Add the last point -alpha * delta.
44-
abxr.push_back(pp.pairing_group->Zp->neg(pp.pairing_group->Zp->mul(alpha, msk.d)));
80+
abxr.push_back(pp.pairing_group->Zp->neg(pp.pairing_group->Zp->mul(alpha, *d)));
4581

4682
// Raise the vector to g1 and return.
4783
return pp.pairing_group->Gp->g1_raise(abxr);
4884
}
4985

5086
G2Vec Aggre::keygen(const AggrePP& pp, const AggreMsk& msk, const IntVec& y, int p, const IntVec& sel){
5187
// Convert the input y integer vector to FpVec.
52-
FpVec y_vec = pp.pairing_group->Zp->from_int(y);
88+
const FpVec y_vec = pp.pairing_group->Zp->from_int(y);
5389

54-
// Select r and bi based on the input sel.
55-
FpVec sel_r, sel_bi;
56-
if (sel.empty()){
57-
sel_r = msk.r;
58-
sel_bi = msk.bi;
90+
// Sample the random point beta.
91+
const Fp beta = pp.pairing_group->Zp->rand();
92+
93+
// Create pointers for values that needs to be used and static variables to hold computed values.
94+
const Fp* di;
95+
const FpVec *r, *bi;
96+
Fp temp_di;
97+
FpVec temp_r, temp_bi;
98+
99+
// In this case, first figure out whether the msk values needs to be sampled.
100+
if (msk.compress){
101+
// Only one value is generated.
102+
temp_di = pp.pairing_group->Zp->inv(
103+
msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.d_int, 1)[0]
104+
);
105+
// Sample r and b.
106+
temp_r = msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.r_int, pp.l);
107+
// Compute the bi.
108+
temp_bi = pp.pairing_group->Zp->vec_inv(
109+
msk.hmac->digest_int_to_fp_vec_mod(*pp.pairing_group, msk.b_int, pp.l)
110+
);
111+
112+
// Assign the pointers with generated values.
113+
di = &temp_di;
114+
r = &temp_r;
115+
bi = &temp_bi;
59116
}
60117
else{
61-
for (auto& i : sel){
62-
sel_r.push_back(msk.r[i]);
63-
sel_bi.push_back(msk.bi[i]);
64-
}
118+
// Assign the pointers with values from msk.
119+
di = &msk.di;
120+
r = &msk.r;
121+
bi = &msk.bi;
65122
}
66123

67-
// Sample the random point beta.
68-
const Fp beta = pp.pairing_group->Zp->rand();
124+
// Depends on whether sel is provided, we use the correct set of randomness.
125+
if (sel.empty()){
126+
// Compute b' * y.
127+
const auto biy = pp.pairing_group->Zp->vec_mul(y_vec, *bi);
128+
// Compute beta * b' * y.
129+
auto bbiy = pp.pairing_group->Zp->vec_mul(biy, beta);
130+
// Compute the last point beta * delta' * (p + <r, y>);
131+
auto temp = pp.pairing_group->Zp->vec_ip(y_vec, *r);
132+
temp = pp.pairing_group->Zp->add(temp, Fp(p));
133+
temp = pp.pairing_group->Zp->mul(temp, *di);
134+
temp = pp.pairing_group->Zp->mul(temp, beta);
135+
// Add the last point.
136+
bbiy.push_back(temp);
137+
138+
// Raise the vector to g2 and return.
139+
return pp.pairing_group->Gp->g2_raise(bbiy);
140+
}
141+
142+
// Create the selected vectors.
143+
FpVec sel_r, sel_bi;
144+
for (auto& i : sel){
145+
sel_r.push_back(r->at(i));
146+
sel_bi.push_back(bi->at(i));
147+
}
69148

70149
// Compute b' * y.
71150
const auto biy = pp.pairing_group->Zp->vec_mul(y_vec, sel_bi);
@@ -74,7 +153,7 @@ G2Vec Aggre::keygen(const AggrePP& pp, const AggreMsk& msk, const IntVec& y, int
74153
// Compute the last point beta * delta' * (p + <r, y>);
75154
auto temp = pp.pairing_group->Zp->vec_ip(y_vec, sel_r);
76155
temp = pp.pairing_group->Zp->add(temp, Fp(p));
77-
temp = pp.pairing_group->Zp->mul(temp, msk.di);
156+
temp = pp.pairing_group->Zp->mul(temp, *di);
78157
temp = pp.pairing_group->Zp->mul(temp, beta);
79158
// Add the last point.
80159
bbiy.push_back(temp);

0 commit comments

Comments
 (0)