Browse Source

refactor down one last time, now PublicKey does the actual addition/multiplication

tristangurtler 3 years ago
parent
commit
579500ee80

+ 3 - 2
bgn2/inc/PrivateKey.hpp

@@ -6,15 +6,16 @@
 #include "Scalar.hpp"
 #include "Bipoint.hpp"
 #include "Quadripoint.hpp"
+#include "PublicKey.hpp"
 #include "pairing.hpp"
 
 class PrivateKey
 {
     public:
         PrivateKey() = default; 
-        PrivateKey(const Scalar& a1, const Scalar& b1, const Scalar& c1, const Scalar& d1, const Scalar& a2, const Scalar& b2, const Scalar& c2, const Scalar& d2);
+        PrivateKey(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);
 
-        void set(const Scalar& a1, const Scalar& b1, const Scalar& c1, const Scalar& d1, const Scalar& a2, const Scalar& b2, const Scalar& c2, const Scalar& d2);
+        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);
 
         Scalar decrypt(const Bipoint<curvepoint_fp_t>& ciphertext) const;
         Scalar decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const;

+ 8 - 1
bgn2/inc/PublicKey.hpp

@@ -1,8 +1,10 @@
 #ifndef __PUBLICKEY_HPP
 #define __PUBLICKEY_HPP
 
-#include "Bipoint.hpp"
 #include "Scalar.hpp"
+#include "Bipoint.hpp"
+#include "Quadripoint.hpp"
+#include "pairing.hpp"
 
 class PublicKey
 {
@@ -15,6 +17,11 @@ class PublicKey
 		void encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const;
 		void encrypt(Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const;
 		void encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_fp2_t>& H_element, const Scalar& cleartext) const;
+
+		Bipoint<curvepoint_fp_t> homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const;
+		Bipoint<twistpoint_fp2_t> homomorphic_addition(const Bipoint<twistpoint_fp2_t>& a, const Bipoint<twistpoint_fp2_t>& b) const;
+		Quadripoint homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const;
+		Quadripoint homomorphic_multiplication(const Bipoint<curvepoint_fp_t>& a, const Bipoint<twistpoint_fp2_t>& b) const;
 		
 		Bipoint<curvepoint_fp_t> get_bipoint_curvegen() const;
 		Bipoint<twistpoint_fp2_t> get_bipoint_twistgen() const;	

+ 0 - 14
bgn2/inc/homomorphic_operations.hpp

@@ -1,14 +0,0 @@
-#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 */

+ 1 - 0
bgn2/inc/pairing.hpp

@@ -3,6 +3,7 @@
 
 #include "Quadripoint.hpp"
 #include "Bipoint.hpp"
+
 #include "optate.h"
 
 fp12e_t OptimalAte(const curvepoint_fp_t& op1, const twistpoint_fp2_t& op2);

+ 0 - 2
bgn2/src/PrivateKey.cpp

@@ -1,7 +1,5 @@
 #include "PrivateKey.hpp"
 
-extern const curvepoint_fp_t bn_curvegen;
-
 PrivateKey::PrivateKey(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)
 {
     set(pub_key, a1, b1, c1, d1, a2, b2, c2, d2);

+ 70 - 0
bgn2/src/PublicKey.cpp

@@ -44,6 +44,76 @@ void PublicKey::encrypt(Bipoint<curvepoint_fp_t>& G_element, Bipoint<twistpoint_
     encrypt(H_element, cleartext);
 }
 
+Bipoint<curvepoint_fp_t> homomorphic_addition(const Bipoint<curvepoint_fp_t>& a, const Bipoint<curvepoint_fp_t>& b) const
+{
+    Scalar lambda;
+    lambda.set_random();
+
+    Bipoint<curvepoint_fp_t> 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
+{
+    Scalar lambda;
+    lambda.set_random();
+
+    Bipoint<curvepoint_fp_t> 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 random_mask;
+    Bipoint<curvepoint_fp_t> random_mask_curve;
+    Bipoint<twistpoint_fp2_t> 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 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();
+    
+    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;
+}
+
 Bipoint<curvepoint_fp_t> PublicKey::get_bipoint_curvegen() const
 {
     return bipoint_curvegen;

+ 0 - 79
bgn2/src/homomorphic_operations.cpp

@@ -1,79 +0,0 @@
-#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;
-}

+ 5 - 0
bgn2/src/keygen.cpp

@@ -56,6 +56,11 @@ void keygen(PublicKey& public_key, PrivateKey& private_key)
     h_b2 = b2 * bn_twistgen;
     Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
     Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
+
+    full_g.make_affine();
+    full_g1.make_affine();
+    full_h.make_affine();
+    full_h1.make_affine();
         
     public_key.set(full_g, full_h, full_g1, full_h1);
 }