Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::sha256 Class Reference

#include <sha256.hpp>

Inheritance diagram for fc::sha256:

Classes

class  encoder
 

Public Member Functions

 sha256 ()
 
 sha256 (const string &hex_str)
 
 sha256 (const char *data, size_t size)
 
string str () const
 
 operator string () const
 
const char * data () const
 
char * data ()
 
size_t data_size () const
 
uint32_t pop_count () const
 
uint16_t clz () const
 
uint32_t approx_log_32 () const
 
void set_to_inverse_approx_log_32 (uint32_t x)
 

Static Public Member Functions

static sha256 hash (const char *d, uint32_t dlen)
 
static sha256 hash (const string &)
 
static sha256 hash (const sha256 &)
 
template<typename T >
static sha256 hash (const T &t)
 
static double inverse_approx_log_32_double (uint32_t x)
 

Public Attributes

uint64_t _hash [4]
 

Friends

template<typename T >
Toperator<< (T &ds, const sha256 &ep)
 
template<typename T >
Toperator>> (T &ds, sha256 &ep)
 
sha256 operator<< (const sha256 &h1, uint32_t i)
 
sha256 operator>> (const sha256 &h1, uint32_t i)
 
bool operator== (const sha256 &h1, const sha256 &h2)
 
bool operator!= (const sha256 &h1, const sha256 &h2)
 
sha256 operator^ (const sha256 &h1, const sha256 &h2)
 
bool operator>= (const sha256 &h1, const sha256 &h2)
 
bool operator> (const sha256 &h1, const sha256 &h2)
 
bool operator< (const sha256 &h1, const sha256 &h2)
 

Detailed Description

Definition at line 11 of file sha256.hpp.

Constructor & Destructor Documentation

◆ sha256() [1/3]

fc::sha256::sha256 ( )

Definition at line 14 of file sha256.cpp.

14{ memset( _hash, 0, sizeof(_hash) ); }
uint64_t _hash[4]
Definition sha256.hpp:100
memset(pInfo->slotDescription, ' ', 64)
Here is the call graph for this function:

◆ sha256() [2/3]

fc::sha256::sha256 ( const string & hex_str)
explicit

Definition at line 20 of file sha256.cpp.

20 {
21 auto bytes_written = fc::from_hex( hex_str, (char*)_hash, sizeof(_hash) );
22 if( bytes_written < sizeof(_hash) )
23 memset( (char*)_hash + bytes_written, 0, (sizeof(_hash) - bytes_written) );
24 }
uint8_t from_hex(char c)
Definition hex.cpp:6
Here is the call graph for this function:

◆ sha256() [3/3]

fc::sha256::sha256 ( const char * data,
size_t size )
explicit

Definition at line 15 of file sha256.cpp.

