Skip to content

DamgardJurik Encryption Incorrect Results #71

Open

Description

@lemonviv

I wrote a short program to test DamgardJurik homomorphic encryption operations, but the results seem to be unstable. The code has been enclosed. I compiled the code and ran several times without any modification. The results are as follows, sometimes the result is correct, but sometimes it returns a big number or just corrupts with information "Sizes of ciphertexts do not match".

xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 123
Plaintext addition is: 246
Decrypted plaintext addition integer is: 246
Plaintext multiplication is: 12300
Decrypted plaintext multiplication integer is: 12300
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 857489264902169263442986642532019006464198707881808385057494651564759059744
Plaintext addition is: 246
Decrypted plaintext addition integer is: 246
Plaintext multiplication is: 12300
Decrypted plaintext multiplication integer is: 12300
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 123
Plaintext addition is: 246
Decrypted plaintext addition integer is: 246
Plaintext multiplication is: 12300
Decrypted plaintext multiplication integer is: 12300
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 7613194334867773861088025509117736357398436373830484580326431442625258663984
terminate called after throwing an instance of 'std::invalid_argument'
what(): Sizes of ciphertexts do not match
Aborted (core dumped)
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 123
Plaintext addition is: 246
Decrypted plaintext addition integer is: 1091673282608333264355044839660329762660415024419661640799801461129230991879
Plaintext multiplication is: 12300
Decrypted plaintext multiplication integer is: 12300
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 123
Plaintext addition is: 246
Decrypted plaintext addition integer is: 246
Plaintext multiplication is: 12300
Decrypted plaintext multiplication integer is: 12300
xxx@xxx-OptiPlex-7040:$ ./paillier.o
Original plaintext is: 123
Decrypted plaintext integer is: 123
terminate called after throwing an instance of 'std::invalid_argument'
what(): Sizes of ciphertexts do not match
Aborted (core dumped)

I do not know what is the problem. Any help or advice would be appreciated. Thank you.
Test Code(1).pdf

