Преглед на файлове

simple integers BGV example

Rener Oliveira (Ubuntu WSL) преди 2 години
родител
ревизия
9d9188c2a1

BIN
demoData/ciphertext1.txt


BIN
demoData/ciphertext2.txt


BIN
demoData/ciphertext3.txt


+ 224 - 0
demoData/cryptocontext.json

@@ -0,0 +1,224 @@
+{
+    "value0": {
+        "polymorphic_id": 1073741824,
+        "ptr_wrapper": {
+            "id": 2147483649,
+            "data": {
+                "cereal_class_version": 1,
+                "cc": {
+                    "polymorphic_id": 2147483649,
+                    "polymorphic_name": "lbcrypto::CryptoParametersBFVRNS",
+                    "ptr_wrapper": {
+                        "id": 2147483650,
+                        "data": {
+                            "cereal_class_version": 0,
+                            "value0": {
+                                "cereal_class_version": 0,
+                                "value0": {
+                                    "cereal_class_version": 0,
+                                    "value0": {
+                                        "cereal_class_version": 0,
+                                        "elp": {
+                                            "polymorphic_id": 1073741824,
+                                            "ptr_wrapper": {
+                                                "id": 2147483651,
+                                                "data": {
+                                                    "cereal_class_version": 0,
+                                                    "value0": {
+                                                        "cereal_class_version": 0,
+                                                        "co": 16384,
+                                                        "rd": 8192,
+                                                        "2n": true,
+                                                        "cm": {
+                                                            "cereal_class_version": 0,
+                                                            "v": [
+                                                                3220946945,
+                                                                805244932,
+                                                                1275033600,
+                                                                50331648,
+                                                                4294966208,
+                                                                1048575
+                                                            ],
+                                                            "m": 180,
+                                                            "s": 0
+                                                        },
+                                                        "ru": {
+                                                            "v": [
+                                                                0
+                                                            ],
+                                                            "m": 0,
+                                                            "s": 0
+                                                        },
+                                                        "bm": {
+                                                            "v": [
+                                                                0
+                                                            ],
+                                                            "m": 0,
+                                                            "s": 0
+                                                        },
+                                                        "br": {
+                                                            "v": [
+                                                                0
+                                                            ],
+                                                            "m": 0,
+                                                            "s": 0
+                                                        }
+                                                    },
+                                                    "p": [
+                                                        {
+                                                            "polymorphic_id": 1073741824,
+                                                            "ptr_wrapper": {
+                                                                "id": 2147483652,
+                                                                "data": {
+                                                                    "cereal_class_version": 0,
+                                                                    "value0": {
+                                                                        "cereal_class_version": 0,
+                                                                        "co": 16384,
+                                                                        "rd": 8192,
+                                                                        "2n": true,
+                                                                        "cm": {
+                                                                            "cereal_class_version": 0,
+                                                                            "v": 1152921504606830593
+                                                                        },
+                                                                        "ru": {
+                                                                            "v": 25959043411404
+                                                                        },
+                                                                        "bm": {
+                                                                            "v": 0
+                                                                        },
+                                                                        "br": {
+                                                                            "v": 0
+                                                                        }
+                                                                    }
+                                                                }
+                                                            }
+                                                        },
+                                                        {
+                                                            "polymorphic_id": 1073741824,
+                                                            "ptr_wrapper": {
+                                                                "id": 2147483653,
+                                                                "data": {
+                                                                    "value0": {
+                                                                        "co": 16384,
+                                                                        "rd": 8192,
+                                                                        "2n": true,
+                                                                        "cm": {
+                                                                            "v": 1152921504606748673
+                                                                        },
+                                                                        "ru": {
+                                                                            "v": 100406242475323
+                                                                        },
+                                                                        "bm": {
+                                                                            "v": 0
+                                                                        },
+                                                                        "br": {
+                                                                            "v": 0
+                                                                        }
+                                                                    }
+                                                                }
+                                                            }
+                                                        },
+                                                        {
+                                                            "polymorphic_id": 1073741824,
+                                                            "ptr_wrapper": {
+                                                                "id": 2147483654,
+                                                                "data": {
+                                                                    "value0": {
+                                                                        "co": 16384,
+                                                                        "rd": 8192,
+                                                                        "2n": true,
+                                                                        "cm": {
+                                                                            "v": 1152921504606683137
+                                                                        },
+                                                                        "ru": {
+                                                                            "v": 45474351589225
+                                                                        },
+                                                                        "bm": {
+                                                                            "v": 0
+                                                                        },
+                                                                        "br": {
+                                                                            "v": 0
+                                                                        }
+                                                                    }
+                                                                }
+                                                            }
+                                                        }
+                                                    ],
+                                                    "m": {
+                                                        "v": [
+                                                            0
+                                                        ],
+                                                        "m": 0,
+                                                        "s": 0
+                                                    }
+                                                }
+                                            }
+                                        },
+                                        "enp": {
+                                            "polymorphic_id": 1073741824,
+                                            "ptr_wrapper": {
+                                                "id": 2147483655,
+                                                "data": {
+                                                    "cereal_class_version": 0,
+                                                    "m": 65537,
+                                                    "ru": {
+                                                        "v": 0
+                                                    },
+                                                    "bm": {
+                                                        "v": 0
+                                                    },
+                                                    "bru": {
+                                                        "v": 0
+                                                    },
+                                                    "g": 0,
+                                                    "bs": 8192
+                                                }
+                                            }
+                                        }
+                                    },
+                                    "dp": 3.190000057220459,
+                                    "am": 36.0,
+                                    "ns": 1,
+                                    "rw": 0,
+                                    "md": 2,
+                                    "mo": 1,
+                                    "pmo": 1,
+                                    "mmo": 1,
+                                    "exm": 0,
+                                    "dnm": 0,
+                                    "slv": 0,
+                                    "fdp": 0.0
+                                },
+                                "ks": 1,
+                                "rs": 4,
+                                "encs": 0,
+                                "muls": 3,
+                                "dnum": 2,
+                                "ab": 60,
+                                "eb": 0
+                            }
+                        }
+                    }
+                },
+                "kt": {
+                    "polymorphic_id": 2147483650,
+                    "polymorphic_name": "lbcrypto::SchemeBFVRNS",
+                    "ptr_wrapper": {
+                        "id": 2147483656,
+                        "data": {
+                            "cereal_class_version": 0,
+                            "value0": {
+                                "cereal_class_version": 0,
+                                "value0": {
+                                    "cereal_class_version": 0,
+                                    "enabled": 11
+                                }
+                            }
+                        }
+                    }
+                },
+                "si": 2
+            }
+        }
+    }
+}

