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

refactoring continues ever further. This won't compile yet, but contains the skeleton for what we actually need to be able to use this in any fashion

tristangurtler преди 3 години
родител
ревизия
7759891eee

+ 42 - 1
bgn2/src/Bipoint.cpp

@@ -1,5 +1,7 @@
 #include "Bipoint.hpp"
 
+extern const scalar_t bn_n;
+
 Bipoint<curvepoint_fp_t>::Bipoint()
 {
 	curvepoint_fp_setneutral(point[0]);
@@ -24,6 +26,46 @@ Bipoint<twistpoint_fp2_t>::Bipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2)
 	twistpoint_fp2_set(point[1], p2);
 }
 
+void Bipoint<curvepoint_fp_t>::receive_encryption(const scalar_t& cleartext, const PublicKey& public_key)
+{
+	scalar_t lambda;
+	scalar_setrandom(lambda, bn_n);
+
+	Bipoint<curvepoint_fp_t> cleartext_as_element, random_mask;
+
+	cleartext_as_element = public_key.get_bipoint_curvegen().scalarmult_vartime(cleartext);
+	cleartext_as_element.makeaffine();
+
+	random_mask = public_key.get_bipoint_curve_groupelt().scalarmult_vartime(lambda);
+	random_mask.makeaffine();
+
+	ciphertext = cleartext_as_element + random_mask;
+	ciphertext.makeaffine();
+
+	point[0] = ciphertext.point[0];
+	point[1] = ciphertext.point[1];
+}
+
+void Bipoint<twistpoint_fp2_t>::receive_encryption(const scalar_t& cleartext, const PublicKey& public_key)
+{
+	scalar_t lambda;
+	scalar_setrandom(lambda, bn_n);
+
+	Bipoint<twistpoint_fp2_t> cleartext_as_element, random_mask;
+
+	cleartext_as_element = public_key.get_bipoint_twistgen().scalarmult_vartime(cleartext);
+	cleartext_as_element.makeaffine();
+
+	random_mask = public_key.get_bipoint_twist_groupelt().scalarmult_vartime(lambda);
+	random_mask.makeaffine();
+
+	ciphertext = cleartext_as_element + random_mask;
+	ciphertext.makeaffine();
+
+	point[0] = ciphertext.point[0];
+	point[1] = ciphertext.point[1];
+}
+
 curvepoint_fp_t& Bipoint<curvepoint_fp_t>::operator[](int n)
 {
 	return point[n];
@@ -96,7 +138,6 @@ bool Bipoint<curvepoint_fp_t>::operator==(const Bipoint<curvepoint_fp_t>& b) con
 	return retval;
 }
 
-
 bool Bipoint<twistpoint_fp2_t>::operator==(const Bipoint<twistpoint_fp2_t>& b) const
 {
 	bool retval = fp2e_iseq(point[0]->m_x, b[0]->m_x);

+ 8 - 1
bgn2/src/Bipoint.hpp

@@ -1,6 +1,9 @@
 #ifndef __BIPOINT_HPP
 #define __BIPOINT_HPP
 
+#include "PublicKey.hpp"
+#include "PrivateKey.hpp"
+
 #include "mydouble.h" 
 extern "C" {
 #include "fpe.h"
@@ -25,6 +28,8 @@ class Bipoint<curvepoint_fp_t>
 		Bipoint();
 		Bipoint(curvepoint_fp_t p1, curvepoint_fp_t p2);
 
+		void receive_encryption(const scalar_t& cleartext, const PublicKey& public_key);
+
 		curvepoint_fp_t& operator[](int n);
 		const curvepoint_fp_t& operator[](int n) const;
 
@@ -44,7 +49,9 @@ class Bipoint<twistpoint_fp2_t>
 {
 	public:
 		Bipoint(); 
-		Bipoint(twistpoint_fp2_t p1,twistpoint_fp2_t p2);	
+		Bipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2);
+
+		receive_encryption(const scalar_t& cleartext, const PublicKey& public_key);
 
 		twistpoint_fp2_t& operator[](int n);
 		const twistpoint_fp2_t& operator[](int n) const;

+ 0 - 76
bgn2/src/BitChiffre.cpp

@@ -1,76 +0,0 @@
-#include "BitChiffre.hpp"
-
-Type BitChiffre::get_type() const
-{
-	return type;
-}
-
-void BitChiffre::print() const
-{
-	cout << "____________" << endl;
-	zout(bit_masque);
-	JUMP;
-	if (type == CURVE)
-	{
-		cout << RED << "si bit_urandom=0 affichage du bipoint u1=(lambda1*i1*g, lambda1*j1*g)" << RESET << endl; 
-		bipoint_curve.print();
-	}
-	if (type == TWIST)
-	{
-		bipoint_twist.print();
-	}
-	cout << "____________" << endl;
-
-}
-
-void BitChiffre::print_bit_masque() const
-{
-	zout(bit_masque);
-}
-
-Bipoint<curvepoint_fp_t> BitChiffre::get_bipoint_curve() const
-{
-		return bipoint_curve;
-}
-
-Bipoint<twistpoint_fp2_t> BitChiffre::get_bipoint_twist() const
-{
-		return bipoint_twist;
-}
-
-BitChiffre::BitChiffre(F2 a,Bipoint<curvepoint_fp_t> b ,Bipoint<twistpoint_fp2_t> c)
-{
-	bit_masque = a;
-	bipoint_curve = b;
-	bipoint_twist = c;
-}
-
-void BitChiffre::set_bit_masque(F2 bit)
-//manipulateur pour modifer attribut si nécessaire, méthode définie en dehors de la classe
-{
-	bit_masque = bit;
-}
-
-
-F2 BitChiffre::get_bit_masque() const
-{
-	return bit_masque;
-}
-
-void BitChiffre::set_bipoint_curve(Bipoint<curvepoint_fp_t> b)
-{
-	bipoint_curve = b;
-	type=CURVE;
-}
-
-void BitChiffre::set_bipoint_twist(Bipoint<twistpoint_fp2_t> c)
-{
-	bipoint_twist = c;
-	type=TWIST;
-}
-
-void BitChiffre::makeaffine()
-{
-	bipoint_curve.makeaffine();
-	bipoint_twist.makeaffine();
-}

+ 0 - 38
bgn2/src/BitChiffre.hpp

@@ -1,38 +0,0 @@
-#ifndef __BITCHIFFRE_HPP
-
-#define __BITCHIFFRE_HPP
-
-#include "typedef.h"
-#include "Bipoint.hpp"
-#include "zout.hpp"
-
-
-class BitChiffre
-{
-	public:
-	
-	BitChiffre() = default; //on remet le constructeur par défaut par défaut qui n'existe plus lorsqu'un constructeur est spécifié
-	BitChiffre(F2 a,Bipoint<curvepoint_fp_t> b ,Bipoint<twistpoint_fp2_t> c);
-	void set_bit_masque(F2); //prototype 
-	F2 get_bit_masque() const;
-	Type get_type() const;
-	Bipoint<curvepoint_fp_t> get_bipoint_curve() const;
-	Bipoint<twistpoint_fp2_t> get_bipoint_twist() const;
-	void set_bipoint_curve(Bipoint<curvepoint_fp_t> b);
-	void set_bipoint_twist(Bipoint<twistpoint_fp2_t> c);
-	void print() const;
-	void print_bit_masque() const;
-	void makeaffine();
-
-	
-	private:
-	
-	F2 bit_masque=0; //initialisation, attribut tjrs privé
-	Type type;		
-	Bipoint<curvepoint_fp_t> bipoint_curve;
-	Bipoint<twistpoint_fp2_t> bipoint_twist;
-};
-
-
-//typedef BitChiffre BitEvalL1; //BitEvalL1 est un alias utilisé pour dénommer le type de la somme homomorphique de chiffrés de niveau 1
-#endif

+ 0 - 34
bgn2/src/BitEvalL1.hpp

@@ -1,34 +0,0 @@
-#ifndef __BITEVALL1_HPP
-
-#define __BITEVALL1_HPP
-
-#include "typedef.h"
-#include "Bipoint.hpp"
-#include "zout.hpp"
-
-
-template<typename T>
-class BitEvalL1
-{
-	public:
-	
-	BitEvalL1() = default; //on remet le constructeur par défaut par défaut qui n'existe plus lorsqu'un constructeur est spécifié
-	BitEvalL1(F2 a,Bipoint<T>);
-	void set_bit_masque(F2); //prototype 
-	F2 get_bit_masque() const;
-	Bipoint<T> get_bipoint() const;
-	void set_bipoint(Bipoint<T> b);
-	void print() const;
-	void print_bit_masque() const;
-	void makeaffine();
-
-	
-	private:
-	
-	F2 bit_masque=0; //initialisation, attribut tjrs privé
-	Bipoint<T> bipoint;
-};
-
-#include "BitEvalL1.tpp" 
-
-#endif

+ 0 - 54
bgn2/src/BitEvalL1.tpp

@@ -1,54 +0,0 @@
-template<typename T>
-void BitEvalL1<T>::print() const
-{
-//	cout << "____________" << endl;
-	zout(bit_masque);
-	JUMP;
-	bipoint.print();
-//	cout << "____________" << endl;
-}
-
-template<typename T>
-void BitEvalL1<T>::print_bit_masque() const
-{
-	zout(bit_masque);
-}
-
-template<typename T>
-Bipoint<T> BitEvalL1<T>::get_bipoint() const
-{
-		return bipoint;
-}
-
-
-template<typename T>
-BitEvalL1<T>::BitEvalL1(F2 a,Bipoint<T> b)
-{
-	bit_masque = a;
-	bipoint = b;
-}
-
-template<typename T>
-void BitEvalL1<T>::set_bit_masque(F2 bit)
-//manipulateur pour modifer attribut si nécessaire, méthode définie en dehors de la classe
-{
-	bit_masque = bit;
-}
-
-template<typename T>
-F2 BitEvalL1<T>::get_bit_masque() const
-{
-	return bit_masque;
-}
-
-template<typename T>
-void BitEvalL1<T>::set_bipoint(Bipoint<T> b)
-{
-	bipoint = b;
-}
-
-template<typename T>
-void BitEvalL1<T>::makeaffine()
-{
-	bipoint.makeaffine();
-}

+ 0 - 40
bgn2/src/BitEvalL2.cpp

@@ -1,40 +0,0 @@
-#include "BitEvalL2.hpp"
-
-
-void BitEvalL2::print() const
-{
-	zout(bit_masque);
-	JUMP;
-	quadripoint.print();
-	JUMP;
-}
-
-void BitEvalL2::print_bit_masque() const
-{
-	zout(bit_masque);
-}
-
-
-
-Quadripoint BitEvalL2::get_quadripoint() const
-{
-		return quadripoint;
-}
-
-
-void BitEvalL2::set_bit_masque(F2 bit)
-//manipulateur pour modifer attribut si nécessaire, méthode définie en dehors de la classe
-{
-	bit_masque = bit;
-}
-
-
-F2 BitEvalL2::get_bit_masque() const
-{
-	return bit_masque;
-}
-
-void BitEvalL2::set_quadripoint(Quadripoint b)
-{
-	quadripoint = b;
-}

+ 0 - 28
bgn2/src/BitEvalL2.hpp

@@ -1,28 +0,0 @@
-#ifndef __BITEVALL2_HPP
-
-#define __BITEVALL2_HPP
-
-#include "typedef.h"
-#include "Quadripoint.hpp"
-#include "zout.hpp"
-
-class BitEvalL2
-{
-	public:
-	
-	BitEvalL2() = default; //on remet le constructeur par défaut par défaut qui n'existe plus lorsqu'un constructeur est spécifié
-	void set_bit_masque(F2); //prototype 
-	F2 get_bit_masque() const;
-	Quadripoint get_quadripoint() const;
-	void set_quadripoint(Quadripoint b);
-	void print() const;
-	void print_bit_masque() const;
-
-	
-	private:
-	
-	F2 bit_masque=0; //initialisation, attribut tjrs privé
-	Quadripoint quadripoint;
-};
-
-#endif

+ 0 - 1
bgn2/src/Fp.cpp

@@ -1,5 +1,4 @@
 #include "Fp.hpp"
-// #include "fpe2scalar.hpp" //problem : Fp does not name a type --> solution :  include fpe2scalar.hpp only in the cpp files
 
 extern const double bn_v;
 

+ 2 - 0
bgn2/src/Fp.hpp

@@ -4,6 +4,8 @@
 #include <ostream>
 #include <sstream>
 #include <random>
+#include <gmp.h> 
+#include <gmpxx.h>
 
 #include "mydouble.h"
 extern "C" {

+ 1 - 0
bgn2/src/Quadripoint.hpp

@@ -20,6 +20,7 @@ class Quadripoint
 
 		Quadripoint operator*(const Quadripoint& b) const;
 		Quadripoint operator^(const scalar_t& exp) const;
+		Quadripoint operator++(int);
 		bool operator==(const Quadripoint& b) const;
 
 		Quadripoint square() const;

+ 0 - 174
bgn2/src/additionL1.cpp

@@ -1,174 +0,0 @@
-#include "additionL1.hpp"
-
-//template <typename T>
-//BitEvalL1<T> additionL1 (BitEvalL1<T> a, BitEvalL1<T> b, PublicKey public_key, Type type)
-//{
-	//BitEvalL1<T> somme;
-	//somme.set_bit_masque((a.get_bit_masque()+b.get_bit_masque())%2);
-	//scalar_t lambda;
-	//scalar_setrandom(lambda, bn_r);	
-	//if (type == CURVE) 
-	//{
-		//Bipoint<curvepoint_fp_t> beta, temp, bipoint_curve_subgroupelt; 
-		
-		
-		////ecris(u+u);
-		////temp=public_key.get_bipoint_curvegen()+public_key.get_bipoint_curvegen();
-		////temp.makeaffine();
-		////temp.print();	
-					
-		////ecris(beta1+beta2);
-		//temp=a.get_bipoint()+b.get_bipoint();
-		//temp.makeaffine();
-		////temp.print();
-		
-		////ecris(pi_1(beta1+beta2));
-		////private_key.pi_1(temp).makeaffine();
-		////private_key.pi_1(temp).print();
-		
-		////ecris(pi_1(u1));
-		//bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-		//bipoint_curve_subgroupelt.makeaffine();
-		////private_key.pi_1(bipoint_curve_subgroupelt).print();
-		
-		////ecris(pi_1(beta1+beta2+u1));
-		//beta=temp+bipoint_curve_subgroupelt; 
-		//beta.makeaffine();	
-		////private_key.pi_1(beta).print();
-		
-		//somme.set_bipoint(beta);
-	//}
-	//if (type == TWIST) 
-	//{
-		//Bipoint<twistpoint_fp2_t> beta, temp, bipoint_twist_subgroupelt;
-		//temp=a.get_bipoint()+b.get_bipoint();
-		//temp.makeaffine();	
-		//bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-		//bipoint_twist_subgroupelt.makeaffine();
-		//beta=temp+bipoint_twist_subgroupelt;
-		//beta.makeaffine();
-		//somme.set_bipoint(beta);
-	//}
-	////a.print_bit_masque();
-	////b.print_bit_masque();
-	////somme.print_bit_masque();
-	//return somme;
-//}
-
-BitEvalL1<curvepoint_fp_t> additionL1 (BitEvalL1<curvepoint_fp_t> a, BitEvalL1<curvepoint_fp_t> b, PublicKey public_key)
-{
-	BitEvalL1<curvepoint_fp_t> somme;
-	somme.set_bit_masque((a.get_bit_masque()+b.get_bit_masque())%2);
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);	
-	Bipoint<curvepoint_fp_t> beta, temp, bipoint_curve_subgroupelt; 
-	
-	
-	//ecris(u+u);
-	//temp=public_key.get_bipoint_curvegen()+public_key.get_bipoint_curvegen();
-	//temp.makeaffine();
-	//temp.print();	
-				
-	//ecris(beta1+beta2);
-	temp=a.get_bipoint()+b.get_bipoint();
-	temp.makeaffine();
-	//temp.print();
-	
-	//ecris(pi_1(beta1+beta2));
-	//private_key.pi_1(temp).makeaffine();
-	//private_key.pi_1(temp).print();
-	
-	//ecris(pi_1(u1));
-	bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-	bipoint_curve_subgroupelt.makeaffine();
-	//private_key.pi_1(bipoint_curve_subgroupelt).print();
-	
-	//ecris(pi_1(beta1+beta2+u1));
-	beta=temp+bipoint_curve_subgroupelt; 
-	beta.makeaffine();	
-	//private_key.pi_1(beta).print();
-	
-	somme.set_bipoint(beta);
-	return somme;
-}
-
-BitEvalL1<twistpoint_fp2_t> additionL1 (BitEvalL1<twistpoint_fp2_t> a, BitEvalL1<twistpoint_fp2_t> b, PublicKey public_key)
-{
-	BitEvalL1<twistpoint_fp2_t> somme;
-	somme.set_bit_masque((a.get_bit_masque()+b.get_bit_masque())%2);
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);	
-	Bipoint<twistpoint_fp2_t> beta, temp, bipoint_twist_subgroupelt;
-	temp=a.get_bipoint()+b.get_bipoint();
-	temp.makeaffine();	
-	bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-	bipoint_twist_subgroupelt.makeaffine();
-	beta=temp+bipoint_twist_subgroupelt;
-	beta.makeaffine();
-	somme.set_bipoint(beta);
-	
-	return somme;
-}
-
-BitChiffre additionL1 (BitChiffre a, BitChiffre b, PublicKey public_key)
-{
-	//signature;
-	if (a.get_type() == b.get_type())
-	{
-		BitChiffre somme;
-		somme.set_bit_masque((a.get_bit_masque()+b.get_bit_masque())%2);
-		scalar_t lambda;
-		scalar_setrandom(lambda, bn_r);	
-		if (a.get_type() == CURVE) 
-		{
-			Bipoint<curvepoint_fp_t> beta, temp, bipoint_curve_subgroupelt; 
-			
-			
-			//ecris(u+u);
-			//temp=public_key.get_bipoint_curvegen()+public_key.get_bipoint_curvegen();
-			//temp.makeaffine();
-			//temp.print();	
-						
-			//ecris(beta1+beta2);
-			temp=a.get_bipoint_curve()+b.get_bipoint_curve();
-			temp.makeaffine();
-			//temp.print();
-			
-			//ecris(pi_1(beta1+beta2));
-			//private_key.pi_1(temp).makeaffine();
-			//private_key.pi_1(temp).print();
-			
-			//ecris(pi_1(u1));
-			bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-			bipoint_curve_subgroupelt.makeaffine();
-			//private_key.pi_1(bipoint_curve_subgroupelt).print();
-			
-			//ecris(pi_1(beta1+beta2+u1));
-			beta=temp+bipoint_curve_subgroupelt; 
-			beta.makeaffine();	
-			//private_key.pi_1(beta).print();
-			
-			somme.set_bipoint_curve(beta);
-		}
-		if (a.get_type() == TWIST) 
-		{
-			Bipoint<twistpoint_fp2_t> beta, temp, bipoint_twist_subgroupelt;
-			temp=a.get_bipoint_twist()+b.get_bipoint_twist();
-			temp.makeaffine();	
-			bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-			bipoint_twist_subgroupelt.makeaffine();
-			beta=temp+bipoint_twist_subgroupelt;
-			beta.makeaffine();
-			somme.set_bipoint_twist(beta);
-		}
-		//a.print_bit_masque();
-		//b.print_bit_masque();
-		//somme.print_bit_masque();
-		return somme;
-	}
-	else
-	{
-		cout << "Problème de type dans additionL1" << endl;
-		exit(0);
-	}
-}

+ 0 - 16
bgn2/src/additionL1.hpp

@@ -1,16 +0,0 @@
-#ifndef __ADDITIONL1_HPP
-
-#define __ADDITIONL1_HPP
-
-#include "BitChiffre.hpp"
-#include "BitEvalL1.hpp"
-#include "keygen.hpp"
-
-//template <typename T>
-//BitEvalL1<T> additionL1 (BitEvalL1<T> a, BitEvalL1<T> b, PublicKey public_key, Type type);
-BitEvalL1<curvepoint_fp_t> additionL1 (BitEvalL1<curvepoint_fp_t> a, BitEvalL1<curvepoint_fp_t> b, PublicKey public_key);
-BitEvalL1<twistpoint_fp2_t> additionL1 (BitEvalL1<twistpoint_fp2_t> a, BitEvalL1<twistpoint_fp2_t> b, PublicKey public_key);
-BitChiffre additionL1 (BitChiffre a, BitChiffre b, PublicKey public_key);
-
-
-#endif /* __ADDITIONL1_HPP */

+ 0 - 30
bgn2/src/additionL2.cpp

@@ -1,30 +0,0 @@
-#include "additionL2.hpp"
-
-BitEvalL2 additionL2 (BitEvalL2 a, BitEvalL2 b, PublicKey public_key)
-{
-	BitEvalL2 somme;
-	somme.set_bit_masque((a.get_bit_masque()+b.get_bit_masque())%2);
-	scalar_t lambda1, lambda2;
-	scalar_setrandom(lambda1, bn_r);	
-	scalar_setrandom(lambda2, bn_r);	
-	Quadripoint beta, temp1, temp2, factor3, factor4;
-	Bipoint<curvepoint_fp_t> bipoint_curve_subgroupelt; 
-	Bipoint<twistpoint_fp2_t> bipoint_twist_subgroupelt;
-	temp1=a.get_quadripoint()*b.get_quadripoint();
-	bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda1);
-	bipoint_curve_subgroupelt.makeaffine();
-	bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda2);
-	bipoint_twist_subgroupelt.makeaffine();	
-	//calcul de  e(u,v1)
-	factor3 = pairing(public_key.get_bipoint_curve_groupelt(),bipoint_twist_subgroupelt);
-	//calcul de e(u1,v)
-	factor4 = pairing(bipoint_curve_subgroupelt,public_key.get_bipoint_twist_groupelt());
-	temp2=factor3*factor4;
-	beta=temp1*temp2; 
-	somme.set_quadripoint(beta);
-	//private_key.pi_T(a.get_quadripoint()).print();
-	//private_key.pi_T(b.get_quadripoint()).print();
-	//private_key.pi_T(factor3).print();
-	//private_key.pi_T(factor4).print();
-	return somme;
-}

+ 0 - 12
bgn2/src/additionL2.hpp

@@ -1,12 +0,0 @@
-#ifndef __ADDITIONL2_HPP
-
-#define __ADDITIONL2_HPP
-
-#include "BitEvalL2.hpp"
-#include "keygen.hpp"
-#include "pairing.hpp" 
-
-BitEvalL2 additionL2 (BitEvalL2 a, BitEvalL2 b, PublicKey public_key);
-
-
-#endif /* __ADDITIONL2_HPP */

+ 0 - 333
bgn2/src/chiffrement.cpp

@@ -1,333 +0,0 @@
-
-
-#include "chiffrement.hpp"
-
-//template <typename T>
-//void chiffrement(BitEvalL1<T>& bit_chiffre,F2 bit_clair, PublicKey public_key, Type type)
-//{
-	////signature;
-	//Bipoint<T> bipoint_groupelt, bipoint_subgroupelt; 
-	//scalar_t lambda;
-	//scalar_setrandom(lambda, bn_r);
-
-	////1ere composante
-	///** calcul des bits clairs, urandom et chiffrés **/		
-	//F2 bit_urandom=rand()%2;
-	//bit_chiffre.set_bit_masque(bit_clair != bit_urandom); 
-	
-	////2eme composante
-	///** calcul sur la courbe BN sur Fp **/
-	//if (type==CURVE)
-	//{
-		//bipoint_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-		//bipoint_subgroupelt.makeaffine();
-		
-		//// calcul de b.u+u_1
-		//if (bit_urandom == 1) 
-		//{			
-			//bit_chiffre.set_bipoint(public_key.get_bipoint_curve_groupelt() + bipoint_subgroupelt); //addition et affectation	
-		//}
-		//else
-		//{
-			//bit_chiffre.set_bipoint(bipoint_subgroupelt); //affectation
-		//}
-	//}
-	
-	///** calcul sur le twist BN sur Fp^2 **/
-	//if (type==TWIST)
-	//{
-		
-		//bipoint_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-		//bipoint_subgroupelt.makeaffine();
-		
-		//// calcul de b.v+v_1
-		//if (bit_urandom == 1) 
-		//{
-			//bit_chiffre.set_bipoint(public_key.get_bipoint_twist_groupelt() + bipoint_subgroupelt); //addition et affectation
-		//}
-		//else
-		//{
-			//bit_chiffre.set_bipoint(bipoint_subgroupelt); //affectation
-		//}
-	//}
-	//bit_chiffre.makeaffine();		
-//}
-
-
-void chiffrement(BitEvalL1<curvepoint_fp_t>& bit_chiffre,F2 bit_clair, PublicKey public_key)
-{
-	//signature;
-	Bipoint<curvepoint_fp_t> bipoint_groupelt, bipoint_subgroupelt; 
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);
-
-	//1ere composante
-	/** calcul des bits clairs, urandom et chiffrés **/		
-	F2 bit_urandom=rand()%2;
-	bit_chiffre.set_bit_masque(bit_clair != bit_urandom); 
-	
-	//2eme composante
-	/** calcul sur la courbe BN sur Fp **/
-	bipoint_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-	bipoint_subgroupelt.makeaffine();
-	
-	// calcul de b.u+u_1
-	if (bit_urandom == 1) 
-	{			
-		bit_chiffre.set_bipoint(public_key.get_bipoint_curve_groupelt() + bipoint_subgroupelt); //addition et affectation	
-	}
-	else
-	{
-		bit_chiffre.set_bipoint(bipoint_subgroupelt); //affectation
-	}
-	bit_chiffre.makeaffine();		
-}
-
-
-void chiffrement(BitEvalL1<twistpoint_fp2_t>& bit_chiffre,F2 bit_clair, PublicKey public_key)
-{
-	//signature;
-	Bipoint<twistpoint_fp2_t> bipoint_groupelt, bipoint_subgroupelt; 
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);
-
-	//1ere composante
-	/** calcul des bits clairs, urandom et chiffrés **/		
-	F2 bit_urandom=rand()%2;
-	bit_chiffre.set_bit_masque(bit_clair != bit_urandom); 
-	
-	//2eme composante
-	/** calcul sur le twist BN sur Fp^2 **/
-	bipoint_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-	bipoint_subgroupelt.makeaffine();
-	
-	// calcul de b.u+u_1
-	if (bit_urandom == 1) 
-	{			
-		bit_chiffre.set_bipoint(public_key.get_bipoint_twist_groupelt() + bipoint_subgroupelt); //addition et affectation	
-	}
-	else
-	{
-		bit_chiffre.set_bipoint(bipoint_subgroupelt); //affectation
-	}
-	bit_chiffre.makeaffine();		
-}
-
-void chiffrement(BitChiffre& bit_chiffre,F2 bit_clair, PublicKey public_key, Type type)
-{
-	//signature;
-	Bipoint<curvepoint_fp_t> bipoint_curve_groupelt, bipoint_curve_subgroupelt; // A, B, C, D, temp;
-	Bipoint<twistpoint_fp2_t> bipoint_twist_groupelt, bipoint_twist_subgroupelt;
-	scalar_t lambda,lambda2;
-	scalar_setrandom(lambda, bn_r);
-	scalar_setrandom(lambda2, bn_r);
-
-	//1ere composante
-	/** calcul des bits clairs, urandom et chiffrés **/		
-	F2 bit_urandom=rand()%2;
-	bit_chiffre.set_bit_masque(bit_clair != bit_urandom); // dans F2 -=+=^=XOR bitwise (!= logical XOR résultat pareil pour 0 et 1, mais l'opérateur bitwitse sur des bool convertit le bool en int, fait l'opération bitwise et reconvertit en bool)       m-b //(bit_clair != bit_urandom)
-	//zout (bit_clair,bit_urandom);
-	//bit_chiffre.print_bit_masque(); 
-	//if (bit_clair==0 && bit_urandom==0) {cout << BOLDRED << "Cas 1" << RESET << endl;}
-	//if (bit_clair==0 && bit_urandom==1) {cout << BOLDRED << "Cas 2" << RESET << endl;}
-	//if (bit_clair==1 && bit_urandom==0) {cout << BOLDRED << "Cas 3" << RESET << endl;}
-	//if (bit_clair==1 && bit_urandom==1) {cout << BOLDRED << "Cas 4" << RESET << endl;}
-	
-	//2eme composante
-	/** calcul sur la courbe BN sur Fp**/
-	if (type==CURVE)
-	{
-		////lambda_1[0]=0;lambda_1[1]=0;lambda_1[2]=0;lambda_1[3]=0; // 0
-		//lambda_1[0]=1;lambda_1[1]=0;lambda_1[2]=0;lambda_1[3]=0; // 1
-		//cout << RED << "affichage du scalar_t lambda_1" << RESET <<endl;
-		//scalar_print(stdout, lambda_1); 
-		//JUMP;
-		//public_key.print();
-		
-		
-		bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-		//D.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda2);
-		//D.makeaffine();
-
-		bipoint_curve_subgroupelt.makeaffine();
-		
-		// calcul de b.u+u_1
-		if (bit_urandom == 1) 
-		{
-			//cout << RED << "affichage du bipoint  u" << RESET << endl;
-			//bipoint_curve_groupelt.print();				
-			//cout << RED << "affichage du bipoint  u1" << RESET << endl;
-			//bipoint_curve_subgroupelt.print();				
-			//A=public_key.get_bipoint_curve_groupelt();
-			//A.makeaffine();
-			//representation(A);
-			//B=bipoint_curve_subgroupelt;
-			//B.makeaffine();
-			//representation(B);
-			//C=A;
-			//ecris(A); //u fixe
-			//A.print();
-			//ecris(pi1(A));
-			//private_key.pi_1(A).makeaffine();
-			//private_key.pi_1(A).print();
-			//ecris(B); //u1
-			//B.print();
-			//ecris(pi1(B));
-			//private_key.pi_1(B).makeaffine();
-			//private_key.pi_1(B).print();
-			//ecris(C); //u fixe
-			//C.print();
-			//ecris(pi1(C));
-			//private_key.pi_1(C).makeaffine();
-			//private_key.pi_1(C).print();
-			//ecris(D); //u'1
-			//D.print();
-			//ecris(pi1(D));
-			//private_key.pi_1(D).makeaffine();
-			//private_key.pi_1(D).print();
-			//ecris(((A+B)+C)+D);
-			//temp=((A+B)+C)+D;
-			//temp.makeaffine();
-			//temp.print();				
-			//ecris((A+(B+C))+D);
-			//temp=((A+(B+C))+D);
-			//temp.makeaffine();
-			//temp.print();
-			//ecris(((B+C)+D)+A);
-			//temp=((B+C)+D)+A;
-			//temp.makeaffine();
-			//temp.print();				
-			//ecris((B+(C+D))+A);
-			//temp=((B+(C+D))+A);
-			//temp.makeaffine();
-			//temp.print();								
-			//ecris((A+B)+(C+D));
-			//temp=(A+B)+(C+D);
-			//temp.makeaffine();
-			//temp.print();
-			//representation(temp);
-			//ecris((C+D)+(A+B));
-			//temp=(C+D)+(A+B);
-			//temp.makeaffine();
-			//temp.print();				
-			//ecris(pi1((A+B)+(C+D)));
-			//private_key.pi_1(temp).makeaffine();
-			//private_key.pi_1(temp).print();
-			//ecris(2A);
-			//curvepoint_fp_double(temp[0],A[0]);
-			//curvepoint_fp_double(temp[1],A[1]);
-			//temp.makeaffine();
-			//temp.print();				
-			//ecris(2A+B+D);
-			//curvepoint_fp_double(temp[0],A[0]);
-			//curvepoint_fp_double(temp[1],A[1]);
-			//temp=temp+B+D;
-			//temp.makeaffine();
-			//temp.print();
-			//ecris(B);
-			//B.makeaffine();
-			//B.print();
-			//ecris(B+B);
-			//temp=B+B;
-			//temp.makeaffine();
-			//temp.print();
-			//ecris(A+C);
-			//temp=(A+C);
-			//temp.makeaffine();
-			//temp.print();
-			//ecris(pi1(A+C));
-			//private_key.pi_1(temp).makeaffine();
-			//private_key.pi_1(temp).print();
-			//ecris(B+D);
-			//temp=(B+D);
-			//temp.makeaffine();
-			//temp.print();				
-			//ecris((A+C)+ (B+D));
-			//temp=((A+C)+ (B+D));
-			//temp.makeaffine();
-			//temp.print();
-			//representation(temp);
-			//ecris(pi1(A+C)+ (B+D));
-			//private_key.pi_1(temp).makeaffine();
-			//private_key.pi_1(temp).print();
-			//bit_chiffre.set_bipoint_curve(bipoint_curve_groupelt + bipoint_curve_subgroupelt); //addition et affectation
-			
-			bit_chiffre.set_bipoint_curve(public_key.get_bipoint_curve_groupelt() + bipoint_curve_subgroupelt); //addition et affectation
-
-			
-		}
-		else
-		{
-			//cout << RED << "affichage du bipoint  u1" << RESET << endl;
-			//bipoint_curve_subgroupelt.print();
-			//cout << BOLDRED << "OBJECTIF POINT A L'INFINI" << RESET << endl;
-			bit_chiffre.set_bipoint_curve(bipoint_curve_subgroupelt); //affectation
-		}
-	}
-	
-	//3eme composante
-	/** calcul sur le twist BN sur Fp^2 **/
-
-	if (type==TWIST)
-	{
-		
-		bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-		bipoint_twist_subgroupelt.makeaffine();
-		
-		// calcul de b.v+v_1
-		if (bit_urandom == 1) 
-		{
-			//bit_chiffre.set_bipoint_twist(bipoint_twist_groupelt + bipoint_twist_subgroupelt); //addition et affectation
-			
-			bit_chiffre.set_bipoint_twist(public_key.get_bipoint_twist_groupelt() + bipoint_twist_subgroupelt); //addition et affectation
-		}
-		else
-		{
-			bit_chiffre.set_bipoint_twist(bipoint_twist_subgroupelt); //affectation
-		}
-	}
-	bit_chiffre.makeaffine();		
-}
-
-
-
-// chiffrement BGN-F utile pour calculer Enc(1) et Enc(s) dans les chiffrés de niveau 2
-void chiffrement(Bipoint<curvepoint_fp_t>& ciphertext,F2 bit_clair, PublicKey public_key)
-{
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);
-	Bipoint<curvepoint_fp_t> bipoint_curve_groupelt, bipoint_curve_subgroupelt;
-	bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda);
-	bipoint_curve_subgroupelt.makeaffine();
-	
-	// calcul de m.u+u_1
-	if (bit_clair == 1) 
-	{
-		ciphertext = public_key.get_bipoint_curve_groupelt() + bipoint_curve_subgroupelt; //addition et affectation	
-	}
-	else
-	{
-		ciphertext = bipoint_curve_subgroupelt; //affectation
-	}
-	ciphertext.makeaffine();
-}
-
-void chiffrement(Bipoint<twistpoint_fp2_t>& ciphertext,F2 bit_clair, PublicKey public_key)
-{
-	scalar_t lambda;
-	scalar_setrandom(lambda, bn_r);
-	Bipoint<twistpoint_fp2_t> bipoint_twist_groupelt, bipoint_twist_subgroupelt;
-	bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda);
-	bipoint_twist_subgroupelt.makeaffine();
-	
-	// calcul de m.v+v_1
-	if (bit_clair == 1) 
-	{
-		ciphertext = public_key.get_bipoint_twist_groupelt() + bipoint_twist_subgroupelt; //addition et affectation	
-	}
-	else
-	{
-		ciphertext = bipoint_twist_subgroupelt; //affectation
-	}	
-	ciphertext.makeaffine();
-}