Activity

  1. changed the title [-]DamgardJurik Homomorphic computation error[/-] [+]DamgardJurik Encryption Incorrect Results[/+] on Oct 4, 2019
  2. liorko87 commented on Oct 22, 2019

    @liorko87
    Contributor

    At void test_enc_and_dec() function this line is not complete at your PDF:
    biginteger r = getRandomInRange(0, dynamic_pointer_cast<DamgardJurikPu

    Please complete it.

  3. lemonviv commented on Oct 23, 2019

    @lemonviv
    Author

    `
    #include <iostream>
    #include <libscapi/include/mid_layer/DamgardJurikEnc.hpp>

    void test_enc_and_dec() {

    std::cout<<"****** Test Encryption and Decryption ******"<<std::endl;
    auto random = get_seeded_prg();
    // create a DamgardJurik encryption object
    DamgardJurikEnc encryptor;
    
    // generate a key pair using the object
    DJKeyGenParameterSpec spec;
    auto pair = encryptor.generateKey(&spec);
    encryptor.setKey(pair.first, pair.second);
    biginteger r = getRandomInRange(0, dynamic_pointer_cast<DamgardJurikPublicKey>(pair.first)->getModulus(), random.get());
    // create a plaintext
    BigIntegerPlainText plaintext("123");
    std::cout<<"Original plaintext is: "<<plaintext.getX()<<std::endl;
    shared_ptr<Plaintext> sharePlaintext = make_shared<BigIntegerPlainText>(plaintext);
    shared_ptr<AsymmetricCiphertext> cipher = encryptor.encrypt(sharePlaintext, r);
    shared_ptr<Plaintext> plain = encryptor.decrypt(cipher.get());
    auto decryptedPlaintext = *(dynamic_cast<BigIntegerPlainText*>(plain.get()));
    //std::cout<<"Decrypted plaintext is: "<<plain.get()<<std::endl;
    std::cout<<"Decrypted plaintext integer is: "<<decryptedPlaintext.getX()<<std::endl;
    

    }

    void test_add_and_mult() {

    std::cout<<"****** Test Homomorphic Addition and Multiplication ******"<<std::endl;
    auto random = get_seeded_prg();
    // create a DamgardJurik encryption object
    DamgardJurikEnc encryptor;
    
    // generate a key pair using the object
    DJKeyGenParameterSpec spec;
    auto pair = encryptor.generateKey(&spec);
    encryptor.setKey(pair.first, pair.second);
    biginteger r1 = getRandomInRange(0, dynamic_pointer_cast<DamgardJurikPublicKey>(pair.first)->getModulus(), random.get());
    biginteger r2 = getRandomInRange(0, dynamic_pointer_cast<DamgardJurikPublicKey>(pair.first)->getModulus(), random.get());
    
    // create a plaintext
    BigIntegerPlainText plaintext("123");
    auto sharePlaintext = make_shared<BigIntegerPlainText>(plaintext);
    auto ciphertext = encryptor.encrypt(sharePlaintext, r1);
    BigIntegerPlainText anotherPlaintext("123");
    auto anotherSharePlaintext = make_shared<BigIntegerPlainText>(anotherPlaintext);
    auto anotherCiphertext = encryptor.encrypt(anotherSharePlaintext, r2);
    
    // homomorphic addition
    auto addCiphertext = encryptor.add(ciphertext.get(), anotherCiphertext.get());
    shared_ptr<Plaintext> plain = encryptor.decrypt(addCiphertext.get());
    auto decryptedPlaintext = *(dynamic_cast<BigIntegerPlainText*>(plain.get()));
    std::cout<<"Plaintext addition is: "<<plaintext.getX() + anotherPlaintext.getX()<<std::endl;
    std::cout<<"Decrypted plaintext addition integer is: "<<decryptedPlaintext.getX()<<std::endl;
    
    // homomorphic multiplication
    // const BigIntegerPlainText constant("321");
    // biginteger* constant = 100;
    biginteger a = 100;
    auto multiCiphertext = encryptor.multByConst(ciphertext.get(), a);
    shared_ptr<Plaintext> multiPlain = encryptor.decrypt(multiCiphertext.get());
    auto decryptedMultiPlaintext = *(dynamic_cast<BigIntegerPlainText*>(multiPlain.get()));
    std::cout<<"Plaintext multiplication is: "<<plaintext.getX() * a<<std::endl;
    std::cout<<"Decrypted plaintext multiplication integer is: "<<decryptedMultiPlaintext.getX()<<std::endl;
    

    }

    void test_fixed_precision_add_and_mult() {

    std::cout<<"****** Test Fixed Precision Addition and Multiplication ******"<<std::endl;
    auto random = get_seeded_prg();
    // create a DamgardJurik encryption object
    DamgardJurikEnc encryptor;
    
    // generate a key pair using the object
    DJKeyGenParameterSpec spec;
    auto pair = encryptor.generateKey(&spec);
    encryptor.setKey(pair.first, pair.second);
    biginteger r = getRandomInRange(0, dynamic_pointer_cast<DamgardJurikPublicKey>(pair.first)->getModulus(), random.get());
    
    // create two float plaintext
    float v1 = 0.0123456, v2 = 98.7654321;
    long fv1 = v1 * 100000000, fv2 = v2 * 100000000;
    //std::cout<<"1e8 = "<<1e8<<std::endl;
    std::cout<<"fv1 = "<<fv1<<", fv2 = "<<fv2<<std::endl;
    std::string sfv1 = std::to_string(fv1);
    BigIntegerPlainText plaintext1(sfv1);
    std::cout<<"Plaintext float multiplication result is: "<<v1 * v2<<std::endl;
    auto plain1 = make_shared<BigIntegerPlainText>(plaintext1);
    auto cipher1 = encryptor.encrypt(plain1, r);
    biginteger plain2 = fv2;
    auto multiCipher = encryptor.multByConst(cipher1.get(), plain2);
    shared_ptr<Plaintext> multiPlain = encryptor.decrypt(multiCipher.get());
    auto decryptedPlaintext = *(dynamic_cast<BigIntegerPlainText*>(multiPlain.get()));
    std::cout<<"Decrypted plaintext integer is: "<<decryptedPlaintext.getX()<<std::endl;
    biginteger decryptedInteger = decryptedPlaintext.getX();
    long mult = decryptedInteger.convert_to<long>();
    float multFloat = mult * 0.00000001 * 0.00000001;
    std::cout<<"Decrypted plaintext multiplication is: "<<multFloat<<std::endl;
    

    }

    int main(int argc, char* argv[]) {
    test_enc_and_dec();
    test_add_and_mult();
    test_fixed_precision_add_and_mult();
    }
    `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions