@@ -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
5086G2Vec 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