15 {
16 if (size != sizeof(_hash))
17 FC_THROW_EXCEPTION( exception, "sha256: size mismatch" );
18 memcpy(_hash, data, size );
19 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

Member Function Documentation

◆ approx_log_32()

uint32_t fc::sha256::approx_log_32 ( ) const

Approximate (log_2(x) + 1) * 2**24.

Detailed specs:

  • Return 0 when x == 0.
  • High 8 bits of result simply counts nonzero bits.
  • Low 24 bits of result are the 24 bits of input immediately after the most significant 1 in the input.
  • If above would require reading beyond the end of the input, zeros are used instead.

Definition at line 111 of file sha256.cpp.

112 {
113 uint16_t lzbits = clz();
114 if( lzbits >= 0x100 )
115 return 0;
116 uint8_t nzbits = 0xFF-lzbits;
117 size_t offset = (size_t) (lzbits >> 3);
118 uint8_t* my_bytes = (uint8_t*) data();
119 size_t n = data_size();
120 uint32_t y = (uint32_t( my_bytes[offset ] ) << 0x18)
121 | (uint32_t(offset+1 < n ? my_bytes[offset+1] : 0) << 0x10)
122 | (uint32_t(offset+2 < n ? my_bytes[offset+2] : 0) << 0x08)
123 | (uint32_t(offset+3 < n ? my_bytes[offset+3] : 0) )
124 ;
125 //
126 // lzbits&7 == 7 : 00000001 iff nzbits&7 == 0
127 // lzbits&7 == 6 : 0000001x iff nzbits&7 == 1
128 // lzbits&7 == 5 : 000001xx iff nzbits&7 == 2
129 //
130 y >>= (nzbits & 7);
131 y ^= 1 << 0x18;
132 y |= uint32_t( nzbits ) << 0x18;
133 return y;
134 }
const char * data() const
Definition sha256.cpp:31
size_t data_size() const
Definition sha256.hpp:23
uint16_t clz() const
Definition sha256.cpp:173
uint64_t y
Definition sha3.cpp:34
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
Here is the call graph for this function:

◆ clz()

uint16_t fc::sha256::clz ( ) const

Count leading zero bits

Definition at line 173 of file sha256.cpp.

174 {
175 const uint8_t* my_bytes = (uint8_t*) data();
176 size_t size = data_size();
177 size_t lzbits = 0;
178 static const uint8_t char2lzbits[] = {
179 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
180 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
181 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
182 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
183 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
186 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
188 };
189
190 size_t i = 0;
191
192 while( true )
193 {
194 uint8_t c = my_bytes[i];
195 lzbits += char2lzbits[c];
196 if( c != 0 )
197 break;
198 ++i;
199 if( i >= size )
200 return 0x100;
201 }
202
203 return lzbits;
204 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ data() [1/2]

char * fc::sha256::data ( )

Definition at line 32 of file sha256.cpp.

32{ return (char*)&_hash[0]; }

◆ data() [2/2]

const char * fc::sha256::data ( ) const

Definition at line 31 of file sha256.cpp.

31{ return (const char*)&_hash[0]; }
Here is the caller graph for this function:

◆ data_size()

size_t fc::sha256::data_size ( ) const
inline

Definition at line 23 of file sha256.hpp.

23{ return 256 / 8; }
Here is the caller graph for this function:

◆ hash() [1/4]

sha256 fc::sha256::hash ( const char * d,
uint32_t dlen )
static

Definition at line 44 of file sha256.cpp.

44 {
45 encoder e;
46 e.write(d,dlen);
47 return e.result();
48 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hash() [2/4]

sha256 fc::sha256::hash ( const sha256 & s)
static

Definition at line 54 of file sha256.cpp.

55 {
56 return hash( s.data(), sizeof( s._hash ) );
57 }
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
char * s
Here is the call graph for this function:

◆ hash() [3/4]

sha256 fc::sha256::hash ( const string & s)
static

Definition at line 50 of file sha256.cpp.

50 {
51 return hash( s.c_str(), s.size() );
52 }
Here is the call graph for this function:

◆ hash() [4/4]

template<typename T >
static sha256 fc::sha256::hash ( const T & t)
inlinestatic

Definition at line 30 of file sha256.hpp.

31 {
32 sha256::encoder e;
33 fc::raw::pack(e,t);
34 return e.result();
35 }
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
Here is the call graph for this function:

◆ inverse_approx_log_32_double()

double fc::sha256::inverse_approx_log_32_double ( uint32_t x)
static

Definition at line 163 of file sha256.cpp.

164 {
165 uint8_t nzbits = uint8_t( x >> 0x18 );
166 if( nzbits == 0 )
167 return 0.0;
168 uint32_t b = 1 << 0x18;
169 uint32_t y = (x & (b-1)) | b;
170 return std::ldexp( y, int( nzbits ) - 0x18 );
171 }

◆ operator string()

fc::sha256::operator string ( ) const

Definition at line 29 of file sha256.cpp.

29{ return str(); }
string str() const
Definition sha256.cpp:26

◆ pop_count()

uint32_t fc::sha256::pop_count ( ) const
inline

Definition at line 73 of file sha256.hpp.

74 {
75 return (uint32_t)(__builtin_popcountll(_hash[0]) +
76 __builtin_popcountll(_hash[1]) +
77 __builtin_popcountll(_hash[2]) +
78 __builtin_popcountll(_hash[3]));
79 }

◆ set_to_inverse_approx_log_32()

void fc::sha256::set_to_inverse_approx_log_32 ( uint32_t x)

Definition at line 136 of file sha256.cpp.

137 {
138 uint8_t nzbits = uint8_t( x >> 0x18 );
139 _hash[0] = 0;
140 _hash[1] = 0;
141 _hash[2] = 0;
142 _hash[3] = 0;
143 if( nzbits == 0 )
144 return;
145 uint8_t x0 = uint8_t((x ) & 0xFF);
146 uint8_t x1 = uint8_t((x >> 0x08) & 0xFF);
147 uint8_t x2 = uint8_t((x >> 0x10) & 0xFF);
148 uint8_t* my_bytes = (uint8_t*) data();
149 my_bytes[0x1F] = x0;
150 my_bytes[0x1E] = x1;
151 my_bytes[0x1D] = x2;
152 my_bytes[0x1C] = 1;
153
154 if( nzbits <= 0x18 )
155 {
156 (*this) = (*this) >> (0x18 - nzbits);
157 }
158 else
159 (*this) = (*this) << (nzbits - 0x18);
160 return;
161 }
Here is the call graph for this function:

◆ str()

string fc::sha256::str ( ) const

Definition at line 26 of file sha256.cpp.

26 {
27 return fc::to_hex( (char*)_hash, sizeof(_hash) );
28 }
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 98 of file sha256.cpp.

98 {
99 return !(h1 == h2);
100 }

◆ operator<

bool operator< ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 95 of file sha256.cpp.

95 {
96 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) < 0;
97 }

◆ operator<< [1/2]

sha256 operator<< ( const sha256 & h1,
uint32_t i )
friend

Definition at line 71 of file sha256.cpp.

71 {
72 sha256 result;
73 fc::detail::shift_l( h1.data(), result.data(), result.data_size(), i );
74 return result;
75 }

◆ operator<< [2/2]

template<typename T >
T & operator<< ( T & ds,
const sha256 & ep )
friend

Definition at line 54 of file sha256.hpp.

54 {
55 ds.write( ep.data(), sizeof(ep) );
56 return ds;
57 }
static const Segment ds(Segment::ds)

◆ operator==

bool operator== ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 101 of file sha256.cpp.

101 {
102 // idea to not use memcmp, from:
103 // https://lemire.me/blog/2018/08/22/avoid-lexicographical-comparisons-when-testing-for-string-equality/
104 return
105 h1._hash[0] == h2._hash[0] &&
106 h1._hash[1] == h2._hash[1] &&
107 h1._hash[2] == h2._hash[2] &&
108 h1._hash[3] == h2._hash[3];
109 }

◆ operator>

bool operator> ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 92 of file sha256.cpp.

92 {
93 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) > 0;
94 }

◆ operator>=

bool operator>= ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 89 of file sha256.cpp.

89 {
90 return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) >= 0;
91 }

◆ operator>> [1/2]

sha256 operator>> ( const sha256 & h1,
uint32_t i )
friend

Definition at line 76 of file sha256.cpp.

76 {
77 sha256 result;
78 fc::detail::shift_r( h1.data(), result.data(), result.data_size(), i );
79 return result;
80 }

◆ operator>> [2/2]

template<typename T >
T & operator>> ( T & ds,
sha256 & ep )
friend

Definition at line 60 of file sha256.hpp.

60 {
61 ds.read( ep.data(), sizeof(ep) );
62 return ds;
63 }

◆ operator^

sha256 operator^ ( const sha256 & h1,
const sha256 & h2 )
friend

Definition at line 81 of file sha256.cpp.

81 {
82 sha256 result;
83 result._hash[0] = h1._hash[0] ^ h2._hash[0];
84 result._hash[1] = h1._hash[1] ^ h2._hash[1];
85 result._hash[2] = h1._hash[2] ^ h2._hash[2];
86 result._hash[3] = h1._hash[3] ^ h2._hash[3];
87 return result;
88 }

Member Data Documentation

◆ _hash

uint64_t fc::sha256::_hash[4]

Definition at line 100 of file sha256.hpp.


The documentation for this class was generated from the following files: