forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA_512_256.hpp
106 lines (86 loc) · 2.99 KB
/
SHA_512_256.hpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef _SNARKFRONT_SHA_512_256_HPP_
#define _SNARKFRONT_SHA_512_256_HPP_
#include <array>
#include <cstdint>
#include "Alg.hpp"
#include "Alg_uint.hpp"
#include "AST.hpp"
#include "BitwiseOps.hpp"
#include "Lazy.hpp"
#include "SHA_512.hpp"
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// SHA-512/256
//
template <typename H, typename T, typename MSG, typename F>
class SHA_512_256 : public SHA_512<T, MSG, F>
{
public:
typedef T WordType;
typedef std::array<T, 16> MsgType;
typedef std::array<H, 8> DigType;
SHA_512_256()
: m_setDigest(false)
{}
// overrides base class SHA-512
const std::array<H, 8>& digest() {
if (m_setDigest) {
for (std::size_t i = 0; i < 4; ++i) {
// high 32 bits
m_Hleft256[2*i] = F::xword(F::SHR(
this->m_H[i],
32));
// low 32 bits
m_Hleft256[2*i + 1] = F::xword(F::SHR(
F::SHL(
this->m_H[i],
32),
32));
}
m_setDigest = false;
}
return m_Hleft256;
}
virtual void afterHash() {
m_setDigest = true;
}
protected:
// overrides base class SHA-512
void initHashValue() {
// set initial hash value (NIST FIPS 180-4 section 5.3.6.2)
const std::array<std::uint64_t, 8> a {
0x22312194FC2BF72C, 0x9F555FA3C84C64C2,
0x2393B86B6F53B151, 0x963877195940EABD,
0x96283EE2A88EFFE3, 0xBE5E1E2553863992,
0x2B0199FC2C85B8AA, 0x0EB72DDC81C52CA2 };
for (std::size_t i = 0; i < 8; ++i) {
this->m_H[i] = F::constant(a[i]);
}
}
// truncated 256-bit message digest
std::array<H, 8> m_Hleft256;
bool m_setDigest;
};
////////////////////////////////////////////////////////////////////////////////
// typedefs
//
namespace zk {
template <typename FR> using
SHA512_256 = SHA_512_256<AST_Var<Alg_uint32<FR>>,
AST_Var<Alg_uint64<FR>>,
Lazy<AST_Var<Alg_uint64<FR>>, std::uint64_t>,
SHA_Functions<AST_Node<Alg_uint64<FR>>,
AST_Op<Alg_uint64<FR>>,
BitwiseAST<Alg_uint64<FR>, Alg_uint32<FR>>>>;
} // namespace zk
namespace eval {
typedef SHA_512_256<std::uint32_t,
std::uint64_t,
std::uint64_t,
SHA_Functions<std::uint64_t,
std::uint64_t,
BitwiseINT<std::uint64_t, std::uint32_t>>>
SHA512_256;
} // namespace eval
} // namespace snarkfront
#endif