-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathindexer.rs
More file actions
196 lines (169 loc) · 7.17 KB
/
indexer.rs
File metadata and controls
196 lines (169 loc) · 7.17 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
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
#![allow(non_snake_case)]
use crate::ahp::{
constraint_systems::{arithmetize_matrix, MatrixArithmetization},
AHPForR1CS, Error, LabeledPolynomial,
};
use crate::Vec;
use ark_ff::PrimeField;
use ark_poly::{EvaluationDomain, GeneralEvaluationDomain};
use ark_relations::r1cs::{ConstraintSynthesizer, ConstraintSystem, SynthesisError, SynthesisMode};
use derivative::Derivative;
use crate::ahp::constraint_systems::{
balance_matrices, make_matrices_square_for_indexer, num_non_zero,
};
use core::marker::PhantomData;
/// Information about the index, including the field of definition, the number of
/// variables, the number of constraints, and the maximum number of non-zero
/// entries in any of the constraint matrices.
#[derive(Derivative)]
#[derivative(Clone(bound = ""), Copy(bound = ""))]
pub struct IndexInfo<F> {
/// The total number of variables in the constraint system.
pub num_variables: usize,
/// The number of constraints.
pub num_constraints: usize,
/// The maximum number of non-zero entries in any constraint matrix.
pub num_non_zero: usize,
#[doc(hidden)]
pub f: PhantomData<F>,
}
impl<F: PrimeField> ark_ff::ToBytes for IndexInfo<F> {
fn write<W: ark_std::io::Write>(&self, mut w: W) -> ark_std::io::Result<()> {
(self.num_variables as u64).write(&mut w)?;
(self.num_constraints as u64).write(&mut w)?;
(self.num_non_zero as u64).write(&mut w)
}
}
impl<F: PrimeField> IndexInfo<F> {
/// The maximum degree of polynomial required to represent this index in the
/// the AHP.
pub fn max_degree(&self) -> usize {
AHPForR1CS::<F>::max_degree(self.num_constraints, self.num_variables, self.num_non_zero)
.unwrap()
}
}
/// Represents a matrix.
pub type Matrix<F> = Vec<Vec<(F, usize)>>;
#[derive(Derivative)]
#[derivative(Clone(bound = "F: PrimeField"))]
/// The indexed version of the constraint system.
/// This struct contains three kinds of objects:
/// 1) `index_info` is information about the index, such as the size of the
/// public input
/// 2) `{a,b,c}` are the matrices defining the R1CS instance
/// 3) `{a,b,c}_star_arith` are structs containing information about A^*, B^*, and C^*,
/// which are matrices defined as `M^*(i, j) = M(j, i) * u_H(j, j)`.
pub struct Index<F: PrimeField> {
/// Information about the index.
pub index_info: IndexInfo<F>,
/// The A matrix for the R1CS instance
pub a: Matrix<F>,
/// The B matrix for the R1CS instance
pub b: Matrix<F>,
/// The C matrix for the R1CS instance
pub c: Matrix<F>,
/// Arithmetization of the A* matrix.
pub a_star_arith: MatrixArithmetization<F>,
/// Arithmetization of the B* matrix.
pub b_star_arith: MatrixArithmetization<F>,
/// Arithmetization of the C* matrix.
pub c_star_arith: MatrixArithmetization<F>,
}
impl<'a, F: PrimeField> Index<F> {
/// The maximum degree required to represent polynomials of this index.
pub fn max_degree(&self) -> usize {
self.index_info.max_degree()
}
/// Iterate over the indexed polynomials.
pub fn iter(&self) -> impl Iterator<Item = &LabeledPolynomial<F>> {
vec![
&self.a_star_arith.row,
&self.a_star_arith.col,
&self.a_star_arith.val,
&self.a_star_arith.row_col,
&self.b_star_arith.row,
&self.b_star_arith.col,
&self.b_star_arith.val,
&self.b_star_arith.row_col,
&self.c_star_arith.row,
&self.c_star_arith.col,
&self.c_star_arith.val,
&self.c_star_arith.row_col,
]
.into_iter()
}
}
impl<F: PrimeField> AHPForR1CS<F> {
/// Generate the index for this constraint system.
pub fn index<C: ConstraintSynthesizer<F>>(c: C) -> Result<Index<F>, Error> {
let index_time = start_timer!(|| "AHP::Index");
let constraint_time = start_timer!(|| "Generating constraints");
let ics = ConstraintSystem::new_ref();
ics.set_mode(SynthesisMode::Setup);
c.generate_constraints(ics.clone())?;
end_timer!(constraint_time);
let padding_time = start_timer!(|| "Padding matrices to make them square");
end_timer!(padding_time);
let matrix_processing_time = start_timer!(|| "Processing matrices");
ics.outline_lcs();
make_matrices_square_for_indexer(ics.clone());
let matrices = ics.to_matrices().expect("should not be `None`");
let num_non_zero_val = num_non_zero::<F>(&matrices);
let (mut a, mut b, mut c) = (matrices.a, matrices.b, matrices.c);
balance_matrices(&mut a, &mut b);
end_timer!(matrix_processing_time);
let (num_formatted_input_variables, num_witness_variables, num_constraints, num_non_zero) = (
ics.num_instance_variables(),
ics.num_witness_variables(),
ics.num_constraints(),
num_non_zero_val,
);
let num_variables = num_formatted_input_variables + num_witness_variables;
if num_constraints != num_formatted_input_variables + num_witness_variables {
eprintln!(
"number of (formatted) input_variables: {}",
num_formatted_input_variables
);
eprintln!("number of witness_variables: {}", num_witness_variables);
eprintln!("number of num_constraints: {}", num_constraints);
eprintln!("number of num_non_zero: {}", num_non_zero);
return Err(Error::NonSquareMatrix);
}
if !Self::num_formatted_public_inputs_is_admissible(num_formatted_input_variables) {
return Err(Error::InvalidPublicInputLength);
}
let index_info = IndexInfo {
num_variables,
num_constraints,
num_non_zero,
f: PhantomData,
};
let domain_h = GeneralEvaluationDomain::new(num_constraints)
.ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
let domain_k = GeneralEvaluationDomain::new(num_non_zero)
.ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
let x_domain = GeneralEvaluationDomain::<F>::new(num_formatted_input_variables)
.ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
let b_domain = GeneralEvaluationDomain::<F>::new(3 * domain_k.size() - 3)
.ok_or(SynthesisError::PolynomialDegreeTooLarge)?;
let a_arithmetization_time = start_timer!(|| "Arithmetizing A");
let a_star_arith = arithmetize_matrix("a", &mut a, domain_k, domain_h, x_domain, b_domain);
end_timer!(a_arithmetization_time);
let b_arithmetization_time = start_timer!(|| "Arithmetizing B");
let b_star_arith = arithmetize_matrix("b", &mut b, domain_k, domain_h, x_domain, b_domain);
end_timer!(b_arithmetization_time);
let c_arithmetization_time = start_timer!(|| "Arithmetizing C");
let c_star_arith = arithmetize_matrix("c", &mut c, domain_k, domain_h, x_domain, b_domain);
end_timer!(c_arithmetization_time);
end_timer!(index_time);
Ok(Index {
index_info,
a,
b,
c,
a_star_arith,
b_star_arith,
c_star_arith,
})
}
}