-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.h
More file actions
184 lines (149 loc) · 3.94 KB
/
command.h
File metadata and controls
184 lines (149 loc) · 3.94 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef __COMMAND_H__
#define __COMMAND_H__ 1
#include <list>
#include "number.h"
#include "positive.h"
#include "authorize.h"
#include "elgamal.h"
namespace BankOfEuler {
struct Command {
unsigned int magic; // not static because we are virtual
virtual void read(FILE *fp, bool rm = 1) = 0;
virtual void write(FILE *fp) const = 0;
virtual void hash_update(CTX *ctx, Number &hash) const = 0;
virtual void decrypt(const ElGamalSecret &) = 0;
virtual void encrypt(const ElGamalPublic &) = 0;
virtual bool authorize(CTX *ctx, const AuthorizeCertificate *acert, unsigned int n) = 0;
};
extern Command *read_command(FILE *fp);
struct MergeCommand : Command {
const static unsigned int magic = 0x71346EC;
Number adir, bdir, cdir;
Number aval, bval;
MergeCommand() {
Command::magic = magic;
}
virtual ~MergeCommand() { }
void read(FILE *fp, bool rm = 1) {
if (rm) assert(magic == read_int32(fp));
adir.read(fp);
bdir.read(fp);
cdir.read(fp);
aval.read(fp);
bval.read(fp);
}
void write(FILE *fp) const {
write_int32(magic, fp);
adir.write(fp);
bdir.write(fp);
cdir.write(fp);
aval.write(fp);
bval.write(fp);
}
void hash_update(CTX *ctx, Number &hash) const {
ctx->hash_update(hash, magic);
ctx->hash_update(hash, adir);
ctx->hash_update(hash, bdir);
ctx->hash_update(hash, cdir);
ctx->hash_update(hash, aval);
ctx->hash_update(hash, bval);
}
void decrypt(const ElGamalSecret &egs) {
egs.decrypt(adir);
egs.decrypt(bdir);
egs.decrypt(cdir);
egs.decrypt(aval);
egs.decrypt(bval);
}
void encrypt(const ElGamalPublic &egp) {
egp.encrypt(adir);
egp.encrypt(bdir);
egp.encrypt(cdir);
egp.encrypt(aval);
egp.encrypt(bval);
}
bool authorize(CTX *ctx, const AuthorizeCertificate *acert, unsigned int n);
};
struct CheckCommand : Command {
const static unsigned int magic = 0x71CC6EC;
Number adir;
Number aval;
CheckCommand() {
Command::magic = magic;
}
virtual ~CheckCommand() { }
void read(FILE *fp, bool rm = 1) {
if (rm) assert(magic == read_int32(fp));
adir.read(fp);
aval.read(fp);
}
void write(FILE *fp) const {
write_int32(magic, fp);
adir.write(fp);
aval.write(fp);
}
void hash_update(CTX *ctx, Number &hash) const {
ctx->hash_update(hash, magic);
ctx->hash_update(hash, adir);
ctx->hash_update(hash, aval);
}
void decrypt(const ElGamalSecret &egs) {
egs.decrypt(adir);
egs.decrypt(aval);
}
void encrypt(const ElGamalPublic &egp) {
egp.encrypt(adir);
egp.encrypt(aval);
}
bool authorize(CTX *ctx, const AuthorizeCertificate *acert, unsigned int n);
};
struct SplitCommand : Command {
const static unsigned int magic = 0x57717C0;
Number adir, bdir, cdir;
PositiveProof avalpos, bvalpos;
SplitCommand() {
Command::magic = magic;
}
virtual ~SplitCommand() { }
void read(FILE *fp, bool rm = 1) {
if (rm) assert(magic == read_int32(fp));
adir.read(fp);
bdir.read(fp);
cdir.read(fp);
avalpos.read(fp);
bvalpos.read(fp);
}
void write(FILE *fp) const {
write_int32(magic, fp);
adir.write(fp);
bdir.write(fp);
cdir.write(fp);
avalpos.write(fp);
bvalpos.write(fp);
}
void hash_update(CTX *ctx, Number &hash) const {
ctx->hash_update(hash, magic);
ctx->hash_update(hash, adir);
ctx->hash_update(hash, bdir);
ctx->hash_update(hash, cdir);
ctx->hash_update(hash, avalpos.h, ctx->hn);
ctx->hash_update(hash, bvalpos.h, ctx->hn);
}
void decrypt(const ElGamalSecret &egs) {
egs.decrypt(adir);
egs.decrypt(bdir);
egs.decrypt(cdir);
egs.decrypt(avalpos.gx);
egs.decrypt(bvalpos.gx);
}
void encrypt(const ElGamalPublic &egp) {
egp.encrypt(adir);
egp.encrypt(bdir);
egp.encrypt(cdir);
egp.encrypt(avalpos.gx);
egp.encrypt(bvalpos.gx);
}
bool authorize(CTX *ctx, const AuthorizeCertificate *acert, unsigned int n);
};
}
#endif