|
@@ -1,6 +1,8 @@
|
|
|
#include "Scalar.hpp"
|
|
|
#include <iostream>
|
|
|
|
|
|
+#include "proof.hpp"
|
|
|
+
|
|
|
extern const scalar_t bn_n;
|
|
|
extern const scalar_t bn_p;
|
|
|
mpz_class Scalar::mpz_bn_p = 0;
|
|
@@ -295,14 +297,97 @@ Scalar::SecretScalar Scalar::to_scalar_t() const
|
|
|
return SecretScalar(element);
|
|
|
}
|
|
|
|
|
|
+char byteToHexByte(char in)
|
|
|
+{
|
|
|
+ if (in < 0xA)
|
|
|
+ return in + '0';
|
|
|
+ else
|
|
|
+ return (in - 0xA) + 'a';
|
|
|
+}
|
|
|
+
|
|
|
+char hexByteToByte(char in)
|
|
|
+{
|
|
|
+ if (in >= '0' && in <= '9')
|
|
|
+ return in - '0';
|
|
|
+ else if (in >= 'A' && in <= 'F')
|
|
|
+ return (in - 'A') + 0xA;
|
|
|
+ else
|
|
|
+ return (in - 'a') + 0xA;
|
|
|
+}
|
|
|
+
|
|
|
+std::vector<char> hexToBytes(const std::string& hex)
|
|
|
+{
|
|
|
+ std::vector<char> bytes;
|
|
|
+
|
|
|
+ for (size_t i = 0; i < hex.length(); i += 2)
|
|
|
+ {
|
|
|
+ char partA, partB, currByte;
|
|
|
+
|
|
|
+ partA = hex[i];
|
|
|
+ partB = hex[i + 1];
|
|
|
+
|
|
|
+ partA = hexByteToByte(partA);
|
|
|
+ partB = hexByteToByte(partB);
|
|
|
+
|
|
|
+ currByte = (partA << 4) | partB;
|
|
|
+ bytes.push_back(byte);
|
|
|
+ }
|
|
|
+
|
|
|
+ return bytes;
|
|
|
+}
|
|
|
+
|
|
|
+std::string bytesToHex(const std::vector<char>& bytes)
|
|
|
+{
|
|
|
+ std::string hex;
|
|
|
+
|
|
|
+ for (size_t i = 0; i < bytes.length(); i++)
|
|
|
+ {
|
|
|
+ char partA, partB;
|
|
|
+
|
|
|
+ partA = (0xF0 & bytes[i]) >> 4;
|
|
|
+ partB = (0xF & bytes[i]);
|
|
|
+
|
|
|
+ partA = byteToHexByte(partA);
|
|
|
+ partB = byteToHexByte(partB);
|
|
|
+
|
|
|
+ hex += partA;
|
|
|
+ hex += partB;
|
|
|
+ }
|
|
|
+
|
|
|
+ return hex;
|
|
|
+}
|
|
|
+
|
|
|
std::ostream& operator<<(std::ostream& os, const Scalar& output)
|
|
|
{
|
|
|
- os << output.element;
|
|
|
+ std::string outString = output.element.get_str(16);
|
|
|
+ std::vector<char> bytes = hexToBytes(outString);
|
|
|
+
|
|
|
+ BinarySizeT sizeOfVector(bytes.size());
|
|
|
+ os << sizeOfVector;
|
|
|
+ for (size_t i = 0; i < sizeOfVector.val(); i++)
|
|
|
+ os.write(&(bytes[i]), sizeof(bytes[i]));
|
|
|
+
|
|
|
return os;
|
|
|
}
|
|
|
|
|
|
std::istream& operator>>(std::istream& is, Scalar& input)
|
|
|
{
|
|
|
- is >> input.element;
|
|
|
+ std::vector<char> bytes;
|
|
|
+
|
|
|
+ BinarySizeT sizeOfVector;
|
|
|
+ is >> sizeOfVector;
|
|
|
+
|
|
|
+ for (size_t i = 0; i < sizeOfVector.val(); i++)
|
|
|
+ {
|
|
|
+ char currByte;
|
|
|
+ is.read(&currByte, sizeof(currByte));
|
|
|
+
|
|
|
+ bytes.push_back(currByte);
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string hex = bytesToHex(bytes);
|
|
|
+
|
|
|
+ input.element.set_str(hex, 16);
|
|
|
+
|
|
|
return is;
|
|
|
}
|