-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rs
289 lines (228 loc) · 9.55 KB
/
test.rs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#![allow(unused_imports)]
use ark_ec::{pairing::Pairing, Group};
use ark_ff::Field;
use ark_poly::Polynomial;
use super::{CRSGenerator, CRS};
/// A struct for testing purposes that implements the CRSGenerator trait
pub struct TestCRSGenerator<F: Field, G: Group<ScalarField = F>> {
pub generator: G,
pub point: F,
}
impl<F: Field, G: Group<ScalarField = F>> CRSGenerator<F, G> for TestCRSGenerator<F, G> {
fn generate(&self, degree: usize) -> CRS<G> {
let g1_powers: Vec<G> = (0..=degree)
.map(|i| {
let exponent = self.point.pow(&[i as u64]);
self.generator.mul(exponent)
})
.collect();
CRS { g1_powers }
}
}
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
use crate::{kzg::{prover::prover, system::{KZGSystemImpl, KZGVerifierChallenger}, verifier::verifier, KZGSystem}, util::VerifierChallenge};
use super::*;
use ark_bls12_381::{Bls12_381, Fr as F, G1Projective as G, G1Projective as G1, G2Projective as G2};
use ark_crypto_primitives::sponge::poseidon::PoseidonConfig;
use ark_ff::UniformRand;
use ark_poly::{polynomial::univariate::DensePolynomial, DenseUVPolynomial};
use ark_std::rand::thread_rng;
fn verify_prover_commit_equals_to_g_pow_valuation_point<F, G, P>(
polynomial: &P,
evaluation_point: F,
) where
F: Field,
G: Group<ScalarField = F>,
P: DenseUVPolynomial<F>,
{
// Generate a random generator
let mut rng = thread_rng();
let generator = G::rand(&mut rng);
// Create TestCRSGenerator with the random generator
let crs_generator = TestCRSGenerator {
generator,
point: evaluation_point,
};
// Generate CRS for the degree of the polynomial
let crs = crs_generator.generate(polynomial.degree());
// Assert that the size of CRS is equal to polynomial degree + 1
assert_eq!(
crs.g1_powers.len(),
polynomial.degree() + 1,
"CRS size should be equal to polynomial degree + 1"
);
// Compute the commitment
let commitment = prover::prover_commit(&crs, polynomial);
// Compute f(evaluation_point)
let f_of_point = polynomial.evaluate(&evaluation_point);
// Compute g^(f(evaluation_point))
let expected_commitment = generator.mul(f_of_point);
// Assert that the computed commitment equals g^(f(evaluation_point))
assert_eq!(commitment, expected_commitment);
}
#[test]
fn run_test_prover_commit() {
// Create the polynomial x^3 + x - x^2 - 1
let polynomial = DensePolynomial::from_coefficients_vec(vec![
-F::from(1u64), // constant term
F::from(1u64), // x^1 coefficient
-F::from(1u64), // x^2 coefficient
F::from(1u64), // x^3 coefficient
]);
verify_prover_commit_equals_to_g_pow_valuation_point::<F, G, DensePolynomial<F>>(
&polynomial,
F::from(2u64),
);
}
fn verify_prover_open_equals_to_g_pow_quotient_polynomial<F, G: PartialEq>(
polynomial: &DensePolynomial<F>,
quotient_polynomial: &DensePolynomial<F>,
evaluation_point: F,
challenge_point: F,
) where
F: Field,
G: Group<ScalarField = F>,
{
let mut rng = thread_rng();
let generator = G::rand(&mut rng);
// Setup CRS
let crs_generator = TestCRSGenerator {
generator,
point: evaluation_point,
};
let crs = crs_generator.generate(polynomial.degree());
// Compute the commitment
let commitment = prover::prover_commit(&crs, polynomial);
// Compute the witness
let proof = prover::prover_open(&crs, polynomial, &challenge_point, &commitment);
// Verify that the witness equals to g^(q(evaluation_point))
let q_of_evaluation_point = quotient_polynomial.evaluate(&evaluation_point);
let expected_witness = generator.mul(q_of_evaluation_point);
assert_eq!(
proof.witness, expected_witness,
"Witness should equal g^(q(evaluation_point))"
);
assert_eq!(
proof.challenge_evaluation,
generator.mul(polynomial.evaluate(&challenge_point)),
"Evaluation should equal g^(q(evaluation_point))"
);
}
#[test]
fn run_test_prover_open() {
// Create the polynomial x^3 + x - x^2 - 1
let polynomial = DensePolynomial::from_coefficients_vec(vec![
-F::from(1u64), // constant term
F::from(1u64), // x^1 coefficient
-F::from(1u64), // x^2 coefficient
F::from(1u64), // x^3 coefficient
]);
// Suppose that the challenge point is 2 -> p(2) = 2^3 + 2 - 2^2 - 1 = 5
// Prover will compute the quotient polynomial q(x) = (f(x) - f(2)) / (x - 2)
// q(x) = (x^3 + x - x^2 - 1 - 5) / (x - 2) = x^2 + x + 3
let challenge_point = F::from(2u64);
// We want to verify that the prover's witness equals to g^(q(2))
// q(2) = 2^2 + 2 + 3 = 7
let quotient_polynomial = DensePolynomial::from_coefficients_vec(vec![
F::from(3u64),
F::from(1u64),
F::from(1u64),
]);
verify_prover_open_equals_to_g_pow_quotient_polynomial::<F, G>(
&polynomial,
"ient_polynomial,
F::from(3u64),
challenge_point,
);
}
#[test]
fn test_prover_verifier_interaction() {
let mut rng = thread_rng();
// Create the polynomial x^3 + x - x^2 - 1
let polynomial = DensePolynomial::from_coefficients_vec(vec![
-F::from(1u64), // constant term
F::from(1u64), // x^1 coefficient
-F::from(1u64), // x^2 coefficient
F::from(1u64), // x^3 coefficient
]);
// Set up the CRS
let evaluation_point = F::from(3u64);
let g1 = G1::rand(&mut rng);
let g2 = G2::rand(&mut rng);
let crs_generator = TestCRSGenerator {
generator: g1,
point: evaluation_point,
};
let crs = crs_generator.generate(polynomial.degree());
// Prover: Create commitment
let commitment = prover::prover_commit::<F, G1, DensePolynomial<F>>(&crs, &polynomial);
// Verifier: Generate challenge
let challenge_point = F::from(2u64);
// Prover: Generate proof
let proof = prover::prover_open(&crs, &polynomial, &challenge_point, &commitment);
// Print out s - challenge
let s_minus_challenge = evaluation_point - challenge_point;
// Compute g_2^(s - challenge)
let g_s_minus_challenge = g2 * s_minus_challenge;
// Printing some debug logs
println!("s - challenge = {:?}", s_minus_challenge);
println!("g^(s - challenge) = {:?}", g_s_minus_challenge);
// Compute f(s) manually
let f_s = polynomial.evaluate(&evaluation_point);
// Compute g1^f(s)
let g1_f_s = g1 * f_s;
// Print out the computed values
println!("Manually computed f(s) = {:?}", f_s);
println!("Manually computed g1^f(s) = {:?}", g1_f_s);
println!("Commitment = {:?}", commitment);
// Verify that the manually computed g1^f(s) matches the commitment
assert_eq!(g1_f_s, commitment, "Manually computed g1^f(s) should equal the commitment");
// Compute f(challenge)
let f_challenge = polynomial.evaluate(&challenge_point);
// Compute g1^f(challenge)
let g1_f_challenge = g1 * f_challenge;
// Print out the computed values
println!("f(challenge) = {:?}", f_challenge);
println!("g1^f(challenge) = {:?}", g1_f_challenge);
println!("verify: commitment - g_1^(-y) = {:?}", commitment - g1_f_challenge);
println!("=================\n");
// Verifier: Verify the proof
let g2_s = g2 * evaluation_point;
let result = verifier::verify::<Bls12_381>(proof, challenge_point, g2, g2_s);
assert!(result, "Verification should succeed");
}
#[test]
#[ignore]
fn test_kzg_system_prove_verify() {
// Set up the KZG system
let rng = &mut thread_rng();
let degree = 10;
// Use TestCRSGenerator
let generator = G1::rand(rng);
let point = F::rand(rng);
let crs_generator = TestCRSGenerator { generator, point };
let crs = crs_generator.generate(degree);
let g2 = G2::rand(rng);
let s = F::rand(rng);
let g2_s = g2 * s;
// Create a PoseidonConfig for the verifier challenger
let poseidon_config = PoseidonConfig::<F>::new(8, 57, 5, vec![vec![F::from(1u64); 3]; 3], vec![vec![F::from(0u64); 3]; 65], 2, 1);
let verifier_challenger = KZGVerifierChallenger::new(poseidon_config);
let system = KZGSystemImpl {
crs,
degree,
g2,
g2_s,
verifier_challenge: verifier_challenger,
};
// Generate a random polynomial
let polynomial = DensePolynomial::<F>::rand(degree, rng);
// Prove
let proof = system.prove(&polynomial);
// Verify
let result = system.verify(proof);
assert!(result, "Verification should succeed for a valid proof");
}
}