github.com/ledgerwatch/erigon-lib@v1.0.0/pedersen_hash/ffi_pedersen_hash.cc (about)

     1  #include "ffi_pedersen_hash.h"
     2  #include "pedersen_hash.h"
     3  
     4  #include <array>
     5  
     6  #include "prime_field_element.h"
     7  #include "ffi_utils.h"
     8  
     9  #include "gsl-lite.hpp"
    10  
    11  namespace starkware {
    12  
    13  namespace {
    14  
    15  using ValueType = PrimeFieldElement::ValueType;
    16  
    17  constexpr size_t kElementSize = sizeof(ValueType);
    18  constexpr size_t kOutBufferSize = 1024;
    19  static_assert(kOutBufferSize >= kElementSize, "kOutBufferSize is not big enough");
    20  
    21  }  // namespace
    22  
    23  #ifdef __cplusplus
    24  extern "C" {
    25  #endif
    26  
    27  int Hash(
    28      const gsl::byte in1[kElementSize], const gsl::byte in2[kElementSize],
    29      gsl::byte out[kOutBufferSize]) {
    30    try {
    31      auto hash = PedersenHash(
    32          PrimeFieldElement::FromBigInt(Deserialize(gsl::make_span(in1, kElementSize))),
    33          PrimeFieldElement::FromBigInt(Deserialize(gsl::make_span(in2, kElementSize))));
    34      Serialize(hash.ToStandardForm(), gsl::make_span(out, kElementSize));
    35    } catch (const std::exception& e) {
    36      return HandleError(e.what(), gsl::make_span(out, kOutBufferSize));
    37    } catch (...) {
    38      return HandleError("Unknown c++ exception.", gsl::make_span(out, kOutBufferSize));
    39    }
    40    return 0;
    41  }
    42  
    43  #ifdef __cplusplus
    44  } // extern C
    45  #endif 
    46  }  // namespace starkware
    47  
    48  
    49  
    50  int GoHash(const char* in1, const char* in2, char* out) {
    51  	return starkware::Hash(
    52  	reinterpret_cast<const gsl::byte *>(in1),
    53  	reinterpret_cast<const gsl::byte *>(in2), 
    54  	reinterpret_cast<gsl::byte *>(out));
    55  }
    56