-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslh_adrs.h
166 lines (137 loc) · 3.65 KB
/
slh_adrs.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// slh_adrs.h
// Markku-Juhani O. Saarinen <[email protected]>. See LICENSE.
// === Internal ADRS manipulation functions (Section 4.2)
#ifndef _SLH_ADRS_H_
#define _SLH_ADRS_H_
#include "slh_param.h"
#include <string.h>
// ADRS type constants
#define ADRS_WOTS_HASH 0
#define ADRS_WOTS_PK 1
#define ADRS_TREE 2
#define ADRS_FORS_TREE 3
#define ADRS_FORS_ROOTS 4
#define ADRS_WOTS_PRF 5
#define ADRS_FORS_PRF 6
// Algorithm 1: toInt(x, n)
static inline uint64_t slh_toint(const uint8_t *x, unsigned n)
{
unsigned i;
uint64_t t;
if (n == 0)
return 0;
t = (uint64_t) x[0];
for (i = 1; i < n; i++) {
t <<= 8;
t += (uint64_t) x[i];
}
return t;
}
// Algorithm 2: toByte(x, n)
static inline void slh_tobyte(uint8_t *x, uint64_t t, unsigned n)
{
unsigned i;
if (n == 0)
return;
for (i = n - 1; i > 0; i--) {
x[i] = (uint8_t) (t & 0xFF);
t >>= 8;
}
x[0] = (uint8_t) t;
}
// === Clear / initialize
static inline void adrs_zero(slh_ctx_t *ctx)
{
ctx->adrs->u32[0] = 0;
ctx->adrs->u32[1] = 0;
ctx->adrs->u32[2] = 0;
ctx->adrs->u32[3] = 0;
ctx->adrs->u32[4] = 0;
ctx->adrs->u32[5] = 0;
ctx->adrs->u32[6] = 0;
ctx->adrs->u32[7] = 0;
}
// === Set layer address.
static inline void adrs_set_layer_address(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[0] = rev8_be32(x);
}
// === Set tree addresss.
static inline void adrs_set_tree_address(slh_ctx_t *ctx, uint64_t x)
{
// bytes a[4:8] of tree address are always zero
ctx->adrs->u32[2] = rev8_be32(x >> 32);
ctx->adrs->u32[3] = rev8_be32(x & 0xFFFFFFFF);
}
// === Set key pair Address.
static inline void adrs_set_key_pair_address(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[5] = rev8_be32(x);
}
// === Get key pair Address.
static inline uint32_t adrs_get_key_pair_address(const slh_ctx_t *ctx)
{
return rev8_be32(ctx->adrs->u32[5]);
}
// === Set FORS tree height.
static inline void adrs_set_tree_height(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[6] = rev8_be32(x);
}
// === Set WOTS+ chain address.
static inline void adrs_set_chain_address(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[6] = rev8_be32(x);
}
// === Set FORS tree index.
static inline void adrs_set_tree_index(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[7] = rev8_be32(x);
}
// === Get FORS tree index.
static inline uint32_t adrs_get_tree_index(const slh_ctx_t *ctx)
{
return rev8_be32(ctx->adrs->u32[7]);
}
// === Set WOTS+ hash address.
static inline void adrs_set_hash_address(slh_ctx_t *ctx, uint32_t x)
{
ctx->adrs->u32[7] = rev8_be32(x);
}
static inline void adrs_set_type(slh_ctx_t *ctx, uint32_t y)
{
ctx->adrs->u32[4] = rev8_be32(y);
}
static inline uint32_t adrs_get_type(slh_ctx_t *ctx)
{
return rev8_be32(ctx->adrs->u32[4]);
}
// === "Function ADRS.setTypeAndClear(Y) for addresses sets the type
// of the ADRS to Y and sets the final 12 bytes of the ADRS to zero."
static inline void adrs_set_type_and_clear(slh_ctx_t *ctx, uint32_t y)
{
ctx->adrs->u32[4] = rev8_be32(y);
ctx->adrs->u32[5] = 0;
ctx->adrs->u32[6] = 0;
ctx->adrs->u32[7] = 0;
}
static inline void adrs_set_type_and_clear_not_kp(slh_ctx_t *ctx, uint32_t y)
{
ctx->adrs->u32[4] = rev8_be32(y);
ctx->adrs->u32[6] = 0;
ctx->adrs->u32[7] = 0;
}
// === Compressed 22-byte address ADRSc used with SHA-2.
static inline void adrsc_22(const slh_ctx_t *ctx, uint8_t *ac)
{
int i;
ac[0] = ctx->adrs->u8[3];
for (i = 0; i < 8; i++) {
ac[i + 1] = ctx->adrs->u8[i + 8];
}
for (i = 0; i < 13; i++) {
ac[i + 9] = ctx->adrs->u8[i + 19];
}
}
// _SLH_ADRS_H_
#endif