Browse Source

okay actually another refactor, this time to make one final class that collects everything else. I anticipate this will be easier to use when it comes time to start running things using this library

tristangurtler 3 years ago
parent
commit
a06b1672ec
7 changed files with 156 additions and 415 deletions
  1. 35 0
      bgn2/inc/BGN.hpp
  2. 4 2
      bgn2/inc/Scalar.hpp
  3. 0 95
      bgn2/inc/bgn.hpp
  4. 0 10
      bgn2/inc/keygen.hpp
  5. 117 0
      bgn2/src/BGN.cpp
  6. 0 242
      bgn2/src/bgn.cpp
  7. 0 66
      bgn2/src/keygen.cpp

+ 35 - 0
bgn2/inc/BGN.hpp

@@ -0,0 +1,35 @@
+#ifndef __BGN_HPP
+#define __BGN_HPP
+
+#include "Scalar.hpp"
+#include "Bipoint.hpp"
+#include "Quadripoint.hpp"
+#include "PublicKey.hpp"
+#include "PrivateKey.hpp"
+
+class BGN
+{
+	public:
+		BGN();
+
+		void encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const;
+		void encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const;
+		void encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const;
+
+		Bipoint<curvepoint_fp_t> homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const;
+		Bipoint<twistpoint_fp2_t> homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const;
+		Quadripoint homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const;
+		Quadripoint homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const;
+
+		Scalar decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const;
+        Scalar decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const;
+        Scalar decrypt(const Quadripoint& ciphertext) const;
+
+        PublicKey get_public_key() const;
+
+	private:
+		PublicKey public_key;
+		PrivateKey private_key;
+};
+
+#endif /* __BGN_HPP */

+ 4 - 2
bgn2/inc/Scalar.hpp

@@ -1,5 +1,5 @@
-#ifndef __FP_HPP
-#define __FP_HPP
+#ifndef __SCALAR_HPP
+#define __SCALAR_HPP
 
 #include <ostream>
 #include <stdlib.h>
@@ -9,6 +9,8 @@
 #include "Bipoint.hpp"
 #include "Quadripoint.hpp"
 
