plebble
sha256.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3  PLEBBLE
4 -------------------------------------------------------------------------------
5  Copyright (C) 2019-2020 KATLAS Technology. (http://katlastechnology.com)
6  Copyright (C) 2017-2020 Marcos Mayorga. (mm@mm-studios.com)
7 
8  This file is part of our Plebble(R) Platform.
9 
10  The code below cannot be copied, used for any purpose other than the one
11  agreed and/or distributed without the express permission of
12  KATLAS Technology.
13 -------------------------------------------------------------------------------
14 
15 
16 */
17 #ifndef USGOV_58cbbc93ca211d1d3a8d23b3465a0ddfa220130869c491eb636d63f598241f67
18 #define USGOV_58cbbc93ca211d1d3a8d23b3465a0ddfa220130869c491eb636d63f598241f67
19 
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string>
23 #include <cassert>
24 #include <cstring>
25 #include <array>
26 #include <us/gov/likely.h>
27 #include "ripemd160.h"
28 
29 namespace us { namespace gov {
30 namespace crypto {
31 using namespace std;
32 
33  class sha256 {
34  public:
35  static constexpr uint32_t output_size = 32;
36  sha256();
37 
38  struct value_type:array<unsigned char,output_size> {
39  typedef sha256 hasher_t;
40 
42  value_type(int) { zero(); }
43  bool operator < (const value_type& other) const; //result depends on endianness (different results in different archs), ok for local hash tables
44  bool is_zero() const;
45  void zero();
46  string to_b58() const;
47  static value_type from_b58(const string&);
48  };
49 
50  void write(const unsigned char* data, size_t len);
51  void write(const vector<unsigned char>&);
52  void write(const string&data);
53  void write(const ripemd160::value_type& data);
54  void finalize(unsigned char hash[output_size]);
56  void reset();
57 
58  private:
59  void initialize(uint32_t* s);
60  uint32_t inline Ch(uint32_t x, uint32_t y, uint32_t z) { return z ^ (x & (y ^ z)); }
61  uint32_t inline Maj(uint32_t x, uint32_t y, uint32_t z) { return (x & y) | (z & (x | y)); }
62  uint32_t inline Sigma0(uint32_t x) { return (x >> 2 | x << 30) ^ (x >> 13 | x << 19) ^ (x >> 22 | x << 10); }
63  uint32_t inline Sigma1(uint32_t x) { return (x >> 6 | x << 26) ^ (x >> 11 | x << 21) ^ (x >> 25 | x << 7); }
64  uint32_t inline sigma0(uint32_t x) { return (x >> 7 | x << 25) ^ (x >> 18 | x << 14) ^ (x >> 3); }
65  uint32_t inline sigma1(uint32_t x) { return (x >> 17 | x << 15) ^ (x >> 19 | x << 13) ^ (x >> 10); }
66  void inline Round(uint32_t a, uint32_t b, uint32_t c, uint32_t& d, uint32_t e, uint32_t f, uint32_t g, uint32_t& h, uint32_t k, uint32_t w) {
67  uint32_t t1 = h + Sigma1(e) + Ch(e, f, g) + k + w;
68  uint32_t t2 = Sigma0(a) + Maj(a, b, c);
69  d += t1;
70  h = t1 + t2;
71  }
72  void transform(uint32_t* s, const unsigned char* chunk);
73 
74  private:
75  uint32_t s[8];
76  unsigned char buf[64];
77  uint64_t bytes;
78  };
79 
80  inline ostream& operator << (ostream& os, const us::gov::crypto::sha256::value_type& v) {
81  os << v.to_b58();
82  return os;
83  }
84  inline istream& operator >> (istream& is, const us::gov::crypto::sha256::value_type& v) {
85  string s;
86  is >> s;
87  v.from_b58(s);
88  return is;
89  }
90 
91 }
92 }}
93 
94 namespace std {
95 
96  template <>
97  struct hash<us::gov::crypto::sha256::value_type> {
99  return *reinterpret_cast<const size_t*>(&k[0]);
100 
101  }
102  };
103 
104 }
105 
106 #endif
us::gov::crypto::sha256::sigma1
uint32_t sigma1(uint32_t x)
Definition: sha256.h:65
us::gov::crypto::sha256::value_type::hasher_t
sha256 hasher_t
Definition: sha256.h:39
us::gov::crypto::sha256::write
void write(const string &data)
std::hash< us::gov::crypto::sha256::value_type >::operator()
size_t operator()(const us::gov::crypto::sha256::value_type &k) const
Definition: sha256.h:98
endian_rw.h
us::gov::crypto::sha256::Sigma1
uint32_t Sigma1(uint32_t x)
Definition: sha256.h:63
us::gov::crypto::WriteBE32
static void WriteBE32(unsigned char *ptr, uint32_t x)
Definition: endian_rw.h:24
us::gov::crypto::sha256::write
void write(const unsigned char *data, size_t len)
us
Definition: daemon.h:22
us::gov::crypto::ripemd160::value_type
Definition: ripemd160.h:38
unlikely
#define unlikely(x)
Definition: likely.h:30
us::gov::crypto::operator<<
ostream & operator<<(ostream &os, const ec::keys::pub_t &k)
Definition: ec.h:147
us::gov::crypto::sha256::value_type::value_type
value_type(int)
Definition: sha256.h:42
us::gov::crypto::sha256::reset
void reset()
us::gov::crypto::sha256::value_type
Definition: sha256.h:38
likely.h
c
us::gov::crypto::sha256 c
Definition: sha256.cpp:29
us::gov::crypto::sha256
Definition: sha256.h:33
us::gov::crypto::sha256::value_type::from_b58
static value_type from_b58(const string &)
us::gov::crypto::sha256::sha256
sha256()
us::gov::crypto::sha256::Maj
uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
Definition: sha256.h:61
us::gov::crypto::sha256::write
void write(const ripemd160::value_type &data)
us::gov::crypto::sha256::initialize
void initialize(uint32_t *s)
us::gov::crypto::b58::decode
std::vector< unsigned char > decode(const std::string &s)
Definition: base58.cpp:54
us::gov::crypto::sha256::value_type::is_zero
bool is_zero() const
us::gov::crypto::WriteBE64
static void WriteBE64(unsigned char *ptr, uint64_t x)
Definition: endian_rw.h:25
us::gov::crypto::sha256::bytes
uint64_t bytes
Definition: sha256.h:77
ripemd160.h
us::gov::crypto::sha256::value_type::zero
void zero()
sha256.h
us::gov::crypto::sha256::Ch
uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
Definition: sha256.h:60
us::gov::crypto::sha256::write
void write(const vector< unsigned char > &)
c
Definition: client.cpp:417
us::gov::crypto::sha256::value_type::value_type
value_type()
Definition: sha256.h:41
us::gov::crypto::sha256::value_type::to_b58
string to_b58() const
std
Definition: app.h:380
us::gov::crypto::sha256::transform
void transform(uint32_t *s, const unsigned char *chunk)
us::gov::crypto::b58::encode
std::string encode(const std::string &s)
Definition: base58.cpp:62
base58.h
us::gov::crypto
Definition: base58.cpp:19
us::gov::crypto::sha256::finalize
void finalize(value_type &)
us::gov::crypto::sha256::Sigma0
uint32_t Sigma0(uint32_t x)
Definition: sha256.h:62
anonymous_namespace{ripemd160.cpp}::Round
void Round(uint32_t &a, uint32_t b, uint32_t &c, uint32_t d, uint32_t e, uint32_t f, uint32_t x, uint32_t k, int r)
Definition: ripemd160.cpp:50
gov.h
us::gov::crypto::sha256::finalize
void finalize(unsigned char hash[output_size])
us::gov::crypto::sha256::sigma0
uint32_t sigma0(uint32_t x)
Definition: sha256.h:64
us::gov::crypto::ReadBE32
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: endian_rw.h:22
us::gov::crypto::operator>>
istream & operator>>(istream &is, ec::keys::pub_t &k)
Definition: ec.h:152
us::gov::crypto::sha256::Round
void Round(uint32_t a, uint32_t b, uint32_t c, uint32_t &d, uint32_t e, uint32_t f, uint32_t g, uint32_t &h, uint32_t k, uint32_t w)
Definition: sha256.h:66