+ 0 - 27
bgn2/src/chiffrement.hpp

@@ -1,27 +0,0 @@
-#ifndef __CHIFFREMENT_HPP
-
-#define __CHIFFREMENT_HPP
-
-
-#include "BitChiffre.hpp"
-#include "BitEvalL1.hpp"
-#include "keygen.hpp"
-#include "representation.hpp"
-
-//template <typename T>
-//void chiffrement(BitEvalL1<T>& bit_chiffre,F2 bit_clair, PublicKey public_key, Type type);
-void chiffrement(BitEvalL1<curvepoint_fp_t>& bit_chiffre,F2 bit_clair, PublicKey public_key);
-void chiffrement(BitEvalL1<twistpoint_fp2_t>& bit_chiffre,F2 bit_clair, PublicKey public_key);
-
-void chiffrement(BitChiffre& bit_chiffre,F2 bit_clair, PublicKey public_key, Type type);
-void chiffrement(Bipoint<curvepoint_fp_t>& ciphertext,F2 bit_clair, PublicKey public_key);
-void chiffrement(Bipoint<twistpoint_fp2_t>& ciphertext,F2 bit_clair, PublicKey public_key);
-
-extern const scalar_t bn_n;
-extern const curvepoint_fp_t bn_curvegen;	
-extern const twistpoint_fp2_t bn_twistgen;
-	
-	
-#endif /* __CHIFFREMENT_HPP */
-
-

+ 0 - 287
bgn2/src/dechiffrement.cpp

@@ -1,287 +0,0 @@
-#include "dechiffrement.hpp"
-
-
-void dechiffrement(F2& bit_dechiffre, BitEvalL1<curvepoint_fp_t> bit_chiffre, PrivateKey private_key)
-{
-	Bipoint<curvepoint_fp_t> bipoint_pi_1_chiffre, bipoint_pi_1_u, bipoint_pi_1_2u;
-	bipoint_pi_1_chiffre = private_key.pi_1(bit_chiffre.get_bipoint()); 
-	if (fpe_iszero(bipoint_pi_1_chiffre[0]->m_z) && fpe_iszero(bipoint_pi_1_chiffre[1]->m_z))
-	{
-		//cout << "cas log=0" << endl;
-		bit_dechiffre = bit_chiffre.get_bit_masque();
-		return;
-	}
-	bipoint_pi_1_u = private_key.pi_1(public_key.get_bipoint_curve_groupelt());
-	if (bipoint_pi_1_chiffre == bipoint_pi_1_u)
-	{
-		//cout << "cas log=1" << endl;
-		bit_dechiffre = (bit_chiffre.get_bit_masque()+1)%2;
-		return;
-	}
-	bipoint_pi_1_2u=bipoint_pi_1_u+bipoint_pi_1_u;
-	bipoint_pi_1_2u.makeaffine();				
-	if (bipoint_pi_1_chiffre == bipoint_pi_1_2u)
-	{
-		//cout << "cas log=2" << endl; //a améliorer
-		bit_dechiffre = bit_chiffre.get_bit_masque();
-		return;
-	}
-	//cout << "cas log >=3" << endl; //a améliorer et a répercuter dans la seconde fonction dechiffrement	
-	
-	//cout << "cas log>2" << endl;
-	Bipoint<curvepoint_fp_t> mul_log=bipoint_pi_1_2u+bipoint_pi_1_u; 
-	mul_log.makeaffine();
-	int log=3;
-	while(!(bipoint_pi_1_chiffre == mul_log))
-	{
-		mul_log=mul_log+bipoint_pi_1_u;
-		mul_log.makeaffine();	
-		log++;
-		//zout(log);
-	}
-	//zout(log);
-	bit_dechiffre = (bit_chiffre.get_bit_masque()+ log)%2;
-}
-
-void dechiffrement(F2& bit_dechiffre, BitEvalL1<twistpoint_fp2_t> bit_chiffre, PrivateKey private_key) // pour les chiffrés de niveau 1
-{
-	Bipoint<twistpoint_fp2_t> bipoint_pi_2_chiffre, bipoint_pi_2_v, bipoint_pi_2_2v;
-	bipoint_pi_2_chiffre = private_key.pi_2(bit_chiffre.get_bipoint()); //pi_2(bv+v1)
-	if (fp2e_iszero(bipoint_pi_2_chiffre[0]->m_z) && fp2e_iszero(bipoint_pi_2_chiffre[1]->m_z))
-	{
-		//cout << "cas log=0" << endl;
-		bit_dechiffre = bit_chiffre.get_bit_masque();
-		return;
-	}
-	bipoint_pi_2_v = private_key.pi_2(public_key.get_bipoint_twist_groupelt());
-	if (bipoint_pi_2_chiffre == bipoint_pi_2_v)
-	{
-		//cout << "cas log=1" << endl;
-		bit_dechiffre = (bit_chiffre.get_bit_masque()+1)%2;
-		return;
-	}
-	bipoint_pi_2_2v=bipoint_pi_2_v+bipoint_pi_2_v;
-	bipoint_pi_2_2v.makeaffine();				
-	if (bipoint_pi_2_chiffre == bipoint_pi_2_2v)
-	{
-		//cout << "cas log=2" << endl; //a améliorer
-		bit_dechiffre = bit_chiffre.get_bit_masque();
-		return;
-	}
-	Bipoint<twistpoint_fp2_t> mul_log=bipoint_pi_2_2v+bipoint_pi_2_v; 
-	mul_log.makeaffine();
-	int log=3;
-	while(!(bipoint_pi_2_chiffre == mul_log))
-	{
-		mul_log=mul_log+bipoint_pi_2_v;
-		mul_log.makeaffine();	
-		log++;
-		//zout(log);
-	}
-	//zout(log);
-	bit_dechiffre = (bit_chiffre.get_bit_masque()+ log)%2;
-}
-
-
-void dechiffrement(F2& bit_dechiffre, BitChiffre bit_chiffre, PrivateKey private_key, Type type)
-{
-	//signature;
-	if (type == CURVE)
-	{	
-		Bipoint<curvepoint_fp_t> bipoint_pi_1_chiffre, bipoint_pi_1_u, bipoint_pi_1_2u;
-		//cout << RED << "affichage du bit chiffré (le bit a et le bipoint bu+u1)" << RESET << endl;
-		//bit_chiffre.print_bit_chiffre(CURVE);
-		bipoint_pi_1_chiffre = private_key.pi_1(bit_chiffre.get_bipoint_curve());  //pi_1(bu+u1)
-		//cout << RED << "affichage du bipoint  pi_1(bu+u1)" << RESET << endl;
-		//bipoint_pi_1_chiffre.print();	
-		//public_key.print();	
-		//cout << RED << "affichage du bipoint  pi_1(u)" << RESET << endl;
-		//bipoint_pi_1_u.print();	
-		//cout << RED << "affichage du bipoint  pi_1(2u)=2pi_1(u)=pi_1(u)+pi_1(u)" << RESET << endl;
-		
-		//(bipoint_pi_1_2u).print();			
-		//si le bipoint est (0,0) alors log=0 sinon log=1, bitdecode=bitmasque+log	
-		////bit_chiffre.print_bit_masque();
-		//bit_chiffre.get_bipoint_curve().print();
-		//bipoint_pi_1_chiffre.print();		
-		if (fpe_iszero(bipoint_pi_1_chiffre[0]->m_z) && fpe_iszero(bipoint_pi_1_chiffre[1]->m_z))
-		{
-			//cout << "cas log=0" << endl;
-			bit_dechiffre = bit_chiffre.get_bit_masque();
-			return;
-		}
-		bipoint_pi_1_u = private_key.pi_1(public_key.get_bipoint_curve_groupelt()); 
-		if (bipoint_pi_1_chiffre == bipoint_pi_1_u)
-		{
-			//cout << "cas log=1" << endl;
-			bit_dechiffre = (bit_chiffre.get_bit_masque()+1)%2;
-			return;
-		}
-		bipoint_pi_1_2u=bipoint_pi_1_u+bipoint_pi_1_u;
-		bipoint_pi_1_2u.makeaffine();				
-		if (bipoint_pi_1_chiffre == bipoint_pi_1_2u)
-		{
-			//cout << "cas log=2" << endl; //a améliorer
-			bit_dechiffre = bit_chiffre.get_bit_masque();
-			return;
-		}
-		//cout << "cas log >=3" << endl; //a améliorer et a répercuter dans la seconde fonction dechiffrement
-		Bipoint<curvepoint_fp_t> mul_log=bipoint_pi_1_2u+bipoint_pi_1_u; 
-		mul_log.makeaffine();
-		int log=3;
-		while(!(bipoint_pi_1_chiffre == mul_log))
-		{
-			mul_log=mul_log+bipoint_pi_1_u;
-			mul_log.makeaffine();	
-			log++;
-			//zout(log);
-		}
-		//zout(log);
-		bit_dechiffre = (bit_chiffre.get_bit_masque()+ log)%2;		
-	}
-	if (type == TWIST)
-	{
-		Bipoint<twistpoint_fp2_t> bipoint_pi_2_chiffre, bipoint_pi_2_v, bipoint_pi_2_2v;
-		bipoint_pi_2_chiffre = private_key.pi_2(bit_chiffre.get_bipoint_twist()); //pi_2(bv+v1)
-		//bipoint_pi_2_v = private_key.pi_2(public_key.get_bipoint_twist_groupelt());
-		//bipoint_pi_2_chiffre.print_point(0);
-		//jump;
-		//bipoint_pi_2_chiffre.print_point(1);
-		//JUMP;
-		//bipoint_pi_2_v.print_point(0);
-		//jump;
-		//bipoint_pi_2_v.print_point(1);
-		//JUMP;
-		//zout(bit_chiffre.get_bit_masque());
-		//bit_chiffre.print_bit_masque();
-		if (fp2e_iszero(bipoint_pi_2_chiffre[0]->m_z) && fp2e_iszero(bipoint_pi_2_chiffre[1]->m_z))
-		{
-			//cout << "cas log=0" << endl;
-			bit_dechiffre = bit_chiffre.get_bit_masque();
-			return;
-		}
-		bipoint_pi_2_v = private_key.pi_2(public_key.get_bipoint_twist_groupelt());
-		if (bipoint_pi_2_chiffre == bipoint_pi_2_v)
-		{
-			//cout << "cas log=1" << endl;
-			bit_dechiffre = (bit_chiffre.get_bit_masque()+1)%2;
-			return;
-		}
-		bipoint_pi_2_2v=bipoint_pi_2_v+bipoint_pi_2_v;
-		bipoint_pi_2_2v.makeaffine();				
-		if (bipoint_pi_2_chiffre == bipoint_pi_2_2v)
-		{
-			//cout << "cas log=2" << endl; //a améliorer
-			bit_dechiffre = bit_chiffre.get_bit_masque();
-			return;
-		}
-		Bipoint<twistpoint_fp2_t> mul_log=bipoint_pi_2_2v+bipoint_pi_2_v; 
-		mul_log.makeaffine();
-		int log=3;
-		while(!(bipoint_pi_2_chiffre == mul_log))
-		{
-			mul_log=mul_log+bipoint_pi_2_v;
-			mul_log.makeaffine();	
-			log++;
-			//zout(log);
-		}
-		//zout(log);
-		bit_dechiffre = (bit_chiffre.get_bit_masque()+ log)%2;
-	}
-}
-
-
-void dechiffrement(F2& bit_dechiffre, Bipoint<curvepoint_fp_t> bipoint, PrivateKey private_key) 
-//routine pour les évalués de niveau 3 et 4, déchiffrement sans Catalano Fiore, calcul d'un log seulement, prend en entrée un bipoint de type curve (inutile de traiter le cas twist car on peut évaluer les circuits de niveau 3 et 4 en traitant seuelement un des deux types, le type Curve opère dans Fp, les opérations sont moins couteuses) et non pas un chiffré de niveau 1
-{
-	Bipoint<curvepoint_fp_t> bipoint_pi_1_chiffre, bipoint_pi_1_u, bipoint_pi_1_2u;
-	bipoint_pi_1_chiffre = private_key.pi_1(bipoint);  //pi_1(bu+u1)
-	if (fpe_iszero(bipoint_pi_1_chiffre[0]->m_z) && fpe_iszero(bipoint_pi_1_chiffre[1]->m_z))
-	{
-		//cout << "cas log=0" << endl;
-		bit_dechiffre = 0;
-	}
-	else
-	{
-		bipoint_pi_1_u = private_key.pi_1(public_key.get_bipoint_curve_groupelt()); 
-		if (bipoint_pi_1_chiffre == bipoint_pi_1_u)
-		{
-			//cout << "cas log=1" << endl;
-			bit_dechiffre = 1;
-		}
-		else 
-		{
-			bipoint_pi_1_2u=bipoint_pi_1_u+bipoint_pi_1_u;
-			bipoint_pi_1_2u.makeaffine();				
-			if (bipoint_pi_1_chiffre == bipoint_pi_1_2u)
-			{
-				//cout << "cas log=2" << endl; //a améliorer
-				bit_dechiffre = 0;
-			}
-			else
-			{
-				//cout << "cas log >=3" << endl; //a améliorer et a répercuter dans la seconde fonction dechiffrement
-				Bipoint<curvepoint_fp_t> mul_log=bipoint_pi_1_2u+bipoint_pi_1_u; 
-				mul_log.makeaffine();
-				int log=3;
-				while(!(bipoint_pi_1_chiffre == mul_log))
-				{
-					mul_log=mul_log+bipoint_pi_1_u;
-					mul_log.makeaffine();	
-					log++;
-					//zout(log);
-				}
-				//zout(log);
-				bit_dechiffre = log%2;				
-			}
-		}
-	}	
-}
-
-
-void dechiffrement(F2& bit_dechiffre, Bipoint<twistpoint_fp2_t> bipoint, PrivateKey private_key) 
-//finalement, on fait le cas twist pour aider au debug
-{
-	Bipoint<twistpoint_fp2_t> bipoint_pi_2_chiffre, bipoint_pi_2_v, bipoint_pi_2_2v;
-	bipoint_pi_2_chiffre = private_key.pi_2(bipoint);  //pi_2(bu+u1)
-	if (fp2e_iszero(bipoint_pi_2_chiffre[0]->m_z) && fp2e_iszero(bipoint_pi_2_chiffre[1]->m_z))
-	{
-		//cout << "cas log=0" << endl;
-		bit_dechiffre = 0;
-	}
-	else
-	{
-		bipoint_pi_2_v = private_key.pi_2(public_key.get_bipoint_twist_groupelt()); 
-		if (bipoint_pi_2_chiffre == bipoint_pi_2_v)
-		{
-			//cout << "cas log=1" << endl;
-			bit_dechiffre = 1;
-		}
-		else 
-		{
-			bipoint_pi_2_2v=bipoint_pi_2_v+bipoint_pi_2_v;
-			bipoint_pi_2_2v.makeaffine();				
-			if (bipoint_pi_2_chiffre == bipoint_pi_2_2v)
-			{
-				//cout << "cas log=2" << endl; //a améliorer
-				bit_dechiffre = 0;
-			}
-			else
-			{
-				//cout << "cas log >=3" << endl; //a améliorer et a répercuter dans la seconde fonction dechiffrement
-				Bipoint<twistpoint_fp2_t> mul_log=bipoint_pi_2_2v+bipoint_pi_2_v; 
-				mul_log.makeaffine();
-				int log=3;
-				while(!(bipoint_pi_2_chiffre == mul_log))
-				{
-					mul_log=mul_log+bipoint_pi_2_v;
-					mul_log.makeaffine();	
-					log++;
-					//zout(log);
-				}
-				//zout(log);
-				//bit_dechiffre = log%2;
-			}
-		}
-	}	
-}

+ 0 - 16
bgn2/src/dechiffrement.hpp

@@ -1,16 +0,0 @@
-#ifndef __DECHIFFREMENT_HPP
-
-#define __DECHIFFREMENT_HPP
-
-#include "BitChiffre.hpp"
-#include "BitEvalL1.hpp"
-#include "keygen.hpp"
-
-void dechiffrement(F2& bit_dechiffre, BitEvalL1<curvepoint_fp_t> bit_chiffre, PrivateKey private_key); // pour les chiffrés de niveau 1
-void dechiffrement(F2& bit_dechiffre, BitEvalL1<twistpoint_fp2_t> bit_chiffre, PrivateKey private_key); // pour les chiffrés de niveau 1
-void dechiffrement(F2& bit_dechiffre, BitChiffre bit_chiffre, PrivateKey private_key, Type type); // pour les chiffrés de niveau 1
-void dechiffrement(F2& bit_dechiffre, Bipoint<curvepoint_fp_t> bipoint, PrivateKey private_key); // routine pour les évalués de niveau 3 et 4
-void dechiffrement(F2& bit_dechiffre, Bipoint<twistpoint_fp2_t> bipoint, PrivateKey private_key); // routine pour les évalués de niveau 3 et 4
-
-
-#endif /* __DECHIFFREMENT_HPP */