+#include "scalar.h"
+
 class Scalar
 {
     public:

+ 0 - 95
bgn2/inc/bgn.hpp

@@ -1,95 +0,0 @@
-#ifndef __BGN_HPP
-
-#define __BGN_HPP
-
-
-
-#include <bitset>
-#include <cinttypes>
-#include <cmath>
-#include <typeinfo>
-#include <cxxabi.h>
-#include <stdlib.h>
-#include <time.h>
-#include <chrono>
-#include <limits.h>		/* for CHAR_BIT */
-#include "gengetopt.h"
-#include "typedef.h"
-#include "zout.hpp"
-#include "size.hpp"
-#include "mydouble.h" 
-extern "C" {
-#include "fpe.h"
-}
-#include "quadruplet.hpp"
-#include "curvepoint_fp.h"
-extern "C" {
-#include "fp2e.h"	
-}
-#include "twistpoint_fp2.h"
-#include "Bipoint.hpp"
-#include "BitChiffre.hpp"
-#include "fpe2scalar.hpp"
-#include "chiffrement.hpp"
-#include "circuit_chiffrement.hpp" 
-#include "Fp.hpp"
-#include "keygen.hpp"
-#include "circuit_minmaj.hpp" 
-#include "circuit_additionL1.hpp" 
-#include "circuit_additionL2.hpp" 
-#include "circuit_multiplicationL1.hpp" 
-#include "circuit_multiplicationL1L2.hpp" 
-#include "circuit_additionL3.hpp"
-#include "circuit_multiplicationL2.hpp"
-#include "circuit_additionL4.hpp"
-#include "circuit_size.hpp" 
-#include "circuit_minmaj3.hpp" 
-#include "circuit_minmaj2.hpp" 
-#include "circuit_time.hpp" 
-#include "circuit_demo.hpp"
-#include "circuit_ip.hpp"
-#include "circuit_scalar_product.hpp"
-#include "circuit_time2.hpp" 
-
-
-// sans mydouble.h la cible « ../obj/bgn_check.o » a échouée erreur: conflicting declaration of C function ‘CheckDouble round(const CheckDouble&)’  --> ajouter mydouble.h devant le extern
-// sans extern "C" la cible « bgn » a échouée référence indéfinie vers « fpe_iszero(fpe_struct const*) »
-
-
-// macro pour gengetopt
-#define DEV 0
-#define USR 1
-
-#define MINMAJ 0
-#define CHIFFREMENT 1
-#define ADDITIONL1 2
-#define ADDITIONL2 3
-#define ADDITIONL3 4
-#define ADDITIONL4 5
-#define MULTIPLICATIONL1 6
-#define MULTIPLICATIONL1L2 7
-#define MULTIPLICATIONL2 8
-#define SIZE 9
-#define TIME 10
-#define MINMAJ2 11
-#define MINMAJ3 12
-#define DEMO 13
-#define IP 14 
-#define SCALAR_PRODUCT 15 
-#define TIME2 16
-
-	// BGN-Freeman-CF
-	// en théorie pk:=(p, G, H, G_T, e, g, h, (g^a1,g^b1), (h^a2,h^b2))
-	// en pratique pk:=(p, g, h, a1, b1, a2, b2)	
-	// en sécurité 128 : p et r sont des entiers de 256 bits
-	// on peut fixer p,r et t avec le x0 utilisé par Schwabe
-	// E(Fp)={(x,y) :  y^2 = x^3 + 5, x,y in Fp}
-	// g est un générateur de E(Fp) d'ordre p+1-t 
-	// E[r]={(x,y): y^2 = x^3 + 5, x,y in Fp^12, r*(x,y)=0}
-	// Ker(Pi-p)={(x,y): (x^p,y^p)=p*(x,y), x,y in Fp^12}
-	// h est un générateur de E[r] inter Ker (Pi -p)
-	// a1d1-b1c1 = a2d2-b2c2 = 1 choisis au hasard dans Fp
-	// générer trois éléments non tous nuls (not all zero) au hasard détermine un unique quatrième élément
-
-
-#endif /* __BGN_HPP */

+ 0 - 10
bgn2/inc/keygen.hpp

@@ -1,10 +0,0 @@
-#ifndef __KEYGEN_HPP
-#define __KEYGEN_HPP
-
-#include "Scalar.hpp"
-#include "PrivateKey.hpp"
-#include "PublicKey.hpp"
-
-void keygen(PublicKey& public_key, PrivateKey& private_key);
-
-#endif

+ 117 - 0
bgn2/src/BGN.cpp

@@ -0,0 +1,117 @@
+#include "BGN.hpp"
+
+BGN::BGN()
+{
+	Scalar a1, b1, c1, d1, a2, b2, c2, d2;
+    
+    while (true)
+    {
+        a1.set_random();
+        b1.set_random();
+        c1.set_random();
+
+        if (a1 != 0)
+        {
+            d1 = (b1 * c1 + Scalar(1)) / a1;
+            break;
+        }
+    }
+
+    while (true)
+    {
+        a2.set_random();
+        b2.set_random();
+        c2.set_random();
+
+        if (a2 != 0)
+        {
+            d2 = (b2 * c2 + Scalar(1)) / a2;
+            break;
+        }   
+    }
+
+    Scalar r1, r2, r3, r4;
+    r1.set_random();
+    r2.set_random();
+    r3.set_random();
+    r4.set_random();
+
+    curvepoint_fp_t g_part1, g_part2, g_a1, g_b1;
+    g_part1 = r1 * bn_curvegen;
+    g_part2 = r2 * bn_curvegen;
+    g_a1 = a1 * bn_curvegen;
+    g_b1 = b1 * bn_curvegen;
+    Bipoint<curvepoint_fp_t> full_g(g_part1, g_part2);
+    Bipoint<curvepoint_fp_t> full_g1(g_a1, g_b1);
+
+    twistpoint_fp2_t h_part1, h_part2, h_a2, h_b2;
+    h_part1 = r3 * bn_twistgen;
+    h_part2 = r4 * bn_twistgen;
+    h_a2 = a2 * bn_twistgen;
+    h_b2 = b2 * bn_twistgen;
+    Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
+    Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
+
+    full_g.make_affine();
+    full_g1.make_affine();
+    full_h.make_affine();
+    full_h1.make_affine();
+        
+    public_key.set(full_g, full_h, full_g1, full_h1);
+    private_key.set(public_key, a1, b1, c1, d1, a2, b2, c2, d2);
+}
+
+void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const
+{
+	public_key.encrypt(G_element, cleartext);
+}
+
+void BGN::encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
+{
+	public_key.encrypt(H_element, cleartext);
+}
+
+void BGN::encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
+{
+	public_key.encrypt(G_element, H_element, cleartext);
+}
+
+Bipoint<curvepoint_fp_t> BGN::homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const
+{
+	return public_key.homomorphic_addition(a, b);
+}
+
+Bipoint<twistpoint_fp2_t> BGN::homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
+{
+	return public_key.homomorphic_addition(a, b);
+}
+
+Quadripoint BGN::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
+{
+	return public_key.homomorphic_addition(a, b);
+}
+
+Quadripoint BGN::homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
+{
+	return public_key.homomorphic_multiplication(a, b);
+}
+
+Scalar BGN::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const
+{
+	return private_key.decrypt(ciphertext);
+}
+
+Scalar BGN::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const
+{
+	return private_key.decrypt(ciphertext);
+}
+
+Scalar BGN::decrypt(const Quadripoint& ciphertext) const
+{
+	return private_key.decrypt(ciphertext);
+}
+
+PublicKey BGN::get_public_key() const
+{
+	return public_key;
+}

+ 0 - 242
bgn2/src/bgn.cpp

@@ -1,242 +0,0 @@
-#include "bgn.hpp"
-
-
-
-int main(int argc, char *argv[])
-{
-//signature;	
-/** début chrono **/
-    using Clock = std::chrono::high_resolution_clock;
-    using std::chrono::nanoseconds;
-    using std::chrono::duration_cast;
-
-    auto temps_initial = Clock::now();
-   	double temps_en_nanosecondes;    
-	double temps_en_secondes, temps_en_secondes_par_symbole;
-	
-	//clock_t temps_initial, temps_final;  
-	//    
-	//temps_initial = clock (); /* initial time in microseconds */
-	
-		
-/** gengetopt **/
-
-
-	gengetopt_args_info args_info;
-    if (cmdline_parser(argc, argv, &args_info) != 0)
-	{
-		exit(1) ;
-	}
-
-/** message clair **/	
-	
-	
-	string message_clair;
-	//if (args_info.subtitle_source_arg==DEV)
-	if(!args_info.subtitle_given)
-	{
-		message_clair="~{|`}azertyQWERTY&é,;:!asdfasdfasdfasdfsadfasdfasdfasdfasdfasdfasdfasdfasgfdyeryh bfht gsSLRJslrkgjDLGJDALKDJ:SILFJOIElkdefgwlkerjgtfeswrjg;wrtjhlksdfmhtls/fdkmjhtsltjhmlsrtjhlsfmtjdlj.dtjhmldtjhmmmtlkmtdrlhkmmmmmmhyjldtmrh.lmd.ltlsgrtmglhksdr.tmhdtlkhmmdkrthmlkdrtmld.rkhk.rthlkdrtmhd.rktmh.ldrktmhkr.mdth.lrij"; //clair défini par le dév
-		//message_clair="3\/-7rU$7"; // langage leet avec des caractères spéciaux
-	
-	}
-	else 
-	{
-		message_clair=args_info.subtitle_arg;
-		//cout << "Entrez un message : " << endl;
-		//cin >> message_clair; //clair défini par l'utilisateur
-	}
-	{
-		
-	}
-
-
-	int nombre_symboles=message_clair.length();
-	if (args_info.circuit_arg==MINMAJ  || args_info.circuit_arg==MINMAJ2 || args_info.circuit_arg==MINMAJ3 || args_info.circuit_arg==CHIFFREMENT  || args_info.circuit_arg==TIME) 
-	{
-		zout(nombre_symboles,message_clair);
-	}
-	string evalue_dechiffre;
-	evalue_dechiffre.resize(nombre_symboles);
-	
-	
-
-	//extern PublicKey public_key;
-	//debug(clef privée globale pour debug - à modifier après);
-	PrivateKey private_key;
-	keygen(public_key, private_key);
-	//private_key.get();
-	//public_key.get();
-
-
-
-/** génération d'une graine aléatoire **/
-
-	unsigned int graine_urandom;	
-	FILE * urandom;
-    urandom = fopen ("/dev/urandom","r");
-	if (urandom == NULL)
-	{
-		fprintf(stderr, "Could not open device file /dev/urandom");
-		exit(1);
-	}	
-	fread(&graine_urandom,sizeof(unsigned int),1,urandom);// la graine sert pour creer indirectement un nombre aléatoire via un PRNG
-	// voir test_encrypt.cpp pour la génération d'aléa directement à partir d'urandom
-	fclose (urandom);
-	srand(graine_urandom);
-
-/** chiffrement message clair **/
-
-	if (args_info.circuit_arg==MINMAJ || args_info.circuit_arg==MINMAJ2|| args_info.circuit_arg==MINMAJ3 || args_info.circuit_arg==CHIFFREMENT) 
-	{
-		//	omp_set_num_threads(1);	    //version séquentielle  
-		omp_set_num_threads(omp_get_max_threads());
-		#pragma omp parallel 
-		{
-			#pragma omp single	//sinon l'instruction est executée par chaque thread
-			{
-				zout(omp_get_num_threads()); //le nombre de thread n'est modifié que dans la section parallel
-			}
-				
-			#pragma omp for		
-				for (unsigned int i=0; i<message_clair.length();i++) //pour chaque lettre
-				{
-					if (args_info.circuit_arg==MINMAJ)
-					{
-						circuit_minmaj(evalue_dechiffre[i],message_clair[i],public_key, private_key);	
-					}
-					if (args_info.circuit_arg==MINMAJ2)
-					{
-						circuit_minmaj2(evalue_dechiffre[i],message_clair[i],public_key, private_key);	
-					}
-					if (args_info.circuit_arg==MINMAJ3)
-					{
-						circuit_minmaj3(evalue_dechiffre[i],message_clair[i],public_key, private_key);	
-					}
-					if (args_info.circuit_arg==CHIFFREMENT)
-					{
-						circuit_chiffrement(message_clair[i],public_key, private_key);
-
-					}			
-			}
-		}
-		omp_set_num_threads(1);	
-		if (args_info.circuit_arg==MINMAJ|| args_info.circuit_arg==MINMAJ2 || args_info.circuit_arg==MINMAJ3)
-		{
-			zout(evalue_dechiffre);
-		}
-		else
-		{
-			cout << RESET <<"vérification du chiffrement bit à bit et du déchiffrement du message en mode CURVE et TWIST" << endl;
-		}		
-	}
-	if (args_info.circuit_arg==DEMO)
-	{
-		circuit_demo(evalue_dechiffre[0],message_clair[rand() % nombre_symboles],public_key, private_key);	
-	    cout << "Ce mode est verbeux. Utilisez le mode minmaj pour traiter plus d'un caractère." << endl; 
-	}	
-	if (args_info.circuit_arg==SIZE)
-	{
-		circuit_size(public_key,private_key);
-	}		
-	if (args_info.circuit_arg==TIME)
-	{
-		circuit_time(message_clair,public_key, private_key);
-	}		
-	if (args_info.circuit_arg==ADDITIONL1)
-	{
-		circuit_additionL1(public_key, private_key);
-	}	
-	if (args_info.circuit_arg==ADDITIONL2)
-	{
-		circuit_additionL2(public_key, private_key);
-	}					
-	if (args_info.circuit_arg==ADDITIONL3)
-	{
-		circuit_additionL3(public_key, private_key);
-	}		
-	if (args_info.circuit_arg==ADDITIONL4)
-	{
-		circuit_additionL4(public_key, private_key);
-	}		
-	if (args_info.circuit_arg==MULTIPLICATIONL1)
-	{
-		circuit_multiplicationL1(public_key, private_key);
-	}	
-	if (args_info.circuit_arg==MULTIPLICATIONL1L2)
-	{
-		circuit_multiplicationL1L2(public_key, private_key);
-	}					
-	if (args_info.circuit_arg==MULTIPLICATIONL2)
-	{
-		circuit_multiplicationL2(public_key, private_key);
-	}		
-	if (args_info.circuit_arg==IP)
-	{
-		circuit_ip(public_key, private_key);
-	}	
-	if (args_info.circuit_arg==SCALAR_PRODUCT)
-	{
-		circuit_scalar_product(public_key, private_key);
-	}		
-	if (args_info.circuit_arg==TIME2)
-	{
-		circuit_time2(public_key, private_key);
-	}	
-/** fin chrono **/
-	//temps_final = clock (); /* final time in microseconds */
-	//temps_en_secondes = (temps_final - temps_initial) * 1e-6; /* total time in seconds */ 
-	////cout << BLUE << "running time = " << temps_en_secondes  << " sec" << RESET << endl;
-	//zout(temps_en_secondes) ;
-	//if (args_info.circuit_arg==MINMAJ  || args_info.circuit_arg==MINMAJ2 || args_info.circuit_arg==MINMAJ3|| args_info.circuit_arg==CHIFFREMENT) 
-	//{
-		//temps_en_secondes_par_symbole=temps_en_secondes/nombre_symboles;
-		//zout(temps_en_secondes_par_symbole);
-	//}
-	
-	auto temps_final = Clock::now();
-	temps_en_nanosecondes = duration_cast<nanoseconds>(temps_final-temps_initial).count(); /* total time in nanoseconds */ 
-	temps_en_secondes= temps_en_nanosecondes*1e-9; //on pourrait passer directement en secondes avec chrono mais on perdrait de la précision avec leurs arrondis
-	if (!args_info.circuit_arg==TIME)
-	{
-		zout(temps_en_secondes);
-	}
-	if (args_info.circuit_arg==MINMAJ  || args_info.circuit_arg==MINMAJ2 || args_info.circuit_arg==MINMAJ3|| args_info.circuit_arg==CHIFFREMENT) 
-	{
-		temps_en_secondes_par_symbole=temps_en_secondes/nombre_symboles;
-		zout(temps_en_secondes_par_symbole);
-	}
-		
-	return EXIT_SUCCESS;
-}
-
-
-
-/** chiffrement **/
-
-	//cout << YELLOW << "Encryption" << RESET << endl;
-
-	////mesurer le temps
-	//temps_final = clock (); /* final time in microseconds */
-	//temps_en_secondes = (temps_final - temps_initial) * 1e-6; 			/* total time in seconds */ 
-	//zout(temps_en_secondes) ;
-	//temps_initial = temps_final;
-/** évaluation **/
-
-	//cout << YELLOW << "Homomorphic Evaluation" << RESET << endl;
-
-	////mesurer le temps
-	//temps_final = clock (); /* final time in microseconds */
-	//temps_en_secondes = (temps_final - temps_initial) * 1e-6; 			/* total time in seconds */ 
-	//zout(temps_en_secondes) ;
-	//temps_initial = temps_final;
-	
-/** déchiffrement **/
-
-	//cout << YELLOW << "Decryption" << RESET << endl;
-
-	////mesurer le temps
-	//temps_final = clock (); /* final time in microseconds */
-	//temps_en_secondes = (temps_final - temps_initial) * 1e-6; 			/* total time in seconds */ 
-	//zout(temps_en_secondes) ;
-	//temps_initial = temps_final;
-

+ 0 - 66
bgn2/src/keygen.cpp

@@ -1,66 +0,0 @@
-#include "keygen.hpp"
-
-extern const curvepoint_fp_t bn_curvegen;   
-extern const twistpoint_fp2_t bn_twistgen;
-
-void keygen(PublicKey& public_key, PrivateKey& private_key)
-{
-    Scalar a1, b1, c1, d1, a2, b2, c2, d2;
-    
-    while (true)
-    {
-        a1.set_random();
-        b1.set_random();
-        c1.set_random();
-
-        if (a1 != 0)
-        {
-            d1 = (b1 * c1 + Scalar(1)) / a1;
-            break;
-        }
-    }
-
-    while (true)
-    {
-        a2.set_random();
-        b2.set_random();
-        c2.set_random();
-
-        if (a2 != 0)
-        {
-            d2 = (b2 * c2 + Scalar(1)) / a2;
-            break;
-        }   
-    }
-
-    private_key.set(a1, b1, c1, d1, a2, b2, c2, d2);
-    
-    Scalar r1, r2, r3, r4;
-    r1.set_random();
-    r2.set_random();
-    r3.set_random();
-    r4.set_random();
-
-    curvepoint_fp_t g_part1, g_part2, g_a1, g_b1;
-    g_part1 = r1 * bn_curvegen;
-    g_part2 = r2 * bn_curvegen;
-    g_a1 = a1 * bn_curvegen;
-    g_b1 = b1 * bn_curvegen;
-    Bipoint<curvepoint_fp_t> full_g(g_part1, g_part2);
-    Bipoint<curvepoint_fp_t> full_g1(g_a1, g_b1);
-
-    twistpoint_fp2_t h_part1, h_part2, h_a2, h_b2;
-    h_part1 = r3 * bn_twistgen;
-    h_part2 = r4 * bn_twistgen;
-    h_a2 = a2 * bn_twistgen;
-    h_b2 = b2 * bn_twistgen;
-    Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
-    Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
-
-    full_g.make_affine();
-    full_g1.make_affine();
-    full_h.make_affine();
-    full_h1.make_affine();
-        
-    public_key.set(full_g, full_h, full_g1, full_h1);
-}