Browse Source

updates to make serialization slightly more efficient

tristangurtler 1 year ago
parent
commit
e22e4ed40e

+ 1 - 0
bgn2/inc/Scalar.hpp

@@ -26,6 +26,7 @@ class Scalar
         void set(const scalar_t& input);
         void set(mpz_class input);
         void set_random();
+        void set_field_random();
 
         mpz_class toInt() const;
 

+ 6 - 6
bgn2/src/BGN.cpp

@@ -9,9 +9,9 @@ BGN::BGN()
     
     while (true)
     {
-        a1.set_random();
-        b1.set_random();
-        c1.set_random();
+        a1.set_field_random();
+        b1.set_field_random();
+        c1.set_field_random();
 
         if (a1 != Scalar(0))
         {
@@ -22,9 +22,9 @@ BGN::BGN()
 
     while (true)
     {
-        a2.set_random();
-        b2.set_random();
-        c2.set_random();
+        a2.set_field_random();
+        b2.set_field_random();
+        c2.set_field_random();
 
         if (a2 != Scalar(0))
         {

+ 2 - 2
bgn2/src/Bipoint.cpp

@@ -225,7 +225,7 @@ std::ostream& operator<<(std::ostream& os, const CurveBipoint& output)
 
 	for (int i = 0; i < 2; i++)
 	{
-		if (fpe_iszero(affine_out[i]->m_z))
+		if ((os.flags() & std::ios::hex) && fpe_iszero(affine_out[i]->m_z))
 			os << "Infinity";
 		else
 			os << Fpe(affine_out[i]->m_x) << Fpe(affine_out[i]->m_y) << Fpe(affine_out[i]->m_z);
@@ -255,7 +255,7 @@ std::ostream& operator<<(std::ostream& os, const TwistBipoint& output)
 
 	for (int i = 0; i < 2; i++)
 	{
-		if (fp2e_iszero(affine_out[i]->m_z))
+		if ((os.flags() & std::ios::hex) && fp2e_iszero(affine_out[i]->m_z))
 			os << "Infinity";
 		else
 			os << Fp2e(affine_out[i]->m_x) << Fp2e(affine_out[i]->m_y) << Fp2e(affine_out[i]->m_z);

+ 4 - 1
bgn2/src/Curvepoint.cpp

@@ -174,7 +174,10 @@ std::ostream& operator<<(std::ostream& os, const Curvepoint& output)
     Curvepoint affine_out = output;
     affine_out.make_affine();
     
-    os << Fpe(affine_out.point->m_x) << Fpe(affine_out.point->m_y) << Fpe(affine_out.point->m_z);
+    if ((os.flags() & std::ios::hex) && fpe_iszero(affine_out.point->m_z))
+        os << "Infinity";
+    else
+        os << Fpe(affine_out.point->m_x) << Fpe(affine_out.point->m_y) << Fpe(affine_out.point->m_z);
 
     return os;
 }

+ 8 - 4
bgn2/src/PrivateKey.cpp

@@ -88,8 +88,8 @@ Scalar BGNPrivateKey::decrypt(const Quadripoint& ciphertext)
 
 std::ostream& operator<<(std::ostream& os, const BGNPrivateKey& output)
 {
-    os << output.a1 << output.b1 << output.c1 << output.d1;
-    os << output.a2 << output.b2 << output.c2 << output.d2;
+    os << output.a1 << output.b1 << output.c1;
+    os << output.a2 << output.b2 << output.c2;
     os << output.pi_1_curvegen;
     os << output.pi_2_twistgen;
     os << output.pi_T_pairgen;
@@ -99,8 +99,12 @@ std::ostream& operator<<(std::ostream& os, const BGNPrivateKey& output)
 
 std::istream& operator>>(std::istream& is, BGNPrivateKey& input)
 {
-    is >> input.a1 >> input.b1 >> input.c1 >> input.d1;
-    is >> input.a2 >> input.b2 >> input.c2 >> input.d2;
+    is >> input.a1 >> input.b1 >> input.c1;
+    input.d1 = ((input.b1).fieldMult(input.c1).fieldAdd(Scalar(1))).fieldMult((input.a1).fieldMultInverse());
+
+    is >> input.a2 >> input.b2 >> input.c2;
+    input.d2 = ((input.b2).fieldMult(input.c2).fieldAdd(Scalar(1))).fieldMult((input.a2).fieldMultInverse());
+    
     is >> input.pi_1_curvegen;
     is >> input.pi_2_twistgen;
     is >> input.pi_T_pairgen;

+ 3 - 4
bgn2/src/PublicKey.cpp

@@ -178,8 +178,6 @@ std::ostream& operator<<(std::ostream& os, const BGNPublicKey& output)
     os << output.bipoint_twistgen;
     os << output.bipoint_curve_subgroup_gen;
     os << output.bipoint_twist_subgroup_gen;
-    os << output.quadripoint_subgroup_gen_a;
-    os << output.quadripoint_subgroup_gen_b;
 
     return os;
 }
@@ -190,8 +188,9 @@ std::istream& operator>>(std::istream& is, BGNPublicKey& input)
     is >> input.bipoint_twistgen;
     is >> input.bipoint_curve_subgroup_gen;
     is >> input.bipoint_twist_subgroup_gen;
-    is >> input.quadripoint_subgroup_gen_a;
-    is >> input.quadripoint_subgroup_gen_b;
+
+    input.quadripoint_subgroup_gen_a = pairing(input.bipoint_curvegen, input.bipoint_twist_subgroup_gen);
+    input.quadripoint_subgroup_gen_b = pairing(input.bipoint_curve_subgroup_gen, input.bipoint_twistgen);
 
     return is;
 }

+ 12 - 0
bgn2/src/Scalar.cpp

@@ -2,6 +2,7 @@
 #include <iostream>
 
 extern const scalar_t bn_n;
+extern const scalar_t bn_p;
 mpz_class Scalar::mpz_bn_p = 0;
 mpz_class Scalar::mpz_bn_n = 0;
 
@@ -64,6 +65,17 @@ void Scalar::set_random()
     set(temp);
 }
 
+void Scalar::set_field_random()
+{
+    scalar_t temp;
+    
+    /* There's only one occasion we actually want a random integer
+     * in the field, and that's when we generate a private BGN key. */
+    scalar_setrandom(temp, bn_p);
+
+    set(temp);
+}
+
 mpz_class Scalar::toInt() const
 {
     return element;

+ 1 - 0
dclxvi-20130329/src/parameters.c

@@ -25,6 +25,7 @@ EXTERN const signed char bn_6uplus2_naf[BN_6UPLUS2_NAFLEN] = {0, 0, 0, 1, 0, 0,
 //EXTERN const scalar_t bn_u =        {0x5BBC1015F02AC17DULL, 0x0000000000000000ULL, 0x0000000000000000ULL, 0x0000000000000000ULL};
 EXTERN const scalar_t bn_n       = {0x1A2EF45B57AC7261ULL, 0x2E8D8E12F82B3924ULL, 0xAA6FECB86184DC21ULL, 0x8FB501E34AA387F9ULL};
 EXTERN const scalar_t bn_pminus2 = {0x185CAC6C5E089665ULL, 0xEE5B88D120B5B59EULL, 0xAA6FECB86184DC21ULL, 0x8FB501E34AA387F9ULL};
+EXTERN const scalar_t bn_p       = {0x185CAC6C5E089667ULL, 0xEE5B88D120B5B59EULL, 0xAA6FECB86184DC21ULL, 0x8FB501E34AA387F9ULL};
 
 //EXTERN const unsigned long bn_u_bitsize = 63;