Skip to content

Commit

Permalink
fix: sha256 circuit with multi-bytes input
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie-Cui committed Nov 22, 2024
1 parent 2f3a8d3 commit d44abc1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
13 changes: 0 additions & 13 deletions yacl/engine/plaintext/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockType> 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<uint8_t> out(total_out_bitnum / 8);
// std::memcpy(out.data(), result.data(), out.size());
// SPDLOG_INFO(result.to_string());
// return out;

std::vector<uint8_t> out(total_out_bitnum / 8);

Expand Down
8 changes: 2 additions & 6 deletions yacl/engine/plaintext/executor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ TEST(CryptoTest, Aes128Test) {
TEST(CryptoTest, Sha256Test) {
/* GIVEN */
auto input = crypto::FastRandBytes(crypto::RandLtN(10));

// std::array<char, 3> temp = {'a', 'b', 'c'};
// std::string temp = "1";
std::array<char, 1> message = {'1'};
auto message = crypto::FastRandBytes(10);
auto in_buf = io::BuiltinBFCircuit::PrepareSha256Input(message);

/* WHEN */
Expand All @@ -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
5 changes: 3 additions & 2 deletions yacl/io/circuit/bristol_fashion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(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
Expand Down Expand Up @@ -214,7 +216,6 @@ class BuiltinBFCircuit {
std::filesystem::current_path().string());
}


static std::array<uint8_t, 32> GetSha256InitialHashValues() {
std::array<uint8_t, 32> standard_init_array = {
0x6a, 0x09, 0xe6, 0x67, 0xbb, 0x67, 0xae, 0x85, 0x3c, 0x6e, 0xf3,
Expand Down

0 comments on commit d44abc1

Please sign in to comment.