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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/* Copyright (c) 2020 tevador <tevador@gmail.com> */
/* See LICENSE for licensing information */
/*
* HashX is an algorithm designed for client puzzles and proof-of-work schemes.
* While traditional cryptographic hash functions use a fixed one-way
* compression function, each HashX instance represents a unique pseudorandomly
* generated one-way function.
*
* Example of usage:
*
#include <hashx.h>
#include <stdio.h>
int main() {
char seed[] = "this is a seed that will generate a hash function";
char hash[HASHX_SIZE];
hashx_ctx* ctx = hashx_alloc(HASHX_COMPILED);
if (ctx == HASHX_NOTSUPP)
ctx = hashx_alloc(HASHX_INTERPRETED);
if (ctx == NULL)
return 1;
if (!hashx_make(ctx, seed, sizeof(seed)))
return 1;
hashx_exec(ctx, 123456789, hash);
hashx_free(ctx);
for (unsigned i = 0; i < HASHX_SIZE; ++i)
printf("%02x", hash[i] & 0xff);
printf("\n");
return 0;
}
*
*/
#ifndef HASHX_H
#define HASHX_H
#include <stdint.h>
#include <stddef.h>
/*
* Input of the hash function.
*
* Counter mode (default): a 64-bit unsigned integer
* Block mode: pointer to a buffer and the number of bytes to be hashed
*/
#ifndef HASHX_BLOCK_MODE
#define HASHX_INPUT uint64_t input
#else
#define HASHX_INPUT const void* input, size_t size
#endif
/* The default (and maximum) hash size is 32 bytes */
#ifndef HASHX_SIZE
#define HASHX_SIZE 32
#endif
/* Opaque struct representing a HashX instance */
typedef struct hashx_ctx hashx_ctx;
/* Type of hash function */
typedef enum hashx_type {
HASHX_INTERPRETED,
HASHX_COMPILED
} hashx_type;
/* Sentinel value used to indicate unsupported type */
#define HASHX_NOTSUPP ((hashx_ctx*)-1)
#if defined(_WIN32) || defined(__CYGWIN__)
#define HASHX_WIN
#endif
/* Shared/static library definitions */
#ifdef HASHX_WIN
#ifdef HASHX_SHARED
#define HASHX_API __declspec(dllexport)
#elif !defined(HASHX_STATIC)
#define HASHX_API __declspec(dllimport)
#else
#define HASHX_API
#endif
#define HASHX_PRIVATE
#else
#ifdef HASHX_SHARED
#define HASHX_API __attribute__ ((visibility ("default")))
#else
#define HASHX_API __attribute__ ((visibility ("hidden")))
#endif
#define HASHX_PRIVATE __attribute__ ((visibility ("hidden")))
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* Allocate a HashX instance.
*
* @param type is the type of instance to be created.
*
* @return pointer to a new HashX instance. Returns NULL on memory allocation
* failure and HASHX_NOTSUPP if the requested type is not supported.
*/
HASHX_API hashx_ctx* hashx_alloc(hashx_type type);
/*
* Create a new HashX function from seed.
*
* @param ctx is pointer to a HashX instance.
* @param seed is a pointer to the seed value.
* @param size is the size of the seed.
*
* @return 1 on success, 0 on failure.
*/
HASHX_API int hashx_make(hashx_ctx* ctx, const void* seed, size_t size);
/*
* Execute the HashX function.
*
* @param ctx is pointer to a HashX instance. A HashX function must have
* been previously created by calling hashx_make.
* @param HASHX_INPUT is the input to be hashed (see definition above).
* @param output is a pointer to the result buffer. HASHX_SIZE bytes will be
* written.
s*/
HASHX_API void hashx_exec(const hashx_ctx* ctx, HASHX_INPUT, void* output);
/*
* Free a HashX instance.
*
* @param ctx is pointer to a HashX instance.
*/
HASHX_API void hashx_free(hashx_ctx* ctx);
#ifdef __cplusplus
}
#endif
#endif
|