forked from alitalia/mos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracle.c
39 lines (33 loc) · 901 Bytes
/
oracle.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
#include <stdint.h>
#include <stddef.h>
#include <blake2.h>
#include "libot/ot.h"
#include "bitmath.h"
/**
* Extends `out` to `to` bytes in blocks of HASHBYTES.
*/
void prg_extend(uint8_t *out, size_t to)
{
#define STEPH BLAKE2B_OUTBYTES
if (to < STEPH) {
blake2(out, out, NULL, to, KAPPA/8, 0);
return;
}
blake2(out, out, NULL, STEPH, KAPPA/8, 0);
for (int times = to / STEPH - 1; times > 0; times--) {
if (blake2(out+STEPH, out, NULL, STEPH, STEPH, 0) == -1) {
perror("Cannot hash");
exit(EXIT_FAILURE);
}
out += STEPH;
}
blake2(out+STEPH, out, NULL, to % STEPH, STEPH, 0);
}
void hash(uint8_t *out, uint8_t *in, const size_t j, const size_t inlen)
{
static const int jsize = sizeof(size_t);
uint8_t inj[inlen + jsize];
memcpy(inj, in, inlen);
memcpy(inj + inlen, &j, jsize);
blake2(out, inj, NULL, octs(KAPPA), inlen + jsize, 0);
}