@@ -17,12 +17,15 @@ use crate::{
17
17
BlobRef , Bytes48Ref , Cell , CellID , CellRef , KZGCommitment , KZGProof ,
18
18
} ;
19
19
20
+ /// Errors that can occur while calling a method in the Prover API
20
21
#[ derive( Debug ) ]
21
22
pub enum ProverError {
22
23
Serialization ( SerializationError ) ,
23
24
RecoveryFailure ( VerifierError ) ,
24
25
}
25
26
27
+ /// Context object that is used to call functions in the prover API.
28
+ /// This includes, computing the commitments, proofs and cells.
26
29
pub struct ProverContext {
27
30
fk20 : FK20 ,
28
31
// TODO: We don't need the commit key, since we use FK20 to compute the proofs
@@ -45,8 +48,16 @@ pub struct ProverContext {
45
48
impl ProverContext {
46
49
pub fn new ( ) -> Self {
47
50
let ( commit_key, _) = create_eth_commit_opening_keys ( ) ;
51
+ // The number of points that we will make an opening proof for,
52
+ // ie a proof will attest to the value of the polynomial at these points.
48
53
let point_set_size = FIELD_ELEMENTS_PER_CELL ;
54
+
55
+ // The number of points that we will be making proofs for.
56
+ //
57
+ // Note: it is easy to calculate the number of proofs that we need to make
58
+ // by doing number_of_points_to_open / point_set_size.
49
59
let number_of_points_to_open = FIELD_ELEMENTS_PER_EXT_BLOB ;
60
+
50
61
let fk20 = FK20 :: new ( & commit_key, point_set_size, number_of_points_to_open) ;
51
62
52
63
let poly_domain = Domain :: new ( FIELD_ELEMENTS_PER_BLOB ) ;
@@ -63,27 +74,43 @@ impl ProverContext {
63
74
}
64
75
}
65
76
77
+ /// Computes the KZG commitment to the polynomial represented by the blob.
66
78
pub fn blob_to_kzg_commitment ( & self , blob : BlobRef ) -> Result < KZGCommitment , ProverError > {
79
+ // Deserialize the blob into scalars. The blob is in lagrange form.
67
80
let mut scalars =
68
81
serialization:: deserialize_blob_to_scalars ( blob) . map_err ( ProverError :: Serialization ) ?;
82
+
83
+ // Reverse the order of the scalars, so that they are in normal order.
84
+ // ie not in bit-reversed order.
69
85
reverse_bit_order ( & mut scalars) ;
70
86
87
+ // Commit to the polynomial.
71
88
let commitment: G1Point = self . commit_key_lagrange . commit_g1 ( & scalars) . into ( ) ;
89
+
90
+ // Serialize the commitment.
72
91
Ok ( serialize_g1_compressed ( & commitment) )
73
92
}
74
93
94
+ /// Computes the cells and the KZG proofs for the given blob.
75
95
pub fn compute_cells_and_kzg_proofs (
76
96
& self ,
77
97
blob : BlobRef ,
78
98
) -> Result < ( [ Cell ; CELLS_PER_EXT_BLOB ] , [ KZGProof ; CELLS_PER_EXT_BLOB ] ) , ProverError > {
79
- // Deserialize the blob into scalars ( lagrange form)
99
+ // Deserialize the blob into scalars. The blob is in lagrange form.
80
100
let mut scalars =
81
101
serialization:: deserialize_blob_to_scalars ( blob) . map_err ( ProverError :: Serialization ) ?;
102
+
103
+ // Reverse the order of the scalars, so that they are in normal order.
104
+ // ie not in bit-reversed order.
82
105
reverse_bit_order ( & mut scalars) ;
83
106
107
+ // Convert the polynomial from lagrange to monomial form.
84
108
let poly_coeff = self . poly_domain . ifft_scalars ( scalars) ;
109
+
110
+ // Compute the proofs and the evaluations of the polynomial.
85
111
let ( proofs, evaluations) = self . fk20 . compute_multi_opening_proofs ( poly_coeff) ;
86
112
113
+ // Serialize the evaluations into `Cell`s.
87
114
let cells = evaluations
88
115
. iter ( )
89
116
. map ( |eval| serialization:: serialize_scalars_to_cell ( eval) )
@@ -92,6 +119,7 @@ impl ProverContext {
92
119
. try_into ( )
93
120
. expect ( & format ! ( "expected {} number of cells" , CELLS_PER_EXT_BLOB ) ) ;
94
121
122
+ // Serialize the proofs into `KZGProof`s.
95
123
let proofs: Vec < _ > = proofs. iter ( ) . map ( serialize_g1_compressed) . collect ( ) ;
96
124
let proofs: [ KZGProof ; CELLS_PER_EXT_BLOB ] = proofs
97
125
. try_into ( )
@@ -100,15 +128,23 @@ impl ProverContext {
100
128
Ok ( ( cells, proofs) )
101
129
}
102
130
131
+ #[ deprecated( note = "This function is deprecated, use `compute_cells_and_kzg_proofs` instead" ) ]
103
132
pub fn compute_cells ( & self , blob : BlobRef ) -> Result < [ Cell ; CELLS_PER_EXT_BLOB ] , ProverError > {
104
- // Deserialize the blob into scalars ( lagrange form)
133
+ // Deserialize the blob into scalars. The blob is in lagrange form.
105
134
let mut scalars =
106
135
serialization:: deserialize_blob_to_scalars ( blob) . map_err ( ProverError :: Serialization ) ?;
136
+
137
+ // Reverse the order of the scalars, so that they are in normal order.
138
+ // ie not in bit-reversed order.
107
139
reverse_bit_order ( & mut scalars) ;
108
140
141
+ // Convert the polynomial from lagrange to monomial form.
109
142
let poly_coeff = self . poly_domain . ifft_scalars ( scalars) ;
143
+
144
+ // Compute the evaluations of the polynomial at the points that we need to make proofs for.
110
145
let evaluations = self . fk20 . compute_evaluation_sets ( poly_coeff) ;
111
146
147
+ // Serialize the evaluations into cells.
112
148
let cells = evaluations
113
149
. iter ( )
114
150
. map ( |eval| serialization:: serialize_scalars_to_cell ( eval) )
@@ -120,24 +156,28 @@ impl ProverContext {
120
156
Ok ( cells)
121
157
}
122
158
159
+ /// Recovers the cells and computes the KZG proofs, given a subset of cells.
123
160
pub fn recover_cells_and_proofs (
124
161
& self ,
125
162
cell_ids : Vec < CellID > ,
126
163
cells : Vec < CellRef > ,
127
164
_proofs : Vec < Bytes48Ref > ,
128
165
) -> Result < ( [ Cell ; CELLS_PER_EXT_BLOB ] , [ KZGProof ; CELLS_PER_EXT_BLOB ] ) , ProverError > {
166
+ // Use erasure decoding to recover all of the cells.
167
+ // These will be in serialized form
129
168
let cells = self
130
169
. verifier_context
131
170
. recover_all_cells ( cell_ids, cells)
132
171
. map_err ( |err| ProverError :: RecoveryFailure ( err) ) ?;
133
172
134
- // Concatenate the cells together
173
+ // Concatenate the cells together and extract the blob.
135
174
let extension_blob = cells. into_iter ( ) . flatten ( ) . collect :: < Vec < _ > > ( ) ;
136
175
137
- // We do not need the extension blob, only the blob
138
- // which is the first BYTES_PER_BLOB bytes.
176
+ // To compute the proofs, we need the Blob.
177
+ // The blob will be the first BYTES_PER_BLOB bytes from the extension blob .
139
178
let blob = & extension_blob[ ..BYTES_PER_BLOB ] ;
140
179
180
+ // Compute the cells and the proofs for the given blob.
141
181
self . compute_cells_and_kzg_proofs ( blob)
142
182
}
143
183
}
0 commit comments