Skip to content

Commit

Permalink
check input ciphertext to encryptn, removed unittests copy for pke to…
Browse files Browse the repository at this point in the history
… identify tests to add and some cleanup (this branch fixes issue #235)
  • Loading branch information
sararv22 committed Jan 31, 2023
1 parent a61b995 commit 5d96039
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1,369 deletions.
9 changes: 4 additions & 5 deletions src/binfhe/examples/pke/boolean-pke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main() {
// and HE standard. Other common options are TOY, MEDIUM, STD192, and STD256.
// MEDIUM corresponds to the level of more than 100 bits for both quantum and
// classical computer attacks.
cc.GenerateBinFHEContext(STD128Q_OPT);//STD128);
cc.GenerateBinFHEContext(STD128);

// Sample Program: Step 2: Key Generation

Expand All @@ -67,10 +67,10 @@ int main() {
// If you wish to get a fresh encryption without bootstrapping, write
// auto ct1 = cc.Encrypt(sk, 1, FRESH);
auto ct1N = cc.EncryptN(keyTriple->publicKey, 1);
auto ct1 = cc.Encryptn(keyTriple->keySwitchingKey, ct1N);
auto ct1 = cc.Encryptn(keyTriple->keySwitchingKey, ct1N);

auto ct2N = cc.EncryptN(keyTriple->publicKey, 1);
auto ct2 = cc.Encryptn(keyTriple->keySwitchingKey, ct2N);
auto ct2 = cc.Encryptn(keyTriple->keySwitchingKey, ct2N);

LWEPlaintext result;

Expand All @@ -79,7 +79,6 @@ int main() {

std::cout << "Result of encrypted ciphertext of 1 = " << result << std::endl;


// Sample Program: Step 4: Evaluation

// Compute (1 AND 1) = 1; Other binary gate options are OR, NAND, and NOR
Expand Down
25 changes: 16 additions & 9 deletions src/binfhe/examples/pke/boolean-serial-binary-pke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,26 @@ int main() {
std::cout << "Generating keys." << std::endl;

// Generating the secret key
auto kt1 = cc1.KeyGenTriple();
auto sk1 = kt1->secretKey;
auto pk1 = kt1->publicKey;
auto kt1 = cc1.KeyGenTriple();
auto sk1 = kt1->secretKey;
auto pk1 = kt1->publicKey;
auto ksk1 = kt1->keySwitchingKey;


auto params = cc1.GetParams()->GetLWEParams();

std::cout << "Q: " << params->GetQ() << std::endl;
std::cout << "N: " << params->GetN() << std::endl;
std::cout << "q: " << params->Getq() << std::endl;
std::cout << "n: " << params->Getn() << std::endl;

// Generate the bootstrapping keys
cc1.BTKeyGen(sk1);

std::cout << "Done generating all keys." << std::endl;

// Encryption for a ciphertext that will be serialized
auto ct1N = cc1.EncryptN(pk1, 1);
auto ct1 = cc1.Encryptn(ksk1, ct1N);
auto ct1 = cc1.Encryptn(ksk1, ct1N);

// CODE FOR SERIALIZATION

Expand All @@ -88,23 +95,23 @@ int main() {
}
std::cout << "The key switching key has been serialized." << std::endl;

// Serializing secret key
// Serializing secret key

if (!Serial::SerializeToFile(DATAFOLDER + "/sk1.txt", sk1, SerType::BINARY)) {
std::cerr << "Error serializing sk1" << std::endl;
return 1;
}
std::cout << "The secret key sk1 key been serialized." << std::endl;

// Serializing public key switching key
// Serializing public key switching key

if (!Serial::SerializeToFile(DATAFOLDER + "/ksk1.txt", ksk1, SerType::BINARY)) {
std::cerr << "Error serializing ksk1" << std::endl;
return 1;
}
std::cout << "The public key switching key ksk1 key been serialized." << std::endl;

// Serializing public key
// Serializing public key

if (!Serial::SerializeToFile(DATAFOLDER + "/pk1.txt", pk1, SerType::BINARY)) {
std::cerr << "Error serializing pk1" << std::endl;
Expand Down Expand Up @@ -185,7 +192,7 @@ int main() {
// OPERATIONS WITH DESERIALIZED KEYS AND CIPHERTEXTS

auto ct2N = cc.EncryptN(pk, 1);
auto ct2 = cc.Encryptn(ksk, ct2N);
auto ct2 = cc.Encryptn(ksk, ct2N);

std::cout << "Running the computation" << std::endl;

Expand Down
26 changes: 15 additions & 11 deletions src/binfhe/lib/binfhecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ void BinFHEContext::GenerateBinFHEContext(BINFHE_PARAMSET set, BINFHE_METHOD met
NativeInteger Q(
PreviousPrime<NativeInteger>(FirstPrime<NativeInteger>(params.numberBits, params.cyclOrder), params.cyclOrder));

usint ringDim = params.cyclOrder / 2;
auto lweparams = (PRIME == params.modKS) ?
std::make_shared<LWECryptoParams>(params.latticeParam, ringDim, params.mod, Q, Q,
usint ringDim = params.cyclOrder / 2;
auto lweparams = (PRIME == params.modKS) ?
std::make_shared<LWECryptoParams>(params.latticeParam, ringDim, params.mod, Q, Q,
params.stdDev, params.baseKS) :
std::make_shared<LWECryptoParams>(params.latticeParam, ringDim, params.mod, Q, params.modKS,
std::make_shared<LWECryptoParams>(params.latticeParam, ringDim, params.mod, Q, params.modKS,
params.stdDev, params.baseKS);
auto rgswparams = std::make_shared<RingGSWCryptoParams>(ringDim, Q, params.mod, params.gadgetBase, params.baseRK,
method, params.stdDev);
Expand Down Expand Up @@ -208,8 +208,8 @@ LWECiphertext BinFHEContext::Encrypt(ConstLWEPrivateKey sk, const LWEPlaintext&
return ct;
}

LWECiphertext BinFHEContext::EncryptN(ConstLWEPublicKey pk, const LWEPlaintext& m,
LWEPlaintextModulus p, NativeInteger mod) const {
LWECiphertext BinFHEContext::EncryptN(ConstLWEPublicKey pk, const LWEPlaintext& m, LWEPlaintextModulus p,
NativeInteger mod) const {
auto& LWEParams = m_params->GetLWEParams();
auto Q = LWEParams->GetQ();
if (mod == 0)
Expand All @@ -222,15 +222,19 @@ LWECiphertext BinFHEContext::EncryptN(ConstLWEPublicKey pk, const LWEPlaintext&

LWECiphertext BinFHEContext::Encryptn(ConstLWESwitchingKey ksk, ConstLWECiphertext ct, BINFHE_OUTPUT output) const {
auto& LWEParams = m_params->GetLWEParams();
//auto Q = LWEParams->GetQ();
//if (mod == 0)
// mod = Q;
auto Q = LWEParams->GetQ();
auto N = LWEParams->GetN();

if ((ct->GetLength() != N) && (ct->GetModulus() != Q)) {
std::string errMsg("ERROR: Ciphertext dimension and modulus are not large N and Q");
OPENFHE_THROW(config_error, errMsg);
}

LWECiphertext ct1 = m_LWEscheme->Encryptn(LWEParams, ksk, ct);

//if ((output != FRESH) && (p == 4)) {
// if ((output != FRESH) && (p == 4)) {
// ct = m_binfhescheme->Bootstrap(m_params, m_BTKey, ct);
//}
// }

return ct1;
}
Expand Down
Loading

0 comments on commit 5d96039

Please # to comment.