Browse Source

bgn2 now compiles (although whether it does anything remains to be seen

tristangurtler 3 years ago
parent
commit
732ae10471

+ 73 - 0
bgn2/Makefile

@@ -0,0 +1,73 @@
+INC_PATH = inc
+SRC_PATH = src
+OBJ_PATH = obj
+BIN_PATH = bin
+
+BGN_PATH = .
+BGN_INC_PATH = $(BGN_PATH)/$(INC_PATH)
+BGN_SRC_PATH = $(BGN_PATH)/$(SRC_PATH)
+BGN_OBJ_PATH = $(BGN_PATH)/$(OBJ_PATH)
+BGN_BIN_PATH = $(BGN_PATH)/$(BIN_PATH)
+
+666_PATH = ../dclxvi-20130329
+666_INC_PATH = $(666_PATH)/$(INC_PATH)
+666_SRC_PATH = $(666_PATH)/$(SRC_PATH)
+666_OBJ_PATH = $(666_PATH)/$(OBJ_PATH)
+666_BIN_PATH = $(666_PATH)/$(BIN_PATH)
+
+CPP = g++
+CPPFLAGS = -std=c++14 -Wall -I$(BGN_INC_PATH) -I$(666_INC_PATH) -O2
+LDFLAGS = -lgmp -lgmpxx
+
+CC = gcc
+CFLAGS = -std=c99 -O3 -fomit-frame-pointer -I$(666_INC_PATH)
+C_LDFLAGS = -lm
+
+all: $(BGN_BIN_PATH) $(BGN_OBJ_PATH) $(666_OBJ_PATH) $(BGN_BIN_PATH)/main
+
+$(BGN_BIN_PATH):
+	mkdir -p $@
+
+$(BGN_OBJ_PATH):
+	mkdir -p $@
+
+$(666_OBJ_PATH):
+	mkdir -p $@
+
+BGN_FULL_SRC = $(wildcard $(BGN_SRC_PATH)/*.cpp)
+BGN_SRC = $(filter-out $(BGN_SRC_PATH)/main.cpp, $(BGN_FULL_SRC)) 
+BGN_OBJ = $(patsubst $(BGN_SRC_PATH)/%.cpp, $(BGN_OBJ_PATH)/%.o, $(BGN_SRC))
+
+$(BGN_OBJ_PATH)/%.o: $(BGN_SRC_PATH)/%.cpp
+	$(CPP) $(CPPFLAGS) -DQHASM -c -o $@ $< 
+
+666_ALL_C_SRC = $(wildcard $(666_SRC_PATH)/*.c)
+666_C_SRC = $(filter-out $(666_SRC_PATH)/bilintest.c $(666_SRC_PATH)/speedtest.c $(666_SRC_PATH)/test_curvepoint_multiscalar.c $(666_SRC_PATH)/test_twistpoint_multiscalar.c $(666_SRC_PATH)/twistpoint_fp2_multiscalar.c $(666_SRC_PATH)/curvepoint_fp_multiscalar.c, $(666_ALL_C_SRC))
+666_C_OBJ = $(patsubst $(666_SRC_PATH)/%.c, $(666_OBJ_PATH)/%_c_with_as.o, $(666_C_SRC))
+
+$(666_OBJ_PATH)/%_c_with_as.o: $(666_SRC_PATH)/%.c $(666_INC_PATH)/%.h
+	$(CC) $(CFLAGS) -DQHASM -c -o $@ $<
+
+666_AS_SRC = $(wildcard $(666_SRC_PATH)/*.s)
+666_AS_OBJ = $(patsubst $(666_SRC_PATH)/%.s, $(666_OBJ_PATH)/%_as.o, $(666_AS_SRC))
+
+$(666_OBJ_PATH)/%_as.o: $(666_SRC_PATH)/%.s
+	$(CC) $(CFLAGS) -fPIC -c -o $@ $^
+
+$(BGN_OBJ_PATH)/bgn.a: $(BGN_OBJ) $(666_AS_OBJ) $(666_C_OBJ)
+	rm -f $@
+	ar cr $@ $^
+
+$(BGN_BIN_PATH)/main: $(BGN_OBJ_PATH)/main.o $(BGN_OBJ_PATH)/bgn.a
+	$(CPP) $(CPPFLAGS) -no-pie -o $@ $^ $(LDFLAGS)
+
+.PHONY: clean
+
+clean: bgn_clean 666_clean 
+
+bgn_clean:
+	-rm $(BIN_PATH)/*
+	-rm $(OBJ_PATH)/*
+
+666_clean:
+	-rm $(666_OBJ_PATH)/*

+ 10 - 10
bgn2/inc/BGN.hpp

@@ -1,9 +1,9 @@
 #ifndef __BGN_HPP
 #define __BGN_HPP
 
-#include "Scalar.hpp"
 #include "Bipoint.hpp"
 #include "Quadripoint.hpp"
+#include "Scalar.hpp"
 #include "PublicKey.hpp"
 #include "PrivateKey.hpp"
 
@@ -12,18 +12,18 @@ 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;
+		void encrypt(CurveBipoint& G_element, const Scalar& cleartext) const;
+		void encrypt(TwistBipoint& H_element, const Scalar& cleartext) const;
+		void encrypt(CurveBipoint& G_element, TwistBipoint& 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;
+		CurveBipoint homomorphic_addition(const CurveBipoint& a, const CurveBipoint& b) const;
+		TwistBipoint homomorphic_addition(const TwistBipoint& a, const TwistBipoint& 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;
+		Quadripoint homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& 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;
+		Scalar decrypt(const CurveBipoint& ciphertext) const;
+        Scalar decrypt(const TwistBipoint& ciphertext) const;
+        Scalar decrypt(const Quadripoint & ciphertext) const;
 
         const PublicKey& get_public_key() const;
         const PrivateKey& get_private_key() const;

+ 37 - 26
bgn2/inc/Bipoint.hpp

@@ -1,66 +1,77 @@
 #ifndef __BIPOINT_HPP
 #define __BIPOINT_HPP
 
+#include <functional>
+
 #include "Scalar.hpp"
 
+extern "C" {
 #include "curvepoint_fp.h"
 #include "twistpoint_fp2.h"
+}
 
-/* It doesn't actually make sense to instantiate this generally;
- * we'll specify for each real type directly.
- * We still have to make something here, though, so it's empty. */
-template <typename T>
-class Bipoint
-{};
-
-template <>
-class Bipoint<curvepoint_fp_t>
+class CurveBipoint
 {
 	public:
-		Bipoint();
-		Bipoint(curvepoint_fp_t p1, curvepoint_fp_t p2);
+		CurveBipoint();
+		CurveBipoint(curvepoint_fp_t p1, curvepoint_fp_t p2);
 
 		curvepoint_fp_t& operator[](int n);
 		const curvepoint_fp_t& operator[](int n) const;
 
-		Bipoint<curvepoint_fp_t> operator+(const Bipoint<curvepoint_fp_t>& b) const;
-		Bipoint<curvepoint_fp_t> operator*(const Scalar& mult) const;
+		CurveBipoint operator+(const CurveBipoint& b) const;
+		CurveBipoint operator*(const Scalar& mult) const;
 
-		bool operator==(const Bipoint<curvepoint_fp_t>& b) const;
-		bool operator!=(const Bipoint<curvepoint_fp_t>& b) const;
+		bool operator==(const CurveBipoint& b) const;
+		bool operator!=(const CurveBipoint& b) const;
 
 		// "double" is a type, so you can't just name the function that I don't think, but that's all this is
-		Bipoint<curvepoint_fp_t> mult_by_2() const;	
+		CurveBipoint mult_by_2() const;	
 
 		void make_affine();
+
+		friend class CurveBipointHash;
 	
 	private:
-		curvepoint_fp_t point[2];	
+		curvepoint_fp_t point[2];
+};
+
+class CurveBipointHash
+{
+	public:
+		size_t operator()(const CurveBipoint& x) const;
 };
 
-template <>
-class Bipoint<twistpoint_fp2_t>
+class TwistBipoint
 {
 	public:
-		Bipoint(); 
-		Bipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2);
+		TwistBipoint(); 
+		TwistBipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2);
 
 		twistpoint_fp2_t& operator[](int n);
 		const twistpoint_fp2_t& operator[](int n) const;
 
-		Bipoint<twistpoint_fp2_t> operator+(const Bipoint<twistpoint_fp2_t>& b) const;
-		Bipoint<twistpoint_fp2_t> operator*(const Scalar& mult) const;
+		TwistBipoint operator+(const TwistBipoint& b) const;
+		TwistBipoint operator*(const Scalar& mult) const;
 
-		bool operator==(const Bipoint<twistpoint_fp2_t>& b) const;
-		bool operator!=(const Bipoint<twistpoint_fp2_t>& b) const;
+		bool operator==(const TwistBipoint& b) const;
+		bool operator!=(const TwistBipoint& b) const;
 
 		// "double" is a type, so you can't just name the function that I don't think, but that's all this is
-		Bipoint<twistpoint_fp2_t> mult_by_2() const;
+		TwistBipoint mult_by_2() const;
 
 		void make_affine();
+
+		friend class TwistBipointHash;
 	
 	private:
 		twistpoint_fp2_t point[2];	
 };
 
+class TwistBipointHash
+{
+	public:
+		size_t operator()(const TwistBipoint& x) const;
+};
+
 #endif

+ 9 - 9
bgn2/inc/PrivateKey.hpp

