Skip to main content

Advanced Cryptographic Support for Blockchain

This framework, together with the two open source libraries safeheron-crypto-suites-cpp and safeheron-multi-party-signature-cpp, implements the multi-party computing (MPC) protocol, zero-knowledge proof (ZKP) protocol, ecies encryption algorithm, ecdsa signature algorithm, etc. in a trusted environment, which has great support for blockchain.

zero-knowledge proof protocol

  • DLogProof
const Curve * curv = GetCurveParam(CurveType::SECP256K1);
BN r = RandomBNLt(curv->n);
BN sk = RandomBNLt(curv->n);
DLogProof proof(CurveType::SECP256K1);
proof.ProveWithR(sk, r);
proof.Verify();
  • PailNProof
PailPubKey pail_pub;
PailPrivKey pail_priv;
CreateKeyPair2048(pail_priv, pail_pub);
PailNProof proof;
proof.Prove(pail_priv);
proof.Verify(pail_pub);

ECIES encryption algorithm

//plain: This is a test string!
//cypher: 04c8e0354cf0acb480467839a78d144f518bc8c3ac6a7c7e4b94ba9045d2ca9299a78dd742981afe7261404f85939cce7efd022ccbd35c4aaa3985947e99905bcdfac8f3d7e6245df9c877e892f843b4a8426d8d8bf78bb945a6580b51fde9d85abc099ff2a5e764988441e773a64b8d87a8eb0ba780274afabf6270fbef748d408b91479ae97fb70b6a82dc11047c27047a12a5dd8f575d984cf8a040d65c688a
const Curve *curv = GetCurveParam(CurveType::P256);
std::string message = "This is a test string!";

BN priv = RandomBNLt(curv->n);
CurvePoint pub = curv->g * priv;

std::string plain;
std::string cypher;
std::string iv;
bool ok = true;
ECIES enc;
enc.set_curve_type(CurveType::P256);
// encrypt
ok = enc.Encrypt(pub, message, iv, cypher);
if (!ok) return false;
// decrypt
ok = enc.Decrypt(priv, cypher, iv, plain);
if (!ok) return false;

if (plain != message) return false;

ECDSA signature algorithm

const int DIGEST_SIZE = 32;
const int SIG_SIZE = 64;
const Curve *curv = GetCurveParam(CurveType::SECP256K1);

BN privkey = safeheron::rand::RandomBNLt(curv->n);
CurvePoint pubkey = curv->g * privkey;

std::string message = "hello world!";
uint8_t digest[DIGEST_SIZE] = {0};
CSHA256 sha;
sha.Write((uint8_t*)message.c_str(), message.length());
sha.Finalize(digest);

uint8_t sig[SIG_SIZE] = {0};
// sign
safeheron::curve::ecdsa::Sign(type, privkey, digest, sig);

// verify
bool pass = safeheron::curve::ecdsa::Verify(type, pubkey, digest, sig);
if (!pass) return false;