Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/sha3/haval_helper.c
1201 views
1
/* $Id: haval_helper.c 218 2010-06-08 17:06:34Z tp $ */
2
/*
3
* Helper code, included (three times !) by HAVAL implementation.
4
*
5
* TODO: try to merge this with md_helper.c.
6
*
7
* ==========================(LICENSE BEGIN)============================
8
*
9
* Copyright (c) 2007-2010 Projet RNRT SAPHIR
10
*
11
* Permission is hereby granted, free of charge, to any person obtaining
12
* a copy of this software and associated documentation files (the
13
* "Software"), to deal in the Software without restriction, including
14
* without limitation the rights to use, copy, modify, merge, publish,
15
* distribute, sublicense, and/or sell copies of the Software, and to
16
* permit persons to whom the Software is furnished to do so, subject to
17
* the following conditions:
18
*
19
* The above copyright notice and this permission notice shall be
20
* included in all copies or substantial portions of the Software.
21
*
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
*
30
* ===========================(LICENSE END)=============================
31
*
32
* @author Thomas Pornin <[email protected]>
33
*/
34
35
#undef SPH_XCAT
36
#define SPH_XCAT(a, b) SPH_XCAT_(a, b)
37
#undef SPH_XCAT_
38
#define SPH_XCAT_(a, b) a ## b
39
40
static void
41
#ifdef SPH_UPTR
42
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)
43
#else
44
SPH_XCAT(haval, PASSES)
45
#endif
46
(sph_haval_context *sc, const void *data, size_t len)
47
{
48
unsigned current;
49
50
#if SPH_64
51
current = (unsigned)sc->count & 127U;
52
#else
53
current = (unsigned)sc->count_low & 127U;
54
#endif
55
while (len > 0) {
56
unsigned clen;
57
#if !SPH_64
58
sph_u32 clow, clow2;
59
#endif
60
61
clen = 128U - current;
62
if (clen > len)
63
clen = (unsigned) len;
64
memcpy(sc->buf + current, data, clen);
65
data = (const unsigned char *)data + clen;
66
current += clen;
67
len -= clen;
68
if (current == 128U) {
69
DSTATE;
70
IN_PREPARE(sc->buf);
71
72
RSTATE;
73
SPH_XCAT(CORE, PASSES)(INW);
74
WSTATE;
75
current = 0;
76
}
77
#if SPH_64
78
sc->count += clen;
79
#else
80
clow = sc->count_low;
81
clow2 = SPH_T32(clow + clen);
82
sc->count_low = clow2;
83
if (clow2 < clow)
84
sc->count_high ++;
85
#endif
86
}
87
}
88
89
#ifdef SPH_UPTR
90
static void
91
SPH_XCAT(haval, PASSES)(sph_haval_context *sc, const void *data, size_t len)
92
{
93
unsigned current;
94
size_t orig_len;
95
#if !SPH_64
96
sph_u32 clow, clow2;
97
#endif
98
DSTATE;
99
100
if (len < 256U) {
101
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);
102
return;
103
}
104
#if SPH_64
105
current = (unsigned)sc->count & 127U;
106
#else
107
current = (unsigned)sc->count_low & 127U;
108
#endif
109
if (current > 0) {
110
unsigned clen;
111
112
clen = 128U - current;
113
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, clen);
114
data = (const unsigned char *)data + clen;
115
len -= clen;
116
}
117
#if !SPH_UNALIGNED
118
if (((SPH_UPTR)data & 3U) != 0) {
119
SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);
120
return;
121
}
122
#endif
123
orig_len = len;
124
RSTATE;
125
while (len >= 128U) {
126
IN_PREPARE(data);
127
128
SPH_XCAT(CORE, PASSES)(INW);
129
data = (const unsigned char *)data + 128U;
130
len -= 128U;
131
}
132
WSTATE;
133
if (len > 0)
134
memcpy(sc->buf, data, len);
135
#if SPH_64
136
sc->count += (sph_u64)orig_len;
137
#else
138
clow = sc->count_low;
139
clow2 = SPH_T32(clow + orig_len);
140
sc->count_low = clow2;
141
if (clow2 < clow)
142
sc->count_high ++;
143
orig_len >>= 12;
144
orig_len >>= 10;
145
orig_len >>= 10;
146
sc->count_high += orig_len;
147
#endif
148
}
149
#endif
150
151
static void
152
SPH_XCAT(SPH_XCAT(haval, PASSES), _close)(sph_haval_context *sc,
153
unsigned ub, unsigned n, void *dst)
154
{
155
unsigned current;
156
DSTATE;
157
158
#if SPH_64
159
current = (unsigned)sc->count & 127U;
160
#else
161
current = (unsigned)sc->count_low & 127U;
162
#endif
163
sc->buf[current ++] = (0x01 << n) | ((ub & 0xFF) >> (8 - n));
164
RSTATE;
165
if (current > 118U) {
166
memset(sc->buf + current, 0, 128U - current);
167
168
do {
169
IN_PREPARE(sc->buf);
170
171
SPH_XCAT(CORE, PASSES)(INW);
172
} while (0);
173
current = 0;
174
}
175
memset(sc->buf + current, 0, 118U - current);
176
sc->buf[118] = 0x01 | (PASSES << 3);
177
sc->buf[119] = sc->olen << 3;
178
#if SPH_64
179
sph_enc64le_aligned(sc->buf + 120, SPH_T64(sc->count << 3));
180
#else
181
sph_enc32le_aligned(sc->buf + 120, SPH_T32(sc->count_low << 3));
182
sph_enc32le_aligned(sc->buf + 124,
183
SPH_T32((sc->count_high << 3) | (sc->count_low >> 29)));
184
#endif
185
do {
186
IN_PREPARE(sc->buf);
187
188
SPH_XCAT(CORE, PASSES)(INW);
189
} while (0);
190
191
WSTATE;
192
haval_out(sc, dst);
193
haval_init(sc, sc->olen, sc->passes);
194
}
195
196
197