TLA Line data Source code
1 : // evmone: Fast Ethereum Virtual Machine implementation
2 : // Copyright 2023 The evmone Authors.
3 : // SPDX-License-Identifier: Apache-2.0
4 :
5 : #include "bloom_filter.hpp"
6 : #include "state.hpp"
7 :
8 : namespace evmone::state
9 : {
10 :
11 : namespace
12 : {
13 : /// Adds an entry to the bloom filter.
14 : /// based on
15 : /// https://ethereum.github.io/execution-specs/autoapi/ethereum/shanghai/bloom/index.html#add-to-bloom
16 UBC 0 : inline void add_to(BloomFilter& bf, const bytes_view& entry)
17 : {
18 0 : const auto hash = keccak256(entry);
19 :
20 : // take the least significant 11-bits of the first three 16-bit values
21 0 : for (const auto i : {0, 2, 4})
22 : {
23 0 : const auto bit_to_set = ((hash.bytes[i] & 0x07) << 8) | hash.bytes[i + 1];
24 0 : const auto bit_index = 0x07FF - bit_to_set;
25 0 : const auto byte_index = bit_index / 8;
26 0 : const auto bit_pos = static_cast<uint8_t>(1 << (7 - (bit_index % 8)));
27 0 : bf.bytes[byte_index] |= bit_pos;
28 : }
29 0 : }
30 :
31 : } // namespace
32 :
33 CBC 53 : BloomFilter compute_bloom_filter(std::span<const Log> logs) noexcept
34 : {
35 53 : BloomFilter res;
36 53 : for (const auto& log : logs)
37 : {
38 UBC 0 : add_to(res, log.addr);
39 0 : for (const auto& topic : log.topics)
40 0 : add_to(res, topic);
41 : }
42 :
43 CBC 53 : return res;
44 : }
45 :
46 53 : BloomFilter compute_bloom_filter(std::span<const TransactionReceipt> receipts) noexcept
47 : {
48 53 : BloomFilter res;
49 :
50 106 : for (const auto& r : receipts)
51 53 : std::transform(
52 53 : res.bytes, std::end(res.bytes), r.logs_bloom_filter.bytes, res.bytes, std::bit_or<>());
53 :
54 53 : return res;
55 : }
56 :
57 UBC 0 : BloomFilter bloom_filter_from_bytes(const bytes_view& data) noexcept
58 : {
59 0 : assert(data.size() == 256);
60 0 : BloomFilter res;
61 0 : std::copy(std::begin(data), std::end(data), std::begin(res.bytes));
62 0 : return res;
63 : }
64 :
65 : } // namespace evmone::state
|