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_ptrit.c
executable file
·74 lines (61 loc) · 2.02 KB
/
curl_p_ptrit.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
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* (c) 2016 Paul Handy, based on code from come-from-beyond
*/
#include <stdlib.h>
#include <string.h>
#include "include/const.h"
#include "include/indices.h"
#include "include/pcurl_ptrit.h"
static const size_t CURL_INDEX[STATE_LENGTH + 1] = {__INDEX_TABLE};
void ptrit_transform_round(PCurl *const, PCurl *const, size_t const);
void ptrit_sbox(ptrit_t *const, ptrit_t *const, size_t const);
void init_ptrit_curl(PCurl *ctx) { ptrit_curl_reset(ctx); }
void ptrit_curl_absorb(PCurl *ctx, ptrit_t *const trits, size_t length) {
memcpy(ctx->state, trits,
(length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(ptrit_t));
ptrit_transform(ctx);
if (length <= HASH_LENGTH) {
return;
}
ptrit_curl_absorb(ctx, trits + HASH_LENGTH * sizeof(ptrit_t),
length - HASH_LENGTH);
}
void ptrit_curl_squeeze(PCurl *ctx, ptrit_t *const trits, size_t length) {
memcpy(trits, ctx->state,
(length < HASH_LENGTH ? length : HASH_LENGTH) * sizeof(ptrit_t));
ptrit_transform(ctx);
if (length <= HASH_LENGTH) {
return;
}
ptrit_curl_squeeze(ctx, trits + HASH_LENGTH * sizeof(ptrit_t),
length - HASH_LENGTH);
}
void ptrit_transform(PCurl *const ctx) {
PCurl s;
ptrit_transform_round(ctx, &s, ctx->type);
memcpy(ctx->state, s.state, sizeof(ptrit_t) * STATE_LENGTH);
ptrit_curl_reset(&s);
}
void ptrit_transform_round(PCurl *const ctx, PCurl *const s, size_t const i) {
if (i == 0) {
return;
}
ptrit_sbox(s->state, ctx->state, 0);
ptrit_transform_round(s, ctx, i - 1);
}
void ptrit_sbox(ptrit_t *const c, ptrit_t *const s, size_t const i) {
if (i == STATE_LENGTH) {
return;
}
ptrit_s alpha, beta, gamma, delta;
alpha = s[CURL_INDEX[i]].low;
beta = s[CURL_INDEX[i]].high;
gamma = s[CURL_INDEX[i + 1]].high;
delta = (alpha | (~gamma)) & (s[CURL_INDEX[i + 1]].low ^ beta);
c->low = ~delta;
c->high = (alpha ^ gamma) | delta;
ptrit_sbox(&c[1], s, i + 1);
}
void ptrit_curl_reset(PCurl *ctx) {
memset(ctx->state, 0, sizeof(ptrit_t) * STATE_LENGTH);
}