github.com/ledgerwatch/erigon-lib@v1.0.0/pedersen_hash/ffi_utils.cc (about) 1 #include <endian.h> 2 #include <algorithm> 3 #include <cstring> 4 5 #include "ffi_utils.h" 6 7 namespace starkware { 8 9 using ValueType = PrimeFieldElement::ValueType; 10 11 int HandleError(const char* msg, gsl::span<gsl::byte> out) { 12 const size_t copy_len = std::min<size_t>(strlen(msg), out.size() - 1); 13 memcpy(out.data(), msg, copy_len); 14 memset(out.data() + copy_len, 0, out.size() - copy_len); 15 return 1; 16 } 17 18 ValueType Deserialize(const gsl::span<const gsl::byte> span) { 19 const size_t N = ValueType::LimbCount(); 20 ASSERT(span.size() == N * sizeof(uint64_t), "Source span size mismatches BigInt size."); 21 std::array<uint64_t, N> value{}; 22 gsl::copy(span, gsl::byte_span(value)); 23 for (uint64_t& x : value) { 24 x = le64toh(x); 25 } 26 return ValueType(value); 27 } 28 29 void Serialize(const ValueType& val, const gsl::span<gsl::byte> span_out) { 30 const size_t N = ValueType::LimbCount(); 31 ASSERT(span_out.size() == N * sizeof(uint64_t), "Span size mismatches BigInt size."); 32 for (size_t i = 0; i < N; ++i) { 33 uint64_t limb = htole64(val[i]); 34 gsl::copy(gsl::byte_span(limb), span_out.subspan(i * sizeof(uint64_t), sizeof(uint64_t))); 35 } 36 } 37 38 } // namespace starkware