char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenmtmd: use sha256 for input hashing (#27274)ed1c3a20f

char/aliceai-t5-moe
7.5 KiB299 linesraw
1/*
2SHA-1 in C
3By Steve Reid <steve@edmweb.com>
4100% Public Domain
5
6Test Vectors (from FIPS PUB 180-1)
7"abc"
8  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
9"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
10  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
11A million repetitions of "a"
12  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
13*/
14
15/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
16/* #define SHA1HANDSOFF * Copies data before messing with it. */
17
18#define SHA1HANDSOFF
19
20#include <stdio.h>
21#include <string.h>
22
23/* for uint32_t */
24#include <stdint.h>
25
26#include "sha1.h"
27
28namespace vendor_hash {
29
30
31#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
32
33/* blk0() and blk() perform the initial expand. */
34/* I got the idea of expanding during the round function from SSLeay */
35#if BYTE_ORDER == LITTLE_ENDIAN
36#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
37    |(rol(block->l[i],8)&0x00FF00FF))
38#elif BYTE_ORDER == BIG_ENDIAN
39#define blk0(i) block->l[i]
40#else
41#error "Endianness not defined!"
42#endif
43#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
44    ^block->l[(i+2)&15]^block->l[i&15],1))
45
46/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
47#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
48#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
49#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
50#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
51#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
52
53
54/* Hash a single 512-bit block. This is the core of the algorithm. */
55
56void SHA1Transform(
57    uint32_t state[5],
58    const unsigned char buffer[64]
59)
60{
61    uint32_t a, b, c, d, e;
62
63    typedef union
64    {
65        unsigned char c[64];
66        uint32_t l[16];
67    } CHAR64LONG16;
68
69#ifdef SHA1HANDSOFF
70    CHAR64LONG16 block[1];      /* use array to appear as a pointer */
71
72    memcpy(block, buffer, 64);
73#else
74    /* The following had better never be used because it causes the
75     * pointer-to-const buffer to be cast into a pointer to non-const.
76     * And the result is written through.  I threw a "const" in, hoping
77     * this will cause a diagnostic.
78     */
79    CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
80#endif
81    /* Copy context->state[] to working vars */
82    a = state[0];
83    b = state[1];
84    c = state[2];
85    d = state[3];
86    e = state[4];
87    /* 4 rounds of 20 operations each. Loop unrolled. */
88    R0(a, b, c, d, e, 0);
89    R0(e, a, b, c, d, 1);
90    R0(d, e, a, b, c, 2);
91    R0(c, d, e, a, b, 3);
92    R0(b, c, d, e, a, 4);
93    R0(a, b, c, d, e, 5);
94    R0(e, a, b, c, d, 6);
95    R0(d, e, a, b, c, 7);
96    R0(c, d, e, a, b, 8);
97    R0(b, c, d, e, a, 9);
98    R0(a, b, c, d, e, 10);
99    R0(e, a, b, c, d, 11);
100    R0(d, e, a, b, c, 12);
101    R0(c, d, e, a, b, 13);
102    R0(b, c, d, e, a, 14);
103    R0(a, b, c, d, e, 15);
104    R1(e, a, b, c, d, 16);
105    R1(d, e, a, b, c, 17);
106    R1(c, d, e, a, b, 18);
107    R1(b, c, d, e, a, 19);
108    R2(a, b, c, d, e, 20);
109    R2(e, a, b, c, d, 21);
110    R2(d, e, a, b, c, 22);
111    R2(c, d, e, a, b, 23);
112    R2(b, c, d, e, a, 24);
113    R2(a, b, c, d, e, 25);
114    R2(e, a, b, c, d, 26);
115    R2(d, e, a, b, c, 27);
116    R2(c, d, e, a, b, 28);
117    R2(b, c, d, e, a, 29);
118    R2(a, b, c, d, e, 30);
119    R2(e, a, b, c, d, 31);
120    R2(d, e, a, b, c, 32);
121    R2(c, d, e, a, b, 33);
122    R2(b, c, d, e, a, 34);
123    R2(a, b, c, d, e, 35);
124    R2(e, a, b, c, d, 36);
125    R2(d, e, a, b, c, 37);
126    R2(c, d, e, a, b, 38);
127    R2(b, c, d, e, a, 39);
128    R3(a, b, c, d, e, 40);
129    R3(e, a, b, c, d, 41);
130    R3(d, e, a, b, c, 42);
131    R3(c, d, e, a, b, 43);
132    R3(b, c, d, e, a, 44);
133    R3(a, b, c, d, e, 45);
134    R3(e, a, b, c, d, 46);
135    R3(d, e, a, b, c, 47);
136    R3(c, d, e, a, b, 48);
137    R3(b, c, d, e, a, 49);
138    R3(a, b, c, d, e, 50);
139    R3(e, a, b, c, d, 51);
140    R3(d, e, a, b, c, 52);
141    R3(c, d, e, a, b, 53);
142    R3(b, c, d, e, a, 54);
143    R3(a, b, c, d, e, 55);
144    R3(e, a, b, c, d, 56);
145    R3(d, e, a, b, c, 57);
146    R3(c, d, e, a, b, 58);
147    R3(b, c, d, e, a, 59);
148    R4(a, b, c, d, e, 60);
149    R4(e, a, b, c, d, 61);
150    R4(d, e, a, b, c, 62);
151    R4(c, d, e, a, b, 63);
152    R4(b, c, d, e, a, 64);
153    R4(a, b, c, d, e, 65);
154    R4(e, a, b, c, d, 66);
155    R4(d, e, a, b, c, 67);
156    R4(c, d, e, a, b, 68);
157    R4(b, c, d, e, a, 69);
158    R4(a, b, c, d, e, 70);
159    R4(e, a, b, c, d, 71);
160    R4(d, e, a, b, c, 72);
161    R4(c, d, e, a, b, 73);
162    R4(b, c, d, e, a, 74);
163    R4(a, b, c, d, e, 75);
164    R4(e, a, b, c, d, 76);
165    R4(d, e, a, b, c, 77);
166    R4(c, d, e, a, b, 78);
167    R4(b, c, d, e, a, 79);
168    /* Add the working vars back into context.state[] */
169    state[0] += a;
170    state[1] += b;
171    state[2] += c;
172    state[3] += d;
173    state[4] += e;
174    /* Wipe variables */
175    a = b = c = d = e = 0;
176#ifdef SHA1HANDSOFF
177    memset(block, '\0', sizeof(block));
178#endif
179}
180
181
182/* SHA1Init - Initialize new context */
183
184void SHA1Init(
185    SHA1_CTX * context
186)
187{
188    /* SHA1 initialization constants */
189    context->state[0] = 0x67452301;
190    context->state[1] = 0xEFCDAB89;
191    context->state[2] = 0x98BADCFE;
192    context->state[3] = 0x10325476;
193    context->state[4] = 0xC3D2E1F0;
194    context->count[0] = context->count[1] = 0;
195}
196
197
198/* Run your data through this. */
199
200void SHA1Update(
201    SHA1_CTX * context,
202    const unsigned char *data,
203    uint32_t len
204)
205{
206    uint32_t i;
207
208    uint32_t j;
209
210    j = context->count[0];
211    if ((context->count[0] += len << 3) < j)
212        context->count[1]++;
213    context->count[1] += (len >> 29);
214    j = (j >> 3) & 63;
215    if ((j + len) > 63)
216    {
217        memcpy(&context->buffer[j], data, (i = 64 - j));
218        SHA1Transform(context->state, context->buffer);
219        for (; i + 63 < len; i += 64)
220        {
221            SHA1Transform(context->state, &data[i]);
222        }
223        j = 0;
224    }
225    else
226        i = 0;
227    memcpy(&context->buffer[j], &data[i], len - i);
228}
229
230
231/* Add padding and return the message digest. */
232
233void SHA1Final(
234    unsigned char digest[20],
235    SHA1_CTX * context
236)
237{
238    unsigned i;
239
240    unsigned char finalcount[8];
241
242    unsigned char c;
243
244#if 0    /* untested "improvement" by DHR */
245    /* Convert context->count to a sequence of bytes
246     * in finalcount.  Second element first, but
247     * big-endian order within element.
248     * But we do it all backwards.
249     */
250    unsigned char *fcp = &finalcount[8];
251
252    for (i = 0; i < 2; i++)
253    {
254        uint32_t t = context->count[i];
255
256        int j;
257
258        for (j = 0; j < 4; t >>= 8, j++)
259            *--fcp = (unsigned char) t}
260#else
261    for (i = 0; i < 8; i++)
262    {
263        finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255);      /* Endian independent */
264    }
265#endif
266    c = 0200;
267    SHA1Update(context, &c, 1);
268    while ((context->count[0] & 504) != 448)
269    {
270        c = 0000;
271        SHA1Update(context, &c, 1);
272    }
273    SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
274    for (i = 0; i < 20; i++)
275    {
276        digest[i] = (unsigned char)
277            ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
278    }
279    /* Wipe variables */
280    memset(context, '\0', sizeof(*context));
281    memset(&finalcount, '\0', sizeof(finalcount));
282}
283
284void SHA1(
285    char *hash_out,
286    const char *str,
287    uint32_t len)
288{
289    SHA1_CTX ctx;
290    unsigned int ii;
291
292    SHA1Init(&ctx);
293    for (ii=0; ii<len; ii+=1)
294        SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
295    SHA1Final((unsigned char *)hash_out, &ctx);
296}
297
298} // namespace vendor_hash
299