Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
crypto
siphash.cpp
Go to the documentation of this file.
1
// Copyright (c) 2016-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
crypto/siphash.h
>
6
7
#include <
uint256.h
>
8
9
#include <cassert>
10
#include <span>
11
12
CSipHasher::CSipHasher
(uint64_t k0, uint64_t k1) :
m_state
{k0, k1} {}
13
14
CSipHasher
&
CSipHasher::Write
(uint64_t data)
15
{
16
assert
(
m_count
% 8 == 0);
17
m_state
.
Compress2
(data);
18
m_count
+= 8;
19
return
*
this
;
20
}
21
22
CSipHasher
&
CSipHasher::Write
(std::span<const unsigned char> data)
23
{
24
SipHashState
state{
m_state
.
Copy
()};
25
uint64_t
t
{
m_tmp
};
26
uint8_t c{
m_count
};
27
28
while
(data.size() > 0) {
29
t
|= uint64_t{data.front()} << (8 * (c % 8));
30
c++;
31
if
((c & 7) == 0) {
32
state.
Compress2
(
t
);
33
t
= 0;
34
}
35
data = data.subspan(1);
36
}
37
38
m_state
= state;
39
m_count
= c;
40
m_tmp
=
t
;
41
42
return
*
this
;
43
}
44
45
uint64_t
CSipHasher::Finalize
()
const
46
{
47
return
m_state
.
Copy
()
48
.
Compress2
(
m_tmp
| (uint64_t{
m_count
} << 56))
49
.Finalize4();
50
}
51
52
SipHasher13UJ
&
SipHasher13UJ::Write
(uint64_t data)
noexcept
53
{
54
m_state
.Compress1(data);
55
return
*
this
;
56
}
57
58
SipHasher13UJ
&
SipHasher13UJ::WriteJumbo
(
const
uint256
& hash)
noexcept
59
{
60
m_state
.Compress1Jumbo(hash);
61
return
*
this
;
62
}
63
64
uint64_t
SipHasher13UJ::Finalize
() const noexcept
65
{
66
return
m_state
.
Copy
().
Finalize3U
();
67
}
68
69
uint64_t
PresaltedSipHasher::operator()
(
const
uint256
& val)
const
noexcept
70
{
71
return
m_state
.Copy()
72
.Compress2(val.GetUint64(0))
73
.Compress2(val.GetUint64(1))
74
.Compress2(val.GetUint64(2))
75
.Compress2(val.GetUint64(3))
76
.Compress2(uint64_t{32} << 56)
77
.Finalize4();
78
}
79
80
uint64_t
PresaltedSipHasher::operator()
(
const
uint256
& val, uint32_t extra)
const
noexcept
81
{
82
return
m_state
.Copy()
83
.Compress2(val.GetUint64(0))
84
.Compress2(val.GetUint64(1))
85
.Compress2(val.GetUint64(2))
86
.Compress2(val.GetUint64(3))
87
.Compress2((uint64_t{36} << 56) | extra)
88
.Finalize4();
89
}
CSipHasher::m_count
uint8_t m_count
Only the low 8 bits of the input size matter.
Definition
siphash.h:102
CSipHasher::Finalize
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition
siphash.cpp:45
CSipHasher::m_tmp
uint64_t m_tmp
Definition
siphash.h:101
CSipHasher::m_state
SipHashState m_state
Definition
siphash.h:100
CSipHasher::CSipHasher
CSipHasher(uint64_t k0, uint64_t k1)
Construct a SipHash calculator initialized with 128-bit key (k0, k1).
Definition
siphash.cpp:12
CSipHasher::Write
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition
siphash.cpp:14
PresaltedSipHasher::operator()
uint64_t operator()(const uint256 &val) const noexcept
Equivalent to CSipHasher(k0, k1).Write(val).Finalize().
Definition
siphash.cpp:69
PresaltedSipHasher::m_state
const SipHashState m_state
Definition
siphash.h:206
SipHashState
Shared SipHash state (v0..v3) with its round, compression, and finalization primitives.
Definition
siphash.h:18
SipHashState::Copy
ALWAYS_INLINE SipHashState Copy() const noexcept
Construct a copy of this state.
Definition
siphash.h:48
SipHashState::Finalize3U
ALWAYS_INLINE uint64_t Finalize3U() noexcept
Mutably finalize this state with 3 SipRounds using the unpadded finalizer, and return the resulting h...
Definition
siphash.h:87
SipHashState::Compress2
ALWAYS_INLINE SipHashState & Compress2(uint64_t data) noexcept
Mutably compress one block into this state, with 2 SipRounds.
Definition
siphash.h:67
SipHasher13UJ::Write
SipHasher13UJ & Write(uint64_t data) noexcept
Hash a normal 64-bit value.
Definition
siphash.cpp:52
SipHasher13UJ::WriteJumbo
SipHasher13UJ & WriteJumbo(const uint256 &hash) noexcept
Hash a 256-bit value as a jumbo block.
Definition
siphash.cpp:58
SipHasher13UJ::m_state
SipHashState m_state
Definition
siphash.h:162
SipHasher13UJ::Finalize
uint64_t Finalize() const noexcept
Compute the 64-bit SipHash-1-3-UJ of the data written so far.
Definition
siphash.cpp:64
SipHasher13UJ::SipHasher13UJ
SipHasher13UJ(uint64_t k0, uint64_t k1) noexcept
Construct a SipHash-1-3-UJ calculator initialized with 128-bit key (k0, k1).
Definition
siphash.h:166
uint256
256-bit opaque blob.
Definition
uint256.h:196
siphash.h
ByteUnit::t
@ t
Definition
strencodings.h:52
uint256.h
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.18.0