forked from tanteikg/zkbdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkbdf_verifyPseudo.c
173 lines (145 loc) · 3.94 KB
/
zkbdf_verifyPseudo.c
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
/*
Name: zkbdf_verifyPseudo.c
Author: Tan Teik Guan
Description: Verify function for VDF realization using ZKBoo. Modified from MPC_SHA256_VERIFIER.c
*/
/*
============================================================================
Name : MPC_SHA256_VERIFIER.c
Author : Sobuno
Version : 0.1
Description : Verifies a proof for SHA-256 generated by MPC_SHA256.c
============================================================================
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "shared.h"
int NUM_ROUNDS = 100;
int NUM_LOOPS = 1;
void printbits(uint32_t n) {
if (n) {
printbits(n >> 1);
printf("%d", n & 1);
}
}
int main(int argc, char * argv[]) {
setbuf(stdout, NULL);
init_EVP();
openmp_thread_setup();
char CHALLENGE[BLOCK_SIZE];
char ek[BLOCK_SIZE];
if (argc != 4)
{
printf("Usage: %s <number of rounds (e.g. 20, 40, 60, 80, 100)> <challenge (Max %d char> <eval key (Max %d char)>\n",argv[0],MSG_SIZE,MSG_SIZE);
return -1;
}
NUM_ROUNDS = atoi(argv[1]);
memset(CHALLENGE,0,sizeof(CHALLENGE));
strncpy(CHALLENGE,argv[2],MSG_SIZE);
memset(ek,0,sizeof(ek));
strncpy(ek,argv[3],MSG_SIZE);
printf("Iterations of SHA: %d\n", NUM_ROUNDS);
int i;
i = strlen(ek);
printf("length of ek: %d\n",i);
unsigned char input[BLOCK_SIZE];
memset(input,0,sizeof(input));
for (int j=0;j<i;j++)
input[j] = ek[j];
a as[NUM_ROUNDS];
z zs[NUM_ROUNDS];
FILE *file;
int failed = 0;
char outputFile[3*sizeof(int) + 8];
sprintf(outputFile, "out%i.bin", NUM_ROUNDS);
file = fopen(outputFile, "rb");
if (!file) {
printf("Unable to open file!");
return -1;
}
fread(&as, sizeof(a), NUM_ROUNDS, file);
fread(&zs, sizeof(z), NUM_ROUNDS, file);
fclose(file);
struct timeval begin, delta;
gettimeofday(&begin,NULL);
for(int loops=0;loops<NUM_LOOPS;loops++)
{
uint32_t y1[8];
uint32_t y2[8];
reconstruct(as[0].yp1[0],as[0].yp1[1],as[0].yp1[2],y1);
reconstruct(as[0].yp2[0],as[0].yp2[1],as[0].yp2[2],y2);
printf("Received output for H(ek): ");
for(int i=0;i<8;i++) {
printf("%02X", y1[i]);
}
printf("\n");
printf("Received output for Hmac(ek,challenge): ");
for(int i=0;i<8;i++) {
printf("%02X", y2[i]);
}
printf("\n");
{
SHA256_CTX ctx;
unsigned char expectedhash[SHA256_DIGEST_LENGTH];
int l;
SHA256_Init(&ctx);
SHA256_Update(&ctx, input, strlen(input));
SHA256_Final(expectedhash, &ctx);
for (l=0;l<8;l++)
{
uint32_t temp;
// to take care of big endian
unsigned char tempc[4];
tempc[0] = expectedhash[l*4+3];
tempc[1] = expectedhash[l*4+2];
tempc[2] = expectedhash[l*4+1];
tempc[3] = expectedhash[l*4];
memcpy(&temp,tempc,4);
if (temp != y1[l])
{
printf("hash does not match !!\n");
return -1;
}
}
}
int es[NUM_ROUNDS*2];
unsigned char plaintext[NUM_ROUNDS][16];
memset(&(plaintext[0]),0x30,16);
#pragma omp parallel for
for (int i=0; i<(NUM_ROUNDS); i++)
{
if (i < (NUM_ROUNDS-1))
{
SHA256_CTX ctx;
unsigned char prevroundhash[SHA256_DIGEST_LENGTH];
SHA256_Init(&ctx);
SHA256_Update(&ctx, &(zs[i]), sizeof(z));
SHA256_Final(prevroundhash, &ctx);
memcpy(plaintext[i+1],prevroundhash,16);
}
H3(y1,y2,&(as[i]), 1, &(es[i]));
}
#pragma omp parallel for
for(int i = 0; i<(NUM_ROUNDS); i++) {
int verifyResult = verify(as[i], CHALLENGE, es[i], plaintext[i], zs[i]);
if (verifyResult != 0) {
printf("Not Verified %d\n", i);
failed = 1;
}
}
}
if (!failed)
printf("verified ok \n",i);
gettimeofday(&delta,NULL);
unsigned long inMilli = (delta.tv_sec - begin.tv_sec)*1000000 + (delta.tv_usec - begin.tv_usec);
inMilli /= 1000;
printf("Total time for %d loops: %ju miliseconds\n", NUM_LOOPS,(uintmax_t)inMilli);
printf("Time for 1 loop: %ju miliseconds\n", (uintmax_t)inMilli/NUM_LOOPS);
openmp_thread_cleanup();
cleanup_EVP();
return EXIT_SUCCESS;
}