Unverified Commit 399004b3 authored by svetaro's avatar svetaro

SKALE-1739-Test-DKG-in-SGX Fix DKG Verification

parent c2abb08c
......@@ -10,6 +10,12 @@
#include <memory>
#include "SGXWalletServer.hpp"
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
std::vector<std::string> SplitString(const char* koefs, const char symbol){
std::string str(koefs);
std::string delim;
......@@ -103,7 +109,7 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
return pub_shares_vect;
}
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::string& publicKeys, int n, int t){
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int n, int t){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
......@@ -122,9 +128,11 @@ std::string get_secret_shares(const std::string& polyName, const char* encrypted
uint32_t dec_len;
char cur_share[193];
std::string pub_keyB = publicKeys.substr(64*i, 64*i + 128);
std::string pub_keyB = publicKeys.at(i);//publicKeys.substr(128*i, 128*i + 128);
std::cerr << "pub_keyB is " << pub_keyB << std::endl;
char pubKeyB[129];
strncpy(pubKeyB, pub_keyB.c_str(),129);
strncpy(pubKeyB, pub_keyB.c_str(), 128);
pubKeyB[128] = 0;
get_encr_sshare(eid, &err_status, errMsg1, encrypted_skey, &dec_len,
cur_share, pubKeyB, t, n, i + 1 );
......@@ -138,7 +146,7 @@ std::string get_secret_shares(const std::string& polyName, const char* encrypted
//writeDataToDB(name, hexEncrKey);
//std::cerr << errMsg1 << std::endl << std::endl;
std::cerr << errMsg1 << std::endl << std::endl;
//std::cerr << "iteration " << i <<" result length is " << result.length() << std::endl ;
//std::cerr << "iteration " << i <<" share length is " << strlen(cur_share) << std::endl;
//std::cerr << "iteration " << i <<" share is " << cur_share << std::endl;
......@@ -152,14 +160,10 @@ std::string get_secret_shares(const std::string& polyName, const char* encrypted
return result;
}
bool VerifyShares(const char* encryptedPolyHex, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind ){
bool VerifyShares(const char* publicShares, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind ){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
uint64_t poly_len = 0;
uint8_t* encr_dkg_poly = (uint8_t*) calloc(DKG_MAX_SEALED_LEN, 1);
hex2carray2(encryptedPolyHex, &poly_len, encr_dkg_poly, 6100);
uint64_t dec_key_len ;
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
......@@ -167,18 +171,25 @@ bool VerifyShares(const char* encryptedPolyHex, const char* encr_sshare, const c
//std::cerr << "dec_key_len " << dec_key_len << std::endl;
int result ;
dkg_verification(eid, &err_status, errMsg1, encr_dkg_poly, encr_sshare, encr_key, dec_key_len, t, ind, &result);
//std::cerr << "encr_sshare length is " << strlen(encr_sshare) << std::endl;
//std::cerr << "public shares " << publicShares << std::endl;
char pshares[4097];
strncpy(pshares, publicShares, strlen(publicShares) + 1);
// std::cerr << "pshares " << pshares << std::endl;
dkg_verification(eid, &err_status, errMsg1, pshares, encr_sshare, encr_key, dec_key_len, t, ind, &result);
std::cerr << "errMsg1: " << errMsg1 << std::endl;
free(errMsg1);
free(encr_dkg_poly);
std::cerr << "result is " << result << std::endl;
return result;
}
bool CreateBLSShare( const char * s_shares, const char * encryptedKeyHex){
bool CreateBLSShare( const std::string& BLSKeyName, const char * s_shares, const char * encryptedKeyHex){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
......@@ -188,10 +199,37 @@ bool CreateBLSShare( const char * s_shares, const char * encryptedKeyHex){
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
//std::cerr << " key hex is " << encryptedKeyHex << std::endl;
create_bls_key(eid, &err_status, errMsg1, s_shares, encr_key, dec_key_len, encr_bls_key);
std::cerr << "er msg is " << errMsg1 << std::endl;
if ( err_status != 0){
return false;
}
else return true;
else {
char *hexBLSKey = (char *) calloc(2 * BUF_LEN, 1);
uint32_t enc_len = BUF_LEN;
carray2Hex(encr_bls_key, enc_len, hexBLSKey);
writeDataToDB(BLSKeyName, hexBLSKey);
free(hexBLSKey);
return true;
}
}
std::string GetBLSPubKey(const char * encryptedKeyHex){
char* errMsg1 = (char*) calloc(1024,1);
int err_status = 0;
uint64_t dec_key_len ;
uint8_t encr_bls_key[BUF_LEN];
uint8_t encr_key[BUF_LEN];
hex2carray(encryptedKeyHex, &dec_key_len, encr_key);
char pub_key[320];
get_bls_pub_key(eid, &err_status, errMsg1, encr_key, dec_key_len, pub_key);
std::string result = pub_key;
}
\ No newline at end of file
......@@ -14,10 +14,14 @@ std::vector <std::vector<std::string>> get_verif_vect(const char* encryptedPolyH
std::vector<std::string> SplitString(const char* koefs, const char symbol);
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::string& publicKeys, int n, int t);
std::string get_secret_shares(const std::string& polyName, const char* encryptedPolyHex, const std::vector<std::string>& publicKeys, int n, int t);
bool VerifyShares(const char* publicShares, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind);
bool CreateBLSShare( const std::string& BLSKeyName, const char * s_shares, const char * encryptedKeyHex);
std::string GetBLSPubKey(const char * encryptedKeyHex);
bool VerifyShares(const char* encryptedPolyHex, const char* encr_sshare, const char * encryptedKeyHex, int t, int n, int ind);
bool CreateBLSShare( const char * s_shares, const char * encryptedKeyHex);
#endif //SGXD_DKGCRYPTO_H
......@@ -18,13 +18,14 @@ std::vector<std::string> gen_ecdsa_key(){
status = generate_ecdsa_key(eid, &err_status, errMsg, encr_pr_key, &enc_len, pub_key_x, pub_key_y );
std::vector<std::string> keys(2);
std::cerr << "account key is " << errMsg << std::endl;
char *hexEncrKey = (char *) calloc(2*BUF_LEN, 1);
carray2Hex(encr_pr_key, enc_len, hexEncrKey);
keys.at(0) = hexEncrKey;
keys.at(1) = std::string(pub_key_x) + std::string(pub_key_y);
//std::cerr << "in ECDSACrypto encr key x " << keys.at(0) << std::endl;
std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl;
//std::cerr << "in ECDSACrypto encr_len %d " << enc_len << std::endl;
free(errMsg);
free(pub_key_x);
......
......@@ -76,6 +76,34 @@ void LevelDB::writeString(const std::string &_key, const std::string &_value) {
std::cerr << "written key " << _key << " value " << _value <<std::endl;
}
void LevelDB::deleteDHDKGKey (const std::string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
std::string full_key = "DKG_DH_KEY_" + _key;
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
std::cerr << "key deleted " << full_key << std::endl;
}
void LevelDB::deleteOlegKey (const std::string &_key) {
std::lock_guard<std::recursive_mutex> lock(mutex);
std::string full_key = "key" + _key;
auto status = db->Delete(writeOptions, Slice(_key));
throwExceptionOnError(status);
std::cerr << "key deleted " << full_key << std::endl;
}
void LevelDB::writeByteArray(const char *_key, size_t _keyLen, const char *value,
size_t _valueLen) {
......
......@@ -58,6 +58,10 @@ public:
void writeByteArray(std::string& _key, const char *value,
size_t _valueLen);
void deleteDHDKGKey (const std::string &_key);
void deleteOlegKey (const std::string &_key);
public:
......
......@@ -284,25 +284,35 @@ Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t)
for ( int i = 0; i < t; i++){
std::vector<std::string> cur_coef = verifVector.at(i);
string num = std::to_string(i);
result["Verification Vector"][i][num]["X"]["c0"] = cur_coef.at(0);
result["Verification Vector"][i][num]["X"]["c1"] = cur_coef.at(1);
result["Verification Vector"][i][num]["Y"]["c0"] = cur_coef.at(2);
result["Verification Vector"][i][num]["Y"]["c1"] = cur_coef.at(3);
for ( int j = 0; j < 4; j++ ){
result["Verification Vector"][i][j] = cur_coef.at(j);
}
}
return result;
}
Json::Value getSecretShareImpl(const std::string& polyName, const std::string& publicKeys, int n, int t){
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int n, int t){
std::cerr << " enter getSecretShareImpl" << std::endl;
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
try {
if (publicKeys.size() != n){
result["errorMessage"] = "wrong number of public keys";
return result;
}
std::shared_ptr<std::string> encr_poly_ptr = readFromDb(polyName, "DKGPoly:");
std::string s = get_secret_shares(polyName, encr_poly_ptr->c_str(), publicKeys, n, t);
std::vector<std::string> pubKeys_vect;
for ( int i = 0; i < n ; i++) {
pubKeys_vect.push_back(publicKeys[i].asString());
}
std::string s = get_secret_shares(polyName, encr_poly_ptr->c_str(), pubKeys_vect, n, t);
//std::cerr << "result is " << s << std::endl;
result["SecretShare"] = s;
......@@ -316,21 +326,23 @@ Json::Value getSecretShareImpl(const std::string& polyName, const std::string& p
return result;
}
Json::Value DKGVerificationImpl(const std::string& polyName, const std::string& EthKeyName,
Json::Value DKGVerificationImpl(const std::string& publicShares, const std::string& EthKeyName,
const std::string& SecretShare, int t, int n, int ind){
std::cerr << " enter DKGVerificationImpl" << std::endl;
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
result["result"] = true;
try {
std::shared_ptr<std::string> encryptedPolyHex_ptr = readFromDb(polyName, "DKGPoly:");
//std::string keyName = polyName + "_" + std::to_string(ind);
//std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(EthKeyName, "");
std::shared_ptr<std::string> encryptedKeyHex_ptr = readECDSAKey(EthKeyName);
if ( !VerifyShares(encryptedPolyHex_ptr->c_str(), SecretShare.c_str(), encryptedKeyHex_ptr->c_str(), t, n, ind )){
if ( !VerifyShares(publicShares.c_str(), SecretShare.c_str(), encryptedKeyHex_ptr->c_str(), t, n, ind )){
result["result"] = false;
}
......@@ -345,41 +357,51 @@ Json::Value DKGVerificationImpl(const std::string& polyName, const std::string&
return result;
}
Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int t, int n){
Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n){
std::cerr << "CreateBLSPrivateKeyImpl entered" << std::endl;
std::cerr << " enter DKGVerificationImpl" << std::endl;
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
try {
if (SecretShare.size() != n){
result["errorMessage"] = "wrong number of secret shares";
if (SecretShare.length() != n * 192){
result["errorMessage"] = "wrong length of secret shares";
return result;
}
std::vector<std::string> sshares_vect;
std::cerr << "sshares are " << std::endl;
//std::cerr << "sshares are " << std::endl;
char sshares[192 * n + 1];
for ( int i = 0; i < n ; i++){
sshares_vect.push_back(SecretShare[i].asString());
std::string cur_share = SecretShare.substr(192*i, 192*i + 192);
// std::cerr << " share " << i << " is " << cur_share << std::endl;
sshares_vect.push_back(cur_share);
// std::cerr << sshares_vect[i] << " ";
strncpy(sshares + i * 192, SecretShare[i].asString().c_str(), 192);
strncpy(sshares + i * 192, cur_share.c_str(), 192);
}
sshares[192 * n ] = 0;
std::cerr << sshares << std::endl;
std::cerr << "length is " << strlen(sshares);
//std::cerr << sshares << std::endl;
//std::cerr << "length is " << strlen(sshares);
std::shared_ptr<std::string> encryptedKeyHex_ptr = readECDSAKey(EthKeyName);
bool res = CreateBLSShare(sshares, encryptedKeyHex_ptr->c_str());
bool res = CreateBLSShare(BLSKeyName, sshares, encryptedKeyHex_ptr->c_str());
if ( res){
std::cerr << "key created " << std::endl;
}
else {
std::cerr << "error " << std::endl;
}
/* for ( int i = 0; i < n; i++){
std::string name = polyName + "_" + std::to_string(i) + ":";
levelDb -> deleteDHDKGKey(name);
}*/
} catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl;
result["status"] = _e.status;
......@@ -390,7 +412,28 @@ Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::s
return result;
}
Json::Value GetBLSPublicKeyShareImpl(const std::string & BLSKeyName){
Json::Value result;
result["status"] = 0;
result["errorMessage"] = "";
try {
std::shared_ptr<std::string> encryptedKeyHex_ptr = readFromDb(BLSKeyName, "");
std::string public_key = GetBLSPubKey(encryptedKeyHex_ptr->c_str());
result["BLSPublicKeyShare"] = public_key;
} catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl;
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
}
return result;
}
Json::Value SGXWalletServer::generateDKGPoly(const std::string& polyName, int t){
std::cerr << "entered generateECDSAKey" << std::endl;
lock_guard<recursive_mutex> lock(m);
return generateDKGPolyImpl(polyName, t);
}
......@@ -400,22 +443,28 @@ Json::Value SGXWalletServer::getVerificationVector(const std::string& polyName,
return getVerificationVectorImpl(polyName, n, t);
}
Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t){
Json::Value SGXWalletServer::getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t){
lock_guard<recursive_mutex> lock(m);
return getSecretShareImpl(polyName, publicKeys, n, t);
}
Json::Value SGXWalletServer::DKGVerification( const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index){
Json::Value SGXWalletServer::DKGVerification( const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index){
lock_guard<recursive_mutex> lock(m);
return DKGVerificationImpl(polyName, EthKeyName, SecretShare, t, n, index);
return DKGVerificationImpl(publicShares, EthKeyName, SecretShare, t, n, index);
}
Json::Value SGXWalletServer::CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int t, int n){
Json::Value SGXWalletServer::CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string& SecretShare, int t, int n){
lock_guard<recursive_mutex> lock(m);
return CreateBLSPrivateKeyImpl(BLSKeyName, EthKeyName, SecretShare, t, n);
return CreateBLSPrivateKeyImpl(BLSKeyName, EthKeyName, polyName, SecretShare, t, n);
}
Json::Value SGXWalletServer::GetBLSPublicKeyShare(const std::string & BLSKeyName){
lock_guard<recursive_mutex> lock(m);
return GetBLSPublicKeyShareImpl(BLSKeyName);
}
Json::Value SGXWalletServer::generateECDSAKey(const std::string &_keyName) {
lock_guard<recursive_mutex> lock(m);
return generateECDSAKeyImpl(_keyName);
......@@ -426,12 +475,13 @@ Json::Value SGXWalletServer::getPublicECDSAKey(const std::string &_keyName) {
return getPublicECDSAKeyImpl(_keyName);
}
Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_keyName, const std::string &messageHash ) {
Json::Value SGXWalletServer::ecdsaSignMessageHash(int base, const std::string &_keyName, const std::string &messageHash ) {
lock_guard<recursive_mutex> lock(m);
std::cerr << "entered ecdsaSignMessageHash" << std::endl;
std::cerr << "MessageHash first " << messageHash << std::endl;
return ecdsaSignMessageHashImpl(base,_keyName, messageHash);
}
}
Json::Value
......
......@@ -30,9 +30,10 @@ public:
virtual Json::Value generateDKGPoly(const std::string& polyName, int t);
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t);
virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t);
virtual Json::Value DKGVerification(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
virtual Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int t, int n);
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t);
virtual Json::Value DKGVerification(const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
virtual Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
virtual Json::Value GetBLSPublicKeyShare(const std::string & BLSKeyName);
};
......@@ -58,8 +59,9 @@ Json::Value getPublicECDSAKeyImpl(const std::string& keyName);
Json::Value generateDKGPolyImpl(const std::string& polyName, int t);
Json::Value getVerificationVectorImpl(const std::string& polyName, int n, int t);
Json::Value getSecretShareImpl(const std::string& polyName, const std::string& publicKeys, int n, int t);
Json::Value DKGVerificationImpl(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int t, int n);
Json::Value getSecretShareImpl(const std::string& polyName, const Json::Value& publicKeys, int n, int t);
Json::Value DKGVerificationImpl(const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index);
Json::Value CreateBLSPrivateKeyImpl(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n);
Json::Value GetBLSPublicKeyShareImpl(const std::string & BLSKeyName);
#endif //SGXWALLET_SGXWALLETSERVER_HPP
\ No newline at end of file
......@@ -22,9 +22,11 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
this->bindAndAddMethod(jsonrpc::Procedure("generateDKGPoly", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::generateDKGPolyI);
this->bindAndAddMethod(jsonrpc::Procedure("getVerificationVector", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"polyName",jsonrpc::JSON_STRING, "n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getVerificationVectorI);
this->bindAndAddMethod(jsonrpc::Procedure("getSecretShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"publicKeys",jsonrpc::JSON_STRING,"n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI);
this->bindAndAddMethod(jsonrpc::Procedure("DKGVerification", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING, "EthKeyName",jsonrpc::JSON_STRING, "SecretShare",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::DKGVerificationI);
this->bindAndAddMethod(jsonrpc::Procedure("CreateBLSPrivateKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "BLSKeyName",jsonrpc::JSON_STRING, "EthKeyName",jsonrpc::JSON_STRING,"SecretShare",jsonrpc::JSON_ARRAY,"t", jsonrpc::JSON_INTEGER,"n",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::CreateBLSPrivateKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("getSecretShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "polyName",jsonrpc::JSON_STRING,"publicKeys",jsonrpc::JSON_ARRAY, "n",jsonrpc::JSON_INTEGER,"t",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::getSecretShareI);
this->bindAndAddMethod(jsonrpc::Procedure("DKGVerification", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "publicShares",jsonrpc::JSON_STRING, "EthKeyName",jsonrpc::JSON_STRING, "SecretShare",jsonrpc::JSON_STRING,"t",jsonrpc::JSON_INTEGER, "n",jsonrpc::JSON_INTEGER, "index",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::DKGVerificationI);
this->bindAndAddMethod(jsonrpc::Procedure("CreateBLSPrivateKey", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "BLSKeyName",jsonrpc::JSON_STRING, "EthKeyName",jsonrpc::JSON_STRING, "polyName", jsonrpc::JSON_STRING, "SecretShare",jsonrpc::JSON_STRING,"t", jsonrpc::JSON_INTEGER,"n",jsonrpc::JSON_INTEGER, NULL), &AbstractStubServer::CreateBLSPrivateKeyI);
this->bindAndAddMethod(jsonrpc::Procedure("GetBLSPublicKeyShare", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, "BLSKeyName",jsonrpc::JSON_STRING, NULL), &AbstractStubServer::GetBLSPublicKeyShareI);
}
inline virtual void importBLSKeyShareI(const Json::Value &request, Json::Value &response)
......@@ -61,15 +63,19 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
}
inline virtual void getSecretShareI(const Json::Value &request, Json::Value &response)
{
response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"].asString(), request["n"].asInt(),request["t"].asInt());
response = this->getSecretShare(request["polyName"].asString(), request["publicKeys"], request["n"].asInt(),request["t"].asInt());
}
inline virtual void DKGVerificationI(const Json::Value &request, Json::Value &response)
{
response = this->DKGVerification(request["polyName"].asString(), request["EthKeyName"].asString(), request["SecretShare"].asString(), request["t"].asInt(), request["n"].asInt(), request["index"].asInt());
response = this->DKGVerification(request["publicShares"].asString(), request["EthKeyName"].asString(), request["SecretShare"].asString(), request["t"].asInt(), request["n"].asInt(), request["index"].asInt());
}
inline virtual void CreateBLSPrivateKeyI(const Json::Value &request, Json::Value &response)
{
response = this->CreateBLSPrivateKey(request["BLSKeyName"].asString(), request["EthKeyName"].asString(), request["SecretShare"],request["t"].asInt(), request["n"].asInt());
response = this->CreateBLSPrivateKey(request["BLSKeyName"].asString(), request["EthKeyName"].asString(), request["polyName"].asString(),request["SecretShare"].asString(),request["t"].asInt(), request["n"].asInt());
}
inline virtual void GetBLSPublicKeyShareI(const Json::Value &request, Json::Value &response)
{
response = this->GetBLSPublicKeyShare(request["BLSKeyName"].asString());
}
virtual Json::Value importBLSKeyShare(int index, const std::string& keyShare, const std::string& keyShareName, int n, int t) = 0;
......@@ -81,9 +87,10 @@ class AbstractStubServer : public jsonrpc::AbstractServer<AbstractStubServer>
virtual Json::Value generateDKGPoly(const std::string& polyName, int t) = 0;
virtual Json::Value getVerificationVector(const std::string& polyName, int n, int t) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) = 0;
virtual Json::Value DKGVerification( const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
virtual Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int t, int n) = 0;
virtual Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t) = 0;
virtual Json::Value DKGVerification( const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) = 0;
virtual Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string & SecretShare, int t, int n) = 0;
virtual Json::Value GetBLSPublicKeyShare(const std::string & BLSKeyName) = 0;
};
#endif //JSONRPC_CPP_STUB_ABSTRACTSTUBSERVER_H_
......@@ -92,6 +92,7 @@ BLSEnclave.o: BLSEnclave.cpp \
../trusted_libff/libff/algebra/fields/fp2.tcc \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp \
../trusted_libff/libff/algebra/curves/curve_utils.hpp \
../trusted_libff/libff/algebra/curves/curve_utils.tcc
......@@ -281,6 +282,8 @@ BLSEnclave.h:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp:
../trusted_libff/libff/algebra/curves/curve_utils.hpp:
../trusted_libff/libff/algebra/curves/curve_utils.tcc:
......@@ -89,7 +89,6 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
../trusted_libff/libff/algebra/fields/fp2.tcc \
../trusted_libff/libff/algebra/curves/curve_utils.hpp \
../trusted_libff/libff/algebra/curves/curve_utils.tcc \
../trusted_libff/libff/algebra/fields/fp.hpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp \
../sgxwallet_common.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/unistd.h \
......@@ -279,8 +278,6 @@ DKGUtils.h:
../trusted_libff/libff/algebra/curves/curve_utils.tcc:
../trusted_libff/libff/algebra/fields/fp.hpp:
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp:
../sgxwallet_common.h:
......
......@@ -18,9 +18,11 @@ void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){
char* pb_keyB_x = (char*)malloc(65);
strncpy(pb_keyB_x, pb_keyB, 64);
pb_keyB_x[64] = 0;
char* pb_keyB_y = (char*)malloc(65);
strncpy(pb_keyB_y, pb_keyB + 64, 64);
pb_keyB_y[64] = 0;
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
......@@ -43,6 +45,7 @@ void gen_session_key(char *skey_str, char* pb_keyB, char* common_key){
common_key[i] = '0';
}
strncpy(common_key + n_zeroes, arr_x, strlen(arr_x));
common_key[64] = 0;
mpz_clear(skey);
point_clear(pub_keyB);
......@@ -55,9 +58,11 @@ void session_key_recover(const char *skey_str, const char* sshare, char* common_
char* pb_keyB_x = (char*)malloc(65);
strncpy(pb_keyB_x, sshare + 64, 64);
pb_keyB_x[64] = 0;
char* pb_keyB_y = (char*)malloc(65);
strncpy(pb_keyB_y, sshare + 128, 64);
pb_keyB_y[64] = 0;
domain_parameters curve = domain_parameters_init();
domain_parameters_load_curve(curve, secp256k1);
......@@ -80,10 +85,11 @@ void session_key_recover(const char *skey_str, const char* sshare, char* common_
common_key[i] = '0';
}
strncpy(common_key + n_zeroes, arr_x, strlen(arr_x));
//strncpy(common_key , pb_keyB_x, 64);
//strncpy(common_key , sshare, 64);
mpz_clear(skey);
point_clear(pub_keyB);
point_clear(session_key);
domain_parameters_clear(curve);
free(pb_keyB_x);
free(pb_keyB_y);
......
......@@ -4,7 +4,6 @@
#include "DKGUtils.h"
#include <sgx_tgmp.h>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp>
......@@ -53,6 +52,19 @@ std::string ConvertToString(T field_elem, int base = 10) {
return output;
}
std::string ConvertG2ToString(const libff::alt_bn128_G2 & elem, int base = 10, std::string delim = ":"){
std::string result;
result += ConvertToString(elem.X.c0);
result += delim;
result += ConvertToString(elem.X.c1);
result += delim;
result += ConvertToString(elem.Y.c0);
result += delim;
result += ConvertToString(elem.Y.c1);
return result;
}
std::vector<libff::alt_bn128_Fr> SplitStringToFr(const char* koefs, const char symbol){
std::string str(koefs);
std::string delim;
......@@ -137,6 +149,21 @@ void calc_secret_share(const char* decrypted_koefs, char * s_share,
}
void calc_secret_shareG2(const char* decrypted_koefs, char * s_shareG2,
unsigned _t, unsigned ind){
libff::init_alt_bn128_params();
char symbol = ':';
std::vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_koefs, symbol);
libff::alt_bn128_Fr secret_share = PolynomialValue(poly, libff::alt_bn128_Fr(ind), _t);
libff::alt_bn128_G2 secret_shareG2 = secret_share * libff::alt_bn128_G2::one();
std::string secret_shareG2_str = ConvertG2ToString(secret_shareG2);
strncpy(s_shareG2, secret_shareG2_str.c_str(), secret_shareG2_str.length());
}
void calc_public_shares(const char* decrypted_koefs, char * public_shares,
unsigned _t) {
libff::init_alt_bn128_params();
......@@ -147,26 +174,58 @@ void calc_public_shares(const char* decrypted_koefs, char * public_shares,
for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one() ;
pub_share.to_affine_coordinates();
result += ConvertToString(pub_share.X.c0);
result += ":";
result += ConvertToString(pub_share.X.c1);
result += ":";
result += ConvertToString(pub_share.Y.c0);
result += ":";
result += ConvertToString(pub_share.Y.c1);
result += ",";
std::string pub_share_str = ConvertG2ToString(pub_share);
result += pub_share_str + ",";
}
strncpy(public_shares, result.c_str(), result.length());
}
int Verification (char * decrypted_koefs, mpz_t decr_secret_share, int _t, int ind ){
//extern "C" int __gmpz_set_str (mpz_ptr, const char *, int);
std::string ConvertHexToDec(std::string hex_str){
mpz_t dec;
mpz_init(dec);
mpz_set_str(dec, hex_str.c_str(), 16);
char arr[mpz_sizeinbase (dec, 10) + 2];
char * result = mpz_get_str(arr, 10, dec);
mpz_clear(dec);
return result;
}
int Verification ( char * public_shares, mpz_t decr_secret_share, int _t, int ind ){
std::string pub_shares_str = public_shares;
libff::init_alt_bn128_params();
char symbol = ':';
std::vector<libff::alt_bn128_Fr> poly = SplitStringToFr(decrypted_koefs, symbol);
std::vector<libff::alt_bn128_G2> pub_shares;
uint64_t share_length = 256;
uint8_t coord_length = 64;
for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_G2 pub_share = poly.at(i) * libff::alt_bn128_G2::one();
libff::alt_bn128_G2 pub_share;
uint64_t pos0 = share_length * i;
pub_share.X.c0 = libff::alt_bn128_Fq(ConvertHexToDec(pub_shares_str.substr(pos0, coord_length)).c_str());
pub_share.X.c1 = libff::alt_bn128_Fq(ConvertHexToDec(pub_shares_str.substr(pos0 + coord_length, coord_length)).c_str());
pub_share.Y.c0 = libff::alt_bn128_Fq(ConvertHexToDec(pub_shares_str.substr(pos0 + 2 * coord_length, coord_length)).c_str());
pub_share.Y.c1 = libff::alt_bn128_Fq(ConvertHexToDec(pub_shares_str.substr(pos0 + 3 * coord_length, coord_length)).c_str());
pub_share.Z = libff::alt_bn128_Fq2::one();
//for ( int j = 0; j < 4; j++) {
//uint64_t pos0 = share_length * j;
//std::string coord = ConvertHexToDec(pub_shares_str.substr(pos0 + j * coord_length, coord_length));
// if ( i == 0) {
// memset(public_shares, 0, strlen(public_shares));
// std::string coord = ConvertToString(pub_share.Y.c1);
// strncpy(public_shares, coord.c_str(), coord.length());
// }
//}
pub_shares.push_back(pub_share);
}
......@@ -179,12 +238,50 @@ int Verification (char * decrypted_koefs, mpz_t decr_secret_share, int _t, int i
char * tmp = mpz_get_str(arr, 10, decr_secret_share);
libff::alt_bn128_Fr sshare(tmp);
//strncpy(decrypted_koefs, ConvertToString(val.X.c0).c_str(), 1024);
// strncpy(public_shares, tmp, strlen(tmp));
// std::string res = ConvertHexToDec("fe43567238abcdef98760");
// strncpy(public_shares, res.c_str(), res.length());
libff::alt_bn128_G2 val2 = sshare * libff::alt_bn128_G2::one();
strncpy(decrypted_koefs, ConvertToString(val2.X.c0).c_str(), 1024);
memset(public_shares, 0, strlen(public_shares));
strncpy(public_shares, ConvertToString(val2.X.c0).c_str(), ConvertToString(val2.X.c0).length());
strncpy(public_shares + ConvertToString(val2.X.c0).length(), ":", 1);
strncpy(public_shares + ConvertToString(val2.X.c0).length() + 1, ConvertToString(val2.X.c1).c_str(), 77);
val.to_affine_coordinates();
val2.to_affine_coordinates();
// strncpy(public_shares + strlen(tmp), ":", 1);
// strncpy(public_shares + 77 + 1, ConvertToString(val.X.c0).c_str(), 77);
// strncpy(public_shares + 77 + 78, ":", 1);
// strncpy(public_shares + 77 + 79, ConvertToString(val2.X.c0).c_str(), 77);
/*strncpy(public_shares + 77 + 77 + 79, "\n", 1);
strncpy(public_shares + 144 + 79, ConvertToString(val2.X.c0).c_str(), 77);
strncpy(public_shares + 144 + 78, ":", 1);
strncpy(public_shares + 144 + 77, ConvertToString(val2.X.c1).c_str(), 77);*/
return (val == sshare * libff::alt_bn128_G2::one());
}
void calc_bls_public_key(char* skey, char* pub_key){
libff::alt_bn128_Fr bls_skey(skey);
libff::alt_bn128_G2 public_key = bls_skey * libff::alt_bn128_G2::one();
public_key.to_affine_coordinates();
std::string result = ConvertG2ToString(public_key);
strncpy(pub_key, result.c_str(), result.length());
}
......
......@@ -24,7 +24,11 @@ EXTERNC void calc_secret_share(const char* decrypted_koefs, char * s_share,
EXTERNC void calc_public_shares(const char* decrypted_koefs, char * public_shares,
unsigned _t);
EXTERNC int Verification (char * decrypted_koefs, mpz_t decr_secret_share, int _t, int ind );
EXTERNC int Verification ( char * public_shares, mpz_t decr_secret_share, int _t, int ind);
EXTERNC void calc_bls_public_key(char* skey, char* pub_key);
EXTERNC void calc_secret_shareG2(const char* public_shares, char * s_shareG2,
unsigned _t, unsigned ind);
#endif //SGXD_DKGUTILS_H
......@@ -141,6 +141,7 @@ void generate_ecdsa_key(int *err_status, char *err_string,
mpz_mod(skey, seed, curve->p);
mpz_clear(seed);
//mpz_set_str(skey, "e7af72d241d4dd77bc080ce9234d742f6b22e35b3a660e8c197517b909f63ca8", 16);
//mpz_set_str(skey, "4160780231445160889237664391382223604576", 10);
//mpz_set_str(skey, "4160780231445160889237664391382223604184857153814275770598791864649971919844", 10);
//mpz_set_str(skey, "1", 10);
......@@ -175,7 +176,7 @@ void generate_ecdsa_key(int *err_status, char *err_string,
strncpy(pub_key_y + n_zeroes, arr_y, 1024 - n_zeroes);
char skey_str[mpz_sizeinbase (skey, ECDSA_SKEY_BASE) + 2];
char* s = mpz_get_str(skey_str, ECDSA_SKEY_BASE, skey);
snprintf(err_string, BUF_LEN, "skey is %s len %d\n", skey_str, strlen(skey_str));
snprintf(err_string, BUF_LEN, "skey is %s len %d\n", skey_str, strlen(skey_str));
uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);
......@@ -412,6 +413,8 @@ void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_s
gen_dkg_poly(dkg_secret, _t);
snprintf(err_string, BUF_LEN,"poly is %s ", dkg_secret);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, DKG_BUFER_LENGTH);//sizeof(sgx_sealed_data_t) + sizeof(dkg_secret);
sgx_status_t status = sgx_seal_data(0, NULL, DKG_BUFER_LENGTH, (uint8_t*)dkg_secret, sealedLen,(sgx_sealed_data_t*)encrypted_dkg_secret);
......@@ -574,7 +577,7 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
uint32_t enc_len;
generate_ecdsa_key(err_status, err_string, encrypted_skey, &enc_len, pub_key_x, pub_key_y);
snprintf(err_string, BUF_LEN,"pub_key_x is %s", pub_key_x);
// snprintf(err_string, BUF_LEN,"pub_key_x is %s", pub_key_x);
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_skey, NULL, 0, (uint8_t *)skey, &enc_len);
......@@ -583,25 +586,28 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
return;
}
snprintf(err_string, BUF_LEN,"unsealed random skey is %s\n", skey);
char * common_key = (char *)malloc(65);
gen_session_key(skey, pub_keyB, common_key);
//snprintf(err_string, BUF_LEN,"common key is %s", common_key);
//snprintf(err_string + 81, BUF_LEN,"pub_key_B is %s length is %d", pub_keyB, strlen(pub_keyB));
//snprintf(err_string + 88, BUF_LEN - 88,"\ncommon key is %s", common_key);
char* s_share = (char *)malloc(65);
//char s_share[65];
calc_secret_share(Decrypted_dkg_poly, s_share, _t, _n, ind);
//snprintf(err_string, BUF_LEN,"secret share is %s", s_share);
snprintf(err_string + 88, BUF_LEN,"\nsecret share is %s", s_share);
char* cypher = (char *)malloc(65);
xor_encrypt(common_key, s_share, cypher);
//snprintf(err_string, BUF_LEN,"cypher is %s length is %d", cypher, strlen(cypher));
//snprintf(err_string, BUF_LEN ,"cypher is %s length is %d", cypher, strlen(cypher));
strncpy(result_str, cypher, strlen(cypher));
strncpy(result_str + strlen(cypher), pub_key_x, strlen(pub_key_x));
strncpy(result_str + strlen(pub_key_x) + strlen(pub_key_y), pub_key_y, strlen(pub_key_y));
//snprintf(err_string, BUF_LEN,"s_share is %s length is %d", result_str, strlen(result_str));
// snprintf(err_string, BUF_LEN,"s_share is %s length is %d", result_str, strlen(result_str));
//mpz_clear(skey);
//free(skey);
......@@ -612,18 +618,29 @@ void get_encr_sshare(int *err_status, char *err_string, uint8_t *encrypted_skey,
free(cypher);
}
void dkg_verification(int *err_status, char* err_string, const uint8_t * encrypted_dkg_secret, const char* s_share,
uint8_t* encrypted_key, uint64_t key_len, unsigned _t, int _ind, int * result){
/*void complaint_response(int *err_status, char *err_string, uint8_t *encrypted_DHkey, uint8_t *encrypted_koefs, uint32_t* dec_len,
char* s_key, char* s_shareG2, uint8_t _t, uint8_t _n, uint8_t ind1, uint8_t ind2){
char* decrypted_dkg_secret = (char*)malloc(DKG_BUFER_LENGTH);
uint32_t enc_len;
uint32_t decr_len ;
decrypt_dkg_secret(err_status, err_string, encrypted_dkg_secret, (uint8_t*)decrypted_dkg_secret, &decr_len);
if (*err_status != 0) {
snprintf(err_string, BUF_LEN,"sgx_unseal_poly failed with status %d", *err_status);
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_DHkey, NULL, 0, (uint8_t *)skey, &enc_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
return;
}
char* s_shareG2 = (char *)malloc(196);
calc_secret_shareG2(decrypted_koefs, s_shareG2, _t, ind2);
free(s_shareG2);
}*/
void dkg_verification(int *err_status, char* err_string, const char * public_shares, const char* s_share,
uint8_t* encrypted_key, uint64_t key_len, unsigned _t, int _ind, int * result){
//uint32_t dec_len = 625;
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
......@@ -640,23 +657,28 @@ void dkg_verification(int *err_status, char* err_string, const uint8_t * encrypt
char common_key[65];
char decr_sshare[65];
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
xor_decrypt(common_key, encr_sshare, decr_sshare);
//snprintf(err_string, BUF_LEN,"sshare is %s", decr_sshare);
//snprintf(err_string, BUF_LEN,"encr_share is %s", encr_sshare);
//snprintf(err_string, BUF_LEN,"common_key is %s", common_key);
//snprintf(err_string, BUF_LEN,"encr_share is %s length is %d", encr_sshare, strlen(encr_sshare));
//snprintf(err_string, BUF_LEN,"s_share is %s length is %d", s_share, strlen(s_share));
// snprintf(err_string, BUF_LEN,"sshare is %s\n", decr_sshare);
// snprintf(err_string + 75, BUF_LEN - 75,"common_key is %s\n", common_key);
// snprintf(err_string + 153, BUF_LEN - 153," s_key is %s", skey);
mpz_t s;
mpz_init(s);
mpz_set_str(s, decr_sshare, 16);
*result = Verification(decrypted_dkg_secret, s, _t, _ind);
*result = Verification(public_shares, s, _t, _ind);
//snprintf(err_string, BUF_LEN,"val is %s", decrypted_dkg_secret);
snprintf(err_string, BUF_LEN,"data is %s", public_shares);
free(decrypted_dkg_secret);
}
void create_bls_key(int *err_status, char* err_string, const char* s_shares,
......@@ -677,20 +699,47 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_init(sum);
mpz_set_ui(sum, 0);
char encr_sshare[65];
strncpy(encr_sshare, s_shares, 64);
encr_sshare[64] = 0;
char s_share[193];
strncpy(s_share, s_shares, 192);
s_share[192] = 0;
char common_key[65];
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
//snprintf(err_string, BUF_LEN,"comon0 is %s len is %d\n", common_key, strlen(common_key));
for ( int i = 0; i < num_shares; i++) {
char encr_sshare[65];
strncpy(encr_sshare, s_shares + 192 * i, 64);
encr_sshare[64] = 0;
char s_share[193];
strncpy(s_share, s_share + 192 * i, 192);
strncpy(s_share, s_shares + 192 * i, 192);
s_share[192] = 0;
char common_key[65];
session_key_recover(skey, s_share, common_key);
common_key[64] = 0;
//snprintf(err_string + 85*(i+1) , BUF_LEN,"common is %s len is %d\n", common_key, strlen(common_key));
//snprintf(err_string + 201*i , BUF_LEN,"secret is %s",s_share);
char decr_sshare[65];
xor_decrypt(common_key, encr_sshare, decr_sshare);
//decr_sshare[64] = 0;
//snprintf(err_string + 89*i, BUF_LEN,"share is %s length is %d ", decr_sshare, strlen(decr_sshare));
//snprintf(err_string + 65*i, BUF_LEN,"%s ", decr_sshare);
mpz_t decr_secret_share;
mpz_init(decr_secret_share);
......@@ -702,21 +751,22 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_t q;
mpz_init(q);
mpz_set_str(q, "21888242871839275222246405745257275088696311157297823662689037894645226208583", 10);
mpz_set_str(q, "21888242871839275222246405745257275088548364400416034343698204186575808495617", 10);
mpz_t bls_key;
mpz_init(bls_key);
mpz_mod(bls_key, sum, q);
char arr[mpz_sizeinbase(bls_key, 10) + 2];
char *key = mpz_get_str(arr, 10, bls_key);
char key_share[mpz_sizeinbase(bls_key, 10) + 2];
char *key = mpz_get_str(key_share, 10, bls_key);
snprintf(err_string, BUF_LEN,"bls private key is %s", key_share);
uint32_t sealedLen = sgx_calc_sealed_data_size(0, ECDSA_SKEY_LEN);
status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (uint8_t *)key, sealedLen,(sgx_sealed_data_t*)encr_bls_key);
status = sgx_seal_data(0, NULL, ECDSA_SKEY_LEN, (uint8_t *)key_share, sealedLen,(sgx_sealed_data_t*)encr_bls_key);
if( status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"seal bls private key failed");
snprintf(err_string, BUF_LEN,"seal bls private key failed with status %d ", status);
return;
}
......@@ -737,6 +787,23 @@ void create_bls_key(int *err_status, char* err_string, const char* s_shares,
mpz_clear(bls_key);
mpz_clear(sum);
mpz_clear(q);
}
void get_bls_pub_key(int *err_status, char* err_string, uint8_t* encrypted_key, uint64_t key_len, char* bls_pub_key){
char skey[ECDSA_SKEY_LEN];
sgx_status_t status = sgx_unseal_data(
(const sgx_sealed_data_t *)encrypted_key, NULL, 0, (uint8_t *)skey, &key_len);
if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
return;
}
calc_bls_public_key(skey, bls_pub_key);
}
......
......@@ -121,8 +121,8 @@ enclave {
public void dkg_verification(
[user_check] int *err_status,
[out, count = 1024] char* err_string,
[in, count = 3050] const uint8_t* encrypted_dkg_secret,
[in, count = 129] const char* s_share,
[in, count = 4097] const char* public_shares,
[in, count = 193] const char* s_share,
[in, count = 1024] uint8_t* encrypted_key,
uint64_t key_len,
unsigned _t,
......@@ -136,6 +136,13 @@ enclave {
[in, count = 1024] uint8_t* encrypted_key,
uint64_t key_len,
[out, count = 1024] uint8_t * encr_bls_key);
public void get_bls_pub_key(
[user_check]int *err_status,
[out, count = 1024] char* err_string,
[in, count = 1024] uint8_t* encrypted_key,
uint64_t key_len,
[out, count = 320] char* bls_pub_key);
};
......
......@@ -116,7 +116,7 @@
"name": "getSecretShare",
"params": {
"polyName": "key1",
"publicKeys": "123",
"publicKeys": ["122","1222"],
"n": 3,
"t": 3
},
......@@ -129,9 +129,9 @@
{
"name": "DKGVerification",
"params": {
"polyName": "p2",
"EthKeyName":"key1",
"SecretShare": "123",
"publicShares": "123",
"EthKeyName":"key_j",
"SecretShare": "f_ij",
"n": 3,
"t": 3,
"index" : 2
......@@ -139,7 +139,7 @@
"returns": {
"status": 0,
"errorMessage": "12345",
"result": true
"result": True
}
},
{
......@@ -147,7 +147,8 @@
"params": {
"BLSKeyName": "key",
"EthKeyName":"key1",
"SecretShare": ["122","1222"],
"polyName":"p1",
"SecretShare": "122",
"n": 3,
"t": 3
},
......@@ -156,6 +157,18 @@
"errorMessage": "12345",
"BLSKeyName": "key"
}
},
{
"name": "GetBLSPublicKeyShare",
"params": {
"BLSKeyName": "key"
},
"returns": {
"status": 0,
"errorMessage": "12345",
"BLSPublicKeyShare": "123"
}
}
......
......@@ -106,7 +106,7 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value getSecretShare(const std::string& polyName, const std::string& publicKeys, int n, int t) throw (jsonrpc::JsonRpcException)
Json::Value getSecretShare(const std::string& polyName, const Json::Value& publicKeys, int n, int t) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["polyName"] = polyName;
......@@ -119,14 +119,14 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value DKGVerification(const std::string& polyName, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) throw (jsonrpc::JsonRpcException)
Json::Value DKGVerification(const std::string& publicShares, const std::string& EthKeyName, const std::string& SecretShare, int t, int n, int index) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["EthKeyName"] = EthKeyName;
p["SecretShare"] = SecretShare;
p["index"] = index;
p["n"] = n;
p["polyName"] = polyName;
p["publicShares"] = publicShares;
p["t"] = t;
Json::Value result = this->CallMethod("DKGVerification",p);
if (result.isObject())
......@@ -134,11 +134,12 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const Json::Value& SecretShare, int n, int t) throw (jsonrpc::JsonRpcException)
Json::Value CreateBLSPrivateKey(const std::string & BLSKeyName, const std::string& EthKeyName, const std::string& polyName, const std::string& SecretShare, int n, int t) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["BLSKeyName"] = BLSKeyName;
p["EthKeyName"] = EthKeyName;
p["polyName"] = polyName;
p["SecretShare"] = SecretShare;
p["n"] = n;
p["t"] = t;
......@@ -148,6 +149,19 @@ class StubClient : public jsonrpc::Client
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
Json::Value GetBLSPublicKeyShare(const std::string & BLSKeyName) throw (jsonrpc::JsonRpcException)
{
Json::Value p;
p["BLSKeyName"] = BLSKeyName;
Json::Value result = this->CallMethod("GetBLSPublicKeyShare",p);
if (result.isObject())
return result;
else
throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString());
}
};
#endif //JSONRPC_CPP_STUB_STUBCLIENT_H_
......@@ -71,6 +71,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "stubclient.h"
std::string stringFromFr(libff::alt_bn128_Fr& el) {
mpz_t t;
......@@ -684,6 +686,20 @@ TEST_CASE("get public ECDSA key", "[get_pub_ecdsa_key_test]") {
sgx_destroy_enclave(eid);
}
/*TEST_CASE( "verification test", "[verify]" ) {
char* pubshares = "0d72c21fc5a43452ad5f36699822309149ce6ce2cdce50dafa896e873f1b8ddd12f65a2e9c39c617a1f695f076b33b236b47ed773901fc2762f8b6f63277f5e30d7080be8e98c97f913d1920357f345dc0916c1fcb002b7beb060aa8b6b473a011bfafe9f8a5d8ea4c643ca4101e5119adbef5ae64f8dfb39cd10f1e69e31c591858d7eaca25b4c412fe909ca87ca7aadbf6d97d32d9b984e93d436f13d43ec31f40432cc750a64ac239cad6b8f78c1f1dd37427e4ff8c1cc4fe1c950fcbcec10ebfd79e0c19d0587adafe6db4f3c63ea9a329724a8804b63a9422e6898c0923209e828facf3a073254ec31af4231d999ba04eb5b7d1e0056d742a65b766f2f3";
char *sec_share = "11592366544581417165283270001305852351194685098958224535357729125789505948557";
mpz_t sshare;
mpz_init(sshare);
mpz_set_str(sshare, "11592366544581417165283270001305852351194685098958224535357729125789505948557", 10);
int result = Verification(pubshares, sshare, 2, 0);
REQUIRE(result == 1);
}*/
#include "stubclient.h"
#include <jsonrpccpp/client/connectors/httpclient.h>
......@@ -704,11 +720,15 @@ TEST_CASE("API test", "[api_test]") {
cerr << "Client inited" << endl;
try {
// cout << c.generateECDSAKey("test_key2") << endl;
//levelDb->deleteOlegKey("0");
//levelDb->deleteOlegKey("1");
// cout << c.generateECDSAKey("key1") << endl;
//cout<<c.getPublicECDSAKey("test_key1");
//cout << c.ecdsaSignMessageHash(16, "known_key1","0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db" );
// cout << c.blsSignMessageHash(TEST_BLS_KEY_NAME, "0x09c6137b97cdf159b9950f1492ee059d1e2b10eaf7d51f3a97d61f2eee2e81db", 2,2,1 );
// cout << c.generateDKGPoly("p2", 2);
// cout << c.generateDKGPoly("p2", 2);
//cout << c.getVerificationVector("polyy", 5, 5);
// cout << c.getSecretShare("p2",
......@@ -719,23 +739,37 @@ TEST_CASE("API test", "[api_test]") {
// "669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e",
// 2,2);
// cout << c.generateDKGPoly("p3", 3);
Json::Value publicKeys;
publicKeys.append("505f55a38f9c064da744f217d1cb993a17705e9839801958cda7c884e08ab4dad7fd8d22953d3ac7f0913de24fd67d7ed36741141b8a3da152d7ba954b0f14e2");
publicKeys.append("378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e");
// cout << c.getSecretShare("p2", publicKeys, 2, 2);
// cout << c.generateDKGPoly("p3", 3);
// cout << c.getSecretShare("p3",
// "669aa790e1c5f5199af82ab0b6f1965c382d23a2ebdda581454adba3fd082a30edab62b545f78f1e402ceef7340a0364a7046633d6151fe7e657d8b8a6352378b3e6fdfe2633256ae1662fcd23466d02ead907b5d4366136341cea5e46f5a7bb67d897d6e35f619810238aa143c416f61c640ed214eb9c67a34c4a31b7d25e6e9d43f1c88581f53af993da1654c9f91829c1fe5344c4452ef8d2d8675c6a051c19029f6e4f82b035fb3552058cf22c5bbafd9e6456d579634987281765d130b0",
// 3,3);
std::string share_big0 = "501e364a6ea516f4812b013bcc150cbb435a2c465c9fd525951264969d8441a986798fd3317c1c3e60f868bb26c4cff837d9185f4be6015d8326437cb5b69480495859cd5a385430ece51252acdc234d8dbde75708b600ac50b2974e813ee26bd87140d88647fcc44df7262bbba24328e8ce622cd627a15b508ffa0db9ae81e0e110fab42cfe40da66b524218ca3c8e5aa3363fbcadef748dc3523a7ffb95b8f5d8141a5163db9f69d1ab223494ed71487c9bb032a74c08a222d897a5e49a617";
std::string share_big = "03f749e2fcc28021895d757ec16d1636784446f5effcd3096b045136d8ab02657b32adc577f421330b81f5b7063df3b08a0621a897df2584b9046ca416e50ecc27e8c3277e981f7e650f8640289be128eecf0105f89a20e5ffb164744c45cf191d627ce9ab6c44e2ef96f230f2a4de742ea43b6f74b56849138026610b2d965605ececba527048a0f29f46334b1cec1d23df036248b24eccca99057d24764acee66c1a3f2f44771d0d237bf9d18c4177277e3ce3dc4e83686a2647fce1565ee0";
std::string share = share_big.substr(0, 192);
//cout << c.DKGVerification("p2", "test_key1", share, 2, 2, 0);
std::string publicShares = "1fc8154abcbf0c2ebf559571d7b57a8995c0e293a73d4676a8f76051a0d0ace30e00a87c9f087254c9c860c3215c4f11e8f85a3e8fae19358f06a0cbddf3df1924b1347b9b58f5bcb20958a19bdbdd832181cfa9f9e9fd698f6a485051cb47b829d10f75b6e227a7d7366dd02825b5718072cd42c39f0352071808622b7db6421b1069f519527e49052a8da6e3720cbda9212fc656eef945f5e56a4159c3b9622d883400460a9eff07fe1873f9b1ec50f6cf70098b9da0b90625b176f12329fa2ecc65082c626dc702d9cfb23a06770d4a2c7867e269efe84e3709b11001fb380a32d609855d1d46bc60f21140c636618b8ff55ed06d7788b6f81b498f96d3f9";
cout << c.DKGVerification(publicShares, "test_key1", "37092c06c423b627c38ff86d1e66608bdc1496ef855b86e9f773441ac0b285d92aa466376a6008de4aab9858aa34848775282c4c3b56370bf25827321619c6e47701c8a32e3f4bb28f5a3b12a09800f318c550cedff6150e9a673ea56ece8b76", 2, 2, 0);
// cout << c.DKGVerification("oleh1", "key0", "37092c06c423b627c38ff86d1e66608bdc1496ef855b86e9f773441ac0b285d92aa466376a6008de4aab9858aa34848775282c4c3b56370bf25827321619c6e47701c8a32e3f4bb28f5a3b12a09800f318c550cedff6150e9a673ea56ece8b76", 2, 2, 0);
Json::Value SecretShare;
SecretShare.append(share_big0);
SecretShare.append(share_big);
cout << c.CreateBLSPrivateKey( "test_bls_key","test_key1", SecretShare, 2, 2 );
//cout << c.CreateBLSPrivateKey( "test_bls_key1","test_key1", "p2", share_big0, 2, 2 );
// std::string shares = "252122c309ed1f32faa897ede140c5b9c1bc07d5d9c94b7a22d4eeb13da7b7142aa466376a6008de4aab9858aa34848775282c4c3b56370bf25827321619c6e47701c8a32e3f4bb28f5a3b12a09800f318c550cedff6150e9a673ea56ece8b76df831dbef474cfc38be1c980130a8d273ff410fbf87deece9d7756a1b08ba9e954c1676cc7f2cac16e16cff0c877d8cf967381321fb4cc78e3638245a1dc85419766d281aff4935cc6eac25c9842032c8f7fae567c57622969599a72c42d2e1e";
std::string shares = "252122c309ed1f32faa897ede140c5b9c1bc07d5d9c94b7a22d4eeb13da7b7142aa466376a6008de4aab9858aa34848775282c4c3b56370bf25827321619c6e47701c8a32e3f4bb28f5a3b12a09800f318c550cedff6150e9a673ea56ece8b7637092c06c423b627c38ff86d1e66608bdc1496ef855b86e9f773441ac0b285d92aa466376a6008de4aab9858aa34848775282c4c3b56370bf25827321619c6e47701c8a32e3f4bb28f5a3b12a09800f318c550cedff6150e9a673ea56ece8b76";
// cout << c.CreateBLSPrivateKey( "test_bls","key0", "oleh1", shares, 2, 2 );
// cout << c.GetBLSPublicKeyShare("test_bls_key");
} catch (JsonRpcException &e) {
cerr << e.what() << endl;
......
......@@ -5,7 +5,7 @@
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
namespace libff {
......
......@@ -9,10 +9,10 @@
#define ALT_BN128_G1_HPP_
#include <vector>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
#include <libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <libff/algebra/curves/curve_utils.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <../trusted_libff/libff/algebra/curves/curve_utils.hpp>
namespace libff {
......
......@@ -5,7 +5,7 @@
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
namespace libff {
......
......@@ -9,8 +9,8 @@
#define ALT_BN128_G2_HPP_
#include <vector>
#include <libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <libff/algebra/curves/curve_utils.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <../trusted_libff/libff/algebra/curves/curve_utils.hpp>
namespace libff {
......
......@@ -5,9 +5,9 @@
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
#include <libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
#include <libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
namespace libff {
......
......@@ -7,9 +7,9 @@
#ifndef ALT_BN128_INIT_HPP_
#define ALT_BN128_INIT_HPP_
#include <libff/algebra/curves/public_params.hpp>
#include <libff/algebra/fields/fp.hpp>
#include <libff/algebra/fields/fp2.hpp>
#include <../trusted_libff/libff/algebra/curves/public_params.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp2.hpp>
namespace libff {
......
......@@ -5,7 +5,7 @@
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
namespace libff {
......
......@@ -7,9 +7,9 @@
#ifndef ALT_BN128_PP_HPP_
#define ALT_BN128_PP_HPP_
#include <libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
#include <libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <libff/algebra/curves/public_params.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.hpp>
#include <../trusted_libff/libff/algebra/curves/public_params.hpp>
namespace libff {
......
......@@ -9,7 +9,7 @@
#define CURVE_UTILS_HPP_
#include <cstdint>
#include <libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
namespace libff {
......
......@@ -14,7 +14,7 @@
#include <cstdint>
#include <libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
namespace libff {
......
......@@ -14,7 +14,7 @@
#ifndef EXPONENTIATION_TCC_
#define EXPONENTIATION_TCC_
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
......@@ -9,9 +9,9 @@
#define FIELD_UTILS_HPP_
#include <cstdint>
#include <libff/algebra/fields/bigint.hpp>
#include <libff/common/double.hpp>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/double.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
......@@ -13,8 +13,8 @@
#include <complex>
#include <stdexcept>
#include <libff/common/double.hpp>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/common/double.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
......@@ -10,9 +10,9 @@
#ifndef FP_HPP_
#define FP_HPP_
#include <libff/common/utils.hpp>
#include <libff/algebra/exponentiation/exponentiation.hpp>
#include <libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
#include <../trusted_libff/libff/algebra/exponentiation/exponentiation.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
namespace libff {
......
......@@ -14,8 +14,8 @@
#include <cstdlib>
#include <limits>
#include <libff/algebra/fields/field_utils.hpp>
#include <libff/algebra/fields/fp_aux.tcc>
#include <../trusted_libff/libff/algebra/fields/field_utils.hpp>
#include <../trusted_libff/libff/algebra/fields/fp_aux.tcc>
namespace libff {
......
......@@ -11,7 +11,8 @@
#define FP2_HPP_
#include <vector>
#include <libff/algebra/fields/fp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp>
namespace libff {
......
......@@ -10,7 +10,7 @@
#ifndef FP2_TCC_
#define FP2_TCC_
#include <libff/algebra/fields/field_utils.hpp>
#include <../trusted_libff/libff/algebra/fields/field_utils.hpp>
namespace libff {
......
......@@ -18,12 +18,12 @@
#include <cassert>
#include <type_traits>
#include <libff/algebra/fields/bigint.hpp>
#include <libff/algebra/fields/fp_aux.tcc>
#include <libff/algebra/scalar_multiplication/multiexp.hpp>
#include <libff/algebra/scalar_multiplication/wnaf.hpp>
#include <libff/common/profiling.hpp>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/algebra/fields/fp_aux.tcc>
#include <../trusted_libff/libff/algebra/scalar_multiplication/multiexp.hpp>
#include <../trusted_libff/libff/algebra/scalar_multiplication/wnaf.hpp>
#include <../trusted_libff/libff/common/profiling.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
#include <cstdio>
#include <vector>
#include <libff/algebra/curves/bn128/bn128_pp.hpp>
#include <libff/algebra/scalar_multiplication/multiexp.hpp>
#include <libff/common/profiling.hpp>
#include <libff/common/rng.hpp>
#include <../trusted_libff/libff/algebra/curves/bn128/bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/scalar_multiplication/multiexp.hpp>
#include <../trusted_libff/libff/common/profiling.hpp>
#include <../trusted_libff/libff/common/rng.hpp>
using namespace libff;
......
......@@ -14,8 +14,8 @@
#include <math.h>
#include <libff/algebra/fields/bigint.hpp>
#include <libff/common/double.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/double.hpp>
namespace libff {
......
......@@ -14,7 +14,7 @@
#include <complex>
#include <libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
namespace libff {
......
......@@ -19,9 +19,9 @@
#include <stdexcept>
#include <vector>
#include <libff/common/default_types/ec_pp.hpp>
#include <libff/common/profiling.hpp>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/common/default_types/ec_pp.hpp>
#include <../trusted_libff/libff/common/profiling.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
#ifndef NO_PROCPS
#include <proc/readproc.h>
......
......@@ -21,6 +21,6 @@ FieldT SHA512_rng(const uint64_t idx);
} // libff
#include <libff/common/rng.tcc>
#include <../trusted_libff/libff/common/rng.tcc>
#endif // RNG_HPP_
......@@ -17,9 +17,9 @@
#include <gmp.h>
#include <openssl/sha.h>
#include <libff/algebra/fields/bigint.hpp>
#include <libff/common/rng.hpp>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/rng.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
......@@ -12,7 +12,7 @@
#include <cstdarg>
#include <cstdint>
#include <libff/common/utils.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
namespace libff {
......
......@@ -10,9 +10,9 @@
#ifndef FP_HPP_
#define FP_HPP_
#include <libff/common/utils.hpp>
#include <libff/algebra/exponentiation/exponentiation.hpp>
#include <libff/algebra/fields/bigint.hpp>
#include <../trusted_libff/libff/common/utils.hpp>
#include <../trusted_libff/libff/algebra/exponentiation/exponentiation.hpp>
#include <../trusted_libff/libff/algebra/fields/bigint.hpp>
namespace libff {
......
/** @file
*****************************************************************************
Implementation of arithmetic in the finite field F[p^2].
*****************************************************************************
* @author This file is part of libff, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#ifndef FP2_HPP_
#define FP2_HPP_
#include <vector>
#include <libff/algebra/fields/fp.hpp>
namespace libff {
template<mp_size_t n, const bigint<n>& modulus>
class Fp2_model;
template<mp_size_t n, const bigint<n>& modulus>
std::ostream& operator<<(std::ostream &, const Fp2_model<n, modulus> &);
template<mp_size_t n, const bigint<n>& modulus>
std::istream& operator>>(std::istream &, Fp2_model<n, modulus> &);
/**
* Arithmetic in the field F[p^2].
*
* Let p := modulus. This interface provides arithmetic for the extension field
* Fp2 = Fp[U]/(U^2-non_residue), where non_residue is in Fp.
*
* ASSUMPTION: p = 1 (mod 6)
*/
template<mp_size_t n, const bigint<n>& modulus>
class Fp2_model {
public:
typedef Fp_model<n, modulus> my_Fp;
static bigint<2*n> euler; // (modulus^2-1)/2
static size_t s; // modulus^2 = 2^s * t + 1
static bigint<2*n> t; // with t odd
static bigint<2*n> t_minus_1_over_2; // (t-1)/2
static my_Fp non_residue; // X^4-non_residue irreducible over Fp; used for constructing Fp2 = Fp[X] / (X^2 - non_residue)
static Fp2_model<n, modulus> nqr; // a quadratic nonresidue in Fp2
static Fp2_model<n, modulus> nqr_to_t; // nqr^t
static my_Fp Frobenius_coeffs_c1[2]; // non_residue^((modulus^i-1)/2) for i=0,1
my_Fp c0, c1;
Fp2_model() {};
Fp2_model(const my_Fp& c0, const my_Fp& c1) : c0(c0), c1(c1) {};
void clear() { c0.clear(); c1.clear(); }
void print() const { printf("c0/c1:\n"); c0.print(); c1.print(); }
static Fp2_model<n, modulus> zero();
static Fp2_model<n, modulus> one();
static Fp2_model<n, modulus> random_element();
bool is_zero() const { return c0.is_zero() && c1.is_zero(); }
bool operator==(const Fp2_model &other) const;
bool operator!=(const Fp2_model &other) const;
Fp2_model operator+(const Fp2_model &other) const;
Fp2_model operator-(const Fp2_model &other) const;
Fp2_model operator*(const Fp2_model &other) const;
Fp2_model operator-() const;
Fp2_model squared() const; // default is squared_complex
Fp2_model inverse() const;
Fp2_model Frobenius_map(unsigned long power) const;
Fp2_model sqrt() const; // HAS TO BE A SQUARE (else does not terminate)
Fp2_model squared_karatsuba() const;
Fp2_model squared_complex() const;
template<mp_size_t m>
Fp2_model operator^(const bigint<m> &other) const;
static size_t size_in_bits() { return 2*my_Fp::size_in_bits(); }
static bigint<n> base_field_char() { return modulus; }
friend std::ostream& operator<< <n, modulus>(std::ostream &out, const Fp2_model<n, modulus> &el);
friend std::istream& operator>> <n, modulus>(std::istream &in, Fp2_model<n, modulus> &el);
};
template<mp_size_t n, const bigint<n>& modulus>
std::ostream& operator<<(std::ostream& out, const std::vector<Fp2_model<n, modulus> > &v);
template<mp_size_t n, const bigint<n>& modulus>
std::istream& operator>>(std::istream& in, std::vector<Fp2_model<n, modulus> > &v);
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n, modulus> operator*(const Fp_model<n, modulus> &lhs, const Fp2_model<n, modulus> &rhs);
template<mp_size_t n, const bigint<n>& modulus>
bigint<2*n> Fp2_model<n, modulus>::euler;
template<mp_size_t n, const bigint<n>& modulus>
size_t Fp2_model<n, modulus>::s;
template<mp_size_t n, const bigint<n>& modulus>
bigint<2*n> Fp2_model<n, modulus>::t;
template<mp_size_t n, const bigint<n>& modulus>
bigint<2*n> Fp2_model<n, modulus>::t_minus_1_over_2;
template<mp_size_t n, const bigint<n>& modulus>
Fp_model<n, modulus> Fp2_model<n, modulus>::non_residue;
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n, modulus> Fp2_model<n, modulus>::nqr;
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n, modulus> Fp2_model<n, modulus>::nqr_to_t;
template<mp_size_t n, const bigint<n>& modulus>
Fp_model<n, modulus> Fp2_model<n, modulus>::Frobenius_coeffs_c1[2];
} // libff
#include <libff/algebra/fields/fp2.tcc>
#endif // FP2_HPP_
/** @file
*****************************************************************************
Implementation of arithmetic in the finite field F[p^2].
*****************************************************************************
* @author This file is part of libff, developed by SCIPR Lab
* and contributors (see AUTHORS).
* @copyright MIT license (see LICENSE file)
*****************************************************************************/
#ifndef FP2_TCC_
#define FP2_TCC_
#include <libff/algebra/fields/field_utils.hpp>
namespace libff {
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::zero()
{
return Fp2_model<n, modulus>(my_Fp::zero(), my_Fp::zero());
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::one()
{
return Fp2_model<n, modulus>(my_Fp::one(), my_Fp::zero());
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::random_element()
{
Fp2_model<n, modulus> r;
r.c0 = my_Fp::random_element();
r.c1 = my_Fp::random_element();
return r;
}
template<mp_size_t n, const bigint<n>& modulus>
bool Fp2_model<n,modulus>::operator==(const Fp2_model<n,modulus> &other) const
{
return (this->c0 == other.c0 && this->c1 == other.c1);
}
template<mp_size_t n, const bigint<n>& modulus>
bool Fp2_model<n,modulus>::operator!=(const Fp2_model<n,modulus> &other) const
{
return !(operator==(other));
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::operator+(const Fp2_model<n,modulus> &other) const
{
return Fp2_model<n,modulus>(this->c0 + other.c0,
this->c1 + other.c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::operator-(const Fp2_model<n,modulus> &other) const
{
return Fp2_model<n,modulus>(this->c0 - other.c0,
this->c1 - other.c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n, modulus> operator*(const Fp_model<n, modulus> &lhs, const Fp2_model<n, modulus> &rhs)
{
return Fp2_model<n,modulus>(lhs*rhs.c0,
lhs*rhs.c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::operator*(const Fp2_model<n,modulus> &other) const
{
/* Devegili OhEig Scott Dahab --- Multiplication and Squaring on Pairing-Friendly Fields.pdf; Section 3 (Karatsuba) */
const my_Fp
&A = other.c0, &B = other.c1,
&a = this->c0, &b = this->c1;
const my_Fp aA = a * A;
const my_Fp bB = b * B;
return Fp2_model<n,modulus>(aA + non_residue * bB,
(a + b)*(A+B) - aA - bB);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::operator-() const
{
return Fp2_model<n,modulus>(-this->c0,
-this->c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::squared() const
{
return squared_complex();
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::squared_karatsuba() const
{
/* Devegili OhEig Scott Dahab --- Multiplication and Squaring on Pairing-Friendly Fields.pdf; Section 3 (Karatsuba squaring) */
const my_Fp &a = this->c0, &b = this->c1;
const my_Fp asq = a.squared();
const my_Fp bsq = b.squared();
return Fp2_model<n,modulus>(asq + non_residue * bsq,
(a + b).squared() - asq - bsq);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::squared_complex() const
{
/* Devegili OhEig Scott Dahab --- Multiplication and Squaring on Pairing-Friendly Fields.pdf; Section 3 (Complex squaring) */
const my_Fp &a = this->c0, &b = this->c1;
const my_Fp ab = a * b;
return Fp2_model<n,modulus>((a + b) * (a + non_residue * b) - ab - non_residue * ab,
ab + ab);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::inverse() const
{
const my_Fp &a = this->c0, &b = this->c1;
/* From "High-Speed Software Implementation of the Optimal Ate Pairing over Barreto-Naehrig Curves"; Algorithm 8 */
const my_Fp t0 = a.squared();
const my_Fp t1 = b.squared();
const my_Fp t2 = t0 - non_residue * t1;
const my_Fp t3 = t2.inverse();
const my_Fp c0 = a * t3;
const my_Fp c1 = - (b * t3);
return Fp2_model<n,modulus>(c0, c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::Frobenius_map(unsigned long power) const
{
return Fp2_model<n,modulus>(c0,
Frobenius_coeffs_c1[power % 2] * c1);
}
template<mp_size_t n, const bigint<n>& modulus>
Fp2_model<n,modulus> Fp2_model<n,modulus>::sqrt() const
{
Fp2_model<n,modulus> one = Fp2_model<n,modulus>::one();
size_t v = Fp2_model<n,modulus>::s;
Fp2_model<n,modulus> z = Fp2_model<n,modulus>::nqr_to_t;
Fp2_model<n,modulus> w = (*this)^Fp2_model<n,modulus>::t_minus_1_over_2;
Fp2_model<n,modulus> x = (*this) * w;
Fp2_model<n,modulus> b = x * w; // b = (*this)^t
#if DEBUG
// check if square with euler's criterion
Fp2_model<n,modulus> check = b;
for (size_t i = 0; i < v-1; ++i)
{
check = check.squared();
}
if (check != one)
{
assert(0);
}
#endif
// compute square root with Tonelli--Shanks
// (does not terminate if not a square!)
while (b != one)
{
size_t m = 0;
Fp2_model<n,modulus> b2m = b;
while (b2m != one)
{
/* invariant: b2m = b^(2^m) after entering this loop */
b2m = b2m.squared();
m += 1;
}
int j = v-m-1;
w = z;
while (j > 0)
{
w = w.squared();
--j;
} // w = z^2^(v-m-1)
z = w.squared();
b = b * z;
x = x * w;
v = m;
}
return x;
}
template<mp_size_t n, const bigint<n>& modulus>
template<mp_size_t m>
Fp2_model<n,modulus> Fp2_model<n,modulus>::operator^(const bigint<m> &pow) const
{
return power<Fp2_model<n, modulus>, m>(*this, pow);
}
template<mp_size_t n, const bigint<n>& modulus>
std::ostream& operator<<(std::ostream &out, const Fp2_model<n, modulus> &el)
{
out << el.c0 << OUTPUT_SEPARATOR << el.c1;
return out;
}
template<mp_size_t n, const bigint<n>& modulus>
std::istream& operator>>(std::istream &in, Fp2_model<n, modulus> &el)
{
in >> el.c0 >> el.c1;
return in;
}
template<mp_size_t n, const bigint<n>& modulus>
std::ostream& operator<<(std::ostream& out, const std::vector<Fp2_model<n, modulus> > &v)
{
out << v.size() << "\n";
for (const Fp2_model<n, modulus>& t : v)
{
out << t << OUTPUT_NEWLINE;
}
return out;
}
template<mp_size_t n, const bigint<n>& modulus>
std::istream& operator>>(std::istream& in, std::vector<Fp2_model<n, modulus> > &v)
{
v.clear();
size_t s;
in >> s;
char b;
in.read(&b, 1);
v.reserve(s);
for (size_t i = 0; i < s; ++i)
{
Fp2_model<n, modulus> el;
in >> el;
v.emplace_back(el);
}
return in;
}
} // libff
#endif // FP2_TCC_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment