Browse Source

additions to make things easier in prsona

tristangurtler 3 years ago
parent
commit
15f16757ca

+ 4 - 0
bgn2/inc/BGN.hpp

@@ -25,6 +25,10 @@ class BGN
         Scalar decrypt(const TwistBipoint& ciphertext);
         Scalar decrypt(const Quadripoint & ciphertext);
 
+        CurveBipoint rerandomize(const CurveBipoint& a) const;
+        TwistBipoint rerandomize(const TwistBipoint& a) const;
+        Quadripoint rerandomize(const Quadripoint& a) const;
+
         const PublicKey& get_public_key() const;
         const PrivateKey& get_private_key() const;
 

+ 44 - 0
bgn2/inc/Curvepoint.hpp

@@ -0,0 +1,44 @@
+#ifndef __CURVEPOINT_HPP
+#define __CURVEPOINT_HPP
+
+#include <ostream>
+#include <functional>
+
+#include "print_helpers.hpp"
+
+extern "C" {
+#include "curvepoint_fp.h"
+}
+
+class Curvepoint
+{
+    public:
+        Curvepoint();
+        Curvepoint(curvepoint_fp_t input);
+
+        Curvepoint operator+(const Curvepoint& b) const;
+        Curvepoint operator*(const Scalar& mult) const;
+
+        bool operator==(const Curvepoint& b) const;
+        bool operator<(const Curvepoint& b) const;
+        bool operator>(const Curvepoint& b) const;
+        bool operator<=(const Curvepoint& b) const;
+        bool operator>=(const Curvepoint& b) const;
+        bool operator!=(const Curvepoint& b) const;
+
+        void make_affine();
+
+        friend class CurvepointHash;
+        friend std::ostream& operator<<(std::ostream& os, const Curvepoint& output);   
+        
+    private:
+        curvepoint_fp_t point;
+};
+
+class CurvepointHash
+{
+    public:
+        size_t operator()(const Curvepoint& x) const;
+};
+
+#endif

+ 7 - 5
bgn2/inc/PrivateKey.hpp

@@ -1,5 +1,5 @@
-#ifndef __PRIVATEKEY_HPP
-#define __PRIVATEKEY_HPP
+#ifndef __BGN_PRIVATEKEY_HPP
+#define __BGN_PRIVATEKEY_HPP
 
 #include <iostream>
 #include <unordered_map>
@@ -10,17 +10,19 @@
 #include "PublicKey.hpp"
 #include "pairing.hpp"
 
-class PrivateKey
+class BGNPrivateKey
 {
     public:
+        BGNPrivateKey(const BGNPrivateKey& other);
+        
         Scalar decrypt(const CurveBipoint& ciphertext);
         Scalar decrypt(const TwistBipoint& ciphertext);
         Scalar decrypt(const Quadripoint & ciphertext);
     
     private:
-        PrivateKey();
+        BGNPrivateKey();
         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);
+        void set(const BGNPublicKey& 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);
 
         CurveBipoint pi_1(const CurveBipoint& input) const;
         TwistBipoint pi_2(const TwistBipoint& input) const;

+ 10 - 5
bgn2/inc/PublicKey.hpp

@@ -1,14 +1,15 @@
-#ifndef __PUBLICKEY_HPP
-#define __PUBLICKEY_HPP
+#ifndef __BGN_PUBLICKEY_HPP
+#define __BGN_PUBLICKEY_HPP
 
 #include "Scalar.hpp"
 #include "Bipoint.hpp"
 #include "Quadripoint.hpp"
 #include "pairing.hpp"
 
-class PublicKey
+class BGNPublicKey
 {
 	public:
+		BGNPublicKey(const BGNPublicKey& other);
 		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;
@@ -17,14 +18,18 @@ class PublicKey
 		TwistBipoint homomorphic_addition(const TwistBipoint& a, const TwistBipoint& b) const;
 		Quadripoint homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const;
 		Quadripoint homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const;
-		
+
+		CurveBipoint rerandomize(const CurveBipoint& a) const;
+		TwistBipoint rerandomize(const TwistBipoint& a) const;
+		Quadripoint rerandomize(const Quadripoint& a) 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();
+		BGNPublicKey();
 		void set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1);
 		friend class BGN;
 

+ 15 - 0
bgn2/src/BGN.cpp

@@ -103,6 +103,21 @@ Quadripoint BGN::homomorphic_multiplication(const CurveBipoint& a, const TwistBi
 	return public_key.homomorphic_multiplication(a, b);
 }
 
+void BGN::rerandomize(CurveBipoint& G_element) const
+{
+    public_key.rerandomize(G_element);
+}
+
+void BGN::rerandomize(TwistBipoint& H_element) const
+{
+    public_key.rerandomize(H_element);
+}
+
+void BGN::rerandomize(Quadripoint& Gt_element) const
+{
+    public_key.rerandomize(Gt_element);
+}
+
 Scalar BGN::decrypt(const CurveBipoint& ciphertext)
 {
 	return private_key.decrypt(ciphertext);

+ 176 - 0
bgn2/src/Curvepoint.cpp

@@ -0,0 +1,176 @@
+#include "Curvepoint.hpp"
+
+Curvepoint::Curvepoint()
+{
+    curvepoint_fp_setneutral(point);
+}
+
+Curvepoint::Curvepoint(curvepoint_fp_t input)
+{
+    curvepoint_fp_set(point, input);
+}
+
+Curvepoint Curvepoint::operator+(const Curvepoint& b) const
+{
+    Curvepoint retval;
+
+    if (equal(point, b.point))
+        curvepoint_fp_double(retval.point, point);
+    else
+        curvepoint_fp_add_vartime(retval.point, point, b.point);
+
+    return retval;
+}
+
+Curvepoint Curvepoint::operator*(const Scalar& exp) const
+{
+    Curvepoint retval;
+
+    exp.mult(retval.point, point);
+
+    return retval;
+}
+
+bool Curvepoint::operator==(const Curvepoint& b) const
+{
+    bool retval;
+    curvepoint_fp_t affine_this_point, affine_b_point;
+
+    curvepoint_fp_set(affine_this_point, point);
+    curvepoint_fp_set(affine_b_point, b.point);
+
+    if (!(fpe_isone(affine_this_point->m_z) || fpe_iszero(affine_this_point->m_z)))
+        curvepoint_fp_makeaffine(affine_this_point);
+
+    if (!(fpe_isone(affine_b_point->m_z) || fpe_iszero(affine_b_point->m_z)))
+        curvepoint_fp_makeaffine(affine_b_point);
+
+    retval =           fpe_iseq(affine_this_point->m_x, affine_b_point->m_x);
+    retval = retval && fpe_iseq(affine_this_point->m_y, affine_b_point->m_y);
+    retval = retval || (fpe_iszero(affine_this_point->m_z) && fpe_iszero(affine_b_point->m_z));
+
+    return retval;
+}
+
+bool Curvepoint::operator<(const Curvepoint& b) const
+{
+    bool lessThan[2];
+    bool equal[2];
+    curvepoint_fp_t affine_this_point, affine_b_point;
+
+    lessThan[0] = lessThan[1] = false;
+
+    curvepoint_fp_set(affine_this_point, point);
+    curvepoint_fp_set(affine_b_point, b.point);
+
+    if (fpe_iszero(affine_this_point->m_z))
+    {
+        // this case would be equal
+        if (fpe_iszero(affine_b_point->m_z))
+            return false;
+
+        // point at infinity is less than all other points
+        return true;
+    }
+
+    if (fpe_iszero(affine_b_point->m_z))
+        return false;
+
+    // already checked for the point at infinity, so we don't have to redo that here
+    if (!fpe_isone(affine_this_point->m_z))
+        curvepoint_fp_makeaffine(affine_this_point);
+
+    if (!fpe_isone(affine_b_point->m_z))
+        curvepoint_fp_makeaffine(affine_b_point);
+
+    for (int i = 11; i >= 0; i--)
+    {
+        if (affine_this_point->m_x[i] > affine_b_point->m_x[i])
+        {
+            lessThan[0] = false;
+            break;
+        }
+        if (affine_this_point->m_x[i] < affine_b_point->m_x[i])
+        {
+            lessThan[0] = true;
+            break;
+        }
+    }
+
+    for (int i = 11; i >= 0; i--)
+    {
+        if (affine_this_point->m_y[i] > affine_b_point->m_y[i])
+        {
+            lessThan[1] = false;
+            break;
+        }
+        if (affine_this_point->m_y[i] < affine_b_point->m_y[i])
+        {
+            lessThan[1] = true;
+            break;
+        }
+    }
+
+    equal[0] = fpe_iseq(affine_this_point->m_x, affine_b_point->m_x);
+    equal[1] = fpe_iseq(affine_this_point->m_y, affine_b_point->m_y);
+
+    // sort is lesser x value first, and then lesser y value second if x's are equal
+    return equal[0] ? (equal[1] ? false : lessThan[1]) : lessThan[0];
+}
+
+bool Curvepoint::operator>(const Curvepoint& b) const
+{
+    return !(*this < b);
+}
+
+bool Curvepoint::operator<=(const Curvepoint& b) const
+{
+    return (*this == b) || (*this < b);
+}
+
+bool Curvepoint::operator>=(const Curvepoint& b) const
+{
+    return (*this == b) || !(*this < b);
+}
+
+bool Curvepoint::operator!=(const Curvepoint& b) const
+{
+    return !(*this == b);
+}
+
+void Curvepoint::make_affine()
+{
+    if (!(fpe_isone(point->m_z) || fpe_iszero(point->m_z)))
+        curvepoint_fp_makeaffine(point);
+}
+
+std::ostream& operator<<(std::ostream& os, const Curvepoint& output)
+{
+    os << "[";
+    os << Fpe(output.point->m_x) << ", " << Fpe(output.point->m_y) << ", " << Fpe(output.point->m_z);
+    os << "]";
+
+    return os;
+}
+
+size_t CurvepointHash::operator()(const Curvepoint& x) const
+{
+    if (fpe_iszero(x.point->m_z))
+    {
+        return 0;
+    }
+
+    size_t retval;
+    std::hash<double> hasher;
+
+    Curvepoint affine_x = x;
+    affine_x.make_affine();
+
+    for (int j = 0; j < 12; j++)
+    {
+        retval ^= hasher(affine_x.point->m_x->v[j]);
+        retval ^= hasher(affine_x.point->m_y->v[j]);
+    }
+
+    return retval;
+}

+ 20 - 8
bgn2/src/PrivateKey.cpp

@@ -1,6 +1,18 @@
 #include "PrivateKey.hpp"
 
-Scalar PrivateKey::decrypt(const CurveBipoint& ciphertext)
+BGNPrivateKey::BGNPrivateKey(const BGNPrivateKey& other)
+    : a1(other.a1), b1(other.b1), c1(other.c1), d1(other.d1),
+    a2(other.a2), b2(other.b2), c2(other.c2), d2(other.d2),
+    pi_1_curvegen(other.pi_1_curvegen), pi_2_twistgen(other.pi_2_twistgen),
+    pi_T_pairgen(other.pi_T_pairgen), curve_memoizer(other.curve_memoizer),
+    twist_memoizer(other.twist_memoizer), pair_memoizer(other.pair_memoizer),
+    curve_max_checked(other.curve_max_checked),
+    twist_max_checked(other.twist_max_checked),
+    pair_max_checked(other.pair_max_checked)
+{ }
+
+
+Scalar BGNPrivateKey::decrypt(const CurveBipoint& ciphertext)
 {
     CurveBipoint pi_1_ciphertext = pi_1(ciphertext); 
 
@@ -25,7 +37,7 @@ Scalar PrivateKey::decrypt(const CurveBipoint& ciphertext)
     return curve_max_checked;
 }
 
-Scalar PrivateKey::decrypt(const TwistBipoint& ciphertext)
+Scalar BGNPrivateKey::decrypt(const TwistBipoint& ciphertext)
 {
     TwistBipoint pi_2_ciphertext = pi_2(ciphertext);
 
@@ -50,7 +62,7 @@ Scalar PrivateKey::decrypt(const TwistBipoint& ciphertext)
 }
 
 
-Scalar PrivateKey::decrypt(const Quadripoint& ciphertext)
+Scalar BGNPrivateKey::decrypt(const Quadripoint& ciphertext)
 {
     Quadripoint pi_T_ciphertext = pi_T(ciphertext); 
 
@@ -74,10 +86,10 @@ Scalar PrivateKey::decrypt(const Quadripoint& ciphertext)
     return pair_max_checked;
 }
 
-PrivateKey::PrivateKey()
+BGNPrivateKey::BGNPrivateKey()
 { }
 
-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)
+void BGNPrivateKey::set(const BGNPublicKey& 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;
@@ -102,7 +114,7 @@ void PrivateKey::set(const PublicKey& pub_key, const Scalar& a1, const Scalar& b
     this->pair_memoizer[this->pi_T_pairgen * this->pair_max_checked] = this->pair_max_checked;
 }
 
-CurveBipoint PrivateKey::pi_1(const CurveBipoint& input) const
+CurveBipoint BGNPrivateKey::pi_1(const CurveBipoint& input) const
 {
     CurveBipoint retval;
     curvepoint_fp_t temp0, temp1;
@@ -133,7 +145,7 @@ CurveBipoint PrivateKey::pi_1(const CurveBipoint& input) const
     return retval;
 }
 
-TwistBipoint PrivateKey::pi_2(const TwistBipoint& input) const
+TwistBipoint BGNPrivateKey::pi_2(const TwistBipoint& input) const
 {
     TwistBipoint retval;
     twistpoint_fp2_t temp0, temp1;
@@ -164,7 +176,7 @@ TwistBipoint PrivateKey::pi_2(const TwistBipoint& input) const
     return retval;     
 }
 
-Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
+Quadripoint BGNPrivateKey::pi_T(const Quadripoint& input) const
 {
     Quadripoint retval;
     fp12e_t temp0, temp1, temp2, temp3;

+ 58 - 13
bgn2/src/PublicKey.cpp

@@ -1,6 +1,12 @@
 #include "PublicKey.hpp"
 
-void PublicKey::encrypt(CurveBipoint& G_element, const Scalar& cleartext) const
+BGNPublicKey::BGNPublicKey(const BGNPublicKey& other)
+    : bipoint_curvegen(other.bipoint_curvegen), bipoint_twistgen(other.bipoint_twistgen),
+    bipoint_curve_subgroup_gen(other.bipoint_curve_subgroup_gen),
+    bipoint_twist_subgroup_gen(other.bipoint_twist_subgroup_gen)
+{ }
+
+void BGNPublicKey::encrypt(CurveBipoint& G_element, const Scalar& cleartext) const
 {
     Scalar lambda;
     lambda.set_random();
@@ -12,7 +18,7 @@ void PublicKey::encrypt(CurveBipoint& G_element, const Scalar& cleartext) const
     G_element = cleartext_as_element + random_mask;
 }
 
-void PublicKey::encrypt(TwistBipoint& H_element, const Scalar& cleartext) const
+void BGNPublicKey::encrypt(TwistBipoint& H_element, const Scalar& cleartext) const
 {
     Scalar lambda;
     lambda.set_random();
@@ -24,13 +30,13 @@ void PublicKey::encrypt(TwistBipoint& H_element, const Scalar& cleartext) const
     H_element = cleartext_as_element + random_mask;
 }
 
-void PublicKey::encrypt(CurveBipoint& G_element, TwistBipoint& H_element, const Scalar& cleartext) const
+void BGNPublicKey::encrypt(CurveBipoint& G_element, TwistBipoint& H_element, const Scalar& cleartext) const
 {
     encrypt(G_element, cleartext);
     encrypt(H_element, cleartext);
 }
 
-CurveBipoint PublicKey::homomorphic_addition(const CurveBipoint& a, const CurveBipoint& b) const
+CurveBipoint BGNPublicKey::homomorphic_addition(const CurveBipoint& a, const CurveBipoint& b) const
 {
     Scalar lambda;
     lambda.set_random();
@@ -41,7 +47,7 @@ CurveBipoint PublicKey::homomorphic_addition(const CurveBipoint& a, const CurveB
     return a + b + random_mask;
 }
 
-TwistBipoint PublicKey::homomorphic_addition(const TwistBipoint& a, const TwistBipoint& b) const
+TwistBipoint BGNPublicKey::homomorphic_addition(const TwistBipoint& a, const TwistBipoint& b) const
 {
     Scalar lambda;
     lambda.set_random();
@@ -52,7 +58,7 @@ TwistBipoint PublicKey::homomorphic_addition(const TwistBipoint& a, const TwistB
     return a + b + random_mask;
 }
 
-Quadripoint PublicKey::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
+Quadripoint BGNPublicKey::homomorphic_addition(const Quadripoint& a, const Quadripoint& b) const
 {
     Quadripoint random_mask;
     CurveBipoint random_mask_curve;
@@ -69,7 +75,7 @@ Quadripoint PublicKey::homomorphic_addition(const Quadripoint& a, const Quadripo
     return a + b + random_mask;
 }
 
-Quadripoint PublicKey::homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const
+Quadripoint BGNPublicKey::homomorphic_multiplication(const CurveBipoint& a, const TwistBipoint& b) const
 {
     Quadripoint random_mask;
     CurveBipoint random_mask_curve;
@@ -86,30 +92,69 @@ Quadripoint PublicKey::homomorphic_multiplication(const CurveBipoint& a, const T
     return pairing(a, b) + random_mask;
 }
 
-CurveBipoint PublicKey::get_bipoint_curvegen() const
+CurveBipoint rerandomize(const CurveBipoint& a) const
+{
+    Scalar lambda;
+    lambda.set_random();
+
+    CurveBipoint random_mask;
+    random_mask = bipoint_curve_subgroup_gen * lambda;
+
+    return a + random_mask;
+}
+
+TwistBipoint rerandomize(const TwistBipoint& a) const
+{
+    Scalar lambda;
+    lambda.set_random();
+
+    TwistBipoint random_mask;
+    random_mask = bipoint_twist_subgroup_gen * lambda;
+
+    return a + random_mask;
+}
+
+Quadripoint rerandomize(const Quadripoint& a) const
+{
+    Quadripoint random_mask;
+    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_twist = bipoint_twist_subgroup_gen * lambda2;
+    random_mask = pairing(bipoint_curvegen, random_mask_twist) + pairing(random_mask_curve, bipoint_twistgen);
+
+    return a + random_mask;
+}
+
+CurveBipoint BGNPublicKey::get_bipoint_curvegen() const
 {
     return bipoint_curvegen;
 }
 
-TwistBipoint PublicKey::get_bipoint_twistgen() const
+TwistBipoint BGNPublicKey::get_bipoint_twistgen() const
 {
     return bipoint_twistgen;
 }
 
-CurveBipoint PublicKey::get_bipoint_curve_subgroup_gen() const
+CurveBipoint BGNPublicKey::get_bipoint_curve_subgroup_gen() const
 {
     return bipoint_curve_subgroup_gen;
 }
 
-TwistBipoint PublicKey::get_bipoint_twist_subgroup_gen() const
+TwistBipoint BGNPublicKey::get_bipoint_twist_subgroup_gen() const
 {
     return bipoint_twist_subgroup_gen;
 }
 
-PublicKey::PublicKey()
+BGNPublicKey::BGNPublicKey()
 { }
 
-void PublicKey::set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1)
+void BGNPublicKey::set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1)
 {
     bipoint_curvegen = g;
     bipoint_twistgen = h;

+ 4 - 4
bgn2/src/Quadripoint.cpp

@@ -2,10 +2,10 @@
 
 Quadripoint::Quadripoint()
 {
-	fp12e_setone(point[0]);
-	fp12e_setone(point[1]);
-	fp12e_setone(point[2]);
-	fp12e_setone(point[3]);	
+	fp12e_setzero(point[0]);
+	fp12e_setzero(point[1]);
+	fp12e_setzero(point[2]);
+	fp12e_setzero(point[3]);	
 }
 
 Quadripoint::Quadripoint(const fp12e_t& p1, const fp12e_t& p2, const fp12e_t& p3, const fp12e_t& p4)