BIN
demoData/cryptocontext.txt


BIN
demoData/key-eval-rot.txt


BIN
demoData/key-public.txt


BIN
demoData/key-relin.txt


BIN
demoData/key-secret.txt


+ 8 - 1
src/bindings.cpp

@@ -19,7 +19,14 @@ void bind_parameters(py::module &m){
             // getters
             .def("GetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::GetPlaintextModulus)
             .def("GetMultiplicativeDepth", &CCParams<CryptoContextBFVRNS>::GetMultiplicativeDepth);
-    
+    py::class_<CCParams<CryptoContextBGVRNS>, Params>(m, "CCParamsBGVRNS")
+            .def(py::init<>())
+            // setters
+            .def("SetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::SetPlaintextModulus)
+            .def("SetMultiplicativeDepth",&CCParams<CryptoContextBGVRNS>::SetMultiplicativeDepth)
+            // getters
+            .def("GetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::GetPlaintextModulus)
+            .def("GetMultiplicativeDepth", &CCParams<CryptoContextBGVRNS>::GetMultiplicativeDepth);
            
 }
 

+ 92 - 0
src/pke/examples/simple-integers-bgvrns.py

@@ -0,0 +1,92 @@
+# Initial Settings
+from openfhe import *
+
+# Sample Program: Step 1: Set CryptoContext
+parameters = CCParamsBGVRNS()
+parameters.SetPlaintextModulus(65537)
+parameters.SetMultiplicativeDepth(2)
+
+cryptoContext = GenCryptoContext(parameters)
+# Enable features that you wish to use
+cryptoContext.Enable(PKESchemeFeature.PKE)
+cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
+cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
+
+# Sample Program: Step 2: Key Generation
+
+# Generate a public/private key pair
+keypair = cryptoContext.KeyGen()
+
+# Generate the relinearization key
+cryptoContext.EvalMultKeyGen(keypair.secretKey)
+
+# Generate the rotation evaluation keys
+cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
+
+# Sample Program: Step 3: Encryption
+
+# First plaintext vector is encoded
+vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
+
+# Second plaintext vector is encoded
+vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
+plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
+
+# Third plaintext vector is encoded
+vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
+plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
+
+# The encoded vectors are encrypted
+ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
+ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
+ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
+
+#  Sample Program: Step 4: Evaluation
+
+# Homomorphic additions
+ciphertextAdd12 = cryptoContext.EvalAdd(ciphertext1, ciphertext2)
+ciphertextAddResult = cryptoContext.EvalAdd(ciphertextAdd12, ciphertext3)
+
+# Homomorphic Multiplication
+ciphertextMult12 = cryptoContext.EvalMult(ciphertext1, ciphertext2)
+ciphertextMultResult = cryptoContext.EvalMult(ciphertextMult12, ciphertext3)
+
+# Homomorphic Rotations
+ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
+ciphertextRot2 = cryptoContext.EvalRotate(ciphertext1, 2)
+ciphertextRot3 = cryptoContext.EvalRotate(ciphertext1, -1)
+ciphertextRot4 = cryptoContext.EvalRotate(ciphertext1, -2)
+
+# Sample Program: Step 5: Decryption
+
+# Decrypt the result of additions
+plaintextAddResult = Decrypt(ciphertextAddResult,keypair.secretKey)
+
+# Decrypt the result of multiplications
+plaintextMultResult = Decrypt(ciphertextMultResult,keypair.secretKey)
+
+# Decrypt the result of rotations
+plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
+plaintextRot2 = Decrypt(ciphertextRot2,keypair.secretKey)
+plaintextRot3 = Decrypt(ciphertextRot3,keypair.secretKey)
+plaintextRot4 = Decrypt(ciphertextRot4,keypair.secretKey)
+
+
+plaintextRot1.SetLength(len(vectorOfInts1))
+plaintextRot2.SetLength(len(vectorOfInts1))
+plaintextRot3.SetLength(len(vectorOfInts1))
+plaintextRot4.SetLength(len(vectorOfInts1))
+
+print("Plaintext #1: " + str(plaintext1))
+print("Plaintext #2: " + str(plaintext2))
+print("Plaintext #3: " + str(plaintext3))
+
+# Output Results
+print("\nResults of homomorphic computations")
+print("#1 + #2 + #3 = " + str(plaintextAddResult))
+print("#1 * #2 * #3 = " + str(plaintextMultResult))
+print("Left rotation of #1 by 1 = " + str(plaintextRot1))
+print("Left rotation of #1 by 2 = " + str(plaintextRot2))
+print("Right rotation of #1 by 1 = " + str(plaintextRot3))
+print("Right rotation of #1 by 2 = " + str(plaintextRot4))