|
| 1 | +use crate::{ |
| 2 | + base::database::{ |
| 3 | + owned_table_utility::{bigint, owned_table}, |
| 4 | + CommitmentAccessor, OwnedTableTestAccessor, |
| 5 | + }, |
| 6 | + proof_primitive::hyperkzg::{self, HyperKZGCommitment, HyperKZGCommitmentEvaluationProof}, |
| 7 | + sql::{ |
| 8 | + evm_proof_plan::EVMProofPlan, |
| 9 | + parse::QueryExpr, |
| 10 | + proof::{ProofPlan, VerifiableQueryResult}, |
| 11 | + proof_plans::DynProofPlan, |
| 12 | + }, |
| 13 | +}; |
| 14 | +use ark_ec::{AffineRepr, CurveGroup}; |
| 15 | +use ark_ff::PrimeField; |
| 16 | +use itertools::Itertools; |
| 17 | + |
| 18 | +fn evm_verifier_with_extra_args( |
| 19 | + plan: &DynProofPlan, |
| 20 | + verifiable_result: &VerifiableQueryResult<HyperKZGCommitmentEvaluationProof>, |
| 21 | + accessor: &impl CommitmentAccessor<HyperKZGCommitment>, |
| 22 | + extra_args: &[&'static str], |
| 23 | +) -> bool { |
| 24 | + let commitments = plan |
| 25 | + .get_column_references() |
| 26 | + .into_iter() |
| 27 | + .map(|c| accessor.get_commitment(c)) |
| 28 | + .flat_map(|c| { |
| 29 | + c.commitment |
| 30 | + .into_affine() |
| 31 | + .xy() |
| 32 | + .map_or(["0".to_string(), "0".to_string()], |(x, y)| { |
| 33 | + [x.into_bigint().to_string(), y.into_bigint().to_string()] |
| 34 | + }) |
| 35 | + }) |
| 36 | + .join(","); |
| 37 | + let table_lengths = plan |
| 38 | + .get_table_references() |
| 39 | + .into_iter() |
| 40 | + .map(|t| accessor.get_length(&t).to_string()) |
| 41 | + .join(","); |
| 42 | + |
| 43 | + let bincode_options = bincode::config::standard() |
| 44 | + .with_fixed_int_encoding() |
| 45 | + .with_big_endian(); |
| 46 | + let query_bytes = |
| 47 | + bincode::serde::encode_to_vec(EVMProofPlan::new(plan.clone()), bincode_options).unwrap(); |
| 48 | + let proof_bytes = |
| 49 | + bincode::serde::encode_to_vec(&verifiable_result.proof, bincode_options).unwrap(); |
| 50 | + let result_bytes = |
| 51 | + bincode::serde::encode_to_vec(&verifiable_result.result, bincode_options).unwrap(); |
| 52 | + |
| 53 | + std::process::Command::new("../../solidity/scripts/pre_forge.sh") |
| 54 | + .arg("script") |
| 55 | + .arg("-vvvvv") |
| 56 | + .args(extra_args) |
| 57 | + .args(["--tc", "VerifierTest"]) |
| 58 | + .args(["--sig", "verify(bytes,bytes,bytes,uint256[],uint256[])"]) |
| 59 | + .arg("./test/verifier/Verifier.t.post.sol") |
| 60 | + .args([ |
| 61 | + dbg!(hex::encode(&result_bytes)), |
| 62 | + dbg!(hex::encode(&query_bytes)), |
| 63 | + dbg!(hex::encode(&proof_bytes)), |
| 64 | + ]) |
| 65 | + .arg(dbg!(format!("[{table_lengths}]"))) |
| 66 | + .arg(dbg!(format!("[{commitments}]"))) |
| 67 | + .output() |
| 68 | + .unwrap() |
| 69 | + .status |
| 70 | + .success() |
| 71 | +} |
| 72 | +fn evm_verifier_all( |
| 73 | + plan: &DynProofPlan, |
| 74 | + verifiable_result: &VerifiableQueryResult<HyperKZGCommitmentEvaluationProof>, |
| 75 | + accessor: &impl CommitmentAccessor<HyperKZGCommitment>, |
| 76 | +) -> bool { |
| 77 | + evm_verifier_with_extra_args(plan, verifiable_result, accessor, &[]) |
| 78 | + && evm_verifier_with_extra_args(plan, verifiable_result, accessor, &["--via-ir"]) |
| 79 | + && evm_verifier_with_extra_args(plan, verifiable_result, accessor, &["--optimize"]) |
| 80 | + && evm_verifier_with_extra_args( |
| 81 | + plan, |
| 82 | + verifiable_result, |
| 83 | + accessor, |
| 84 | + &["--optimize", "--via-ir"], |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +#[ignore = "This test requires the forge binary to be present"] |
| 89 | +#[test] |
| 90 | +fn we_can_verify_a_simple_filter_using_the_evm() { |
| 91 | + let (ps, vk) = hyperkzg::load_small_setup_for_testing(); |
| 92 | + |
| 93 | + let accessor = OwnedTableTestAccessor::<HyperKZGCommitmentEvaluationProof>::new_from_table( |
| 94 | + "namespace.table".parse().unwrap(), |
| 95 | + owned_table([ |
| 96 | + bigint("a", [5, 3, 2, 5, 3, 2]), |
| 97 | + bigint("b", [0, 1, 2, 3, 4, 5]), |
| 98 | + ]), |
| 99 | + 0, |
| 100 | + &ps[..], |
| 101 | + ); |
| 102 | + let query = QueryExpr::try_new( |
| 103 | + "SELECT b FROM table WHERE a = 5".parse().unwrap(), |
| 104 | + "namespace".into(), |
| 105 | + &accessor, |
| 106 | + ) |
| 107 | + .unwrap(); |
| 108 | + let plan = query.proof_expr(); |
| 109 | + |
| 110 | + let verifiable_result = VerifiableQueryResult::<HyperKZGCommitmentEvaluationProof>::new( |
| 111 | + &EVMProofPlan::new(plan.clone()), |
| 112 | + &accessor, |
| 113 | + &&ps[..], |
| 114 | + &[], |
| 115 | + ) |
| 116 | + .unwrap(); |
| 117 | + |
| 118 | + verifiable_result |
| 119 | + .clone() |
| 120 | + .verify(&EVMProofPlan::new(plan.clone()), &accessor, &&vk, &[]) |
| 121 | + .unwrap(); |
| 122 | + |
| 123 | + assert!(evm_verifier_all(plan, &verifiable_result, &accessor)); |
| 124 | +} |
| 125 | + |
| 126 | +#[ignore = "This test requires the forge binary to be present"] |
| 127 | +#[test] |
| 128 | +fn we_can_verify_a_simple_filter_with_negative_literal_using_the_evm() { |
| 129 | + let (ps, vk) = hyperkzg::load_small_setup_for_testing(); |
| 130 | + |
| 131 | + let accessor = OwnedTableTestAccessor::<HyperKZGCommitmentEvaluationProof>::new_from_table( |
| 132 | + "namespace.table".parse().unwrap(), |
| 133 | + owned_table([ |
| 134 | + bigint("a", [5, 3, -2, 5, 3, -2]), |
| 135 | + bigint("b", [0, 1, 2, 3, 4, 5]), |
| 136 | + ]), |
| 137 | + 0, |
| 138 | + &ps[..], |
| 139 | + ); |
| 140 | + let query = QueryExpr::try_new( |
| 141 | + "SELECT b FROM table WHERE a = -2".parse().unwrap(), |
| 142 | + "namespace".into(), |
| 143 | + &accessor, |
| 144 | + ) |
| 145 | + .unwrap(); |
| 146 | + let plan = query.proof_expr(); |
| 147 | + let verifiable_result = VerifiableQueryResult::<HyperKZGCommitmentEvaluationProof>::new( |
| 148 | + &EVMProofPlan::new(plan.clone()), |
| 149 | + &accessor, |
| 150 | + &&ps[..], |
| 151 | + &[], |
| 152 | + ) |
| 153 | + .unwrap(); |
| 154 | + |
| 155 | + assert!(evm_verifier_all(plan, &verifiable_result, &accessor)); |
| 156 | + |
| 157 | + verifiable_result |
| 158 | + .clone() |
| 159 | + .verify(&EVMProofPlan::new(plan.clone()), &accessor, &&vk, &[]) |
| 160 | + .unwrap(); |
| 161 | +} |
| 162 | + |
| 163 | +#[ignore = "This test requires the forge binary to be present"] |
| 164 | +#[test] |
| 165 | +fn we_can_verify_a_complex_filter_using_the_evm() { |
| 166 | + let (ps, vk) = hyperkzg::load_small_setup_for_testing(); |
| 167 | + |
| 168 | + let accessor = OwnedTableTestAccessor::<HyperKZGCommitmentEvaluationProof>::new_from_table( |
| 169 | + "namespace.table".parse().unwrap(), |
| 170 | + owned_table([ |
| 171 | + bigint("a", [5, 3, 2, 5, 3, 2, 102, 104, 107, 108]), |
| 172 | + bigint("b", [0, 1, 2, 3, 4, 5, 33, 44, 55, 6]), |
| 173 | + bigint("c", [6, 7, 8, 9, 10, 11, 14, 15, 73, 23]), |
| 174 | + bigint("d", [5, 7, 2, 5, 4, 1, 12, 22, 22, 22]), |
| 175 | + ]), |
| 176 | + 0, |
| 177 | + &ps[..], |
| 178 | + ); |
| 179 | + let query = QueryExpr::try_new( |
| 180 | + "SELECT b,c FROM table WHERE a = d".parse().unwrap(), |
| 181 | + "namespace".into(), |
| 182 | + &accessor, |
| 183 | + ) |
| 184 | + .unwrap(); |
| 185 | + let plan = query.proof_expr(); |
| 186 | + let verifiable_result = VerifiableQueryResult::<HyperKZGCommitmentEvaluationProof>::new( |
| 187 | + &EVMProofPlan::new(plan.clone()), |
| 188 | + &accessor, |
| 189 | + &&ps[..], |
| 190 | + &[], |
| 191 | + ) |
| 192 | + .unwrap(); |
| 193 | + |
| 194 | + assert!(evm_verifier_all(plan, &verifiable_result, &accessor)); |
| 195 | + |
| 196 | + verifiable_result |
| 197 | + .clone() |
| 198 | + .verify(&EVMProofPlan::new(plan.clone()), &accessor, &&vk, &[]) |
| 199 | + .unwrap(); |
| 200 | +} |
0 commit comments