This repository has been archived by the owner on May 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
curl_p.c
executable file
·61 lines (49 loc) · 1.78 KB
/
curl_p.c
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
/*
* (c) 2016 Paul Handy, based on code from come-from-beyond
*/
#include <stdio.h>
#include <string.h>
#include "include/indices.h"
#include "include/trit.h"
#define __TRUTH_TABLE 1, 0, -1, 2, 1, -1, 0, 2, -1, 1, 0
static const size_t CURL_INDEX[STATE_LENGTH + 1] = {__INDEX_TABLE};
static const trit_t TRUTH_TABLE[11] = {__TRUTH_TABLE};
void transform(Curl *ctx);
void sbox(Curl *const, Curl *const);
void init_curl(Curl *ctx) { memset(ctx->state, 0, sizeof(ctx->state)); }
void curl_absorb(Curl *ctx, trit_t *const trits, size_t length) {
size_t numChunks = length / HASH_LENGTH + ((length % HASH_LENGTH) ? 1 : 0);
size_t i = 0;
for (; i < numChunks; ++i) {
memcpy(ctx->state, trits + i * HASH_LENGTH * sizeof(trit_t),
(length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(trit_t));
transform(ctx);
length = length < HASH_LENGTH ? 0 : length - HASH_LENGTH;
}
}
void curl_squeeze(Curl *ctx, trit_t *const trits, size_t length) {
size_t numChunks = length / HASH_LENGTH + ((length % HASH_LENGTH) ? 1 : 0);
size_t i = 0;
for (; i < numChunks; ++i) {
memcpy(trits + i * HASH_LENGTH * sizeof(trit_t), ctx->state,
(length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(trit_t));
transform(ctx);
length = length < HASH_LENGTH ? 0 : length - HASH_LENGTH;
}
}
void transform(Curl *const ctx) {
Curl s;
size_t round = 0;
for (; round < ctx->type; ++round) {
memcpy(s.state, ctx->state, sizeof(ctx->state));
sbox(ctx, &s);
}
}
void sbox(Curl *const c, Curl *const s) {
size_t i = 0;
for (; i < STATE_LENGTH; ++i) {
c->state[i] = TRUTH_TABLE[s->state[CURL_INDEX[i]] +
((unsigned)s->state[CURL_INDEX[i + 1]] << 2) + 5];
}
}
void curl_reset(Curl *ctx) { memset(ctx->state, 0, sizeof(ctx->state)); }