+ 74 - 0
bgn2/src/decryption.cpp

@@ -0,0 +1,74 @@
+#include "decryption.hpp"
+
+int decrypt(const Bipoint<curvepoint_fp_t>& ciphertext, const PublicKey& public_key, const PrivateKey& private_key)
+{
+	static std::unordered_map<Bipoint<curvepoint_fp_t>, int> memoizer;
+	static int max_checked = 0;
+	static Bipoint<curvepoint_fp_t> pi_1_curvegen = private_key.pi_1(public_key.get_bipoint_curvegen());
+
+	Bipoint<curvepoint_fp_t> pi_1_ciphertext = private_key.pi_1(ciphertext); 
+
+	auto lookup = memoizer.find(pi_1_ciphertext);
+	if (lookup != memoizer.end())
+	{
+		return lookup->second;
+	}
+
+	Bipoint<curvepoint_fp_t> i = pi_1_curvegen * max_checked;
+	do
+	{
+		memoizer[pi_1_ciphertext] = max_checked++;
+		i = i + pi_1_curvegen;
+	} while (i != pi_1_ciphertext);
+
+	return max_checked - 1;
+}
+
+int decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext, const PrivateKey& private_key) // pour les chiffrés de niveau 1
+{
+	static std::unordered_map<Bipoint<twistpoint_fp2_t>, int> memoizer;
+	static int max_checked = 0;
+	static Bipoint<twistpoint_fp2_t> pi_2_twistgen = private_key.pi_2(public_key.get_bipoint_twistgen());
+
+	Bipoint<twistpoint_fp2_t> pi_2_ciphertext = private_key.pi_2(ciphertext); 
+
+	auto lookup = memoizer.find(pi_2_ciphertext);
+	if (lookup != memoizer.end())
+	{
+		return lookup->second;
+	}
+
+	Bipoint<twistpoint_fp2_t> i = pi_2_twistgen * max_checked;
+	do
+	{
+		memoizer[pi_2_ciphertext] = max_checked++;
+		i = i + pi_2_twistgen;
+	} while (i != pi_2_ciphertext);
+
+	return max_checked - 1;
+}
+
+
+void decrypt(const Quadripoint& ciphertext, const PrivateKey& private_key)
+{
+	static std::unordered_map<Quadripoint, int> memoizer;
+	static int max_checked = 0;
+	static Quadripoint pi_T_pairgen = private_key.pi_T(pairing(public_key.get_bipoint_curvegen(), public_key.get_bipoint_twistgen()));
+
+	Quadripoint pi_T_ciphertext = private_key.pi_T(ciphertext); 
+
+	auto lookup = memoizer.find(pi_T_ciphertext);
+	if (lookup != memoizer.end())
+	{
+		return lookup->second;
+	}
+
+	Quadripoint i = pi_T_pairgen ^ max_checked;
+	do
+	{
+		memoizer[pi_2_ciphertext] = max_checked++;
+		i = i * pi_T_pairgen;
+	} while (i != pi_T_ciphertext);
+
+	return max_checked - 1;
+}

+ 15 - 0
bgn2/src/decryption.hpp

@@ -0,0 +1,15 @@
+#ifndef __DECRYPTION_HPP
+#define __DECRYPTION_HPP
+
+#include <unordered_map>
+
+#include "Bipoint.hpp"
+#include "Quadripoint.hpp"
+#include "PrivateKey.hpp"
+#include "pairing.hpp"
+
+int decrypt(const Bipoint<curvepoint_fp_t>& ciphertext, const PrivateKey& private_key);
+int decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext, const PrivateKey& private_key);
+int decrypt(const Quadripoint& ciphertext, const PrivateKey& private_key);
+
+#endif /* __DECRYPTION_HPP */

+ 0 - 18
bgn2/src/error.h

@@ -1,18 +0,0 @@
-/**
- *   définition des symboles de gestion d'erreurs
- **/
-
-
-#ifndef __ERROR_H
-#define __ERROR_H
-
-#define SUCCESS									0
-#define ERROR_UNDEFINED							1
-#define ERROR_MESSAGE_TOO_BIG					2
-#define ERROR_MESSAGE_NOT_A_VALID_STRING		3
-#define ERROR_CIPHER_TOO_BIG					4
-#define ERROR_WRONG_PUBLIC_KEY_FORMAT			5
-#define ERROR_WRONG_SECRET_KEY_FORMAT			6
-
-
-#endif /* __ERROR_H */

+ 0 - 261
bgn2/src/fpe2scalar.cpp

@@ -1,261 +0,0 @@
-#include "fpe2scalar.hpp"
-
-unsigned long long mpz2ull (mpz_class n)
-{
-  stringstream str;
-  str << n;
-  unsigned long long ull;
-  str >> ull;
-  return ull;
-}
-
-mpz_class  ull2mpz (unsigned long long n)
-{
-  stringstream str;
-  str << n;
-  mpz_class ull;
-  str >> ull;
-  return ull;
-}
-
-mpz_class fpe2mpz(const fpe_t op)
-{
-	
-	
-	//fpe_print(stdout,op);
-	//jump;
-	extern const double bn_v;
-	//zout(bn_v);
-	//for (int i=0;i<12;i++)
-	//{
-		//cout << todouble(op->v[i]) << " " ;
-	//}	
-	//cout << endl;
-	
-	
-	
-	
-	mpz_class poly_at_one=1., increment_factor=6* bn_v;
-	for (int i=0;i<12;i++)
-	{	
-		if (i==0)
-		{
-			poly_at_one=todouble(op->v[0]); 
-			cout.precision ( 15 ); //NB
-			////zout(std::numeric_limits<double>::digits10);
-		}
-		if (i>=1 && i<=6)  {poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor*=bn_v;}
-		if (i==7) {increment_factor*=6.; poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor*=bn_v;}
-		if (i>=8 && i<=11) {poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor*=bn_v;}
-
-
-		//if (i<11) {zout(increment_factor);}
-	}
-	//zout(poly_at_one);
-	
-			
-	
-	///** Méthode fausse 0: division ceil mpz_t, increment_factor=6*bn_v=11208198 l'incrément final est ok avec ce type de division mais on ne part pas du bon incrément au départ. Il faut multiplier, additionner des mpz_class pas des doubles pour le calculer correctement. **/
-	//mpz_t v;
-	//mpz_init_set_str (v, "1868033", 0);
-	//mpz_t six;
-	//mpz_init_set_str (six, "6", 0);
-	//mpz_class poly_at_one=1;
-	//mpz_class increment_factor=36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v; // l'erreur est ici, on peut pas définir un grand nombre en faisant des opérations sur les doubles
-	//zout(increment_factor);
-	//for (int i=11;i>=0;i--)
-	//{	
-		//if (i>=8 && i<=11) {poly_at_one+=increment_factor*todouble(op->v[i]); mpz_cdiv_q(increment_factor.get_mpz_t(), increment_factor.get_mpz_t(), v);}
-		//if (i==7) {mpz_cdiv_q(increment_factor.get_mpz_t(), increment_factor.get_mpz_t(), six); poly_at_one+=increment_factor*todouble(op->v[i]); mpz_cdiv_q(increment_factor.get_mpz_t(), increment_factor.get_mpz_t(), v);}
-		//if (i>=2 && i<=6) {poly_at_one+=increment_factor*todouble(op->v[i]); mpz_cdiv_q(increment_factor.get_mpz_t(), increment_factor.get_mpz_t(), v);}
-		//if (i==1)  {poly_at_one+=increment_factor*todouble(op->v[i]);}
-		//if (i==0) {poly_at_one+=todouble(op->v[0]);  }
-		//if (i>1) {zout(increment_factor);}
-	//}
-	//zout(poly_at_one);	
-	//mpz_clear(v);
-	//mpz_clear(six);
-	
-		
-	/** Méthode fausse 1 Ca marche presque avec / la division tronquée, mpz_class. increment_factor=11208197 **/
-	//mpz_class poly_at_one=1;
-	//mpz_class increment_factor=36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v;
-	//for (int i=11;i>=0;i--)
-	//{	
-		//if (i>=8 && i<=11) {poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor/=bn_v;}
-		//if (i==7) {increment_factor/=6; poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor/=bn_v;}
-		//if (i>=2 && i<=6) {poly_at_one+=increment_factor*todouble(op->v[i]); increment_factor/=bn_v;}
-		//if (i==1)  {poly_at_one+=increment_factor*todouble(op->v[i]);}
-		//if (i==0) {poly_at_one+=todouble(op->v[0]);  }
-		//zout(poly_at_one,increment_factor);
-	//}
-	//zout(poly_at_one);	
-	
-	///** Méthode fausse numéro 2 pour calculer poly_at_one. Tous les opérandes sont des doubles, le résultat est incorrect, la précision est inférieur à 1 sur des doubles de plus de 53 bits. Il faut additionner des mpz_class. **/
-	//mpz_class poly_at_one = 
-	//todouble(op->v[0])  +
-	//todouble(op->v[1])  *  6 * bn_v +
-	//todouble(op->v[2])  *  6 * bn_v * bn_v +
-	//todouble(op->v[3])  *  6 * bn_v * bn_v * bn_v +
-	//todouble(op->v[4])  *  6 * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[5])  *  6 * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[6])  *  6 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[7])  * 36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[8])  * 36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[9])  * 36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[10]) * 36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v +
-	//todouble(op->v[11]) * 36 * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v * bn_v; 
-	//zout(poly_at_one);
-	
-	
-	mpz_class bn_u, bn_p;
-	bn_u=1;//attention pow(bn_v,3); ne marche pas ni bn_v*bn_v*bn_v
-	
-	for (int i =0;i<3;i++)
-	{
-		bn_u*=bn_v;
-	}
-	bn_p=36 * bn_u * bn_u * bn_u * bn_u+36 * bn_u * bn_u * bn_u+24 * bn_u * bn_u+6 * bn_u + 1;
-	//zout(bn_p);
-	mpz_class field_element = poly_at_one % bn_p; //32 octets 256 bits le reste n'est pas forcément positif, il est obtenu avec une division dite tronquée, i.e. le reste a le même signe que le divisé, poly_at_one dans ce cas.
-	//field_element = (sgn(field_element)>=0)? field_element :field_element+bn_p;
-	//if (sgn(field_element)<0)
-	//{
-		//field_element+=bn_p;
-	//}
-
-	//zout(field_element);
-	return field_element;
-}
-
-
-
-void fpe2scalar(scalar_t rop, const fpe_t op)
-{
-	
-	mpz_class field_element=fpe2mpz(op);
-	
-		
-	///** Technique du modulo **/
-		///** Calcul d'une puissance entière **/
-		//mpz_class base=1;
-		//for (int i=0; i<64; i++) 
-		//{
-			//base=base*2;
-		//} 
-		////zout(base); // base 2^64
-	//rop[0] = mpz2ull( field_element        % base); //décomposition en base 2^64
-	//rop[1] = mpz2ull((field_element >> 64)  % base);
-	//rop[2] = mpz2ull((field_element >> 128)  % base);
-	//rop[3] = mpz2ull((field_element >> 192)  % base);	
-	
-	//mpz_class test_modulo =ull2mpz(rop[0])+ull2mpz(rop[1])*base +ull2mpz(rop[2])*base*base+ull2mpz(rop[3])*base*base*base; 
-	//zout(test_modulo);
-	
-	/** Technique du masque en C++**/
-	mpz_class mask = 0xffffffffffffffff; // 8 octets 64 bits
-	rop[0] = mpz2ull (field_element        & mask);
-	rop[1] = mpz2ull((field_element >> 64) & mask);
-	rop[2] = mpz2ull((field_element >> 128) & mask);
-	rop[3] = mpz2ull((field_element >> 192) & mask);
-
-	//mpz_class test_mask =ull2mpz(rop[0])+(ull2mpz(rop[1])<<64) +(ull2mpz(rop[2])<<128)+(ull2mpz(rop[3])<<192); //il faut mettre les parenthèses
-	//zout(test_mask); //le test consiste à bien recomposer field_element à partir des rop[]
-	
-	/** Technique du masque en C**/
-	//mpz_t c;	
-	//mpz_and(c,field_element.get_mpz_t() ,mask.get_mpz_t()); 
-	// pour continuer avec la méthode C, il faut définir une fonction de conversion entre mpz_t et ull
-	
-	
-	//zout(rop[0],rop[1],rop[2],rop[3]);
-}
-
-
-mpz_class scalar2mpz( const scalar_t op)
-{
-	mpz_class r,rop;
-    
-	rop  = ull2mpz(op[0]);
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 64); //on doit repasser par l'interface C de GMP
-	rop += ull2mpz(op[1])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 128);
-	rop += ull2mpz(op[2])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 192);
-	rop += ull2mpz(op[3])*r;
-	
-	//zout(mpz_sizeinbase(rop.get_mpz_t(),2));
-	if (mpz_sizeinbase(rop.get_mpz_t(),2) == 256) //si le mpz fait 256 bits, le bit de poids fort est pour le signe
-	{
-		mpz_ui_pow_ui (r.get_mpz_t(), 2, 256);
-		rop=rop-r;
-	}
-	return rop;
-}
-
-
-void mpz2scalar1024(scalar1024 rop, mpz_class field_element)
-{
-	mpz_class mask = 0xffffffffffffffff; // 8 octets 64 bits
-	rop[0] = mpz2ull (field_element        & mask);
-	rop[1] = mpz2ull((field_element >> 64) & mask);
-	rop[2] = mpz2ull((field_element >> 128) & mask);
-	rop[3] = mpz2ull((field_element >> 192) & mask);	
-	rop[4] = mpz2ull((field_element >> 256) & mask);	
-	rop[5] = mpz2ull((field_element >> 320) & mask);
-	rop[6] = mpz2ull((field_element >> 384) & mask);
-	rop[7] = mpz2ull((field_element >> 448) & mask);
-	rop[8] = mpz2ull((field_element >> 512) & mask);
-	rop[9] = mpz2ull((field_element >> 576) & mask);
-	rop[10] = mpz2ull((field_element >> 640) & mask);
-	rop[11] = mpz2ull((field_element >> 704) & mask);
-	rop[12] = mpz2ull((field_element >> 768) & mask);
-	rop[13] = mpz2ull((field_element >> 832) & mask);
-	rop[14] = mpz2ull((field_element >> 896) & mask);
-	rop[15] = mpz2ull((field_element >> 960) & mask);
-}
-
-mpz_class scalar1024_2mpz( const scalar1024 op)
-{
-	mpz_class r,rop;
-    
-	rop  = ull2mpz(op[0]);
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 64); //on doit repasser par l'interface C de GMP
-	rop += ull2mpz(op[1])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 128);
-	rop += ull2mpz(op[2])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 192);
-	rop += ull2mpz(op[3])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 256);
-	rop += ull2mpz(op[4])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 320);
-	rop += ull2mpz(op[5])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 384);
-	rop += ull2mpz(op[6])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 448);
-	rop += ull2mpz(op[7])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 512);
-	rop += ull2mpz(op[8])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 576);
-	rop += ull2mpz(op[9])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 640);
-	rop += ull2mpz(op[10])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 704);
-	rop += ull2mpz(op[11])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 768);
-	rop += ull2mpz(op[12])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 832);
-	rop += ull2mpz(op[13])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 896);
-	rop += ull2mpz(op[14])*r;
-	mpz_ui_pow_ui (r.get_mpz_t(), 2, 960);
-	rop += ull2mpz(op[15])*r;
-	
-	//zout(mpz_sizeinbase(rop.get_mpz_t(),2));
-	if (mpz_sizeinbase(rop.get_mpz_t(),2) == 1024) //si le mpz fait 256 bits, le bit de poids fort est pour le signe
-	{
-		mpz_ui_pow_ui (r.get_mpz_t(), 2, 1024);
-		rop=rop-r;
-	}
-	return rop;
-}

