-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsha2-dyn-test.ts
211 lines (185 loc) · 4.88 KB
/
sha2-dyn-test.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {
Bytes,
DynamicProof,
FeatureFlags,
Struct,
Undefined,
VerificationKey,
Void,
ZkProgram,
} from "npm:o1js";
import {
DynamicBytes,
DynamicSHA2,
DynamicString,
Sha2FinalIteration,
Sha2IterationState,
} from "npm:mina-credentials/dynamic";
console.log("Starting test :thumbsup:");
class Bytes32 extends Bytes(32) {}
class Payload extends DynamicString({ maxLength: 100 }) {}
class Sha2_Input extends Struct({
payload: Payload,
}) {}
class Sha2_Output extends Struct({
digest: Bytes32,
}) {}
class HashState extends Sha2IterationState(256) {}
const BLOCKS_PER_ITERATION = 7;
class HashFinalIteration
extends Sha2FinalIteration(256, BLOCKS_PER_ITERATION) {}
// const Sha2PrivUpdate = ZkProgram({
// name: "sha2-priv-update",
// publicOutput: HashState,
// methods: {
// empty: {
// privateInputs: [],
// // deno-lint-ignore require-await
// async method() {
// return {
// publicOutput: HashState.initial(),
// };
// },
// },
// },
// });
const Sha2Priv = ZkProgram({
name: "privinput",
publicOutput: Sha2_Output,
methods: {
finalize: {
privateInputs: [Payload, HashFinalIteration],
// deno-lint-ignore require-await
async method(
payload: Payload,
finalIter: HashFinalIteration,
) {
const initState = HashState.initial();
const digest = DynamicSHA2.finalize(initState, finalIter, payload);
return { publicOutput: new Sha2_Output({ digest }) };
},
},
},
});
const privFF = await FeatureFlags.fromZkProgram(
Sha2Priv,
);
export class DynPrivProof extends DynamicProof<
Void,
Sha2_Output
> {
static override publicInputType = Void;
static override publicOutputType = Sha2_Output;
static override maxProofsVerified = 0 as const;
static override featureFlags = privFF;
}
const DynCheckerPriv = ZkProgram({
name: "privchecker",
methods: {
check: {
privateInputs: [
DynPrivProof,
VerificationKey,
],
// deno-lint-ignore require-await
async method(proof, vk) {
proof.verify(vk);
},
},
},
});
const Sha2Pub = ZkProgram({
name: "pubinput",
publicInput: Sha2_Input,
publicOutput: Sha2_Output,
methods: {
finalize: {
privateInputs: [HashFinalIteration],
// deno-lint-ignore require-await
async method(
inp: Sha2_Input,
finalIter: HashFinalIteration,
) {
const initState = HashState.initial();
const digest = DynamicSHA2.finalize(initState, finalIter, inp.payload);
return { publicOutput: new Sha2_Output({ digest }) };
},
},
},
});
const pubFF = await FeatureFlags.fromZkProgram(
Sha2Pub,
);
export class DynPubProof extends DynamicProof<
Sha2_Input,
Sha2_Output
> {
static override publicInputType = Sha2_Input;
static override publicOutputType = Sha2_Output;
static override maxProofsVerified = 0 as const;
static override featureFlags = pubFF;
}
const DynCheckerPub = ZkProgram({
name: "pubchecker",
methods: {
check: {
privateInputs: [
DynPubProof,
VerificationKey,
],
// deno-lint-ignore require-await
async method(proof, vk) {
proof.verify(vk);
},
},
},
});
///////////////
const payload = Payload.from("123");
const { iterations, final: finalIter } = DynamicSHA2.split(
256,
BLOCKS_PER_ITERATION,
payload,
);
console.log(iterations.length);
console.time("compile priv");
const vkPriv = (await Sha2Priv.compile()).verificationKey;
console.timeEnd("compile priv");
console.time("prove priv");
const proofPriv = await Sha2Priv.finalize(payload, finalIter);
console.timeEnd("prove priv");
console.time("compile priv dyn");
await DynCheckerPriv.compile();
console.timeEnd("compile priv dyn");
console.time("prove priv dyn");
const dynamizedPriv = DynPrivProof.fromProof(proofPriv.proof);
const proofPrivDyn = await DynCheckerPriv.check(
dynamizedPriv,
vkPriv,
);
console.timeEnd("prove priv dyn");
console.time("verify priv dyn");
const isValid = await DynCheckerPriv.verify(proofPrivDyn.proof);
console.log("priv dyn validation:", isValid);
console.timeEnd("verify priv dyn");
/// now public
console.time("compile pub");
const vkPub = (await Sha2Pub.compile()).verificationKey;
console.timeEnd("compile pub");
console.time("prove pub");
const proofPub = await Sha2Pub.finalize(new Sha2_Input({ payload }), finalIter);
console.timeEnd("prove pub");
console.time("compile pub dyn");
await DynCheckerPub.compile();
console.timeEnd("compile pub dyn");
console.time("prove pub dyn");
const dynamizedPub = DynPubProof.fromProof(proofPub.proof);
const proofPubDyn = await DynCheckerPub.check(
dynamizedPub,
vkPub,
);
console.timeEnd("prove pub dyn");
console.time("verify pub dyn");
const isValidPub = await DynCheckerPub.verify(proofPubDyn.proof);
console.log("pub dyn validation:", isValidPub);
console.timeEnd("verify pub dyn");