github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/StadiumForWaterCarver/src/GoApis.cpp (about) 1 // 2 // Created by wyongcan on 2019/10/14. 3 // 4 5 #include <iostream> 6 #include <vector> 7 8 #include "GoApis.h" 9 #include "ElGammal.h" 10 #include "Utils.h" 11 #include "CurvePoint.h" 12 #include "CipherTable.h" 13 #include "Permutation.h" 14 15 extern G_q G; 16 const int KEY_SIZE = 32; 17 ElGammal *elgammal = nullptr; 18 19 using namespace ::std; 20 21 void printKey(char *data, int len) 22 { 23 for (int i = 0; i < len; i++) 24 { 25 printf("%02x", (unsigned char)data[i]); 26 } 27 } 28 29 void shuffle_gen(char *commitments, int m, int n, char **shuffledCommitments, int *shuffledCommitmentsLen, 30 int **permutation, int *permutationLen, char **proof, int *proofLen) 31 { 32 if (!elgammal) 33 { 34 elgammal = (ElGammal *)create_pub_key_use_dero_H(); 35 } 36 init(); 37 resetM_N(m, n); 38 ZZ mod = G.get_mod(); 39 auto ciphers = new vector<vector<Cipher_elg> *>(getM()); 40 for (int i = 0; i < m; i++) 41 { 42 auto v = new vector<Cipher_elg>(n); 43 for (int j = 0; j < n; j++) 44 { 45 CurvePoint point; 46 point.deserialize(&commitments[(i * n + j) * KEY_SIZE]); 47 (*v)[j] = Cipher_elg(Mod_p(point, mod)); 48 } 49 (*ciphers)[i] = v; 50 } 51 auto cipherTable = new CipherTable(ciphers, m); 52 cipherTable->set_dimentions(m, n); 53 string shuffle_input(cipherTable->encode_all_ciphers()); 54 for (auto &cipher : *ciphers) 55 { 56 delete cipher; 57 } 58 delete ciphers; 59 ciphers = nullptr; 60 delete cipherTable; 61 cipherTable = nullptr; 62 char *input = (char *)shuffle_input.c_str(); 63 char *shuffled_ciphers; 64 int shuffled_ciphers_len; 65 void *cached_shuffle = shuffle_internal(elgammal, input, shuffle_input.size(), m * n, 66 &shuffled_ciphers, &shuffled_ciphers_len, permutation, permutationLen); 67 prove(cached_shuffle, proof, proofLen, nullptr, nullptr); 68 string inp(shuffled_ciphers, shuffled_ciphers_len); 69 cipherTable = new CipherTable(inp, m, elgammal); 70 ciphers = cipherTable->getCMatrix(); 71 m = cipherTable->rows(); 72 n = cipherTable->cols(); 73 *shuffledCommitmentsLen = m * n; 74 *shuffledCommitments = new char[*shuffledCommitmentsLen * KEY_SIZE]; 75 for (int i = 0; i < m; i++) 76 { 77 auto v = ciphers->at(i); 78 for (int j = 0; j < n; j++) 79 { 80 CurvePoint point; 81 point = (*v)[j].get_u(); 82 point.serialize(&((*shuffledCommitments)[(i * n + j) * KEY_SIZE])); 83 // cout << "point" << i * n + j << ":"; 84 // printKey(&((*shuffledCommitments)[(i * n + j) * KEY_SIZE]), 32); 85 // cout << endl; 86 } 87 } 88 delete cipherTable; 89 cipherTable = nullptr; 90 } 91 92 void shuffle_gen_with_regulation(char *commitments, int m, int n, char **shuffledCommitments, 93 int *shuffledCommitmentsLen, char **proof, int *proofLen, int *permutation_in, char *R_in) 94 { 95 if (!elgammal) 96 { 97 elgammal = (ElGammal *)create_pub_key_use_dero_H(); 98 } 99 init(); 100 resetM_N(m, n); 101 ZZ mod = G.get_mod(); 102 auto ciphers = new vector<vector<Cipher_elg> *>(getM()); 103 for (int i = 0; i < m; i++) 104 { 105 auto v = new vector<Cipher_elg>(n); 106 for (int j = 0; j < n; j++) 107 { 108 CurvePoint point; 109 point.deserialize(&commitments[(i * n + j) * KEY_SIZE]); 110 (*v)[j] = Cipher_elg(Mod_p(point, mod)); 111 } 112 (*ciphers)[i] = v; 113 } 114 auto cipherTable = new CipherTable(ciphers, m); 115 cipherTable->set_dimentions(m, n); 116 string shuffle_input(cipherTable->encode_all_ciphers()); 117 for (auto &cipher : *ciphers) 118 { 119 delete cipher; 120 } 121 delete ciphers; 122 ciphers = nullptr; 123 delete cipherTable; 124 cipherTable = nullptr; 125 char *input = (char *)shuffle_input.c_str(); 126 char *shuffled_ciphers; 127 int shuffled_ciphers_len; 128 vector<long> v; 129 v.resize(m * n); 130 for (int i = 0; i < v.size(); i++) 131 { 132 v[i] = permutation_in[i] + 1; 133 } 134 auto pi = new vector<vector<vector<long> *> *>(m); 135 Permutation::perm_matrix(pi, v, n, m); 136 auto R = new vector<vector<ZZ> *>(m); 137 vector<ZZ> *r = 0; 138 long i, j; 139 for (i = 0; i < m; i++) 140 { 141 r = new vector<ZZ>(n); 142 for (j = 0; j < n; j++) 143 { 144 r->at(j) = ZZFromBytes((const unsigned char *)&R_in[(i * n + j) * 32], 32); 145 } 146 R->at(i) = r; 147 } 148 void *cached_shuffle = shuffle_internal(elgammal, input, shuffle_input.size(), m * n, 149 &shuffled_ciphers, &shuffled_ciphers_len, nullptr, nullptr, pi, R); 150 prove(cached_shuffle, proof, proofLen, nullptr, nullptr); 151 string inp(shuffled_ciphers, shuffled_ciphers_len); 152 cipherTable = new CipherTable(inp, m, elgammal); 153 ciphers = cipherTable->getCMatrix(); 154 m = cipherTable->rows(); 155 n = cipherTable->cols(); 156 *shuffledCommitmentsLen = m * n; 157 *shuffledCommitments = new char[*shuffledCommitmentsLen * KEY_SIZE]; 158 for (int i = 0; i < m; i++) 159 { 160 auto v = ciphers->at(i); 161 for (int j = 0; j < n; j++) 162 { 163 CurvePoint point; 164 point = (*v)[j].get_u(); 165 point.serialize(&((*shuffledCommitments)[(i * n + j) * KEY_SIZE])); 166 // cout << "point" << i * n + j << ":"; 167 // printKey(&((*shuffledCommitments)[(i * n + j) * KEY_SIZE]), 32); 168 // cout << endl; 169 } 170 } 171 delete cipherTable; 172 cipherTable = nullptr; 173 } 174 175 int shuffle_ver(char *commitments, int m, int n, char *shuffledCommitments, int shuffledCommitmentsLen, char *proof, 176 int proofLen) 177 { 178 init(); 179 resetM_N(m, n); 180 ZZ mod = G.get_mod(); 181 auto ciphers = new vector<vector<Cipher_elg> *>(getM()); 182 for (int i = 0; i < m; i++) 183 { 184 auto v = new vector<Cipher_elg>(n); 185 for (int j = 0; j < n; j++) 186 { 187 CurvePoint point; 188 point.deserialize(&commitments[(i * n + j) * KEY_SIZE]); 189 (*v)[j] = Cipher_elg(Mod_p(point, mod)); 190 } 191 (*ciphers)[i] = v; 192 } 193 auto cipherTable = new CipherTable(ciphers, m); 194 cipherTable->set_dimentions(m, n); 195 string shuffle_input(cipherTable->encode_all_ciphers()); 196 for (auto &cipher : *ciphers) 197 { 198 delete cipher; 199 } 200 delete ciphers; 201 ciphers = nullptr; 202 delete cipherTable; 203 cipherTable = nullptr; 204 if (shuffledCommitmentsLen % m != 0) 205 { 206 return 0; 207 } 208 n = shuffledCommitmentsLen / m; 209 ciphers = new vector<vector<Cipher_elg> *>(getM()); 210 for (int i = 0; i < m; i++) 211 { 212 auto v = new vector<Cipher_elg>(n); 213 for (int j = 0; j < n; j++) 214 { 215 CurvePoint point; 216 point.deserialize(&shuffledCommitments[(i * n + j) * KEY_SIZE]); 217 (*v)[j] = Cipher_elg(Mod_p(point, mod)); 218 // cout << "point" << i * n + j << ":"; 219 // printKey(&shuffledCommitments[(i * n + j) * KEY_SIZE], 32); 220 // cout << endl; 221 } 222 (*ciphers)[i] = v; 223 } 224 cipherTable = new CipherTable(ciphers, m); 225 cipherTable->set_dimentions(m, n); 226 string shuffle_output(cipherTable->encode_all_ciphers()); 227 for (auto &cipher : *ciphers) 228 { 229 delete cipher; 230 } 231 delete ciphers; 232 ciphers = nullptr; 233 delete cipherTable; 234 cipherTable = nullptr; 235 int ret = verify(1, proof, proofLen, (char *)shuffle_input.c_str(), shuffle_input.size(), 236 (char *)shuffle_output.c_str(), shuffle_output.size(), nullptr, 0); 237 return ret; 238 } 239 240 void deleteCharArray(char *ptr) 241 { 242 delete[] ptr; 243 } 244 245 void deleteIntArray(int *ptr) 246 { 247 delete[] ptr; 248 }