#include "proof.hpp" /* Altered from answer at * https://stackoverflow.com/questions/51144505/generate-sha-3-hash-in-c-using-openssl-library */ // Convert the bytes to a single integer, then make that a Scalar Scalar bytes_to_scalar(const std::vector& bytes) { std::stringstream stream; for (uint8_t b : bytes) { stream << std::setw(2) << std::setfill('0') << std::hex << static_cast(b); } mpz_class value; value.set_str(stream.str(), 16); return Scalar(value); } // Random Oracle (i.e. SHA3_256) Scalar oracle(const std::string& input) { uint32_t digest_length = SHA256_DIGEST_LENGTH; const EVP_MD* algorithm = EVP_sha3_256(); uint8_t* digest = static_cast(OPENSSL_malloc(digest_length)); EVP_MD_CTX* context = EVP_MD_CTX_new(); EVP_DigestInit_ex(context, algorithm, NULL); EVP_DigestUpdate(context, input.c_str(), input.size()); EVP_DigestFinal_ex(context, digest, &digest_length); EVP_MD_CTX_destroy(context); std::vector digestBytes(digest, digest + digest_length); Scalar output = bytes_to_scalar(digestBytes); OPENSSL_free(digest); return output; } Proof::Proof() { /* Do nothing */ } Proof::Proof(std::string hbc) : hbc(hbc) { /* Do nothing */ } void Proof::clear() { hbc.clear(); curvepointUniversals.clear(); curveBipointUniversals.clear(); challengeParts.clear(); responseParts.clear(); } std::ostream& operator<<(std::ostream& os, const Proof& output) { if (!output.hbc.empty()) { os << true; os << output.hbc.size(); os << output.hbc; return os; } os << false; os << output.curvepointUniversals.size(); for (size_t i = 0; i < output.curvepointUniversals.size(); i++) os << output.curvepointUniversals[i]; os << output.curveBipointUniversals.size(); for (size_t i = 0; i < output.curveBipointUniversals.size(); i++) os << output.curveBipointUniversals[i]; os << output.challengeParts.size(); for (size_t i = 0; i < output.challengeParts.size(); i++) os << output.challengeParts[i]; os << output.responseParts.size(); for (size_t i = 0; i < output.responseParts.size(); i++) os << output.responseParts[i]; return os; } std::istream& operator>>(std::istream& is, Proof& input) { bool hbc; is >> hbc; if (hbc) { size_t numBytes; is >> numBytes; char* buffer = new char[numBytes + 1]; is.read(buffer, numBytes); input.hbc = buffer; delete buffer; return is; } size_t numElements; is >> numElements; for (size_t i = 0; i < numElements; i++) { Twistpoint x; is >> x; input.curvepointUniversals.push_back(x); } is >> numElements; for (size_t i = 0; i < numElements; i++) { TwistBipoint x; is >> x; input.curveBipointUniversals.push_back(x); } is >> numElements; for (size_t i = 0; i < numElements; i++) { Scalar x; is >> x; input.challengeParts.push_back(x); } is >> numElements; for (size_t i = 0; i < numElements; i++) { Scalar x; is >> x; input.responseParts.push_back(x); } return is; }