use lib_q_stark_baby_bear::BabyBear;
use lib_q_stark_commit::{
BatchOpeningRef,
ExtensionMmcs,
Mmcs,
};
use lib_q_stark_field::extension::BinomialExtensionField;
use lib_q_stark_field::{
Field,
PrimeCharacteristicRing,
};
use lib_q_stark_matrix::Dimensions;
use lib_q_stark_matrix::dense::RowMajorMatrix;
use lib_q_stark_merkle::MerkleTreeMmcs;
use lib_q_stark_shake128::Shake128Hash;
use lib_q_stark_symmetric::{
CompressionFunctionFromHasher,
SerializingHasher,
};
type Val = BabyBear;
type Challenge = BinomialExtensionField<Val, 4>;
type MyHash = SerializingHasher<Shake128Hash>;
type MyCompress = CompressionFunctionFromHasher<Shake128Hash, 2, 32>;
type ValMmcs = MerkleTreeMmcs<<Val as Field>::Packing, u8, MyHash, MyCompress, 32>;
type ChallengeMmcs = ExtensionMmcs<Val, Challenge, ValMmcs>;
fn mmcs() -> ChallengeMmcs {
let inner = ValMmcs::new(MyHash::new(Shake128Hash), MyCompress::new(Shake128Hash));
ChallengeMmcs::new(inner)
}
fn matrix(height: usize, width: usize) -> RowMajorMatrix<Challenge> {
RowMajorMatrix::new(
(0..height * width)
.map(|i| Challenge::from(Val::new(i as u32 + 1)))
.collect(),
width,
)
}
#[test]
fn honest_extension_opening_is_accepted_at_every_row() {
let mmcs = mmcs();
let height = 8;
let width = 2;
let (commitment, prover_data) = mmcs.commit_matrix(matrix(height, width));
let dims = [Dimensions { width, height }];
for index in 0..height {
let opening = mmcs.open_batch(index, &prover_data);
mmcs.verify_batch(&commitment, &dims, index, BatchOpeningRef::from(&opening))
.unwrap_or_else(|e| {
panic!("honest extension opening at index {index} rejected: {e:?}")
});
}
}
#[test]
fn tampered_extension_value_is_rejected() {
let mmcs = mmcs();
let height = 8;
let width = 2;
let (commitment, prover_data) = mmcs.commit_matrix(matrix(height, width));
let dims = [Dimensions { width, height }];
let index = 4;
let mut tampered = mmcs.open_batch(index, &prover_data);
tampered.opened_values[0][0] += Challenge::ONE;
let result = mmcs.verify_batch(&commitment, &dims, index, BatchOpeningRef::from(&tampered));
assert!(
result.is_err(),
"ExtensionMmcs::verify_batch accepted a tampered extension-field opened value"
);
}