-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
60 lines (50 loc) · 2.02 KB
/
Copy pathmain.c
File metadata and controls
60 lines (50 loc) · 2.02 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
#include "schnorr.h"
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int rounds = 10;
if (argc > 1) {
char *end_ptr = NULL;
long parsed = strtol(argv[1], &end_ptr, 10);
if (end_ptr == argv[1] || *end_ptr != '\0' || parsed <= 0 || parsed > 10000) {
fprintf(stderr, "Usage: %s [rounds]\n", argv[0]);
return EXIT_FAILURE;
}
rounds = (int)parsed;
}
SchnorrParams params;
if (!schnorr_generate_safe_prime_params(¶ms)) {
fprintf(stderr, "Failed to generate Schnorr parameters.\n");
return EXIT_FAILURE;
}
SchnorrKeyPair keys;
schnorr_keygen(¶ms, &keys);
printf("Schnorr ZKP demo (interactive sigma protocol)\n");
printf("p=%" PRIu64 ", q=%" PRIu64 ", g=%" PRIu64 "\n", params.p, params.q, params.g);
printf("Public key y=g^x mod p: %" PRIu64 "\n", keys.public_y);
printf("Running %d proof rounds...\n\n", rounds);
for (int i = 0; i < rounds; i++) {
SchnorrProof proof;
schnorr_prove(¶ms, &keys, &proof);
int accepted = schnorr_verify(¶ms, keys.public_y, &proof);
printf("Round %d: c=%" PRIu64 ", accepted=%s\n", i + 1, proof.challenge_c, accepted ? "true" : "false");
if (!accepted) {
printf("Verification failed in an honest-proof round.\n");
return EXIT_FAILURE;
}
if (i == rounds - 1) {
SchnorrProof tampered = proof;
tampered.response_s = schnorr_add_mod(tampered.response_s, 1ULL, params.q);
int tampered_ok = schnorr_verify(¶ms, keys.public_y, &tampered);
printf("Tamper test (modified response) accepted=%s\n", tampered_ok ? "true" : "false");
if (tampered_ok) {
printf("Unexpected tamper acceptance.\n");
return EXIT_FAILURE;
}
}
}
printf("\nAll rounds accepted with real witness, tampered proof rejected.\n");
return EXIT_SUCCESS;
}