Browse Source

final touches on that BGN class refactor -- you should only be able to get validly made public and private keys

tristangurtler 3 years ago
parent
commit
b9afa528a7
6 changed files with 53 additions and 53 deletions
  1. 2 1
      bgn2/inc/BGN.hpp
  2. 9 10
      bgn2/inc/PrivateKey.hpp
  3. 4 5
      bgn2/inc/PublicKey.hpp
  4. 6 1
      bgn2/src/BGN.cpp
  5. 20 22
      bgn2/src/PrivateKey.cpp
  6. 12 14
      bgn2/src/PublicKey.cpp

+ 2 - 1
bgn2/inc/BGN.hpp

@@ -25,7 +25,8 @@ class BGN
         Scalar decrypt(const Bipoint<twistpoint_fp2_t>& ciphertext) const;
         Scalar decrypt(const Quadripoint& ciphertext) const;
 
-        PublicKey get_public_key() const;
+        const PublicKey& get_public_key() const;
+        const PublicKey& get_private_key() const;
 
 	private:
 		PublicKey public_key;

+ 9 - 10
bgn2/inc/PrivateKey.hpp

@@ -12,25 +12,24 @@
 class PrivateKey
 {
     public:
-        PrivateKey() = default; 
-        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 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;
         Scalar decrypt(const Quadripoint& ciphertext) const;
     
     private:
-        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;
+        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;
+
+        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;        
 };
 
 #endif

+ 4 - 5
bgn2/inc/PublicKey.hpp

@@ -9,11 +9,6 @@
 class PublicKey
 {
 	public:
-		PublicKey() = default;
-		PublicKey(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 Bipoint<curvepoint_fp_t>& g, const Bipoint<twistpoint_fp2_t>& h, const Bipoint<curvepoint_fp_t>& g1, const Bipoint<twistpoint_fp2_t>& h1);
-		
 		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;
@@ -29,6 +24,10 @@ class PublicKey
 		Bipoint<twistpoint_fp2_t> 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);
+		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))

+ 6 - 1
bgn2/src/BGN.cpp

@@ -111,7 +111,12 @@ Scalar BGN::decrypt(const Quadripoint& ciphertext) const
 	return private_key.decrypt(ciphertext);
 }
 
-PublicKey BGN::get_public_key() const
+const PublicKey& BGN::get_public_key() const
 {
 	return public_key;
 }
+
+const PrivateKey& BGN::get_private_key() const
+{
+    return private_key;
+}

+ 20 - 22
bgn2/src/PrivateKey.cpp

@@ -1,27 +1,5 @@
 #include "PrivateKey.hpp"
 
-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);
-}
-
-void PrivateKey::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)
-{
-    this->a1 = a1;
-    this->b1 = b1;
-    this->c1 = c1;
-    this->d1 = d1;
-
-    this->a2 = a2;
-    this->b2 = b2;
-    this->c2 = c2;
-    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()));
-}
-
 Scalar PrivateKey::decrypt(const Bipoint<curvepoint_fp_t>& ciphertext)
 {
     static std::unordered_map<Bipoint<curvepoint_fp_t>, Scalar> memoizer;
@@ -92,6 +70,26 @@ void PrivateKey::decrypt(const Quadripoint& ciphertext)
     return max_checked - Scalar(1);
 }
 
+PrivateKey::PrivateKey()
+{ }
+
+void PrivateKey::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)
+{
+    this->a1 = a1;
+    this->b1 = b1;
+    this->c1 = c1;
+    this->d1 = d1;
+
+    this->a2 = a2;
+    this->b2 = b2;
+    this->c2 = c2;
+    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()));
+}
+
 Bipoint<curvepoint_fp_t> PrivateKey::pi_1(const Bipoint<curvepoint_fp_t>& input) const
 {
     Bipoint<curvepoint_fp_t> retval;

+ 12 - 14
bgn2/src/PublicKey.cpp

@@ -1,19 +1,5 @@
 #include "PublicKey.hpp"
 
-PublicKey(const Bipoint<curvepoint_fp_t>& g, const Bipoint<twistpoint_fp2_t>& h, const Bipoint<curvepoint_fp_t>& g1, const Bipoint<twistpoint_fp2_t>& h1)
-{
-    set(g, h, g1, h1);
-}
-
-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)
-{
-    bipoint_curvegen = g;
-    bipoint_twistgen = h;
-    
-    bipoint_curve_subgroup_gen = g1;
-    bipoint_twist_subgroup_gen = h1;
-}
-
 void PublicKey::encrypt(Bipoint<curvepoint_fp_t>& G_element, const Scalar& cleartext) const
 {
     Scalar lambda;
@@ -133,3 +119,15 @@ Bipoint<twistpoint_fp2_t> PublicKey::get_bipoint_twist_subgroup_gen() const
 {
     return bipoint_twist_subgroup_gen;
 }
+
+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)
+{
+    bipoint_curvegen = g;
+    bipoint_twistgen = h;
+    
+    bipoint_curve_subgroup_gen = g1;
+    bipoint_twist_subgroup_gen = h1;
+}