@@ -12,24 +12,24 @@
 class PrivateKey
 {
     public:
-        Scalar decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const;
-        Scalar decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const;
-        Scalar decrypt(const Quadripoint& ciphertext) const;
+        Scalar decrypt(const CurveBipoint& ciphertext) const;
+        Scalar decrypt(const TwistBipoint& ciphertext) const;
+        Scalar decrypt(const Quadripoint & ciphertext) const;
     
     private:
         PrivateKey();
         friend class BGN;
         void set(const PublicKey& pub_key, const Scalar& a1, const Scalar& b1, const Scalar& c1, const Scalar& d1, const Scalar& a2, const Scalar& b2, const Scalar& c2, const Scalar& d2);
 
-        Bipoint<curvepoint_fp_t>  pi_1(const Bipoint<curvepoint_fp_t> & input) const;
-        Bipoint<twistpoint_fp2_t> pi_2(const Bipoint<twistpoint_fp2_t>& input) const;
-        Quadripoint               pi_T(const Quadripoint              & input) const;
+        CurveBipoint pi_1(const CurveBipoint& input) const;
+        TwistBipoint pi_2(const TwistBipoint& input) const;
+        Quadripoint  pi_T(const Quadripoint & input) const;
 
         Scalar a1, b1, c1, d1, a2, b2, c2, d2;
 
-        Bipoint<curvepoint_fp_t>  pi_1_curvegen;
-        Bipoint<twistpoint_fp2_t> pi_2_curvegen;
-        Quadripoint               pi_T_curvegen;        
+        CurveBipoint pi_1_curvegen;
+        TwistBipoint pi_2_twistgen;
+        Quadripoint  pi_T_pairgen;        
 };
 
 #endif

+ 15 - 15
bgn2/inc/PublicKey.hpp

@@ -9,29 +9,29 @@
 class PublicKey
 {
 	public:
-		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;
+		void encrypt(CurveBipoint& G_element, const Scalar& cleartext) const;
+		void encrypt(TwistBipoint& H_element, const Scalar& cleartext) const;
+		void encrypt(CurveBipoint& G_element, TwistBipoint& 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;
+		CurveBipoint homomorphic_addition(const CurveBipoint& a, const CurveBipoint& b) const;
+		TwistBipoint homomorphic_addition(const TwistBipoint& a, const TwistBipoint& 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;
+		Quadripoint homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const;
 		
-		Bipoint<curvepoint_fp_t> get_bipoint_curvegen() const;
-		Bipoint<twistpoint_fp2_t> get_bipoint_twistgen() const;	
-		Bipoint<curvepoint_fp_t> get_bipoint_curve_subgroup_gen() const;
-		Bipoint<twistpoint_fp2_t> get_bipoint_twist_subgroup_gen() const;	
+		CurveBipoint get_bipoint_curvegen() const;
+		TwistBipoint get_bipoint_twistgen() const;	
+		CurveBipoint get_bipoint_curve_subgroup_gen() const;
+		TwistBipoint get_bipoint_twist_subgroup_gen() const;	
 		
 	private:
 		PublicKey();
-		void set(const Bipoint<curvepoint_fp_t>& g, const Bipoint<twistpoint_fp2_t>& h, const Bipoint<curvepoint_fp_t>& g1, const Bipoint<twistpoint_fp2_t>& h1);
+		void set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1);
 		friend class BGN;
 
-		Bipoint<curvepoint_fp_t> bipoint_curvegen; // g
-		Bipoint<twistpoint_fp2_t> bipoint_twistgen; // h
-		Bipoint<curvepoint_fp_t> bipoint_curve_subgroup_gen; // (g^(a1), g^(b1))
-		Bipoint<twistpoint_fp2_t> bipoint_twist_subgroup_gen; // (h^(a2), h^(b2))
+		CurveBipoint bipoint_curvegen; // g
+		TwistBipoint bipoint_twistgen; // h
+		CurveBipoint bipoint_curve_subgroup_gen; // (g^(a1), g^(b1))
+		TwistBipoint bipoint_twist_subgroup_gen; // (h^(a2), h^(b2))
 };
 
 #endif

+ 13 - 1
bgn2/inc/Quadripoint.hpp

@@ -1,9 +1,13 @@
 #ifndef __QUADRIPOINT_HPP
 #define __QUADRIPOINT_HPP
 
+#include <functional>
+
 #include "Scalar.hpp"
 
+extern "C" {
 #include "fp12e.h"
+}
 
 class Quadripoint
 {
@@ -21,9 +25,17 @@ class Quadripoint
 		bool operator!=(const Quadripoint& b) const;
 
 		Quadripoint square() const;
+
+		friend class QuadripointHash;
 	
 	private:
 		fp12e_t point[4];
-}; 
+};
+
+class QuadripointHash
+{
+	public:
+		size_t operator()(const Quadripoint& x) const;
+};
 
 #endif

+ 28 - 18
bgn2/inc/Scalar.hpp

@@ -1,15 +1,18 @@
 #ifndef __SCALAR_HPP
 #define __SCALAR_HPP
 
+#include <iomanip>
 #include <ostream>
 #include <stdlib.h>
 #include <sstream>
 #include <gmpxx.h>
 
-#include "Bipoint.hpp"
-#include "Quadripoint.hpp"
-
+extern "C" {
 #include "scalar.h"
+#include "curvepoint_fp.h"
+#include "twistpoint_fp2.h"
+#include "fp12e.h"
+}
 
 class Scalar
 {
@@ -22,7 +25,6 @@ class Scalar
         void set(mpz_class input);
         void set_random();
 
-        Scalar operator-() const;
         Scalar operator+(const Scalar& b) const;
         Scalar operator-(const Scalar& b) const;
         Scalar operator*(const Scalar& b) const;
@@ -32,12 +34,9 @@ class Scalar
         Scalar& operator--();
         Scalar operator--(int);
 
-        curvepoint_fp_t operator*(const curvepoint_fp_t& b) const;
-        twistpoint_fp2_t operator*(const twistpoint_fp2_t& b) const;
-        fp12e_t operator*(const fp12e_t& b) const;
-        Bipoint<curvepoint_fp_t> operator*(const Bipoint<curvepoint_fp_t>& b) const;
-        Bipoint<twistpoint_fp2_t> operator*(const Bipoint<twistpoint_fp2_t>& b) const;
-        Quadripoint operator*(const Quadripoint& b) const;
+        void mult(curvepoint_fp_t rop, const curvepoint_fp_t& op1) const;
+        void mult(twistpoint_fp2_t rop, const twistpoint_fp2_t& op1) const;
+        void mult(fp12e_t rop, const fp12e_t& op1) const;
 
         bool operator==(const Scalar& b) const;
         bool operator!=(const Scalar& b) const;
@@ -47,14 +46,17 @@ class Scalar
     private:
         class SecretScalar
         {
-            SecretScalar();
-            SecretScalar(const Scalar& input);
-            SecretScalar(mpz_class input);
+            public:
+                SecretScalar();
+                SecretScalar(const Scalar& input);
+                SecretScalar(mpz_class input);
 
-            // Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
-            // Solution: make the return value a reference
-            // This feels bad, I know, but it will only be used in places where the variable remains in scope for the duration of usage
-            const scalar_t& expose() const;
+                /* Problem: thanks to the magic of weird typedefs, scalar_t is actually an array, which complicates returning it
+                 * Solution: make the return value a reference
+                 *
+                 * This feels bad, I know, but it will only be used in places where the variable remains in scope for the duration of usage
+                 * That's also why this class is private -- so it cannot be misused. */
+                const scalar_t& expose() const;
 
             private:
                 void set(mpz_class input);
@@ -62,7 +64,15 @@ class Scalar
         };
         SecretScalar to_scalar_t() const;
 
-        static const mpz_class mpz_bn_n("8FB501E34AA387F9AA6FECB86184DC212E8D8E12F82B39241A2EF45B57AC7261", 16);
+        /* This is the thing everything else is modulused of;
+         *  whenever we do arithmetic of scalars,
+         *  we're doing arithmetic on field elements (\in F_p),
+         *  not directly on curvepoints, so we want p, not n.
+         * Do keep in mind, though, that this means Scalars shouldn't in general
+         *  have arithmetic done on them prior to interacting with curvepoints,
+         *  if you're calculating something like an exponentiation of products 
+         *  of Scalars (or similar). */
+        static const mpz_class mpz_bn_p;
 
         mpz_class element;
 };

+ 3 - 2
bgn2/inc/pairing.hpp

@@ -4,10 +4,11 @@
 #include "Quadripoint.hpp"
 #include "Bipoint.hpp"
 
+extern "C" {
 #include "optate.h"
+}
 
-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);
+Quadripoint pairing(const CurveBipoint& op1, const TwistBipoint& op2);
 
 #endif
 

+ 29 - 22
bgn2/src/BGN.cpp

@@ -1,5 +1,8 @@
 #include "BGN.hpp"
 
