proof.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "proof.hpp"
  2. /* Altered from answer at
  3. * https://stackoverflow.com/questions/51144505/generate-sha-3-hash-in-c-using-openssl-library
  4. */
  5. // Convert the bytes to a single integer, then make that a Scalar
  6. Scalar bytes_to_scalar(const std::vector<uint8_t>& bytes)
  7. {
  8. std::stringstream stream;
  9. for (uint8_t b : bytes)
  10. {
  11. stream << std::setw(2)
  12. << std::setfill('0')
  13. << std::hex
  14. << static_cast<int>(b);
  15. }
  16. mpz_class value;
  17. value.set_str(stream.str(), 16);
  18. return Scalar(value);
  19. }
  20. // Random Oracle (i.e. SHA3_256)
  21. Scalar oracle(const std::string& input)
  22. {
  23. uint32_t digest_length = SHA256_DIGEST_LENGTH;
  24. const EVP_MD* algorithm = EVP_sha3_256();
  25. uint8_t* digest = static_cast<uint8_t*>(OPENSSL_malloc(digest_length));
  26. EVP_MD_CTX* context = EVP_MD_CTX_new();
  27. EVP_DigestInit_ex(context, algorithm, NULL);
  28. EVP_DigestUpdate(context, input.c_str(), input.size());
  29. EVP_DigestFinal_ex(context, digest, &digest_length);
  30. EVP_MD_CTX_destroy(context);
  31. std::vector<uint8_t> digestBytes(digest, digest + digest_length);
  32. Scalar output = bytes_to_scalar(digestBytes);
  33. OPENSSL_free(digest);
  34. return output;
  35. }