summaryrefslogtreecommitdiff
path: root/src/ext/equix/include/equix.h
blob: 01ab249437a3a77e1b0d0b5f3c2096a97c7be951 (plain)
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
141
142
143
144
145
/* Copyright (c) 2020 tevador <tevador@gmail.com> */
/* See LICENSE for licensing information */

#ifndef EQUIX_H
#define EQUIX_H

#include <stdint.h>
#include <stddef.h>

/*
 * The solver will return at most this many solutions.
 */
#define EQUIX_MAX_SOLS 8

/*
 * The number of indices.
 */
#define EQUIX_NUM_IDX 8

/*
 * 16-bit index.
 */
typedef uint16_t equix_idx;

/*
 *  The solution.
 */
typedef struct equix_solution {
    equix_idx idx[EQUIX_NUM_IDX];
} equix_solution;

/*
 * Solution verification results
 */
typedef enum equix_result {
    EQUIX_OK,               /* Solution is valid */
    EQUIX_CHALLENGE,        /* The challenge is invalid (the internal hash
                               function doesn't pass validation). */
    EQUIX_ORDER,            /* Indices are not in the correct order. */
    EQUIX_PARTIAL_SUM,      /* The partial sums of the hash values don't
                               have the required number of trailing zeroes. */
    EQUIX_FINAL_SUM         /* The hash values don't sum to zero. */
} equix_result;

/*
 * Opaque struct that holds the Equi-X context
 */
typedef struct equix_ctx equix_ctx;

/*
 * Flags for context creation
*/
typedef enum equix_ctx_flags {
    EQUIX_CTX_VERIFY = 0,       /* Context for verification */
    EQUIX_CTX_SOLVE = 1,        /* Context for solving */
    EQUIX_CTX_COMPILE = 2,      /* Compile internal hash function */
    EQUIX_CTX_HUGEPAGES = 4,    /* Allocate solver memory using HugePages */
} equix_ctx_flags;

/* Sentinel value used to indicate unsupported type */
#define EQUIX_NOTSUPP ((equix_ctx*)-1)

#if defined(_WIN32) || defined(__CYGWIN__)
#define EQUIX_WIN
#endif

/* Shared/static library definitions */
#ifdef EQUIX_WIN
    #ifdef EQUIX_SHARED
        #define EQUIX_API __declspec(dllexport)
    #elif !defined(EQUIX_STATIC)
        #define EQUIX_API __declspec(dllimport)
    #else
        #define EQUIX_API
    #endif
    #define EQUIX_PRIVATE
#else
    #ifdef EQUIX_SHARED
        #define EQUIX_API __attribute__ ((visibility ("default")))
    #else
        #define EQUIX_API __attribute__ ((visibility ("hidden")))
    #endif
    #define EQUIX_PRIVATE __attribute__ ((visibility ("hidden")))
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Allocate an Equi-X context.
 *
 * @param flags is the type of context to be created
 *
 * @return pointer to a newly created context. Returns NULL on memory
 *         allocation failure and EQUIX_NOTSUPP if the requested type
 *         is not supported.
 */
EQUIX_API equix_ctx* equix_alloc(equix_ctx_flags flags);

/*
* Free an Equi-X a context.
*
* @param ctx is a pointer to the context
*/
EQUIX_API void equix_free(equix_ctx* ctx);

/*
 * Find Equi-X solutions for the given challenge.
 *
 * @param ctx             pointer to an Equi-X context
 * @param challenge       pointer to the challenge data
 * @param challenge_size  size of the challenge
 * @param output          pointer to the output array where solutions will be
 *                        stored
 *
 * @return the number of solutions found
 */
EQUIX_API int equix_solve(
    equix_ctx* ctx,
    const void* challenge,
    size_t challenge_size,
    equix_solution output[EQUIX_MAX_SOLS]);

/*
 * Verify an Equi-X solution.
 *
 * @param ctx             pointer to an Equi-X context
 * @param challenge       pointer to the challenge data
 * @param challenge_size  size of the challenge
 * @param solution        pointer to the solution to be verified
 *
 * @return verification result
*/
EQUIX_API equix_result equix_verify(
    equix_ctx* ctx,
    const void* challenge,
    size_t challenge_size,
    const equix_solution* solution);

#ifdef __cplusplus
}
#endif

#endif