use super::Matrix;
use air::proof::Queries;
use crypto::{ElementHasher, MerkleTree};
use math::FieldElement;
use utils::collections::Vec;
pub struct ConstraintCommitment<E: FieldElement, H: ElementHasher<BaseField = E::BaseField>> {
evaluations: Matrix<E>,
commitment: MerkleTree<H>,
}
impl<E: FieldElement, H: ElementHasher<BaseField = E::BaseField>> ConstraintCommitment<E, H> {
pub fn new(evaluations: Matrix<E>, commitment: MerkleTree<H>) -> ConstraintCommitment<E, H> {
assert_eq!(
evaluations.num_rows(),
commitment.leaves().len(),
"number of rows in constraint evaluation matrix must be the same as number of leaves in constraint commitment"
);
ConstraintCommitment {
evaluations,
commitment,
}
}
pub fn root(&self) -> H::Digest {
*self.commitment.root()
}
#[allow(unused)]
pub fn tree_depth(&self) -> usize {
self.commitment.depth()
}
pub fn query(self, positions: &[usize]) -> Queries {
let merkle_proof = self
.commitment
.prove_batch(positions)
.expect("failed to generate a Merkle proof for constraint queries");
let mut evaluations = Vec::new();
for &position in positions {
let mut row = vec![E::ZERO; self.evaluations.num_cols()];
self.evaluations.read_row_into(position, &mut row);
evaluations.push(row);
}
Queries::new(merkle_proof, evaluations)
}
}