-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmall.cpp
More file actions
147 lines (111 loc) · 2.89 KB
/
small.cpp
File metadata and controls
147 lines (111 loc) · 2.89 KB
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
#include "small.h"
#include "number.h"
using namespace BankOfEuler;
const unsigned int SmallRequest::magic = 0x56;
bool SmallRequest::verify(CTX *ctx) {
if (gx < 1 || gx >= ctx->p)
return false;
unsigned int n = gy.size();
if (n != ctx->rounds)
return false;
for (unsigned int i = 0; i < n; ++i)
if (gy[i] < 1 || gy[i] >= ctx->p)
return false;
return true;
}
void SmallRequest::generate(CTX *ctx, const Number &x, SmallNonce *nonce) {
unsigned int n = ctx->rounds;
nonce->y.set_size(n);
assert(x < ctx->k);
nonce->y.randomize(n, ctx->k - x); // x << k, so k - x is almost k
gx.powmod(ctx->g, x, ctx->p);
gy.set_size(n);
for (unsigned int i = 0; i < n; ++i) {
gy[i].powmod(ctx->g, nonce->y[i], ctx->p);
}
}
const unsigned int SmallChallenge::magic = 0x57;
void SmallChallenge::generate(SCTX *sctx, const SmallRequest &r) {
t = time(NULL) + sctx->expiry;
gx = r.gx;
unsigned int n = r.gy.size();
c.urandomb(n);
sctx->hash_init(h);
sctx->hash_update(h, magic);
sctx->hash_update(h, gx);
sctx->hash_update(h, t);
sctx->hash_update(h, n);
Number t;
for (unsigned int i = 0; i < n; ++i) {
if (c.tstbit(i)) {
t = r.gy[i] * gx;
t %= sctx->p;
} else {
t = r.gy[i];
}
sctx->hash_update(h, t);
}
sctx->hash_final(h);
sh.powmod(h, sctx->sd, sctx->sn);
}
const unsigned int SmallResponse::magic = 0x58;
bool SmallResponse::verify(CTX *ctx) {
if (t < time(NULL))
return false;
unsigned int n = yx.size();
Number h2;
ctx->hash_init(h2);
ctx->hash_update(h2, SmallChallenge::magic);
ctx->hash_update(h2, gx);
ctx->hash_update(h2, t);
ctx->hash_update(h2, n);
for (unsigned int i = 0; i < n; ++i) {
if (yx[i] < 0 || yx[i] >= ctx->k)
return false;
Number gy;
gy.powmod(ctx->g, yx[i], ctx->p);
ctx->hash_update(h2, gy);
}
ctx->hash_final(h2);
if (h != h2)
return false;
return h.verify(sh, ctx->se, ctx->sn);
}
void SmallResponse::generate(CTX *ctx, const SmallChallenge &c, const Number &x, const SmallNonce *nonce) {
gx = c.gx;
h = c.h;
sh = c.sh;
t = c.t;
unsigned int n = nonce->y.size();
yx.set_size(n);
for (unsigned int i = 0; i < n; ++i) {
yx[i] = nonce->y[i];
if (c.c.tstbit(i))
yx[i] += x;
}
}
const unsigned int SmallProof::magic = 0x59;
void SmallProof::generate(SCTX *sctx, const SmallResponse &r) {
gx = r.gx;
n = r.yx.size();
t = time(NULL);
sctx->hash_init(h);
sctx->hash_update(h, magic);
sctx->hash_update(h, gx);
sctx->hash_update(h, t);
sctx->hash_update(h, n);
sctx->hash_final(h);
sh.powmod(h, sctx->sd, sctx->sn);
}
bool SmallProof::verify(CTX *ctx) {
Number h2;
ctx->hash_init(h2);
ctx->hash_update(h2, magic);
ctx->hash_update(h2, gx);
ctx->hash_update(h2, t);
ctx->hash_update(h2, n);
ctx->hash_final(h2);
if (h != h2)
return false;
return h.verify(sh, ctx->se, ctx->sn);
}