Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/lyra2/Sponge.h
1201 views
1
/**
2
* Header file for Blake2b's internal permutation in the form of a sponge.
3
* This code is based on the original Blake2b's implementation provided by
4
* Samuel Neves (https://blake2.net/)
5
*
6
* Author: The Lyra PHC team (http://www.lyra-kdf.net/) -- 2014.
7
*
8
* This software is hereby placed in the public domain.
9
*
10
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
11
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
12
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
14
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
15
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
16
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
19
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21
*/
22
#ifndef SPONGE_H_
23
#define SPONGE_H_
24
25
#include <stdint.h>
26
27
/* Blake2b IV Array */
28
static const uint64_t blake2b_IV[8] =
29
{
30
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
31
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
32
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
33
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
34
};
35
36
/* Blake2b's rotation */
37
static __inline uint64_t rotr64(const uint64_t w, const unsigned c) {
38
#ifdef _MSC_VER
39
return _rotr64(w, c);
40
#else
41
return ( w >> c ) | ( w << ( 64 - c ) );
42
#endif
43
}
44
45
/* Blake2b's G function */
46
#define G(r,i,a,b,c,d) do { \
47
a = a + b; \
48
d = rotr64(d ^ a, 32); \
49
c = c + d; \
50
b = rotr64(b ^ c, 24); \
51
a = a + b; \
52
d = rotr64(d ^ a, 16); \
53
c = c + d; \
54
b = rotr64(b ^ c, 63); \
55
} while(0)
56
57
58
/*One Round of the Blake2b's compression function*/
59
#define ROUND_LYRA(r) \
60
G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
61
G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
62
G(r,2,v[ 2],v[ 6],v[10],v[14]); \
63
G(r,3,v[ 3],v[ 7],v[11],v[15]); \
64
G(r,4,v[ 0],v[ 5],v[10],v[15]); \
65
G(r,5,v[ 1],v[ 6],v[11],v[12]); \
66
G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
67
G(r,7,v[ 3],v[ 4],v[ 9],v[14]);
68
69
//---- Housekeeping
70
void initState(uint64_t state[/*16*/]);
71
72
//---- Squeezes
73
void squeeze(uint64_t *state, unsigned char *out, unsigned int len);
74
void reducedSqueezeRow0(uint64_t* state, uint64_t* row, const uint32_t nCols);
75
76
//---- Absorbs
77
void absorbBlock(uint64_t *state, const uint64_t *in);
78
void absorbBlockBlake2Safe(uint64_t *state, const uint64_t *in);
79
80
//---- Duplexes
81
void reducedDuplexRow1(uint64_t *state, uint64_t *rowIn, uint64_t *rowOut, const uint32_t nCols);
82
void reducedDuplexRowSetup(uint64_t *state, uint64_t *rowIn, uint64_t *rowInOut, uint64_t *rowOut, const uint32_t nCols);
83
void reducedDuplexRow(uint64_t *state, uint64_t *rowIn, uint64_t *rowInOut, uint64_t *rowOut, const uint32_t nCols);
84
85
//---- Misc
86
void printArray(unsigned char *array, unsigned int size, char *name);
87
88
#endif /* SPONGE_H_ */
89
90