+ 0 - 16
bgn2/src/fpe2scalar.hpp

@@ -1,16 +0,0 @@
-#ifndef __DECOMPOSITION_HPP
-
-#define __DECOMPOSITION_HPP
-
-#include "bgn.hpp"
-#include <sstream>
-#include "scalar1024.hpp"
-
-void fpe2scalar(scalar_t rop, const fpe_t op); //choix d'utiliser des procédures dans le main pour lisibilté
-mpz_class fpe2mpz(const fpe_t);
-unsigned long long mpz2ull (mpz_class n);
-mpz_class  ull2mpz (unsigned long long n);
-mpz_class  scalar2mpz(const scalar_t op);
-void mpz2scalar1024(scalar1024 rop, mpz_class field_element);
-mpz_class scalar1024_2mpz( const scalar1024 op);
-#endif /* __DECOMPOSITION_HPP */

+ 0 - 3
bgn2/src/gengetopt.h

@@ -1,3 +0,0 @@
-/** gengetopt **/
-
-#include "option.h"

+ 79 - 0
bgn2/src/homomorphic_operations.cpp

@@ -0,0 +1,79 @@
+#include "homomorphic_operations.hpp"
+
+extern const scalar_t bn_n;
+
+Bipoint<curvepoint_fp_t> bgn_homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b, const PublicKey& public_key)
+{
+	Bipoint<curvepoint_fp_t> retval, random_mask;
+
+	scalar_t lambda;
+	scalar_setrandom(lambda, bn_n);
+
+	random_mask = public_key.get_bipoint_curve_groupelt().scalarmult_vartime(lambda);
+	random_mask.makeaffine();
+
+	retval = a + b + random_mask;
+	retval.makeaffine();
+
+	return retval;
+}
+
+Bipoint<twistpoint_fp2_t> bgn_homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b, const PublicKey& public_key)
+{
+	Bipoint<curvepoint_fp_t> retval, random_mask;
+
+	scalar_t lambda;
+	scalar_setrandom(lambda, bn_n);
+
+	random_mask = public_key.get_bipoint_twist_groupelt().scalarmult_vartime(lambda);
+	random_mask.makeaffine();
+
+	retval = a + b + random_mask;
+	retval.makeaffine();
+
+	return retval;
+}
+
+Quadripoint bgn_homomorphic_addition(const Quadripoint& a, const Quadripoint& b, const PublicKey& public_key)
+{
+	Quadripoint retval, random_mask;
+	Bipoint<curvepoint_fp_t> random_mask_curve;
+	Bipoint<twistpoint_fp2_t> random_mask_twist;
+	
+	scalar_t lambda1, lambda2;
+	scalar_setrandom(lambda1, bn_n);
+	scalar_setrandom(lambda2, bn_n);
+
+	random_mask_curve = public_key.get_bipoint_curve_groupelt().scalarmult_vartime(lambda1);
+	random_mask_curve.makeaffine();
+
+	random_mask_twist = public_key.get_bipoint_twist_groupelt().scalarmult_vartime(lambda2);
+	random_mask_twist.makeaffine();
+
+	random_mask = pairing(public_key.get_bipoint_curvegen(), random_mask_twist) * pairing(random_mask_curve, public_key.get_bipoint_twistgen());
+	retval = a * b * random_mask;
+
+	return retval;
+}
+
+Quadripoint bgn_homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b, const PublicKey& public_key)
+{
+	Quadripoint retval, random_mask;
+	Bipoint<curvepoint_fp_t> random_mask_curve;
+	Bipoint<twistpoint_fp2_t> random_mask_twist;
+	
+	scalar_t lambda1, lambda2;
+	scalar_setrandom(lambda1, bn_n);
+	scalar_setrandom(lambda2, bn_n);
+
+	random_mask_curve = public_key.get_bipoint_curve_groupelt().scalarmult_vartime(lambda1);
+	random_mask_curve.makeaffine();
+
+	random_mask_twist = public_key.get_bipoint_twist_groupelt().scalarmult_vartime(lambda2);
+	random_mask_twist.makeaffine();
+
+	random_mask = pairing(public_key.get_bipoint_curvegen(), random_mask_twist) * pairing(random_mask_curve, public_key.get_bipoint_twistgen());
+	retval = pairing(a, b) * random_mask;
+
+	return retval;
+}

+ 14 - 0
bgn2/src/homomorphic_operations.hpp

@@ -0,0 +1,14 @@
+#ifndef __HOMOMORPHIC_OPERATIONS_HPP
+#define __HOMOMORPHIC_OPERATIONS_HPP
+
+#include "Bipoint.hpp"
+#include "Quadripoint.hpp"
+#include "PublicKey.hpp"
+#include "pairing.hpp"
+
+Bipoint<curvepoint_fp_t> bgn_homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b, const PublicKey& public_key);
+Bipoint<twistpoint_fp2_t> bgn_homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b, const PublicKey& public_key);
+Quadripoint bgn_homomorphic_addition(const Quadripoint& a, const Quadripoint& b, const PublicKey& public_key);
+Quadripoint bgn_homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b, const PublicKey& public_key);
+
+#endif /* __HOMOMORPHIC_OPERATIONS_HPP */

+ 0 - 370
bgn2/src/multiplicationL1.cpp

@@ -1,370 +0,0 @@
-#include "multiplicationL1.hpp"
-
-//template <typename S, typename T>
-BitEvalL2 multiplicationL1 (BitEvalL1<curvepoint_fp_t> eval1, BitEvalL1<twistpoint_fp2_t> eval2, PublicKey public_key)
-{
-	BitEvalL2 produit;
-	F2 s=rand()%2;
-	produit.set_bit_masque(eval1.get_bit_masque()*eval2.get_bit_masque()-s);
-	Bipoint<curvepoint_fp_t> bipoint_curve_subgroupelt;
-	Bipoint<twistpoint_fp2_t> bipoint_twist_subgroupelt;		
-	scalar_t lambda1, lambda2;
-	scalar_setrandom(lambda1, bn_r);	
-	scalar_setrandom(lambda2, bn_r);	
-	bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda1); // calcul de u1
-	bipoint_curve_subgroupelt.makeaffine();
-	bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda2); // calcul de v1
-	bipoint_twist_subgroupelt.makeaffine();
-	public_key.get_bipoint_twist_groupelt().makeaffine();
-	
-	Quadripoint factor1, factor2, factor3, factor4, factor5;
-	Bipoint<curvepoint_fp_t> chiffre_1_curve;
-	Bipoint<twistpoint_fp2_t> chiffre_1_twist, chiffre_s;
-	
-	//calcul de e(beta_1,beta_2)   
-	factor1 = pairing(eval1.get_bipoint(),eval2.get_bipoint());
-	
-	//calcul de  e(Enc(1), a1 beta2 + Enc(s))
-	
-	Bipoint<curvepoint_fp_t>  temp1;		
-	Bipoint<twistpoint_fp2_t> temp2;
-	
-	chiffrement(chiffre_s,s, public_key);
-	chiffre_s.makeaffine();
-	
-	
-	chiffrement(chiffre_1_curve,1, public_key);
-	temp1=chiffre_1_curve;
-	temp1.makeaffine();
-			
-	if (eval1.get_bit_masque() == 1)
-	{
-		temp2 = eval2.get_bipoint()+chiffre_s;
-	}
-	else
-	{
-		temp2 = chiffre_s;
-
-	}	
-	temp2.makeaffine();
-
-	factor2 = pairing(temp1, temp2);
-	
-	//calcul de  e(a2 beta1, Enc(1))
-
-	Bipoint<curvepoint_fp_t>  temp3; // les bipoints sont initialisés par défaut au bipoint à l'infini
-	Bipoint<twistpoint_fp2_t> temp4;
-	chiffrement(chiffre_1_twist,1, public_key);
-	//F2 un;
-	//dechiffrement(un,chiffre_1_twist,private_key);
-	//zout(un);
-	if (eval2.get_bit_masque() == 1) //sinon temp3 doit etre le bipoint à l'infini
-	{
-		temp3 = eval1.get_bipoint(); 
-	}
-	temp3.makeaffine();
-	//temp3.print();
-	
-	temp4=chiffre_1_twist;
-	temp4.makeaffine();
-	//ecris(affichage de chiffre_1_twist);
-	//temp4.print();
-	
-	factor3 = pairing(temp3, temp4);	
-	
-	//ecris(affichage de pi_1(f3[0]));
-	//private_key.pi_1(temp3).print(0);	
-	
-	//ecris(affichage de pi_2(f3[1]));
-	//private_key.pi_2(chiffre_1_twist).print();	
-	
-	//ecris(affichage de e(pi_1(f3[0]),pi_2(f3[1])));
-	//pairing(private_key.pi_1(temp3),private_key.pi_2(temp4)).print(0);	
-
-	//ecris(affichage de e(f3[0],f3[1]));
-	//pairing(temp3,temp4).print(0);	
-			
-	//ecris(affichage de pi_T(e(f3[0],f3[1])));
-	//private_key.pi_T(pairing(temp3,temp4)).print(0);	
-			
-	//calcul de  e(u,v1)
-	factor4 = pairing(public_key.get_bipoint_curve_groupelt(),bipoint_twist_subgroupelt);
-	
-	//calcul de e(u1,v)
-	factor5 = pairing(bipoint_curve_subgroupelt,public_key.get_bipoint_twist_groupelt());
-								
-	produit.set_quadripoint(factor1*factor2*factor3*factor4*factor5);		
-	return produit;
-}
-
-
-
-BitEvalL2 multiplicationL1 (BitChiffre eval1, BitChiffre eval2, PublicKey public_key)
-{
-	//signature;
-	if (eval1.get_type() == CURVE && eval2.get_type() == TWIST)
-	{
-		BitEvalL2 produit;
-		F2 s=rand()%2;
-		//zout(s);
-		//cout << "a1 = " << eval1.get_bit_masque() << endl;
-		//cout << "a2 = " << eval2.get_bit_masque() << endl;
-		//cin.ignore() ; //NB temps[] d'attente
-		produit.set_bit_masque(eval1.get_bit_masque()*eval2.get_bit_masque()-s);
-		//eval1.print_bit_masque();
-		//eval2.print_bit_masque();
-		//ecris(affichage de a1a2-s);
-		//produit.print_bit_masque();
-		Bipoint<curvepoint_fp_t> bipoint_curve_subgroupelt;
-		Bipoint<twistpoint_fp2_t> bipoint_twist_subgroupelt;		
-		scalar_t lambda1, lambda2;
-		scalar_setrandom(lambda1, bn_r);	
-		scalar_setrandom(lambda2, bn_r);
-		//mpz_class mpz1, mpz2;
-		//mpz1=scalar2mpz(lambda1);
-		//mpz2=scalar2mpz(lambda2);			
-		//scalar_print(stdout,lambda1);
-		//JUMP;
-		//zout(mpz1);
-		//scalar_print(stdout,lambda2);
-		//JUMP;
-		//zout(mpz2);	
-		bipoint_curve_subgroupelt.scalarmult_vartime(public_key.get_bipoint_curvegen(),lambda1); // calcul de u1
-		bipoint_curve_subgroupelt.makeaffine();
-		bipoint_twist_subgroupelt.scalarmult_vartime(public_key.get_bipoint_twistgen(),lambda2); // calcul de v1
-		bipoint_twist_subgroupelt.makeaffine();
-		public_key.get_bipoint_twist_groupelt().makeaffine();
-		
-		Quadripoint factor1, factor2, factor3, factor4, factor5;
-		Bipoint<curvepoint_fp_t> chiffre_1_curve;
-		Bipoint<twistpoint_fp2_t> chiffre_1_twist, chiffre_s;
-		
-		//calcul de e(beta_1,beta_2)   
-		factor1 = pairing(eval1.get_bipoint_curve(),eval2.get_bipoint_twist());
-		
-		//calcul de  e(Enc(1), a1 beta2 + Enc(s))
-		
-		Bipoint<curvepoint_fp_t>  temp1;		
-		Bipoint<twistpoint_fp2_t> temp2;
-		
-		chiffrement(chiffre_s,s, public_key);
-		chiffre_s.makeaffine();
-		
-		
-		chiffrement(chiffre_1_curve,1, public_key);
-		temp1=chiffre_1_curve;
-		temp1.makeaffine();
-				
-		if (eval1.get_bit_masque() == 1)
-		{
-			temp2 = eval2.get_bipoint_twist()+chiffre_s;
-		}
-		else
-		{
-			temp2 = chiffre_s;
-
-		}	
-		temp2.makeaffine();
-
-		factor2 = pairing(temp1, temp2);
-		
-		//calcul de  e(a2 beta1, Enc(1))
-
-		Bipoint<curvepoint_fp_t>  temp3; // les bipoints sont initialisés par défaut au bipoint à l'infini
-		Bipoint<twistpoint_fp2_t> temp4;
-		chiffrement(chiffre_1_twist,1, public_key);
-		//F2 un;
-		//dechiffrement(un,chiffre_1_twist,private_key);
-		//zout(un);
-		if (eval2.get_bit_masque() == 1) //sinon temp3 doit etre le bipoint à l'infini
-		{
-			temp3 = eval1.get_bipoint_curve(); 
-		}
-		temp3.makeaffine();
-		//temp3.print();
-		
-		temp4=chiffre_1_twist;
-		temp4.makeaffine();
-		//ecris(affichage de chiffre_1_twist);
-		//temp4.print();
-		
-		factor3 = pairing(temp3, temp4);	
-		
-		//ecris(affichage de pi_1(f3[0]));
-		//private_key.pi_1(temp3).print(0);	
-		
-		//ecris(affichage de pi_2(f3[1]));
-		//private_key.pi_2(chiffre_1_twist).print();	
-		
-		//ecris(affichage de e(pi_1(f3[0]),pi_2(f3[1])));
-		//pairing(private_key.pi_1(temp3),private_key.pi_2(temp4)).print(0);	
-
-		//ecris(affichage de e(f3[0],f3[1]));
-		//pairing(temp3,temp4).print(0);	
-				
-		//ecris(affichage de pi_T(e(f3[0],f3[1])));
-		//private_key.pi_T(pairing(temp3,temp4)).print(0);	
-				
-		//calcul de  e(u,v1)
-		factor4 = pairing(public_key.get_bipoint_curve_groupelt(),bipoint_twist_subgroupelt);
-		
-		//calcul de e(u1,v)
-		factor5 = pairing(bipoint_curve_subgroupelt,public_key.get_bipoint_twist_groupelt());
-		
-		//private_key.pi_1(bipoint_curve_subgroupelt).print();
-		//private_key.pi_2(public_key.get_bipoint_twist_groupelt()).print();
-		
-		//ecris(affichage de e(pi_1(u),pi_2(v1)));
-		//pairing(private_key.pi_1(private_key.pi_1(public_key.get_bipoint_curve_groupelt())),private_key.pi_2(bipoint_twist_subgroupelt)).print();		 
-		
-		//ecris(affichage de e(pi_1(u1),pi_2(v)));
-		//pairing(private_key.pi_1(bipoint_curve_subgroupelt),private_key.pi_2(public_key.get_bipoint_twist_groupelt())).print();
-		
-		//ecris(affichage de e(-j1l1u1[0]+i1l1u1[1],-j2l2v[0]+i2l2v[1]));
-		////void curvepoint_fp_scalarmult_vartime(curvepoint_fp_t rop, const curvepoint_fp_t op, const scalar_t s);
-		////void curvepoint_fp_add_vartime(curvepoint_fp_t rop, const curvepoint_fp_t op1, const curvepoint_fp_t op2);
-		////void curvepoint_fp_neg(curvepoint_fp_t rop, const curvepoint_fp_t op);
-		//curvepoint_fp_t tempc[6], op1;
-		//twistpoint_fp2_t tempt[5], op2;
-		//fp12e_t tempf[60], test, test2, test3, test4;
-		//curvepoint_fp_neg(tempc[0],bipoint_curve_subgroupelt[0]);
-		//curvepoint_fp_scalarmult_vartime(tempc[1],tempc[0],private_key.get("j1").scalar());
-		//curvepoint_fp_scalarmult_vartime(tempc[2],tempc[1],private_key.get("l1").scalar());
-		//curvepoint_fp_scalarmult_vartime(tempc[3],bipoint_curve_subgroupelt[1],private_key.get("i1").scalar());
-		//curvepoint_fp_scalarmult_vartime(tempc[4],tempc[3],private_key.get("l1").scalar());	
-		//curvepoint_fp_makeaffine(tempc[2]);curvepoint_fp_makeaffine(tempc[4]);	
-		//curvepoint_fp_add_vartime(op1,tempc[2],tempc[4]);
-		
-		//twistpoint_fp2_neg(tempt[0],public_key.get_bipoint_twist_groupelt()[0]);
-		//twistpoint_fp2_scalarmult_vartime(tempt[1],tempt[0],private_key.get("j2").scalar());
-		//twistpoint_fp2_scalarmult_vartime(tempt[2],tempt[1],private_key.get("l2").scalar());
-		//twistpoint_fp2_scalarmult_vartime(tempt[3],public_key.get_bipoint_twist_groupelt()[1],private_key.get("i2").scalar());
-		//twistpoint_fp2_scalarmult_vartime(tempt[4],tempt[3],private_key.get("l2").scalar());		
-		//twistpoint_fp2_add_vartime(op2,tempt[5],tempt[4]);		
-		
-		//OptimalAte(test,op1,op2);
-		//fp12e_print(stdout,test);
-		//JUMP;
-		
-		//ecris(affichage de e(-j1l1u1[0],-j2l2v[0]+i2l2v[1])e(i1l1u1[1],-j2l2v[0]+i2l2v[1]));
-		//OptimalAte(tempf[0],tempc[2],op2);
-		//OptimalAte(tempf[1],tempc[4],op2);
-		//fp12e_mul(test2,tempf[0],tempf[1]);
-		//fp12e_print(stdout,test2);
-		//JUMP;		
-		
-		
-		//ecris(affichage de e(-j1l1u1[0],-j2l2v[0])e(-j1l1u1[0],i2l2v[1])e(i1l1u1[1],-j2l2v[0])e(i1l1u1[1],i2l2v[1]));
-		//OptimalAte(tempf[2],tempc[2],tempt[2]);
-		//OptimalAte(tempf[3],tempc[2],tempt[4]);		
-		//OptimalAte(tempf[4],tempc[4],tempt[2]);
-		//OptimalAte(tempf[5],tempc[4],tempt[4]);			
-		//fp12e_mul(tempf[6],tempf[2],tempf[3]);	
-		//fp12e_mul(tempf[7],tempf[4],tempf[5]);		
-		//fp12e_mul(test3,tempf[6],tempf[7]);
-		//fp12e_print(stdout,test3);
-		//JUMP;		
-		
-
-		
-		//ecris(affichage de e(u1[0],v[0])^((-j1l1)(-j2l2))	e(u1[0],v[1])^((-j1l1)(i2l2))	e(u1[1],v[0])^((i1l1)(-j2l2))	e(u1[1],v[1])^((i1l1)(i2l2)));
-		
-		
-		//curvepoint_fp_makeaffine(bipoint_curve_subgroupelt[0]);curvepoint_fp_makeaffine(bipoint_curve_subgroupelt[1]);	
-		//twistpoint_fp2_makeaffine(public_key.get_bipoint_twist_groupelt()[0]);twistpoint_fp2_makeaffine(public_key.get_bipoint_twist_groupelt()[1]);
-		
-		
-		//OptimalAte(tempf[8],bipoint_curve_subgroupelt[0],public_key.get_bipoint_twist_groupelt()[0]);
-		//OptimalAte(tempf[9],bipoint_curve_subgroupelt[0],public_key.get_bipoint_twist_groupelt()[1]);
-		//OptimalAte(tempf[10],bipoint_curve_subgroupelt[1],public_key.get_bipoint_twist_groupelt()[0]);
-		//OptimalAte(tempf[11],bipoint_curve_subgroupelt[1],public_key.get_bipoint_twist_groupelt()[1]);
-		
-		//fp12e_invert(tempf[12],tempf[8]);
-		//fp12e_pow_vartime(tempf[13],tempf[12],private_key.get("j1").scalar());
-		//fp12e_pow_vartime(tempf[14],tempf[13],private_key.get("l1").scalar());
-		//fp12e_invert(tempf[15],tempf[14]);
-		//fp12e_pow_vartime(tempf[16],tempf[15],private_key.get("j2").scalar());
-		//fp12e_pow_vartime(tempf[17],tempf[16],private_key.get("l2").scalar());
-		
-		//fp12e_invert(tempf[18],tempf[9]);
-		//fp12e_pow_vartime(tempf[19],tempf[18],private_key.get("j1").scalar());
-		//fp12e_pow_vartime(tempf[20],tempf[19],private_key.get("l1").scalar());
-		//fp12e_pow_vartime(tempf[21],tempf[20],private_key.get("i2").scalar());
-		//fp12e_pow_vartime(tempf[22],tempf[21],private_key.get("l2").scalar());
-		
-		//fp12e_invert(tempf[23],tempf[10]);
-		//fp12e_pow_vartime(tempf[24],tempf[23],private_key.get("i1").scalar());
-		//fp12e_pow_vartime(tempf[25],tempf[24],private_key.get("l1").scalar());
-		//fp12e_pow_vartime(tempf[26],tempf[25],private_key.get("j2").scalar());
-		//fp12e_pow_vartime(tempf[27],tempf[26],private_key.get("l2").scalar());
-		
-		//fp12e_pow_vartime(tempf[28],tempf[27],private_key.get("i1").scalar());
-		//fp12e_pow_vartime(tempf[29],tempf[28],private_key.get("l1").scalar());
-		//fp12e_pow_vartime(tempf[30],tempf[29],private_key.get("i2").scalar());
-		//fp12e_pow_vartime(tempf[31],tempf[30],private_key.get("l2").scalar());
-		
-
-		//fp12e_mul(tempf[32],tempf[17],tempf[22]);
-		//fp12e_mul(tempf[33],tempf[27],tempf[31]);
-		//fp12e_mul(test4,tempf[32],tempf[33]);
-		
-		//fp12e_print(stdout,test4);
-		//JUMP;	
-		
-		
-		//ecris(affichage de e(u1[0],v[0])^((-j1l1)(-j2l2)));
-		//fp12e_print(stdout,tempf[17]);
-		//JUMP;		
-		
-		//ecris(affichage de e(-j1l1u1[0],-j2l2v[0]));
-		//fp12e_print(stdout,tempf[6]);
-		//JUMP;		
-		
-		//zout(scalar2mpz(private_key.get("i1").scalar()));
-		//zout(scalar2mpz(private_key.get("j1").scalar()));
-		//zout(scalar2mpz(private_key.get("k1").scalar()));
-		//zout(scalar2mpz(private_key.get("l1").scalar()));
-		//zout(scalar2mpz(private_key.get("i2").scalar()));
-		//zout(scalar2mpz(private_key.get("j2").scalar()));
-		//zout(scalar2mpz(private_key.get("k2").scalar()));
-		//zout(scalar2mpz(private_key.get("l2").scalar()));
-		
-		//ecris(affichage de e(u1[0],v[0])^j1);
-		//fp12e_pow_vartime(tempf[34],tempf[8],private_key.get("j1").scalar());
-		//fp12e_print(stdout,tempf[34]);
-		//JUMP;		
-		
-		//ecris(affichage de e(j1u1[0],v[0]));		
-		//curvepoint_fp_scalarmult_vartime(tempc[5],bipoint_curve_subgroupelt[0],private_key.get("j1").scalar());
-		//curvepoint_fp_makeaffine(tempc[5]);
-		//twistpoint_fp2_makeaffine(public_key.get_bipoint_twist_groupelt()[0]);
-		//OptimalAte(tempf[36],tempc[5],public_key.get_bipoint_twist_groupelt()[0]);
-		//fp12e_print(stdout,tempf[36]);
-		//JUMP;			
-		//exit(0);	
-		
-		
-		//ecris(affichage de e(pi_1(u),pi_2(v)));
-		//pairing(private_key.pi_1(public_key.get_bipoint_curve_groupelt()),private_key.pi_2(public_key.get_bipoint_twist_groupelt())).print(0);		
-
-		//ecris(affichage de (e(pi_1(u),pi_2(v)))^2);
-		//pairing(private_key.pi_1(public_key.get_bipoint_curve_groupelt()),private_key.pi_2(public_key.get_bipoint_twist_groupelt())).square().print_point(0);
-		
-		//ecris(affichage de (e(pi_1(u),pi_2(v)))^3);
-		//scalar_t trois; trois[0]=3;
-		//pairing(private_key.pi_1(public_key.get_bipoint_curve_groupelt()),private_key.pi_2(public_key.get_bipoint_twist_groupelt())).pow_vartime(trois).print_point(0);
-		
-		//ecris(affichage de (e(pi_1(u),pi_2(v)))^4);
-		//scalar_t quatre; quatre[0]=4;
-		//pairing(private_key.pi_1(public_key.get_bipoint_curve_groupelt()),private_key.pi_2(public_key.get_bipoint_twist_groupelt())).pow_vartime(quatre).print_point(0);
-
-												
-		produit.set_quadripoint(factor1*factor2*factor3*factor4*factor5);		
-		return produit;
-	}
-	else
-	{
-		cout << "Problème de type dans multiplicationL1, le premier argument doit être un élément de type CURVE et le second, un élément de type TWIST" << endl;
-		exit(0);
-	}
-}

+ 0 - 18
bgn2/src/multiplicationL1.hpp

@@ -1,18 +0,0 @@
-#ifndef __MULTIPLICATIONL1_HPP
-
-#define __MULTIPLICATIONL1_HPP
-
-#include "BitChiffre.hpp"
-#include "BitEvalL2.hpp"
-#include "BitEvalL1.hpp"
-#include "keygen.hpp"
-#include "pairing.hpp"
-#include "chiffrement.hpp"
-#include "fpe2scalar.hpp"
-
-//template <typename S, typename T>
-BitEvalL2 multiplicationL1 (BitEvalL1<curvepoint_fp_t> a, BitEvalL1<twistpoint_fp2_t> b, PublicKey public_key);
-BitEvalL2 multiplicationL1 (BitChiffre a, BitChiffre b, PublicKey public_key);
-
-
-#endif /* __MULTIPLICATIONL1_HPP */

+ 15 - 9
bgn2/src/pairing.cpp

@@ -1,16 +1,22 @@
 #include "pairing.hpp"
 
-void OptimalAte(fp12e_t rop, const curvepoint_fp_t op1,const twistpoint_fp2_t op2)
+fp12e_t OptimalAte(const curvepoint_fp_t& op1, const twistpoint_fp2_t& op2)
 {
-	optate(rop, op2, op1);
+    fp12e_t retval;
+
+	optate(retval, op2, op1);
+
+    return retval;
 }
 
-Quadripoint pairing(Bipoint<curvepoint_fp_t> op1,Bipoint<twistpoint_fp2_t> op2)
+Quadripoint pairing(const Bipoint<curvepoint_fp_t>& op1, const Bipoint<twistpoint_fp2_t>& op2)
 {
-	Quadripoint rop;
-	OptimalAte(rop[0],op1[0],op2[0]);
-	OptimalAte(rop[1],op1[0],op2[1]);
-	OptimalAte(rop[2],op1[1],op2[0]);
-	OptimalAte(rop[3],op1[1],op2[1]);
-	return rop;
+	Quadripoint retval;
+
+	retval[0] = OptimalAte(op1[0], op2[0]);
+	retval[1] = OptimalAte(op1[0], op2[1]);
+	retval[2] = OptimalAte(op1[1], op2[0]);
+	retval[3] = OptimalAte(op1[1], op2[1]);
+	
+    return retval;
 }

+ 2 - 3
bgn2/src/pairing.hpp

@@ -1,13 +1,12 @@
 #ifndef __PAIRING_HPP
-
 #define __PAIRING_HPP
 
 #include "Quadripoint.hpp"
 #include "Bipoint.hpp"
 #include "optate.h"
 
-void OptimalAte(fp12e_t rop, const curvepoint_fp_t op1,const twistpoint_fp2_t op2);
-Quadripoint pairing(Bipoint<curvepoint_fp_t> op1,Bipoint<twistpoint_fp2_t> op2);
+fp12e_t OptimalAte(const curvepoint_fp_t& op1, const twistpoint_fp2_t& op2);
+Quadripoint pairing(const Bipoint<curvepoint_fp_t>& op1, const Bipoint<twistpoint_fp2_t>& op2);
 
 #endif
 

+ 0 - 52
bgn2/src/representation.cpp

@@ -1,52 +0,0 @@
-#include "representation.hpp"
-
-void representation(Bipoint<curvepoint_fp_t> op)
- {
-	 representation(op[0]);
-	 representation(op[1]);
-}
-
-void representation(const curvepoint_fp_t op)
-{
-mpz_class X,Y;
-	
-	extern const double bn_v;
-	mpz_class bn_u, bn_p;
-	bn_u=1;//pow(bn_v,3); ne marche pas ni bn_v*bn_v*bn_v moral: d'abord initialiser pour travailler avec des mpz_class plutôt que des doubles
-	
-	for (int i =0;i<3;i++)
-	{
-		bn_u*=bn_v;
-	}
-	bn_p=36 * bn_u * bn_u * bn_u * bn_u+36 * bn_u * bn_u * bn_u+24 * bn_u * bn_u+6 * bn_u + 1;	
-	zout(bn_u,bn_v);
-	mpz_class f[12],g[12];
-	for (int i=0;i<12;i++)
-	{
-		f[i]=todouble((op->m_x) -> v[i]);
-		g[i]=todouble((op->m_y) -> v[i]);
-		//cout << "f[" <<i<<"] = "<< f[i] << endl;		
-	}
-	X=poly(f);
-	Y=poly(g);	
-
-	zout(X%bn_p,Y%bn_p);
-}
-
-
-mpz_class poly(mpz_class coeff[12])
-{
-	extern const double bn_v;
-	mpz_class rop=coeff[0]+6*coeff[1]*bn_v
-	+6*coeff[2]*bn_v*bn_v
-	+6*coeff[3]*bn_v*bn_v*bn_v
-	+6*coeff[4]*bn_v*bn_v*bn_v*bn_v
-	+6*coeff[5]*bn_v*bn_v*bn_v*bn_v*bn_v
-	+6*coeff[6]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v
-	+36*coeff[7]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v
-	+36*coeff[8]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v
-	+36*coeff[9]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v
-	+36*coeff[10]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v
-	+36*coeff[11]*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v*bn_v;
-	return rop;
-}

+ 0 - 16
bgn2/src/representation.hpp

@@ -1,16 +0,0 @@
-#ifndef __REPRESENTATION_HPP
-
-#define __REPRESENTATION_HPP
-
-#include "Bipoint.hpp"
-#include "Fp.hpp" 
-#include "zout.hpp"
-#include "Quadripoint.hpp"
-#include "fp12e.h"
-#include "size.hpp"
-
-void representation(Bipoint<curvepoint_fp_t> op);
-void representation(const curvepoint_fp_t op);
-mpz_class poly(mpz_class coeff[12]);
-
-#endif

+ 0 - 41
bgn2/src/size.hpp

@@ -1,41 +0,0 @@
-#ifndef __SIZE_HPP
-#define __SIZE_HPP
-
-/**
- *   définition des symboles de taille limite des messages
- **/
- 
-#include <gmp.h> 
-#include <gmpxx.h>
-//en général, pour manipuler de grands entiers
-//en pratique, pour définir x0 entier de 63 bits et calculer p,r et t, entiers de respectivement 256,256 et 128 bits pour une sécurité 128 bits 
- 
-
-// taille en octets pour BGNC2F
-
-//size(n)=3072
-//size(q)=1500 a peu pres
-//size(t)=700 a peu pres
-//eval=XY(1+Z)
-// profondeur 2
-//L=0
-
-// BGN n=pq (poly de degré 2) subgroup decision problem
-//clair Choisir un message m entre 0 et T avec T < q
-//chiffré On calcule le chiffré $c=g^mh^r$.
-
-//BGNCF groupe d'ordre n=pq (certains polys de degré 4) subgroup decision problem - public space
-// clairs M groupe (Z/tZ,+,\times) avec t <<q
-// chiffrés C=MxC' avec C'=(G,\times),~(G_T,*) des groupes cycliques d'ordre pq où p et q sont premiers
-//évalués C^(2L+3) avec L nombre d'addition de pas frais
-
-//BGNC2F (certains polys de degré 4 - déchiffrement plus rapide - plusieurs pairings) decision linear problem (généralisable au k-linear assumption) - public space 
-// clairs M groupe (Z/tZ,+,\times) avec t <<q
-// pairing asymétrique GG=G1^2 HH=G2^2 -> Gt^4 ordre premier sécurité sur DDH dans G1 G2 et sur generalized subgroup dans dans GG et HH 
-
-
-#define MAX_MESSAGE_SIZE
-#define MAX_CIPHER_SIZE
-
-#endif /* __SIZE_HPP */
-

+ 0 - 14
bgn2/src/typedef.h

@@ -1,14 +0,0 @@
-#ifndef __TYPEDEF_H
-#define __TYPEDEF_H
-
-
-#include <iostream>
-#include <bitset>
-using namespace std;
-
-typedef bool F2;
-
-enum Type {CURVE,TWIST};
-
-
-#endif /* __TYPEDEF_H */

+ 0 - 71
bgn2/src/zout.hpp

@@ -1,71 +0,0 @@
-#ifndef __MINIMAL_HPP
-#define __MINIMAL_HPP
-
-#include <iostream>
-#include <cstring>
-
-#define signature cout <<  GREEN <<  __PRETTY_FUNCTION__   << "\t" << __FILE__ << RESET << endl;
-#define where cout <<  GREEN <<  __func__ << " @" << __LINE__  << RESET << endl;
-#define abc cout <<  GREEN << "START (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;
-#define xyz cout <<  GREEN << "END (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;
-#define zout(...) {all_out (#__VA_ARGS__, __VA_ARGS__); cout <<  RED << " (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;} 
-#define zout2(x) cout << #x" = \n" << x <<  RED << " (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;
-#define jump cout << endl;
-#define JUMP cout << endl << endl << endl;
-//#define tab cout << "\t" ; //mauvaise idée d'appeler une macro tab. On peut vouloir appeler un tableau tab dans un programme.
-#define ecris(x) cout << #x << endl;
-#define debug(x) cout << CYAN << #x << " (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;
-#define titre(x) cout << BOLDBLUE << #x << RESET << endl;
-#define green(x) cout << GREEN << #x << RESET << endl;
-#define red(x) cout << RED << #x << RESET << endl;
-#define yellow(x) cout << YELLOW << #x << RESET << endl;
-
-#define grostitre(x) cout << BOLDCYAN << #x << " (" << __func__ << " @" << __LINE__ << ")" << RESET << endl;
-
-//the following are UBUNTU/LINUX ONLY terminal color codes.
-#define RESET   "\033[0m"
-#define BLACK   "\033[30m"      /* Black */
-#define RED     "\033[31m"      /* Red */
-#define GREEN   "\033[32m"      /* Green */
-#define YELLOW  "\033[33m"      /* Yellow */
-#define BLUE    "\033[34m"      /* Blue */
-#define MAGENTA "\033[35m"      /* Magenta */
-#define CYAN    "\033[36m"      /* Cyan */
-#define WHITE   "\033[37m"      /* White */
-#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
-#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
-#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
-#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
-#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
-#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
-#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
-#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
-
-
-using namespace std;
-
-
-/** zout **/
-
-// base case for template recursion when one argument remains
-template <typename Arg1>
-void all_out(const char* name, Arg1 arg1)
-{
-   cout << BOLDBLUE << name << " = \n" << RESET << arg1 ;
-};
-
-// recursive variadic template for multiple arguments
-template <typename Arg1, typename... Args>
-void all_out(const char* names, Arg1 arg1, Args... args)
-{
-    const char* comma = strchr(names, ',');
-    cout << BOLDBLUE;
-    cout.write(names, comma - names)  << " = \n" << RESET << arg1 << endl;
-    all_out(comma + 1, args...);
-};
-
-
-
-
-#endif /* __MINIMAL_HPP */
-