From d44abc1dcfedba400713951646c0fd3a475e5f47 Mon Sep 17 00:00:00 2001 From: Jamie Cui Date: Fri, 22 Nov 2024 19:25:49 +0800 Subject: [PATCH] fix: sha256 circuit with multi-bytes input --- yacl/engine/plaintext/executor.h | 13 ------------- yacl/engine/plaintext/executor_test.cc | 8 ++------ yacl/io/circuit/bristol_fashion.h | 5 +++-- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/yacl/engine/plaintext/executor.h b/yacl/engine/plaintext/executor.h index 9d71e23..3e4aae8 100644 --- a/yacl/engine/plaintext/executor.h +++ b/yacl/engine/plaintext/executor.h @@ -82,19 +82,6 @@ class PlainExecutor { total_out_bitnum += circ_->now[i]; } - // // Make sure that the circuit output wire is full bytes - // YACL_ENFORCE(total_out_bitnum % 8 == 0); - - // const size_t wire_size = wires_.size(); - // dynamic_bitset result(total_out_bitnum); - // for (size_t i = 0; i < total_out_bitnum; ++i) { - // result[total_out_bitnum - i - 1] = wires_[wire_size - i - 1]; - // } - // YACL_ENFORCE(result.size() == total_out_bitnum); - // std::vector out(total_out_bitnum / 8); - // std::memcpy(out.data(), result.data(), out.size()); - // SPDLOG_INFO(result.to_string()); - // return out; std::vector out(total_out_bitnum / 8); diff --git a/yacl/engine/plaintext/executor_test.cc b/yacl/engine/plaintext/executor_test.cc index c7deac5..0fa13ff 100644 --- a/yacl/engine/plaintext/executor_test.cc +++ b/yacl/engine/plaintext/executor_test.cc @@ -210,10 +210,7 @@ TEST(CryptoTest, Aes128Test) { TEST(CryptoTest, Sha256Test) { /* GIVEN */ auto input = crypto::FastRandBytes(crypto::RandLtN(10)); - - // std::array temp = {'a', 'b', 'c'}; - // std::string temp = "1"; - std::array message = {'1'}; + auto message = crypto::FastRandBytes(10); auto in_buf = io::BuiltinBFCircuit::PrepareSha256Input(message); /* WHEN */ @@ -225,9 +222,8 @@ TEST(CryptoTest, Sha256Test) { /* THEN */ auto compare = crypto::Sha256Hash().Update(message).CumulativeHash(); - SPDLOG_INFO(absl::BytesToHexString(ByteContainerView(result))); - SPDLOG_INFO(absl::BytesToHexString(ByteContainerView(compare))); EXPECT_EQ(compare.size(), result.size()); + EXPECT_EQ(memcmp(compare.data(), result.data(), compare.size()), 0); } } // namespace yacl::engine diff --git a/yacl/io/circuit/bristol_fashion.h b/yacl/io/circuit/bristol_fashion.h index 5561541..958d1a9 100644 --- a/yacl/io/circuit/bristol_fashion.h +++ b/yacl/io/circuit/bristol_fashion.h @@ -185,7 +185,9 @@ class BuiltinBFCircuit { // original input message // auto input_reverse = ReverseBytes(absl::MakeSpan(input)); // copy here - std::memcpy(result.data() + offset, input.data(), input_size); + auto input_reverse = std::vector(input.begin(), input.end()); + std::reverse(input_reverse.begin(), input_reverse.end()); + std::memcpy(result.data() + offset, input_reverse.data(), input_size); offset += input_size; // initial hash values @@ -214,7 +216,6 @@ class BuiltinBFCircuit { std::filesystem::current_path().string()); } - static std::array GetSha256InitialHashValues() { std::array standard_init_array = { 0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae, 0x85, 0x3c, 0x6e, 0xf3,