Skip to content

Instantly share code, notes, and snippets.

@pathaine

pathaine/p5.cpp Secret

Last active December 26, 2018 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathaine/b0b50e5e82b4222e0abe661daba80712 to your computer and use it in GitHub Desktop.
Save pathaine/b0b50e5e82b4222e0abe661daba80712 to your computer and use it in GitHub Desktop.
string secretKeyStoragePath = storagePathStr;
secretKeyStoragePath += "/encryption_info_priK.txt";
string publicKeyStoragePath = storagePathStr;
publicKeyStoragePath += "/encryption_info_pubK.txt";
Serialized serializedPublicKey, serializedSecretKey;
// Retrieving secret key
const char *skFile = secretKeyStoragePath.c_str();
FILE* secretKeyFile = fopen(skFile, "r");
if (secretKeyFile == 0)
printf("failed to open %s\n", secretKeyStoragePath.c_str());
char readBufferSK[65536];
FileReadStream sSK(secretKeyFile, readBufferSK, sizeof(readBufferSK));
serializedSecretKey.ParseStream(sSK);
fclose(secretKeyFile);
// Retrieving public key
const char *pkFile = publicKeyStoragePath.c_str();
FILE* publicKeyFile = fopen(pkFile, "r");
char readBufferPK[65536];
FileReadStream sPK(publicKeyFile, readBufferPK, sizeof(readBufferPK));
if (publicKeyFile == 0)
printf("failed to open %s\n", publicKeyStoragePath.c_str());
serializedPublicKey.ParseStream(sPK);
fclose(publicKeyFile);
// Deserializing the secret key and retriving parameters
cc = CryptoContextFactory<DCRTPoly>::DeserializeAndCreateContext(privK);
const auto encodingParams = cc->GetCryptoParameters()->GetEncodingParams();
const auto elementParams = cc->GetCryptoParameters()->GetElementParams();
usint m = elementParams->GetCyclotomicOrder();
PackedEncoding::SetParams(m, encodingParams);
// Deserializing Crypto Keys
LPPrivateKey<DCRTPoly> sk = cc->deserializeSecretKey(serializedSecretKey);
LPPublicKey<DCRTPoly> pk = cc->deserializePublicKey(serializedPublicKey);
if (!sk) {
cerr << "Could not deserialize secret key" << endl;
return 0;
}
if (!pk) {
cerr << "Could not deserialize public key" << endl;
return 0;
}
// Retrieving evaluation keys for multiplication and addition
Serialized ccEmk;
if (!SerializableHelper::ReadSerializationFromFile(storagePathStr + "/key-eval-mult.txt", &ccEmk)) {
cerr << "I cannot read serialization from " << storagePathStr << "/key-eval-mult.txt" << endl;
return 0;
}
Serialized ccEsk;
if (!SerializableHelper::ReadSerializationFromFile(storagePathStr + "/key-eval-sum.txt", &ccEsk)) {
cerr << "I cannot read serialization from " << storagePathStr << "/key-eval-sum.txt" << endl;
return 0;
}
// Deserializing eval keys
cc->DeserializeEvalMultKey(ccEmk);
cc->DeserializeEvalSumKey(ccEsk);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment