123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #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<uint8_t>& bytes)
- {
- std::stringstream stream;
- for (uint8_t b : bytes)
- {
- stream << std::setw(2)
- << std::setfill('0')
- << std::hex
- << static_cast<int>(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<uint8_t*>(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<uint8_t> 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++)
- {
- Curvepoint x;
- is >> x;
- input.curvepointUniversals.push_back(x);
- }
- is >> numElements;
- for (size_t i = 0; i < numElements; i++)
- {
- CurveBipoint 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;
- }
|