adding c++ support for public keys

This commit is contained in:
Gabe Kaptchuk 2019-03-28 23:05:12 -04:00
parent 40ab51de27
commit 2b62bb71bf
2 changed files with 368 additions and 1 deletions

349
include/PublicKey.h Normal file
View File

@ -0,0 +1,349 @@
#ifndef __PUBLICKEY_H__
#define __PUBLICKEY_H__
#include <cstring>
#include <vector>
#include <ostream>
#include <sstream>
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#define HEX_CHARS "0123456789abcdefABCDEF"
#define PUBLIC_KEY_VECTOR_LENGTH 1174
#define PUBLIC_KEY_HEX_STRING_LENGTH PUBLIC_KEY_VECTOR_LENGTH*2
#define PUBLIC_KEY_X_LENGTH 65
#define PUBLIC_KEY_Y_LENGTH 65
#define PUBLIC_KEY_Z_LENGTH 4
#define PUBLIC_KEY_Z_POINT_LENGTH 65
#define PUBLIC_KEY_Z2_LENGTH 4
#define PUBLIC_KEY_Z2_POINT_LENGTH 129
#define PUBLIC_KEY_W_LENGTH 4 //2
#define PUBLIC_KEY_W_POINT_LENGTH 65//130
using namespace rapidjson;
class PublicKey : public std::vector<uint8_t> {
public:
std::string toJson() {
Document json;
// allocator for memory management
Document::AllocatorType& allocator = json.GetAllocator();
// define the document as an object rather than an array
json.SetObject();
if( PUBLIC_KEY_VECTOR_LENGTH != this->size())
return("{}");
// iterator we are going to be using the whole time
std::vector<uint8_t>::iterator it = this->begin();
// Double check the X length
if( PUBLIC_KEY_X_LENGTH != *it )
return("{}");
it++;
Value X(kArrayType);
for( int j = 0; j< PUBLIC_KEY_X_LENGTH; j++)
{
X.PushBack(*it, allocator);
it++;
}
// Double check the Y length
if( PUBLIC_KEY_Y_LENGTH != *it )
return("{}");
it++;
Value Y(kArrayType);
for( int j = 0; j< PUBLIC_KEY_Y_LENGTH; j++)
{
Y.PushBack(*it, allocator);
it++;
}
// Double check the Z number
if( PUBLIC_KEY_Z_LENGTH != *it )
return("{}");
it++;
// ... and that they are the correct length
if( PUBLIC_KEY_Z_POINT_LENGTH != *it )
return("{}");
it++;
Value Z(kArrayType);
for( int i = 0; i< PUBLIC_KEY_Z_LENGTH; i++)
{
Value vec(kArrayType);
for( int j = 0; j< PUBLIC_KEY_Z_POINT_LENGTH; j++)
{
vec.PushBack(*it, allocator);
it++;
}
Z.PushBack(vec, allocator);
}
// Double check the Z2 number
if( PUBLIC_KEY_Z2_LENGTH != *it )
return("{}");
it++;
// ... and that they are the correct length
if( PUBLIC_KEY_Z2_POINT_LENGTH != *it )
return("{}");
it++;
Value Z2(kArrayType);
for( int i = 0; i< PUBLIC_KEY_Z2_LENGTH; i++)
{
Value vec(kArrayType);
for( int j = 0; j< PUBLIC_KEY_Z2_POINT_LENGTH; j++)
{
vec.PushBack(*it, allocator);
it++;
}
Z2.PushBack(vec, allocator);
}
// Double check the W number
if( PUBLIC_KEY_W_LENGTH != *it )
return("{}");
it++;
// ... and that they are the correct length
if( PUBLIC_KEY_W_POINT_LENGTH != *it )
return("{}");
it++;
Value W(kArrayType);
for( int j = 0; j< PUBLIC_KEY_W_LENGTH; j++)
{
Value vec(kArrayType);
for( int j = 0; j< PUBLIC_KEY_W_POINT_LENGTH; j++)
{
vec.PushBack(*it, allocator);
it++;
}
W.PushBack(vec, allocator);
}
// build final json string
json.AddMember("X", X, allocator);
json.AddMember("Y", Y, allocator);
json.AddMember("Z", Z, allocator);
json.AddMember("Z2", Z2, allocator);
json.AddMember("W", W, allocator);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
json.Accept(writer);
return sb.GetString();
}
bool fromJson(std::string s) {
Document json;
json.Parse(s.c_str());
eraseAll();
// Make sure we arent going to get an error when indexing into the JSON
if(!json.HasMember("X"))
return false;
if(!json.HasMember("Y"))
return false;
if(!json.HasMember("Z"))
return false;
if(!json.HasMember("Z2"))
return false;
if(!json.HasMember("W"))
return false;
const Value& X = json["X"];
const Value& Y = json["Y"];
const Value& Z = json["Z"];
const Value& Z2 = json["Z2"];
const Value& W = json["W"];
if(!X.IsArray())
return false;
if(!Y.IsArray())
return false;
if(!Z.IsArray())
return false;
if(!Z2.IsArray())
return false;
if(!W.IsArray())
return false;
if(!(Z.Size() == SizeType(PUBLIC_KEY_Z_LENGTH)))
return false;
if(!(Z2.Size() == SizeType(PUBLIC_KEY_Z2_LENGTH)))
return false;
if(!(W.Size() == SizeType(PUBLIC_KEY_W_LENGTH)))
return false;
// Add the header information
// From here on out, make sure to call cleanupAndFalse() instead of false
// X
this->push_back(PUBLIC_KEY_X_LENGTH);
for(SizeType j = 0; j < X.Size(); j++)
this->push_back(X[j].GetUint64());
// Y
this->push_back(PUBLIC_KEY_Y_LENGTH);
for(SizeType j = 0; j < Y.Size(); j++)
this->push_back(Y[j].GetUint64());
// Z
this->push_back(PUBLIC_KEY_Z_LENGTH);
this->push_back(PUBLIC_KEY_Z_POINT_LENGTH);
for (SizeType i = 0; i < Z.Size(); i++)
{
const Value& vec = Z[i];
if(!vec.IsArray())
return cleanupAndFalse();
if(!(vec.Size() == SizeType(PUBLIC_KEY_Z_POINT_LENGTH)))
return cleanupAndFalse();
for(SizeType j = 0; j < vec.Size(); j++)
{
this->push_back(vec[j].GetUint64());
}
}
//Z2
this->push_back(PUBLIC_KEY_Z2_LENGTH);
this->push_back(PUBLIC_KEY_Z2_POINT_LENGTH);
for (SizeType i = 0; i < Z2.Size(); i++)
{
const Value& vec = Z2[i];
if(!vec.IsArray())
return cleanupAndFalse();
if(!(vec.Size() == SizeType(PUBLIC_KEY_Z2_POINT_LENGTH)))
return cleanupAndFalse();
for(SizeType j = 0; j < vec.Size(); j++)
{
this->push_back(vec[j].GetUint64());
}
}
// W
this->push_back(PUBLIC_KEY_W_LENGTH);
this->push_back(PUBLIC_KEY_W_POINT_LENGTH);
for (SizeType i = 0; i < W.Size(); i++)
{
const Value& vec = W[i];
if(!vec.IsArray())
return cleanupAndFalse();
if(!(vec.Size() == SizeType(PUBLIC_KEY_W_POINT_LENGTH)))
return cleanupAndFalse();
for(SizeType j = 0; j < vec.Size(); j++)
{
this->push_back(vec[j].GetUint64());
}
}
// Make sure the final length is good
if(!(this->size() == PUBLIC_KEY_VECTOR_LENGTH))
return cleanupAndFalse();
return true;
}
// Stolen from https://github.com/zeutro/openabe/blob/master/src/include/openabe/utils/zbytestring.h
bool fromHex(std::string s) {
if((s.find_first_not_of(HEX_CHARS) != std::string::npos) ||
(s.size() % 2 != 0)) {
return false;
}
if ( s.length() != PUBLIC_KEY_HEX_STRING_LENGTH)
return false;
std::string hex_str;
std::stringstream ss;
int tmp;
this->clear();
for (size_t i = 0; i < s.size(); i += 2) {
hex_str = s[i];
hex_str += s[i+1];
ss << hex_str;
ss >> std::hex >> tmp;
this->push_back(tmp & 0xFF);
ss.clear();
}
return true;
}
std::string toHex() const {
std::stringstream ss;
int hex_len = 2;
char hex[hex_len+1];
std::memset(hex, 0, hex_len+1);
for (std::vector<uint8_t>::const_iterator it = this->begin();
it != this->end(); ++it) {
sprintf(hex, "%02X", *it);
ss << hex;
}
return ss.str();
}
std::string toLowerHex() const {
std::stringstream ss;
int hex_len = 2;
char hex[hex_len+1];
std::memset(hex, 0, hex_len+1);
for (std::vector<uint8_t>::const_iterator it = this->begin() ; it != this->end(); ++it) {
sprintf(hex, "%02x", *it);
ss << hex;
}
return ss.str();
}
void eraseAll() {
this->erase(this->begin(), this->end());
}
private:
bool cleanupAndFalse() {
eraseAll();
return false;
}
};
#endif

View File

@ -2,6 +2,7 @@
#include "libbolt.h"
#include "ChannelToken.h"
#include "PublicKey.h"
int main()
{
@ -28,7 +29,9 @@ int main()
// std::cout << "Validate Channel Close Successful" << std::endl;
// 810 bytes => [g-count] [g-len] [g-bytes_i] [g-bytes_count] [c-len] [c-bytes] [r-len] [r-bytes]
Channel Token
std::string channel_token = "05810405c5562fb520201b61b27a4a1328276535238bd05c48045c90fe4e336b5eab761cbd8a2b018298d1e1842580250aa412b3c3e745282b6fc1d89332572d2bcc110785dde055dd35d5bb91fa43ed4fcf3f69f8051134f0f738a40e3480be301b50b5487f5067665fd9272037fddc9029d60ca694d9340422c5cecc46822b91050d0405a2eb579382919cb178376fbec679af6720cf67e744d93fe43cdf02ba412bc1041f579f2c29c6975e0039d0b98912fcbca4af11b9e997da33f50a84dacc470306dd8a90c94d1130592f3e7d87db7e14b9295ecc9edb2aca593b00ceaa0d00b09a7ac8401ab90bdadd7fd3cf6b9cbdaa91f651668cc6adc65b8ebeece32762bc0406a6e14af00d9dc61117d7f07c2021a3422ac09a8c371377fcb4ea2fe8604a4219c2963c3f2f60c56b3dfab46e03e6917e69026270c5d4b7c533d069b63b3ccb085b141dccd23c3ef76cfcd928bc2a892d28f55e19aea980e4480282e72a5484f9453bea8aade1a3c1cc9711a01bf2c415e9179b7869e059192cad231ae867ad0406b08e977b847a5a887a0ec6e3cd0e95400f011c26e055c9cf941dbc157fb301a0e9e2c369fa744fb8fabf27a2ad559132cb026dc2dc8ba13dc0207d501cd00f042029e9d6f977c8ab6bca652c5e7311bf310f4235e24fce1237e98e42b7d7ca97c149b42c5969f42577566414e574a39cde9be6106a0f706bfe20a9801c592e0402e16db2c17416e48641eb9dff678e252df7739d5d8fa57a90892f269c4dc0bd6dc1a8edb21ce89c19aca06f76d85314b6206d7fa4c805068015fc0511064d3304a6e87378912de1c173adf3922407abec3664cb03e60d53e390ac57bf8b381a6b63c5d23d4d41e17cfe27628328d9c4af9215baddf6ed082f821a9e6344f1718104054e6c0db00bb143aee9fce0f28ba16f554fb5388675059fbe6318ea86645b78814dde89f36a1123e3f77412b8164b3f90c254f38352997c4d35359bd8cd65e201a10de92a7e1639ce4daa6b5bd2f1ba27dc9cb8dc98a2cd04e4c8096c9432d173e65d3eef70b530daca876d836abe5f4bd037f3bc2275529cbe3e3661ea8aac201ed26d3ca410cf9522908b2b2c204c393f2cf98f78a13ec258f8cf81976f52db";
ChannelToken token;
@ -43,6 +46,21 @@ int main()
std::cout << token_from_json.toJson() << std::endl;
Public Key
std::string public_key = "4104131f86dc3cc0f088736f2d1a00e3a812d448a90bc1c5689797a109628764f1c21b64ff783a2b60f4b68e281a947e04a70e38305d1c266badb966189fb9c27ae84104057639b31850fef002f1ea152cdd6c01cfa70fa4c92e1a7a27d1799814383bdf2c96c0f3c0fec4d802f8b658608cd7e0bfd28acb5814412f705eeea209f1bd330441042304ac9deac1fa328ad7080b06efa1a65cab43d20b6b0aa25d999a70f910b8a80c52a226c010c6a5095c2e69f4c79b8740c75c80bd7c636a3a00f4ebbd31d472042fdd8f24a1ec8a02bf658e830508a62ec8ad6fa3df52af4424587742ada8ffa419c081e1274758dc08568b1b7aae5f0bdb5fef04ed8b317063f57978414eb9db04222327bdfb69cb29669bafa50e7b9831225e3e053bbc77f228bc002a2749c5fe179c726c71fa5b6db248ea5228a0b6a6f143cf201b7f8c0ca48ab937be364f850422561e2dada073cffc6cd73115fd42acc9ad82e524f0559a5e795a25f555aa9328128057ff6a8c678926b530a185406bd2f67c70d8c983b582a46473ce16492c04810405a2eb579382919cb178376fbec679af6720cf67e744d93fe43cdf02ba412bc1041f579f2c29c6975e0039d0b98912fcbca4af11b9e997da33f50a84dacc470306dd8a90c94d1130592f3e7d87db7e14b9295ecc9edb2aca593b00ceaa0d00b09a7ac8401ab90bdadd7fd3cf6b9cbdaa91f651668cc6adc65b8ebeece32762bc0406a6e14af00d9dc61117d7f07c2021a3422ac09a8c371377fcb4ea2fe8604a4219c2963c3f2f60c56b3dfab46e03e6917e69026270c5d4b7c533d069b63b3ccb085b141dccd23c3ef76cfcd928bc2a892d28f55e19aea980e4480282e72a5484f9453bea8aade1a3c1cc9711a01bf2c415e9179b7869e059192cad231ae867ad0406b08e977b847a5a887a0ec6e3cd0e95400f011c26e055c9cf941dbc157fb301a0e9e2c369fa744fb8fabf27a2ad559132cb026dc2dc8ba13dc0207d501cd00f042029e9d6f977c8ab6bca652c5e7311bf310f4235e24fce1237e98e42b7d7ca97c149b42c5969f42577566414e574a39cde9be6106a0f706bfe20a9801c592e0402e16db2c17416e48641eb9dff678e252df7739d5d8fa57a90892f269c4dc0bd6dc1a8edb21ce89c19aca06f76d85314b6206d7fa4c805068015fc0511064d3304a6e87378912de1c173adf3922407abec3664cb03e60d53e390ac57bf8b381a6b63c5d23d4d41e17cfe27628328d9c4af9215baddf6ed082f821a9e6344f1710441041dc2e3d4173966de7d1ed5e27bc22fe16c76e5608cee95596ebb60c3f02f6bc90a065eafd105538555a1f4f29fd90ce54472145df3e80e9b997fd98eebcaf3dd042af359d72df9e9652b3032d431810625ba80429d24fb67e878606b5ed890dc0010630af8ee84335853328b988574d48b331db1ba5509bb006b63251ad38548500428c058139c2ef503b78bd7f43d41a3dcd85ee081500a324ceafb05271b4132641a197b773c007a2e5a05b1c241bdbd36bf837bf41f9c53bea7259e2384e31d080427f5183459d41bcd52035895b2a80b327a14b644dfdda3eecffcc0d1d4143bb92453076c1a93e220ee5661d2971d5bdd3c57e3f8c64297823d0f4a0f8968e017";
PublicKey pk_m;
assert(pk_m.fromHex(public_key));
std::cout << pk_m.toJson() << std::endl;
PublicKey pk_m_from_json;
assert(pk_m_from_json.fromJson(pk_m.toJson()));
std::cout << pk_m_from_json.toJson() << std::endl;
return 0;
}