-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_product_proof.c
100 lines (75 loc) · 2.32 KB
/
test_product_proof.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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <openssl/bn.h>
#include "product_proof.h"
#include "test_common.h"
void mu_test_ProductStatement_New()
{
const int bits = 180;
IntegerGroup g = IntegerGroup_Generate(bits);
BIGNUM *commit_a = IntegerGroup_RandomElement(g);
BIGNUM *commit_b = IntegerGroup_RandomElement(g);
BIGNUM *commit_c = IntegerGroup_RandomElement(g);
mu_ensure(commit_a);
mu_ensure(commit_b);
mu_ensure(commit_c);
ProductStatement proof = ProductStatement_New(g, commit_a, commit_b, commit_c);
BN_free(commit_a);
BN_free(commit_b);
BN_free(commit_c);
ProductStatement_Free(proof);
IntegerGroup_Free(g);
}
void mu_test_ProductProof_Correct()
{
for(int i=0; i<50; i++) {
const int bits = 180;
IntegerGroup g = IntegerGroup_Generate(bits);
BIGNUM* a = IntegerGroup_RandomExponent(g);
BIGNUM* b = IntegerGroup_RandomExponent(g);
BIGNUM* c = BN_new();
mu_ensure(a);
mu_ensure(b);
mu_ensure(c);
// c = a*b mod q
mu_ensure(BN_mod_mul(c, a, b, IntegerGroup_GetQ(g), IntegerGroup_GetCtx(g)));
BIGNUM* r_a = IntegerGroup_RandomExponent(g);
BIGNUM* r_b = IntegerGroup_RandomExponent(g);
BIGNUM* r_c = IntegerGroup_RandomExponent(g);
mu_ensure(r_a);
mu_ensure(r_b);
mu_ensure(r_c);
BIGNUM* commit_a = IntegerGroup_Commit(g, a, r_a);
BIGNUM* commit_b = IntegerGroup_Commit(g, b, r_b);
BIGNUM* commit_c = IntegerGroup_Commit(g, c, r_c);
mu_ensure(commit_a);
mu_ensure(commit_b);
mu_ensure(commit_c);
ProductStatement proof = ProductStatement_New(g, commit_a, commit_b, commit_c);
ProductEvidence ev = ProductEvidence_New(proof, a, r_a, r_b, r_c);
mu_ensure(ProductEvidence_Verify(ev, proof));
// Write params to temp file
FILE *file = tmpfile();
mu_ensure(file);
mu_ensure(ProductEvidence_Serialize(ev, file));
rewind(file);
ProductEvidence ev2 = ProductEvidence_Unserialize(file);
mu_ensure(ev2);
mu_ensure(ProductEvidence_Verify(ev2, proof));
fclose(file);
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(commit_a);
BN_free(commit_b);
BN_free(commit_c);
BN_free(r_a);
BN_free(r_b);
BN_free(r_c);
ProductEvidence_Free(ev);
ProductEvidence_Free(ev2);
ProductStatement_Free(proof);
IntegerGroup_Free(g);
}
}