+extern const curvepoint_fp_t bn_curvegen;
+extern const twistpoint_fp2_t bn_twistgen;
+
 BGN::BGN()
 {
 	Scalar a1, b1, c1, d1, a2, b2, c2, d2;
@@ -10,7 +13,7 @@ BGN::BGN()
         b1.set_random();
         c1.set_random();
 
-        if (a1 != 0)
+        if (a1 != Scalar(0))
         {
             d1 = (b1 * c1 + Scalar(1)) / a1;
             break;
@@ -23,7 +26,7 @@ BGN::BGN()
         b2.set_random();
         c2.set_random();
 
-        if (a2 != 0)
+        if (a2 != Scalar(0))
         {
             d2 = (b2 * c2 + Scalar(1)) / a2;
             break;
@@ -37,20 +40,24 @@ BGN::BGN()
     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);
+    
+    r1.mult(g_part1, bn_curvegen);
+    r2.mult(g_part2, bn_curvegen);
+    a1.mult(g_a1, bn_curvegen);
+    b1.mult(g_b1, bn_curvegen);
+
+    CurveBipoint full_g(g_part1, g_part2);
+    CurveBipoint 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);
+
+    r3.mult(h_part1, bn_twistgen);
+    r4.mult(h_part2, bn_twistgen);
+    a2.mult(h_a2, bn_twistgen);
+    b2.mult(h_b2, bn_twistgen);
+    
+    TwistBipoint full_h(h_part1, h_part2);
+    TwistBipoint full_h1(h_a2, h_b2);
 
     full_g.make_affine();
     full_g1.make_affine();
@@ -61,27 +68,27 @@ BGN::BGN()
     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
+void BGN::encrypt(CurveBipoint& 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
+void BGN::encrypt(TwistBipoint& 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
+void BGN::encrypt(CurveBipoint& G_element, TwistBipoint& 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
+CurveBipoint BGN::homomorphic_addition(const CurveBipoint& a, const CurveBipoint& 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
+TwistBipoint BGN::homomorphic_addition(const TwistBipoint& a, const TwistBipoint& b) const
 {
 	return public_key.homomorphic_addition(a, b);
 }
@@ -91,17 +98,17 @@ Quadripoint BGN::homomorphic_addition(const Quadripoint& a, const Quadripoint& b
 	return public_key.homomorphic_addition(a, b);
 }
 
-Quadripoint BGN::homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
+Quadripoint BGN::homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const
 {
 	return public_key.homomorphic_multiplication(a, b);
 }
 
-Scalar BGN::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const
+Scalar BGN::decrypt(const CurveBipoint& ciphertext) const
 {
 	return private_key.decrypt(ciphertext);
 }
 
-Scalar BGN::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const
+Scalar BGN::decrypt(const TwistBipoint& ciphertext) const
 {
 	return private_key.decrypt(ciphertext);
 }

+ 110 - 62
bgn2/src/Bipoint.cpp

@@ -1,52 +1,52 @@
 #include "Bipoint.hpp"
 
-Bipoint<curvepoint_fp_t>::Bipoint()
+CurveBipoint::CurveBipoint()
 {
 	curvepoint_fp_setneutral(point[0]);
 	curvepoint_fp_setneutral(point[1]);
 }
 
-Bipoint<twistpoint_fp2_t>::Bipoint()
+TwistBipoint::TwistBipoint()
 {
 	twistpoint_fp2_setneutral(point[0]);
 	twistpoint_fp2_setneutral(point[1]);
 }
 
-Bipoint<curvepoint_fp_t>::Bipoint(curvepoint_fp_t p1, curvepoint_fp_t p2)
+CurveBipoint::CurveBipoint(curvepoint_fp_t p1, curvepoint_fp_t p2)
 {
 	curvepoint_fp_set(point[0], p1);
 	curvepoint_fp_set(point[1], p2);
 }
 
-Bipoint<twistpoint_fp2_t>::Bipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2)
+TwistBipoint::TwistBipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2)
 {
 	twistpoint_fp2_set(point[0], p1);
 	twistpoint_fp2_set(point[1], p2);
 }
 
-curvepoint_fp_t& Bipoint<curvepoint_fp_t>::operator[](int n)
+curvepoint_fp_t& CurveBipoint::operator[](int n)
 {
 	return point[n];
 }
 
-twistpoint_fp2_t& Bipoint<twistpoint_fp2_t>::operator[](int n)
+twistpoint_fp2_t& TwistBipoint::operator[](int n)
 {
 	return point[n];
 }
 
-const curvepoint_fp_t& Bipoint<curvepoint_fp_t>::operator[](int n) const
+const curvepoint_fp_t& CurveBipoint::operator[](int n) const
 {
 	return point[n];
 }
 
-const twistpoint_fp2_t& Bipoint<twistpoint_fp2_t>::operator[](int n) const
+const twistpoint_fp2_t& TwistBipoint::operator[](int n) const
 {
 	return point[n];
 }
 
-Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::operator+(const Bipoint<curvepoint_fp_t>& b) const
+CurveBipoint CurveBipoint::operator+(const CurveBipoint& b) const
 {
-	Bipoint<curvepoint_fp_t> retval;
+	CurveBipoint retval;
 
 	curvepoint_fp_add_vartime(retval[0], point[0], b.point[0]);
 	curvepoint_fp_add_vartime(retval[1], point[1], b.point[1]);
@@ -54,9 +54,9 @@ Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::operator+(const Bipoint<curve
 	return retval;
 }
 
-Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::operator+(const Bipoint<twistpoint_fp2_t>& b) const
+TwistBipoint TwistBipoint::operator+(const TwistBipoint& b) const
 {
-	Bipoint<twistpoint_fp2_t> retval;
+	TwistBipoint retval;
 
 	twistpoint_fp2_add_vartime(retval[0], point[0], b.point[0]);
 	twistpoint_fp2_add_vartime(retval[1], point[1], b.point[1]);
@@ -64,83 +64,87 @@ Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::operator+(const Bipoint<twi
 	return retval;
 }
 
-Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::operator*(const Scalar& mult) const
+CurveBipoint CurveBipoint::operator*(const Scalar& exp) const
 {
-	Bipoint<curvepoint_fp_t> retval;
+	CurveBipoint retval;
 
-	retval[0] = mult * point[0];
-	retval[1] = mult * point[1];
+	exp.mult(retval[0], point[0]);
+	exp.mult(retval[1], point[1]);
 
 	return retval;
 }
 
-Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::operator*(const Scalar& mult) const
+TwistBipoint TwistBipoint::operator*(const Scalar& exp) const
 {
-	Bipoint<twistpoint_fp2_t> retval;
+	TwistBipoint retval;
 
-	retval[0] = mult * point[0];
-	retval[1] = mult * point[1];
+	exp.mult(retval[0], point[0]);
+	exp.mult(retval[1], point[1]);
 
 	return retval;
 }
 
-bool Bipoint<curvepoint_fp_t>::operator==(const Bipoint<curvepoint_fp_t>& b) const
+bool CurveBipoint::operator==(const CurveBipoint& b) const
 {
+	curvepoint_fp_t affine_point0, affine_point1, affine_b0, affine_b1;
+
+	curvepoint_fp_set(affine_point0, point[0]);
+	curvepoint_fp_set(affine_point1, point[1]);
+	curvepoint_fp_set(affine_b0, b[0]);
+	curvepoint_fp_set(affine_b1, b[1]);
+
+	curvepoint_fp_makeaffine(affine_point0);
+	curvepoint_fp_makeaffine(affine_point1);
+	curvepoint_fp_makeaffine(affine_b0);
+	curvepoint_fp_makeaffine(affine_b1);
+
 	bool retval;
 
-	fpe_t point0_x1z2, point0_y1z2, point0_x2z1, point0_y2z1, point1_x1z2, point1_y1z2, point1_x2z1, point1_y2z1;
-	fpe_mul(point0_x1z2, point[0]->m_x, b[0]->m_z);
-	fpe_mul(point0_y1z2, point[0]->m_y, b[0]->m_z);
-	fpe_mul(point0_x2z1, point[0]->m_z, b[0]->m_x);
-	fpe_mul(point0_y2z1, point[0]->m_z, b[0]->m_y);
-	fpe_mul(point1_x1z2, point[1]->m_x, b[1]->m_z);
-	fpe_mul(point1_y1z2, point[1]->m_y, b[1]->m_z);
-	fpe_mul(point1_x2z1, point[1]->m_z, b[1]->m_x);
-	fpe_mul(point1_y2z1, point[1]->m_z, b[1]->m_y);
-
-	retval   = fpe_iseq(point0_x1z2, point0_x2z1);
-	retval &&= fpe_iseq(point0_y1z2, point0_y2z1); 
-	retval &&= fpe_iseq(point1_x1z2, point1_x2z1);
-	retval &&= fpe_iseq(point1_y1z2, point1_y2z1); 
+	retval =           fpe_iseq(affine_point0->m_x, affine_b0->m_x);
+	retval = retval && fpe_iseq(affine_point0->m_y, affine_b0->m_y); 
+	retval = retval && fpe_iseq(affine_point1->m_x, affine_b1->m_x);
+	retval = retval && fpe_iseq(affine_point1->m_y, affine_b1->m_y);
 
 	return retval;
 }
 
-bool Bipoint<twistpoint_fp2_t>::operator==(const Bipoint<twistpoint_fp2_t>& b) const
+bool TwistBipoint::operator==(const TwistBipoint& b) const
 {
+	twistpoint_fp2_t affine_point0, affine_point1, affine_b0, affine_b1;
+
+	twistpoint_fp2_set(affine_point0, point[0]);
+	twistpoint_fp2_set(affine_point1, point[1]);
+	twistpoint_fp2_set(affine_b0, b[0]);
+	twistpoint_fp2_set(affine_b1, b[1]);
+
+	twistpoint_fp2_makeaffine(affine_point0);
+	twistpoint_fp2_makeaffine(affine_point1);
+	twistpoint_fp2_makeaffine(affine_b0);
+	twistpoint_fp2_makeaffine(affine_b1);
+
 	bool retval;
 
-	fp2e_t point0_x1z2, point0_y1z2, point0_x2z1, point0_y2z1, point1_x1z2, point1_y1z2, point1_x2z1, point1_y2z1;
-	fp2e_mul(point0_x1z2, point[0]->m_x, b[0]->m_z);
-	fp2e_mul(point0_y1z2, point[0]->m_y, b[0]->m_z);
-	fp2e_mul(point0_x2z1, point[0]->m_z, b[0]->m_x);
-	fp2e_mul(point0_y2z1, point[0]->m_z, b[0]->m_y);
-	fp2e_mul(point1_x1z2, point[1]->m_x, b[1]->m_z);
-	fp2e_mul(point1_y1z2, point[1]->m_y, b[1]->m_z);
-	fp2e_mul(point1_x2z1, point[1]->m_z, b[1]->m_x);
-	fp2e_mul(point1_y2z1, point[1]->m_z, b[1]->m_y);
-
-	retval   = fp2e_iseq(point0_x1z2, point0_x2z1);
-	retval &&= fp2e_iseq(point0_y1z2, point0_y2z1); 
-	retval &&= fp2e_iseq(point1_x1z2, point1_x2z1);
-	retval &&= fp2e_iseq(point1_y1z2, point1_y2z1);
+	retval =           fp2e_iseq(affine_point0->m_x, affine_b0->m_x);
+	retval = retval && fp2e_iseq(affine_point0->m_y, affine_b0->m_y); 
+	retval = retval && fp2e_iseq(affine_point1->m_x, affine_b1->m_x);
+	retval = retval && fp2e_iseq(affine_point1->m_y, affine_b1->m_y);
 
 	return retval;
 }
 
-bool Bipoint<curvepoint_fp_t>::operator!=(const Bipoint<curvepoint_fp_t>& b) const
+bool CurveBipoint::operator!=(const CurveBipoint& b) const
 {
 	return !(*this == b);
 }
 
-bool Bipoint<twistpoint_fp2_t>::operator!=(const Bipoint<twistpoint_fp2_t>& b) const
+bool TwistBipoint::operator!=(const TwistBipoint& b) const
 {
 	return !(*this == b);
 }
 
-Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::mult_by_2() const
+CurveBipoint CurveBipoint::mult_by_2() const
 {
-	Bipoint<curvepoint_fp_t> retval;
+	CurveBipoint retval;
 
 	curvepoint_fp_double(retval[0], point[0]);
 	curvepoint_fp_double(retval[1], point[1]);
@@ -148,9 +152,9 @@ Bipoint<curvepoint_fp_t> Bipoint<curvepoint_fp_t>::mult_by_2() const
 	return retval;
 }
 
-Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::mult_by_2() const
+TwistBipoint TwistBipoint::mult_by_2() const
 {
-	Bipoint<twistpoint_fp2_t> retval;
+	TwistBipoint retval;
 
 	twistpoint_fp2_double(retval[0], point[0]);
 	twistpoint_fp2_double(retval[1], point[1]);
@@ -158,14 +162,58 @@ Bipoint<twistpoint_fp2_t> Bipoint<twistpoint_fp2_t>::mult_by_2() const
 	return retval;
 }
 
-void Bipoint<curvepoint_fp_t>::make_affine()
+void CurveBipoint::make_affine()
+{
+	if (!(fpe_isone(point[0]->m_z)))
+		curvepoint_fp_makeaffine(point[0]);
+	if (!(fpe_isone(point[1]->m_z)))
+		curvepoint_fp_makeaffine(point[1]);
+}
+
+void TwistBipoint::make_affine()
 {
-	curvepoint_fp_makeaffine(point[0]);
-	curvepoint_fp_makeaffine(point[1]);
+	if (!(fp2e_isone(point[0]->m_z)))
+		twistpoint_fp2_makeaffine(point[0]);
+	if (!(fp2e_isone(point[1]->m_z)))
+		twistpoint_fp2_makeaffine(point[1]);
 }
 
-void Bipoint<twistpoint_fp2_t>::make_affine()
+size_t CurveBipointHash::operator()(const CurveBipoint& x) const
 {
-	twistpoint_fp2_makeaffine(point[0]);
-	twistpoint_fp2_makeaffine(point[1]);
+	size_t retval = 0;
+	CurveBipoint affine_x = x;
+	std::hash<double> hasher;
+	
+	affine_x.make_affine();
+
+	for (int i = 0; i < 2; i++)
+	{
+		for (int j = 0; j < 12; j++)
+		{
+			retval ^= hasher(affine_x[i]->m_x->v[j]);
+			retval ^= hasher(affine_x[i]->m_y->v[j]);
+		}
+	}
+
+	return retval;
+}
+
+size_t TwistBipointHash::operator()(const TwistBipoint& x) const
+{
+	size_t retval = 0;
+	TwistBipoint affine_x = x;
+	std::hash<double> hasher;
+	
+	affine_x.make_affine();
+
+	for (int i = 0; i < 2; i++)
+	{
+		for (int j = 0; j < 24; j++)
+		{
+			retval ^= hasher(affine_x[i]->m_x->v[j]);
+			retval ^= hasher(affine_x[i]->m_y->v[j]);
+		}
+	}
+
+	return retval;
 }

+ 0 - 163
bgn2/src/Makefile

@@ -1,163 +0,0 @@
-# COMMENTAIRES EN FIN DE FICHIER
-
-CPP = g++
-CPPFLAGS = -std=c++14 -I ../include -I ../gengetopt -I  ../dclxvi-20130329  -O2  -Wextra -fPIC -fopenmp  -D_GLIBCXX_USE_CXX11_ABI=0 
-# -g et -pg rajoutés à la compilation et à l'édition de lien pour profiler le programme 
-# -fprofile-arcs -ftest-coverage rajoutés à la compilation et à l'édition de lien pour couvrir le programme
-LDFLAGS =-Wall  -Wextra -fopenmp -lgmp -lgmpxx #-g -pg 
-EXEC = bgn bgn_check bgn_as 
-
-#chemin sans fichier
-HDRPATH = ../include
-SRCPATH = ../src
-OBJPATH = ../obj
-BINPATH = ../bin
-LIBPATH = ../lib
-666PATH = ../dclxvi-20130329
-VPATH   = $(SRCPATH):$(666PATH):../gengetopt 
-#UPDPATH = ../update
-
-#fichier avec chemin
-HDR 	      = $(wildcard $(HDRPATH)/*.h*)
-ALL_SRC		  = $(wildcard $(SRCPATH)/*.c*)
-SRC           = $(filter-out $(SRCPATH)/test_quadruplet.cpp, $(ALL_SRC)) 
-OBJ		      = $(patsubst $(SRCPATH)/%.cpp,$(OBJPATH)/%.o,$(SRC))
-OBJ_CHECK     = $(patsubst $(SRCPATH)/%.cpp,$(OBJPATH)/%_check.o,$(SRC))
-OBJ_AS        = $(patsubst $(SRCPATH)/%.cpp,$(OBJPATH)/%_as.o,$(SRC))
-ALL_SRC_666	  = $(wildcard $(666PATH)/*.c*)
-SRC_666       = $(filter-out $(666PATH)/bilintest.c $(666PATH)/speedtest.c  $(666PATH)/test_curvepoint_multiscalar.c  $(666PATH)/test_twistpoint_multiscalar.c $(666PATH)/twistpoint_fp2_multiscalar.c $(666PATH)/curvepoint_fp_multiscalar.c, $(ALL_SRC_666))
-ASS_666		  = $(wildcard $(666PATH)/*.s)
-#HDR_UPD	      = $(wildcard $(UPDPATH)/*.h)
-#SRC_UPD       = $(wildcard $(UPDPATH)/*.c)
-#ASS_UPD       = $(wildcard $(UPDPATH)/*.s)
-OBJ_666   	  = $(patsubst $(666PATH)/%.c,$(OBJPATH)/%_666.o,$(SRC_666)) 
-#$(patsubst $(UPDPATH)/%.c,$(OBJPATH)/%.o,$(SRC_UPD))
-OBJ_666_CHECK = $(patsubst $(666PATH)/%.c,$(OBJPATH)/%_666_check.o,$(SRC_666)) 
-#$(patsubst $(UPDPATH)/%.c,$(OBJPATH)/%_check.o,$(SRC_UPD))
-OBJ_666_AS    = $(patsubst $(666PATH)/%.c,$(OBJPATH)/%_666_as.o,$(SRC_666)) $(patsubst $(666PATH)/%.s,$(OBJPATH)/%_666_as.o,$(ASS_666)) 
-#$(patsubst $(UPDPATH)/%.c,$(OBJPATH)/%_as.o,$(SRC_UPD)) $(patsubst $(UPDPATH)/%.s,$(OBJPATH)/%_as.o,$(ASS_UPD))
-LIB    = $(filter-out $(OBJPATH)/option.o $(OBJPATH)/bgn.o $(OBJPATH)/circuit_additionL1.o $(OBJPATH)/circuit_additionL2.o $(OBJPATH)/circuit_additionL3.o $(OBJPATH)/circuit_additionL4.o $(OBJPATH)/circuit_chiffrement.o $(OBJPATH)/circuit_minmaj.o $(OBJPATH)/circuit_minmaj2.o $(OBJPATH)/circuit_minmaj3.o $(OBJPATH)/circuit_multiplicationL1.o $(OBJPATH)/circuit_multiplicationL1L2.o $(OBJPATH)/circuit_multiplicationL2.o $(OBJPATH)/circuit_size.o, $(OBJPATH)/circuit_scalar_product.o, $(OBJPATH)/circuit_time.o, $(OBJPATH)/circuit_time2.o , $(OBJ)) 
-LIB_CHECK   = $(filter-out $(OBJPATH)/option.o $(OBJPATH)/bgn_check.o $(OBJPATH)/circuit_additionL1_check.o $(OBJPATH)/circuit_additionL2_check.o $(OBJPATH)/circuit_additionL3_check.o $(OBJPATH)/circuit_additionL4_check.o $(OBJPATH)/circuit_chiffrement_check.o $(OBJPATH)/circuit_minmaj_check.o $(OBJPATH)/circuit_minmaj2_check.o $(OBJPATH)/circuit_minmaj3_check.o $(OBJPATH)/circuit_multiplicationL1_check.o $(OBJPATH)/circuit_multiplicationL1L2_check.o $(OBJPATH)/circuit_multiplicationL2_check.o $(OBJPATH)/circuit_size_check.o ,$(OBJPATH)/circuit_scalar_product_check.o, $(OBJPATH)/circuit_time_check.o, $(OBJPATH)/circuit_time2_check.o , $(OBJ_CHECK)) 
-LIB_AS   = $(filter-out $(OBJPATH)/option.o $(OBJPATH)/bgn_as.o $(OBJPATH)/circuit_additionL1_as.o $(OBJPATH)/circuit_additionL2_as.o $(OBJPATH)/circuit_additionL3_as.o $(OBJPATH)/circuit_additionL4_as.o $(OBJPATH)/circuit_chiffrement_as.o $(OBJPATH)/circuit_minmaj_as.o $(OBJPATH)/circuit_minmaj2_as.o $(OBJPATH)/circuit_minmaj3_as.o $(OBJPATH)/circuit_multiplicationL1_as.o $(OBJPATH)/circuit_multiplicationL1L2_as.o $(OBJPATH)/circuit_multiplicationL2_as.o $(OBJPATH)/circuit_size_as.o ,$(OBJPATH)/circuit_scalar_product_as.o, $(OBJPATH)/circuit_time_as.o, $(OBJPATH)/circuit_time2_as.o , $(OBJ_AS)) 
-all:  $(EXEC)
-
-bgn: $(OBJ) $(OBJPATH)/option.o $(OBJ_666)  $(OBJPATH)/scalar_sub_nored_666_as.o $(OBJPATH)/heap_rootreplaced_666_as.o
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS)
-
-bgn_check: $(OBJ_CHECK) $(OBJPATH)/option.o $(OBJ_666_CHECK) $(OBJPATH)/scalar_sub_nored_666_as.o $(OBJPATH)/heap_rootreplaced_666_as.o 
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS)
-	
-bgn_as: $(OBJ_AS) $(OBJPATH)/option.o   $(OBJ_666_AS)
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS)
-
-lib:
-	@gcc -shared -fPIC -o $(LIBPATH)/libbgnfcf.so $(LIB)
-
-lib_check:
-	@gcc -shared -fPIC -o $(LIBPATH)/libbgnfcf_check.so $(LIB_CHECK)	
-
-lib_as:
-	@gcc -shared -fPIC -o $(LIBPATH)/libbgnfcf_as.so $(LIB_AS)	
-	
-bgn_lib: $(OBJPATH)/option.o $(OBJ_666)  $(OBJPATH)/scalar_sub_nored_666_as.o $(OBJPATH)/heap_rootreplaced_666_as.o	 $(OBJPATH)/bgn.o $(OBJPATH)/circuit_additionL1.o $(OBJPATH)/circuit_additionL2.o $(OBJPATH)/circuit_additionL3.o $(OBJPATH)/circuit_additionL4.o $(OBJPATH)/circuit_chiffrement.o $(OBJPATH)/circuit_minmaj.o $(OBJPATH)/circuit_minmaj2.o $(OBJPATH)/circuit_minmaj3.o $(OBJPATH)/circuit_multiplicationL1.o $(OBJPATH)/circuit_multiplicationL1L2.o $(OBJPATH)/circuit_multiplicationL2.o $(OBJPATH)/circuit_size.o $(OBJPATH)/circuit_demo.o  $(OBJPATH)/circuit_scalar_product.o $(OBJPATH)/circuit_time.o $(OBJPATH)/circuit_time2.o
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS) -L ../lib -lbgnfcf
-
-bgn_lib_check: $(OBJPATH)/option.o $(OBJ_666_CHECK)  $(OBJPATH)/scalar_sub_nored_666_as.o $(OBJPATH)/heap_rootreplaced_666_as.o	 $(OBJPATH)/bgn_check.o $(OBJPATH)/circuit_additionL1_check.o $(OBJPATH)/circuit_additionL2_check.o $(OBJPATH)/circuit_additionL3_check.o $(OBJPATH)/circuit_additionL4_check.o $(OBJPATH)/circuit_chiffrement_check.o $(OBJPATH)/circuit_minmaj_check.o $(OBJPATH)/circuit_minmaj2_check.o $(OBJPATH)/circuit_minmaj3_check.o $(OBJPATH)/circuit_multiplicationL1_check.o $(OBJPATH)/circuit_multiplicationL1L2_check.o $(OBJPATH)/circuit_multiplicationL2_check.o $(OBJPATH)/circuit_size_check.o  $(OBJPATH)/circuit_scalar_product_check.o $(OBJPATH)/circuit_time_check.o $(OBJPATH)/circuit_time2_check.o
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS) -L ../lib -lbgnfcf_check
-	
-bgn_lib_as:  $(OBJPATH)/option.o   $(OBJ_666_AS) $(OBJPATH)/bgn_as.o $(OBJPATH)/circuit_additionL1_as.o $(OBJPATH)/circuit_additionL2_as.o $(OBJPATH)/circuit_additionL3_as.o $(OBJPATH)/circuit_additionL4_as.o $(OBJPATH)/circuit_chiffrement_as.o $(OBJPATH)/circuit_minmaj_as.o $(OBJPATH)/circuit_minmaj2_as.o $(OBJPATH)/circuit_minmaj3_as.o $(OBJPATH)/circuit_multiplicationL1_as.o $(OBJPATH)/circuit_multiplicationL1L2_as.o $(OBJPATH)/circuit_multiplicationL2_as.o $(OBJPATH)/circuit_size_as.o   $(OBJPATH)/circuit_scalar_product_as.o  $(OBJPATH)/circuit_time_as.o $(OBJPATH)/circuit_time2_as.o 
-	$(CPP) -o $(BINPATH)/$@ $+ $(LDFLAGS) -L ../lib -lbgnfcf_as
-	
-test_quadruplet_check: $(OBJPATH)/test_quadruplet_check.o $(OBJPATH)/BitChiffre_check.o $(OBJPATH)/quadruplet_check.o $(OBJPATH)/$(OBJPATH)/fpe2scalar_check.o  $(OBJ_666_CHECK) $(OBJPATH)/scalar_sub_nored_666_as.o $(OBJPATH)/heap_rootreplaced_666_as.o $(OBJPATH)/Bipoint_check.o 
-	$(CPP) $(LDFLAGS) -o $(BINPATH)/$@ $+
-	
-test_quadruplet_as: $(OBJPATH)/test_quadruplet_as.o $(OBJPATH)/BitChiffre_as.o $(OBJPATH)/quadruplet_as.o $(OBJ_666_AS) $(OBJPATH)/Bipoint_as.o $(OBJPATH)/$(OBJPATH)/fpe2scalar_as.o
-	$(CPP) $(LDFLAGS) -o $(BINPATH)/$@ $+
-		
-option : option.ggo
-	@gengetopt < $<  --file-name=option --output-dir=../gengetopt --unamed-opts
-
-.PHONY: clean	
-
-clean:
-	@rm -rf $(OBJPATH)/*.o  $(BINPATH)/bgn $(BINPATH)/bgn_as $(BINPATH)/bgn_check $(BINPATH)/bgn_lib $(BINPATH)/bgn_lib_as $(BINPATH)/bgn_lib_check prof *.exe *.db *.do *.gch *.out $(OBJPATH)/*.gcda $(OBJPATH)/*.gcno *.gcov callgrind.out.*
-
-
-profile: 
-	@read -p "Enter binary name: " binary && gprof -l $(BINPATH)/$$binary gmon.out >| profile_line && gprof $(BINPATH)/$$binary gmon.out >| profile_function 
-# le fichier gmon.out est crée après avoir executé le programme, il ne suffit pas de compiler et d'éditer les liens avec -g -pg 
-# read pour demander au maker d'affecter une variable
-# $$ pour acceder à cette valeur (elle est définie dans les build commands, pas dans le makefile)
-# && parce que la valeur n'est pas mémorisée
-
-tata=4
-fun:
-	export toto=5
-	sleep 5 && echo $$toto
-	toto=3; echo $$toto
-	echo $(tata)		
-	
-graph:
-	@gprof2dot profile_function >| graph_function.dot
-	@dot -T png -o graph_function.png graph_function.dot
-	@feh graph_function.png&
-	
-doc:
-	cd .. && doxygen  doc/doxygen_conf && firefox doc/html/annotated.html&
-
-
-gdb: 
-	gdb run ../bin/bgn_as	
-	
-valgrind:
-	valgrind --track-origins=yes --leak-check=full  --show-leak-kinds=all  ../bin/bgn_as -s ""
-	
-print-%:
-	@echo '$*=$($*)'
-
-$(OBJPATH)/option.o : option.c 
-	$(CPP) $(CPPFLAGS) -o $@ -c $<	
-		
-$(OBJPATH)/%.o : %.cpp $(HDR) 
-	$(CPP) $(CPPFLAGS) -o $@ -c $<	
-
-$(OBJPATH)/%_check.o :	%.cpp $(HDR) 
-	$(CPP) $(CPPFLAGS) -DCHECK -DNBTESTS=10 -o $@ -c $<	
-	
-$(OBJPATH)/%_as.o :	%.cpp $(HDR) 
-	$(CPP) $(CPPFLAGS) -DQHASM -DNBTESTS=10 -o $@ -c $<		
-		
-$(OBJPATH)/%_666.o :	$(666PATH)/%.c $(666PATH)/%.h 
-	$(CPP) $(CPPFLAGS) -o $@ -c $<		
-		
-$(OBJPATH)/%_666_check.o :	$(666PATH)/%.c $(666PATH)/%.h
-	$(CPP) $(CPPFLAGS) -DCHECK  -o $@ -c $<	
-	
-$(OBJPATH)/%_666_as.o :	$(666PATH)/%.c $(666PATH)/%.h
-	$(CPP) $(CPPFLAGS) -DQHASM  -o $@ -c $<		
-
-$(OBJPATH)/%_666_as.o :	$(666PATH)/%.s
-	$(CPP) $(CPPFLAGS) -o $@ -c $<	
-	
-#$(OBJPATH)/%.o :	$(UPDPATH)/%.c  $(HDR_UPD)
-#	$(CPP) $(CPPFLAGS) -o $@ -c $<	
-
-#$(OBJPATH)/%_check.o :	$(UPDPATH)/%.c $(HDR_UPD)
-#	$(CPP) $(CPPFLAGS) -DCHECK  -o $@ -c $<	
-	
-#$(OBJPATH)/%_as.o :	$(UPDPATH)/%.c $(HDR_UPD)
-#	$(CPP) $(CPPFLAGS) -DQHASM  -o $@ -c $<	
-
-#$(OBJPATH)/%_as.o :	$(UPDPATH)/%.s
-#	$(CPP) $(CPPFLAGS) -o $@ -c $<	
-		
-#$(OBJPATH)/asfunctions.a: $(OBJPATH)/fp2e_add2.o $(OBJPATH)/fp2e_sub2.o \
-#	$(OBJPATH)/fp2e_double2.o $(OBJPATH)/fp2e_triple2.o $(OBJPATH)/fp2e_neg2.o \
-#	$(OBJPATH)/fp2e_mul.o $(OBJPATH)/fp2e_mul_fpe.o $(OBJPATH)/fp2e_short_coeffred.o \
-#	$(OBJPATH)/fp2e_add.o $(OBJPATH)/fp2e_sub.o $(OBJPATH)/fp2e_parallel_coeffmul.o $(OBJPATH)/fp2e_mulxi.o\
-#	$(OBJPATH)/fp2e_double.o $(OBJPATH)/fp2e_triple.o $(OBJPATH)/fp2e_neg.o $(OBJPATH)/fp2e_conjugate.o \
-#	$(OBJPATH)/fpe_mul.o $(OBJPATH)/fp2e_square.o \
-#	$(OBJPATH)/consts.o
-#	rm -f $(OBJPATH)/asfunctions.a
-#	ar cr $(OBJPATH)/asfunctions.a $^
-

+ 107 - 61
bgn2/src/PrivateKey.cpp

@@ -1,11 +1,11 @@
 #include "PrivateKey.hpp"
 
-Scalar PrivateKey::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext)
+Scalar PrivateKey::decrypt(const CurveBipoint& ciphertext) const
 {
-    static std::unordered_map<Bipoint<curvepoint_fp_t>, Scalar> memoizer;
+    static std::unordered_map<CurveBipoint, Scalar, CurveBipointHash> memoizer;
     static Scalar max_checked = Scalar(0);
 
-    Bipoint<curvepoint_fp_t> pi_1_ciphertext = pi_1(ciphertext); 
+    CurveBipoint pi_1_ciphertext = pi_1(ciphertext); 
 
     auto lookup = memoizer.find(pi_1_ciphertext);
     if (lookup != memoizer.end())
@@ -13,7 +13,7 @@ Scalar PrivateKey::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext)
         return lookup->second;
     }
 
-    Bipoint<curvepoint_fp_t> i = pi_1_curvegen * max_checked;
+    CurveBipoint i = pi_1_curvegen * max_checked;
     do
     {
         memoizer[pi_1_ciphertext] = max_checked++;
@@ -23,12 +23,12 @@ Scalar PrivateKey::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext)
     return max_checked - Scalar(1);
 }
 
-Scalar PrivateKey::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext)
+Scalar PrivateKey::decrypt(const TwistBipoint& ciphertext) const
 {
-    static std::unordered_map<Bipoint<twistpoint_fp2_t>, Scalar> memoizer;
+    static std::unordered_map<TwistBipoint, Scalar, TwistBipointHash> memoizer;
     static Scalar max_checked = Scalar(0);
 
-    Bipoint<twistpoint_fp2_t> pi_2_ciphertext = pi_2(ciphertext);
+    TwistBipoint pi_2_ciphertext = pi_2(ciphertext);
 
     auto lookup = memoizer.find(pi_2_ciphertext);
     if (lookup != memoizer.end())
@@ -36,7 +36,7 @@ Scalar PrivateKey::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext)
         return lookup->second;
     }
 
-    Bipoint<twistpoint_fp2_t> i = pi_2_twistgen * max_checked;
+    TwistBipoint i = pi_2_twistgen * max_checked;
     do
     {
         memoizer[pi_2_ciphertext] = max_checked++;
@@ -47,9 +47,9 @@ Scalar PrivateKey::decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext)
 }
 
 
-void PrivateKey::decrypt(const Quadripoint& ciphertext)
+Scalar PrivateKey::decrypt(const Quadripoint& ciphertext) const
 {
-    static std::unordered_map<Quadripoint, Scalar> memoizer;
+    static std::unordered_map<Quadripoint, Scalar, QuadripointHash> memoizer;
     static Scalar max_checked = Scalar(0);
 
     Quadripoint pi_T_ciphertext = pi_T(ciphertext); 
@@ -63,7 +63,7 @@ void PrivateKey::decrypt(const Quadripoint& ciphertext)
     Quadripoint i = pi_T_pairgen * max_checked;
     do
     {
-        memoizer[pi_2_ciphertext] = max_checked++;
+        memoizer[pi_T_ciphertext] = max_checked++;
         i = i + pi_T_pairgen;
     } while (i != pi_T_ciphertext);
 
@@ -86,62 +86,66 @@ void PrivateKey::set(const PublicKey& pub_key, const Scalar& a1, const Scalar& b
     this->d2 = d2;
 
     this->pi_1_curvegen = pi_1(pub_key.get_bipoint_curvegen());
-    this->pi_2_curvegen = pi_2(pub_key.get_bipoint_twistgen());
-    this->pi_T_curvegen = pi_T(pairing(pub_key.get_bipoint_curvegen(), pub_key.get_bipoint_twistgen()));
+    this->pi_2_twistgen = pi_2(pub_key.get_bipoint_twistgen());
+    this->pi_T_pairgen  = pi_T(pairing(pub_key.get_bipoint_curvegen(), pub_key.get_bipoint_twistgen()));
 }
 
-Bipoint<curvepoint_fp_t> PrivateKey::pi_1(const Bipoint<curvepoint_fp_t>& input) const
+CurveBipoint PrivateKey::pi_1(const CurveBipoint& input) const
 {
-    Bipoint<curvepoint_fp_t> retval;
+    CurveBipoint retval;
     curvepoint_fp_t temp0, temp1;
     
 
-    temp0 = b1 * (c1 * input[0]);
+    b1.mult(temp0, input[0]);
+    c1.mult(temp0, temp0);
     curvepoint_fp_neg(temp0, temp0);
 
-    temp1 = a1 * (c1 * input[1]);
+    a1.mult(temp1, input[1]);
+    c1.mult(temp1, temp1);
+
     curvepoint_fp_add_vartime(temp0, temp0, temp1);
-    
-    curvepoint_fp_makeaffine(temp0);
     curvepoint_fp_set(retval[0], temp0);
 
 
-    temp0 = b1 * (d1 * input[0]);
+    b1.mult(temp0, input[0]);
+    d1.mult(temp0, temp0);
     curvepoint_fp_neg(temp0, temp0);
 
-    temp1 = a1 * (d1 * input[1]);
+    a1.mult(temp1, input[1]);
+    d1.mult(temp1, temp1);
+
     curvepoint_fp_add_vartime(temp0, temp0, temp1);
-    
-    curvepoint_fp_makeaffine(temp0);
     curvepoint_fp_set(retval[1], temp0);
 
 
     return retval;
 }
 
-Bipoint<twistpoint_fp2_t> PrivateKey::pi_2(const Bipoint<twistpoint_fp2_t>& input) const
+TwistBipoint PrivateKey::pi_2(const TwistBipoint& input) const
 {
-    Bipoint<twistpoint_fp2_t> retval;
+    TwistBipoint retval;
     twistpoint_fp2_t temp0, temp1;
     
 
-    temp0 = b2 * (c2 * input[0]);
+    b2.mult(temp0, input[0]);
+    c2.mult(temp0, temp0);
     twistpoint_fp2_neg(temp0, temp0);
 
-    temp1 = a2 * (c2 * input[1]);
+    a2.mult(temp1, input[1]);
+    c2.mult(temp1, temp1);
+
     twistpoint_fp2_add_vartime(temp0, temp0, temp1);
-    
-    twistpoint_fp2_makeaffine(temp0);
     twistpoint_fp2_set(retval[0], temp0);
 
 
-    temp0 = b2 * (d2 * input[0]);
+    b2.mult(temp0, input[0]);
+    d2.mult(temp0, temp0);
     twistpoint_fp2_neg(temp0, temp0);
 
-    temp1 = a2 * (d2 * input[1]);
+    a2.mult(temp1, input[1]);
+    d2.mult(temp1, temp1);
+
     twistpoint_fp2_add_vartime(temp0, temp0, temp1);
-    
-    twistpoint_fp2_makeaffine(temp0);
     twistpoint_fp2_set(retval[1], temp0);
 
 
@@ -154,15 +158,27 @@ Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
     fp12e_t temp0, temp1, temp2, temp3;
 
 
-    temp0 = c2 * (b2 * (c1 * (b1 * input[0])));
-    
-    fp12e_invert(temp1, input[1]);
-    temp1 = c2 * (a2 * (c1 * (b1 * temp1)));
+    b1.mult(temp0, input[0]);
+    c1.mult(temp0, temp0);
+    b2.mult(temp0, temp0);
+    c2.mult(temp0, temp0);
     
-    fp12e_invert(temp2, input[2]);
-    temp2 = c2 * (b2 * (c1 * (a1 * temp2)));
+    b1.mult(temp1, input[1]);
+    c1.mult(temp1, temp1);
+    a2.mult(temp1, temp1);
+    c2.mult(temp1, temp1);
+    fp12e_invert(temp1, temp1);
+
+    a1.mult(temp2, input[2]);
+    c1.mult(temp2, temp2);
+    b2.mult(temp2, temp2);
+    c2.mult(temp2, temp2);
+    fp12e_invert(temp2, temp2);
     
-    temp3 = c2 * (a2 * (c1 * (a1 * input[3])));
+    a1.mult(temp3, input[3]);
+    c1.mult(temp3, temp3);
+    a2.mult(temp3, temp3);
+    c2.mult(temp3, temp3);
 
     fp12e_mul(temp0, temp0, temp1);
     fp12e_mul(temp1, temp2, temp3);
@@ -170,17 +186,27 @@ Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
     fp12e_set(retval[0], temp0);
 
 
-    temp0 = d2 * (b2 * (c1 * (b1 * input[0])));
+    b1.mult(temp0, input[0]);
+    c1.mult(temp0, temp0);
+    b2.mult(temp0, temp0);
+    d2.mult(temp0, temp0);
     
-    temp1 = b1 * input[0];
+    b1.mult(temp1, input[1]);
+    c1.mult(temp1, temp1);
+    a2.mult(temp1, temp1);
+    d2.mult(temp1, temp1);
     fp12e_invert(temp1, temp1);
-    temp1 = d2 * (a2 * (c1 * temp1));
 
-    temp2 = b2 * (c1 * (a1 * input[2]));
+    a1.mult(temp2, input[2]);
+    c1.mult(temp2, temp2);
+    b2.mult(temp2, temp2);
+    d2.mult(temp2, temp2);
     fp12e_invert(temp2, temp2);
-    temp2 = d2 * temp2;
-
-    temp3 = d2 * (a2 * (c1 * (a1 * input[3])));
+    
+    a1.mult(temp3, input[3]);
+    c1.mult(temp3, temp3);
+    a2.mult(temp3, temp3);
+    d2.mult(temp3, temp3);
 
     fp12e_mul(temp0, temp0, temp1);
     fp12e_mul(temp1, temp2, temp3);
@@ -188,17 +214,27 @@ Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
     fp12e_set(retval[1], temp0);
     
 
-    temp0 = c2 * (b2 * (d1 * (b1 * input[0])));
+    b1.mult(temp0, input[0]);
+    d1.mult(temp0, temp0);
+    b2.mult(temp0, temp0);
+    c2.mult(temp0, temp0);
     
-    temp1 = b1 * input[0];
+    b1.mult(temp1, input[1]);
+    d1.mult(temp1, temp1);
+    a2.mult(temp1, temp1);
+    c2.mult(temp1, temp1);
     fp12e_invert(temp1, temp1);
-    temp1 = c2 * (a2 * (d1 * temp1));
 
-    temp2 = b2 * (d1 * (a1 * input[2]));
+    a1.mult(temp2, input[2]);
+    d1.mult(temp2, temp2);
+    b2.mult(temp2, temp2);
+    c2.mult(temp2, temp2);
     fp12e_invert(temp2, temp2);
-    temp2 = c2 * temp2;
-
-    temp3 = c2 * (a2 * (d1 * (a1 * input[3])));
+    
+    a1.mult(temp3, input[3]);
+    d1.mult(temp3, temp3);
+    a2.mult(temp3, temp3);
+    c2.mult(temp3, temp3);
     
     fp12e_mul(temp0, temp0, temp1);
     fp12e_mul(temp1, temp2, temp3);
@@ -206,17 +242,27 @@ Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
     fp12e_set(retval[2], temp0);
     
 
-    temp0 = d2 * (b2 * (d1 * (b1 * input[0])));
+    b1.mult(temp0, input[0]);
+    d1.mult(temp0, temp0);
+    b2.mult(temp0, temp0);
+    d2.mult(temp0, temp0);
     
-    temp1 = b1 * input[0];
+    b1.mult(temp1, input[1]);
+    d1.mult(temp1, temp1);
+    a2.mult(temp1, temp1);
+    d2.mult(temp1, temp1);
     fp12e_invert(temp1, temp1);
-    temp1 = d2 * (a2 * (d1 * temp1));
 
-    temp2 = b2 * (d1 * (a1 * input[2]));
+    a1.mult(temp2, input[2]);
+    d1.mult(temp2, temp2);
+    b2.mult(temp2, temp2);
+    d2.mult(temp2, temp2);
     fp12e_invert(temp2, temp2);
-    temp2 = d2 * temp2;
-
-    temp3 = d2 * (a2 * (d1 * (a1 * input[3])));
+    
+    a1.mult(temp3, input[3]);
+    d1.mult(temp3, temp3);
+    a2.mult(temp3, temp3);
+    d2.mult(temp3, temp3);
 
     fp12e_mul(temp0, temp0, temp1);
     fp12e_mul(temp1, temp2, temp3);

+ 21 - 35
bgn2/src/PublicKey.cpp

@@ -1,121 +1,107 @@
 #include "PublicKey.hpp"
 
-void PublicKey::encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const
+void PublicKey::encrypt(CurveBipoint& G_element, const Scalar& cleartext) const
 {
     Scalar lambda;
     lambda.set_random();
 
-    Bipoint<curvepoint_fp_t> cleartext_as_element, random_mask;
+    CurveBipoint cleartext_as_element, random_mask;
     cleartext_as_element = get_bipoint_curvegen() * cleartext;
     random_mask = get_bipoint_curve_subgroup_gen() * lambda;
 
     G_element = cleartext_as_element + random_mask;
 }
 
-void PublicKey::encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
+void PublicKey::encrypt(TwistBipoint& H_element, const Scalar& cleartext) const
 {
     Scalar lambda;
     lambda.set_random();
 
-    Bipoint<twistpoint_fp2_t> cleartext_as_element, random_mask;
+    TwistBipoint cleartext_as_element, random_mask;
     cleartext_as_element = get_bipoint_twistgen() * cleartext;
     random_mask = get_bipoint_twist_subgroup_gen() * lambda;
 
     H_element = cleartext_as_element + random_mask;
 }
 
-void PublicKey::encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const
+void PublicKey::encrypt(CurveBipoint& G_element, TwistBipoint& H_element, const Scalar& cleartext) const
 {
     encrypt(G_element, cleartext);
     encrypt(H_element, cleartext);
 }
 
-Bipoint<curvepoint_fp_t> homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const
+CurveBipoint PublicKey::homomorphic_addition(const CurveBipoint& a, const CurveBipoint& b) const
 {
     Scalar lambda;
     lambda.set_random();
 
-    Bipoint<curvepoint_fp_t> random_mask;
+    CurveBipoint random_mask;
     random_mask = bipoint_curve_subgroup_gen * lambda;
 
     return a + b + random_mask;
 }
 
-Bipoint<twistpoint_fp2_t> homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
+TwistBipoint PublicKey::homomorphic_addition(const TwistBipoint& a, const TwistBipoint& b) const
 {
     Scalar lambda;
     lambda.set_random();
 
-    Bipoint<curvepoint_fp_t> random_mask;
+    TwistBipoint random_mask;
     random_mask = bipoint_twist_subgroup_gen * lambda;
 
     return a + b + random_mask;
 }
 
-Quadripoint homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
+Quadripoint PublicKey::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
 {
     Quadripoint random_mask;
-    Bipoint<curvepoint_fp_t> random_mask_curve;
-    Bipoint<twistpoint_fp2_t> random_mask_twist;
+    CurveBipoint random_mask_curve;
+    TwistBipoint random_mask_twist;
     
     Scalar lambda1, lambda2;
     lambda1.set_random();
     lambda2.set_random();
 
     random_mask_curve = bipoint_curve_subgroup_gen * lambda1;
-    random_mask_curve.make_affine();
-
     random_mask_twist = bipoint_twist_subgroup_gen * lambda2;
-    random_mask_twist.make_affine();
-
     random_mask = pairing(bipoint_curvegen, random_mask_twist) + pairing(random_mask_curve, bipoint_twistgen);
 
     return a + b + random_mask;
 }
 
-Quadripoint homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const
+Quadripoint PublicKey::homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const
 {
     Quadripoint random_mask;
-    Bipoint<curvepoint_fp_t> random_mask_curve;
-    Bipoint<twistpoint_fp2_t> random_mask_twist;
-    
-    Bipoint<curvepoint_fp_t> a_copy = a;
-    Bipoint<twistpoint_fp2_t> b_copy = b;
-
-    a_copy.make_affine();
-    b_copy.make_affine();
+    CurveBipoint random_mask_curve;
+    TwistBipoint random_mask_twist;
     
     Scalar lambda1, lambda2;
     lambda1.set_random();
     lambda2.set_random();
 
     random_mask_curve = bipoint_curve_subgroup_gen * lambda1;
-    random_mask_curve.makeaffine();
-
     random_mask_twist = bipoint_twist_subgroup_gen * lambda2;
-    random_mask_twist.makeaffine();
-
     random_mask = pairing(bipoint_curvegen, random_mask_twist) + pairing(random_mask_curve, bipoint_twistgen);
 
-    return pairing(a_copy, b_copy) + random_mask;
+    return pairing(a, b) + random_mask;
 }
 
-Bipoint<curvepoint_fp_t> PublicKey::get_bipoint_curvegen() const
+CurveBipoint PublicKey::get_bipoint_curvegen() const
 {
     return bipoint_curvegen;
 }
 
-Bipoint<twistpoint_fp2_t> PublicKey::get_bipoint_twistgen() const
+TwistBipoint PublicKey::get_bipoint_twistgen() const
 {
     return bipoint_twistgen;
 }
 
-Bipoint<curvepoint_fp_t> PublicKey::get_bipoint_curve_subgroup_gen() const
+CurveBipoint PublicKey::get_bipoint_curve_subgroup_gen() const
 {
     return bipoint_curve_subgroup_gen;
 }
 
-Bipoint<twistpoint_fp2_t> PublicKey::get_bipoint_twist_subgroup_gen() const
+TwistBipoint PublicKey::get_bipoint_twist_subgroup_gen() const
 {
     return bipoint_twist_subgroup_gen;
 }
@@ -123,7 +109,7 @@ Bipoint<twistpoint_fp2_t> PublicKey::get_bipoint_twist_subgroup_gen() const
 PublicKey::PublicKey()
 { }
 
-void PublicKey::set(const Bipoint<curvepoint_fp_t>& g, const Bipoint<twistpoint_fp2_t>& h, const Bipoint<curvepoint_fp_t>& g1, const Bipoint<twistpoint_fp2_t>& h1)
+void PublicKey::set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1)
 {
     bipoint_curvegen = g;
     bipoint_twistgen = h;

+ 35 - 8
bgn2/src/Quadripoint.cpp

@@ -42,20 +42,22 @@ Quadripoint Quadripoint::operator*(const Scalar& exp) const
 {
 	Quadripoint retval;
 	
-	retval[0] = exp * point[0];
-	retval[1] = exp * point[1];
-	retval[2] = exp * point[2];
-	retval[3] = exp * point[3];
+	exp.mult(retval[0], point[0]);
+	exp.mult(retval[1], point[1]);
+	exp.mult(retval[2], point[2]);
+	exp.mult(retval[3], point[3]);
 
 	return retval;	
 }
 
 bool Quadripoint::operator==(const Quadripoint& b) const
 {
-	bool retval = fp12e_iseq(point[0], b[0]);
-	retval &&= fp12e_iseq(point[1], b[1]);
-	retval &&= fp12e_iseq(point[2], b[2]);
-	retval &&= fp12e_iseq(point[3], b[3]);
+	bool retval;
+	
+	retval =           fp12e_iseq(point[0], b[0]);
+	retval = retval && fp12e_iseq(point[1], b[1]);
+	retval = retval && fp12e_iseq(point[2], b[2]);
+	retval = retval && fp12e_iseq(point[3], b[3]);
 	
 	return retval;
 }
@@ -76,3 +78,28 @@ Quadripoint Quadripoint::square() const
 
 	return retval;	
 }
+
+size_t QuadripointHash::operator()(const Quadripoint& x) const
+{
+	size_t retval = 0;
+	std::hash<double> hasher;
+
+	for (int i = 0; i < 4; i++)
+	{
+		for (int j = 0; j < 24; j++)
+		{
+			/* I'm so sorry for pointer hell here,
+			 * the types are just like this */
+
+			retval ^= hasher(x[i]->m_a->m_a->v[j]);
+			retval ^= hasher(x[i]->m_a->m_b->v[j]);
+			retval ^= hasher(x[i]->m_a->m_c->v[j]);
+
+			retval ^= hasher(x[i]->m_b->m_a->v[j]);
+			retval ^= hasher(x[i]->m_b->m_b->v[j]);
+			retval ^= hasher(x[i]->m_b->m_c->v[j]);
+		}
+	}
+
+	return retval;
+}

+ 43 - 50
bgn2/src/Scalar.cpp

@@ -2,6 +2,8 @@
 
 extern const scalar_t bn_n;
 
+const mpz_class Scalar::mpz_bn_p("8FB501E34AA387F9AA6FECB86184DC21EE5B88D120B5B59E185CAC6C5E089667", 16);
+
 Scalar::Scalar()
 {
     element = 0;
@@ -14,28 +16,42 @@ Scalar::Scalar(const scalar_t& input)
 
 Scalar::Scalar(mpz_class input)
 {
-    element = input;
+    set(input);
 }
 
 void Scalar::set(const scalar_t& input)
 {
-    std::stringstream buffer;
-    std::string temp;
-    buffer << std::hex << input[3] << input[2] << input[1] << input[0];
-    buffer >> temp;
+    std::stringstream bufferstream;
+    std::string buffer;
+    mpz_class temp;
+
+    bufferstream << std::hex << input[3] << input[2] << input[1] << input[0];
+    bufferstream >> buffer;
     
-    element.set_str(temp, 16);
+    temp.set_str(buffer, 16);
+
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
+
+    element = temp;
 }
 
 void Scalar::set(mpz_class input)
 {
-    element = input;
+    mpz_class temp = input;
+
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
+
+    element = temp;
 }
 
 void Scalar::set_random()
 {
     scalar_t temp;
     
+    /* When we ask for a random number,
+     * we really mean a seed to find a random element of a group
+     * and the order of the curve either is bn_n or is divided by it
+     * (not bn_p) */
     scalar_setrandom(temp, bn_n);
 
     set(temp);
@@ -45,7 +61,7 @@ Scalar Scalar::operator+(const Scalar& b) const
 {
     mpz_class temp = element + b.element;
 
-    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return Scalar(temp);
 }
@@ -54,7 +70,7 @@ Scalar Scalar::operator-(const Scalar& b) const
 {
     mpz_class temp = element - b.element;
 
-    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return Scalar(temp);
 }
@@ -63,7 +79,7 @@ Scalar Scalar::operator*(const Scalar& b) const
 {
     mpz_class temp = element * b.element;
 
-    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return Scalar(temp);
 }
@@ -71,10 +87,10 @@ Scalar Scalar::operator*(const Scalar& b) const
 Scalar Scalar::operator/(const Scalar& b) const
 {
     mpz_class temp;
-    mpz_invert(temp.get_mpz_t(), b.element.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_invert(temp.get_mpz_t(), b.element.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     temp *= element;
-    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(temp.get_mpz_t(), temp.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return Scalar(temp);
 }
@@ -82,7 +98,7 @@ Scalar Scalar::operator/(const Scalar& b) const
 Scalar& Scalar::operator++()
 {
     element++;
-    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return *this;
 }
@@ -92,7 +108,7 @@ Scalar Scalar::operator++(int)
     Scalar retval = *this;
     
     element++;
-    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return retval;
 }
@@ -100,7 +116,7 @@ Scalar Scalar::operator++(int)
 Scalar& Scalar::operator--()
 {
     element--;
-    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
+    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return *this;
 }
@@ -110,51 +126,30 @@ Scalar Scalar::operator--(int)
     Scalar retval = *this;
     
     element--;
-    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_n.get_mpz_t());
-
-    return retval;
-}
-
-curvepoint_fp_t Scalar::operator*(const curvepoint_fp_t& b) const
-{
-    curvepoint_fp_t retval;
-
-    curvepoint_fp_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
+    mpz_mod(element.get_mpz_t(), element.get_mpz_t(), mpz_bn_p.get_mpz_t());
 
     return retval;
 }
 
-twistpoint_fp2_t Scalar::operator*(const twistpoint_fp2_t& b) const
+void Scalar::mult(curvepoint_fp_t rop, const curvepoint_fp_t& op1) const
 {
-    twistpoint_fp2_t retval;
+    SecretScalar secret_element = to_scalar_t();
 
-    twistpoint_fp2_scalarmult_vartime(retval, b, element.to_scalar_t().expose());
-
-    return retval;
+    curvepoint_fp_scalarmult_vartime(rop, op1, secret_element.expose());
 }
 
-fp12e_t Scalar::operator*(const fp12e_t& b) const
+void Scalar::mult(twistpoint_fp2_t rop, const twistpoint_fp2_t& op1) const
 {
-    fp12e_t retval;
+    SecretScalar secret_element = to_scalar_t();
 
-    fp12e_pow_vartime(retval, b, element.to_scalar_t().expose());
-
-    return retval;
+    twistpoint_fp2_scalarmult_vartime(rop, op1, secret_element.expose());
 }
 
-Bipoint<curvepoint_fp_t> Scalar::operator*(const Bipoint<curvepoint_fp_t>& b) const
+void Scalar::mult(fp12e_t rop, const fp12e_t& op1) const
 {
-    return b * *this;
-}
+    SecretScalar secret_element = to_scalar_t();
 
-Bipoint<twistpoint_fp2_t> Scalar::operator*(const Bipoint<twistpoint_fp2_t>& b) const
-{
-    return b * *this;
-}
-
-Quadripoint Scalar::operator*(const Quadripoint& b) const
-{
-    return b * *this;
+    fp12e_pow_vartime(rop, op1, secret_element.expose());
 }
 
 bool Scalar::operator==(const Scalar& b) const
@@ -168,9 +163,7 @@ bool Scalar::operator!=(const Scalar& b) const
 }
 
 Scalar::SecretScalar::SecretScalar()
-{
-    element = {0,0,0,0};
-}
+{ }
 
 Scalar::SecretScalar::SecretScalar(const Scalar& input)
 {
@@ -191,7 +184,7 @@ void Scalar::SecretScalar::set(mpz_class input)
 {
     std::stringstream buffer;
     char temp[17];
-    buffer << std::setfill('0') << std::setw(64) << input.get_string(16);
+    buffer << std::setfill('0') << std::setw(64) << input.get_str(16);
 
     for (int i = 3; i >= 0; i--)
     {

+ 59 - 0
bgn2/src/main.cpp

@@ -0,0 +1,59 @@
+#include <iostream>
+
+#include "BGN.hpp"
+
+int main(void)
+{
+    BGN system;
+
+    Scalar test3(3);
+    Scalar test5(5);
+    Scalar decrypted3, decrypted5, decrypted6, decrypted10, decrypted15, decrypted30;
+
+    std::cout << "Scalar test value (3): " << test3 << std::endl;
+    std::cout << "Scalar test value (5): " << test5 << std::endl;
+
+    CurveBipoint encrypted3, encrypted6;
+    TwistBipoint encrypted5, encrypted10;
+    Quadripoint encrypted15, encrypted30;
+
+    std::cout << "Performing encryptions" << std::endl;
+
+    system.encrypt(encrypted3, test3);
+    system.encrypt(encrypted5, test5);
+
+    std::cout << "Performing additions" << std::endl;
+
+    encrypted6 = system.homomorphic_addition(encrypted3, encrypted3);
+    encrypted10 = system.homomorphic_addition(encrypted5, encrypted5);
+
+    std::cout << "Performing multiplication" << std::endl;
+
+    encrypted15 = system.homomorphic_multiplication(encrypted3, encrypted5);
+
+    std::cout << "Performing L2 addition" << std::endl;
+
+    encrypted30 = system.homomorphic_addition(encrypted15, encrypted15);
+
+    std::cout << "Performing decryptions" << std::endl;
+
+    decrypted3  = system.decrypt(encrypted3);
+    std::cout << "Scalar decrypted value (3):   " << decrypted3  << std::endl;
+
+    decrypted5  = system.decrypt(encrypted5);
+    std::cout << "Scalar decrypted value (5):   " << decrypted5  << std::endl;
+
+    decrypted6  = system.decrypt(encrypted6);
+    std::cout << "Scalar decrypted value (6):   " << decrypted6  << std::endl;
+
+    decrypted10 = system.decrypt(encrypted10);
+    std::cout << "Scalar decrypted value (10): "  << decrypted10 << std::endl;
+
+    decrypted15 = system.decrypt(encrypted15);
+    std::cout << "Scalar decrypted value (15): "  << decrypted15 << std::endl;
+
+    decrypted30 = system.decrypt(encrypted30);
+    std::cout << "Scalar decrypted value (30): "  << decrypted30 << std::endl;
+
+    return 0;
+}

+ 10 - 13
bgn2/src/pairing.cpp

@@ -1,22 +1,19 @@
 #include "pairing.hpp"
 
-fp12e_t OptimalAte(const curvepoint_fp_t& op1, const twistpoint_fp2_t& op2)
+Quadripoint pairing(const CurveBipoint& op1, const TwistBipoint& op2)
 {
-    fp12e_t retval;
+    CurveBipoint affine_op1 = op1;
+    TwistBipoint affine_op2 = op2;
 
-	optate(retval, op2, op1);
-
-    return retval;
-}
-
-Quadripoint pairing(const Bipoint<curvepoint_fp_t>& op1, const Bipoint<twistpoint_fp2_t>& op2)
-{
+    affine_op1.make_affine();
+    affine_op2.make_affine();
+    
 	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]);
+	optate(retval[0], affine_op2[0], affine_op1[0]);
+	optate(retval[1], affine_op2[1], affine_op1[0]);
+	optate(retval[2], affine_op2[0], affine_op1[1]);
+	optate(retval[3], affine_op2[1], affine_op1[1]);
 	
     return retval;
 }