Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update DB creation circuits for generic extraction (Part 2) #393

Open
wants to merge 21 commits into
base: generic-mpt-extraction
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7f9cb61
Update the circuits of cells tree, rows tree and block tree for gener…
silathdiir Oct 18, 2024
63b3b2d
Update tests for cells tree.
silathdiir Oct 22, 2024
c7291cf
Update tests for rows tree.
silathdiir Oct 23, 2024
d9f8825
Update tests of block tree.
silathdiir Oct 23, 2024
f59c5d7
Fix test.
silathdiir Oct 23, 2024
e651ab9
Add missing `value` for hash in rows tree.
silathdiir Oct 25, 2024
2717f98
Update verifiable-db/src/row_tree/public_inputs.rs
silathdiir Oct 30, 2024
a5ae612
Fix to ensure `extraction_proof.is_merge or rows_tree_proof.multiplie…
silathdiir Oct 30, 2024
ca0a971
Fix to use `split_and_accumulate`.
silathdiir Oct 30, 2024
2c94c12
Fix the test coverage for the all cells node circuits.
silathdiir Oct 30, 2024
6b791c9
Remove merge flag in rows tree public inputs.
silathdiir Oct 30, 2024
16bea45
Rename `assign_wires` to `assign`.
silathdiir Oct 30, 2024
1f70eb4
Fix to use `PublicInputs::sample`.
silathdiir Oct 30, 2024
21bb637
Fix to `p.partial`.
silathdiir Oct 30, 2024
32f4495
Merge remote-tracking branch 'origin/generic-mpt-extraction' into gen…
silathdiir Oct 30, 2024
31f7685
Delete the `ignore` comment for test `isolution`.
silathdiir Oct 30, 2024
e37e98e
Fix to use `split_and_accumulate`.
silathdiir Oct 30, 2024
322280b
Merge remote-tracking branch 'origin/generic-mpt-extraction' into gen…
silathdiir Nov 7, 2024
9c4ddb7
Rename `Row` to `SecondaryIndexCell`.
silathdiir Nov 7, 2024
4a342a0
Merge remote-tracking branch 'origin/generic-mpt-extraction' into gen…
silathdiir Nov 18, 2024
a139673
Merge remote-tracking branch 'origin/generic-mpt-extraction' into gen…
silathdiir Dec 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mp2-common/src/mpt_sequential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ where
/// Assign the nodes to the wires. The reason we have the output wires
/// as well is due to the keccak circuit that requires some special assignement
/// from the raw vectors.
pub fn assign_wires<F: RichField + Extendable<D>, const D: usize>(
pub fn assign<F: RichField + Extendable<D>, const D: usize>(
&self,
p: &mut PartialWitness<F>,
inputs: &InputWires<DEPTH, NODE_LEN>,
Expand Down Expand Up @@ -497,7 +497,7 @@ mod test {
}

fn prove(&self, pw: &mut PartialWitness<F>, wires: &Self::Wires) {
self.c.assign_wires(pw, &wires.0, &wires.1).unwrap();
self.c.assign(pw, &wires.0, &wires.1).unwrap();
wires.2.assign(
pw,
&create_array(|i| F::from_canonical_u8(self.exp_root[i])),
Expand Down
3 changes: 3 additions & 0 deletions mp2-common/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub type H = <C as GenericConfig<D>>::Hasher;
pub type P = <H as AlgebraicHasher<GoldilocksField>>::AlgebraicPermutation;
pub type HashPermutation = <H as Hasher<F>>::Permutation;

/// The result of hash to integer has 4 Uint32 (128 bits).
pub const HASH_TO_INT_LEN: usize = 4;

/// The flattened length of Poseidon hash, each original field is splitted from an
/// Uint64 into two Uint32.
pub const FLATTEN_POSEIDON_LEN: usize = NUM_HASH_OUT_ELTS * 2;
Expand Down
8 changes: 8 additions & 0 deletions mp2-common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::VerifierCircuitData;
use plonky2::plonk::config::{GenericConfig, GenericHashOut, Hasher};
use plonky2_crypto::u32::arithmetic_u32::U32Target;
use plonky2_ecdsa::gadgets::biguint::BigUintTarget;

use plonky2_ecgfp5::gadgets::{base_field::QuinticExtensionTarget, curve::CurveTarget};
use sha3::Digest;
Expand Down Expand Up @@ -377,6 +378,7 @@ impl<F: RichField> ToFields<F> for HashOut<F> {
self.elements.to_vec()
}
}

pub trait Fieldable<F: RichField> {
fn to_field(&self) -> F;
}
Expand Down Expand Up @@ -444,6 +446,12 @@ impl ToTargets for &[Target] {
}
}

impl ToTargets for BigUintTarget {
fn to_targets(&self) -> Vec<Target> {
self.limbs.iter().map(|u| u.0).collect()
}
}

pub trait TargetsConnector {
fn connect_targets<T: ToTargets>(&mut self, e1: T, e2: T);
fn is_equal_targets<T: ToTargets>(&mut self, e1: T, e2: T) -> BoolTarget;
Expand Down
10 changes: 8 additions & 2 deletions mp2-v1/tests/common/celltree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use mp2_v1::{
row::{CellCollection, Row, RowPayload, RowTreeKey},
},
};
use plonky2::plonk::config::GenericHashOut;
use plonky2::{field::types::Sample, hash::hash_types::HashOut, plonk::config::GenericHashOut};
use ryhope::storage::{
updatetree::{Next, UpdateTree},
RoEpochKvStorage,
Expand Down Expand Up @@ -69,6 +69,8 @@ impl TestContext {
cell.identifier(),
cell.value(),
column.multiplier,
// TODO:
HashOut::rand(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to check it now because it will be invalid otherwise ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's wrong comment, the mpt_metadata parameter is deleted in PR #399.

));
self.b.bench("indexing::cell_tree::leaf", || {
api::generate_proof(self.params(), inputs)
Expand All @@ -93,6 +95,8 @@ impl TestContext {
cell.identifier(),
cell.value(),
column.multiplier,
// TODO:
HashOut::rand(),
left_proof.clone(),
));
debug!(
Expand Down Expand Up @@ -147,6 +151,8 @@ impl TestContext {
cell.identifier(),
cell.value(),
column.multiplier,
// TODO:
HashOut::rand(),
[left_proof, right_proof],
));

Expand All @@ -168,7 +174,7 @@ impl TestContext {
"[+] [+] Merkle SLOT identifier {:?} -> value {} value.digest() = {:?}",
cell.identifier(),
cell.value(),
pi.individual_digest_point()
pi.individual_values_digest_point()
);

self.storage
Expand Down
2 changes: 1 addition & 1 deletion mp2-v1/tests/common/index_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl TestContext {
// TODO: Fix the rows digest in rows tree according to values extraction update.
// <https://github.com/Lagrange-Labs/mapreduce-plonky2/pull/385>
assert_eq!(
row_pi.rows_digest_field(),
row_pi.individual_digest_point(),
ext_pi.value_point(),
"values extracted vs value in db don't match (left row, right mpt (block {})",
node.value.0.to::<u64>()
Expand Down
4 changes: 2 additions & 2 deletions mp2-v1/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn cell_tree_proof_to_hash(proof: &[u8]) -> HashOutput {
.proof
.public_inputs;
verifiable_db::cells_tree::PublicInputs::from_slice(&root_pi)
.root_hash_hashout()
.node_hash()
.to_bytes()
.try_into()
.unwrap()
Expand All @@ -63,7 +63,7 @@ fn row_tree_proof_to_hash(proof: &[u8]) -> HashOutput {
.proof
.public_inputs;
verifiable_db::row_tree::PublicInputs::from_slice(&root_pi)
.root_hash_hashout()
.root_hash()
.to_bytes()
.try_into()
.unwrap()
Expand Down
20 changes: 16 additions & 4 deletions mp2-v1/tests/common/rowtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mp2_v1::{
row::{RowPayload, RowTree, RowTreeKey, ToNonce},
},
};
use plonky2::plonk::config::GenericHashOut;
use plonky2::{field::types::Sample, hash::hash_types::HashOut, plonk::config::GenericHashOut};
use ryhope::{
storage::{
pgsql::PgsqlStorage,
Expand Down Expand Up @@ -132,8 +132,8 @@ impl TestContext {
let pis = cells_tree::PublicInputs::from_slice(&pvk.proof().public_inputs);
debug!(
" Cell Root SPLIT digest: multiplier {:?}, individual {:?}",
pis.multiplier_digest_point(),
pis.individual_digest_point()
pis.multiplier_values_digest_point(),
pis.individual_values_digest_point()
);
}

Expand All @@ -151,6 +151,10 @@ impl TestContext {
id,
value,
multiplier,
// TODO: mpt_metadata
HashOut::rand(),
// TODO: row_unique_data
HashOut::rand(),
cell_tree_proof,
)
.unwrap(),
Expand Down Expand Up @@ -187,6 +191,10 @@ impl TestContext {
value,
multiplier,
context.left.is_some(),
// TODO: mpt_metadata
HashOut::rand(),
// TODO: row_unique_data
HashOut::rand(),
child_proof,
cell_tree_proof,
)
Expand Down Expand Up @@ -229,6 +237,10 @@ impl TestContext {
id,
value,
multiplier,
// TODO: mpt_metadata
HashOut::rand(),
// TODO: row_unique_data
HashOut::rand(),
left_proof,
right_proof,
cell_tree_proof,
Expand Down Expand Up @@ -277,7 +289,7 @@ impl TestContext {
let pi = verifiable_db::row_tree::PublicInputs::from_slice(&pproof.proof().public_inputs);
debug!(
"[--] FINAL MERKLE DIGEST VALUE --> {:?} ",
pi.rows_digest_field()
pi.individual_digest_point()
);
if root_proof_key.primary != primary {
debug!("[--] NO UPDATES on row this turn? row.root().primary = {} vs new primary proving step {}",root_proof_key.primary,primary);
Expand Down
1 change: 1 addition & 0 deletions verifiable-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
num.workspace = true
alloy.workspace = true
anyhow.workspace = true
bincode.workspace = true
Expand Down
52 changes: 33 additions & 19 deletions verifiable-db/src/block_tree/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ mod tests {
*,
};
use crate::{
block_tree::leaf::tests::{compute_expected_hash, compute_expected_set_digest},
block_tree::{
compute_final_digest,
leaf::tests::{compute_expected_hash, compute_expected_set_digest},
},
extraction, row_tree,
};
use mp2_common::{
Expand All @@ -288,13 +291,12 @@ mod tests {
iop::target::Target,
plonk::config::Hasher,
};
use plonky2_ecgfp5::curve::curve::Point;
use rand::{rngs::ThreadRng, thread_rng, Rng};
use recursion_framework::framework_testing::TestingRecursiveCircuits;
use std::iter;

const EXTRACTION_IO_LEN: usize = extraction::test::PublicInputs::<F>::TOTAL_LEN;
const ROWS_TREE_IO_LEN: usize = row_tree::PublicInputs::<F>::TOTAL_LEN;
const ROWS_TREE_IO_LEN: usize = row_tree::PublicInputs::<F>::total_len();

struct TestBuilder<E>
where
Expand Down Expand Up @@ -346,10 +348,9 @@ mod tests {
fn generate_rows_tree_proof(
&self,
rng: &mut ThreadRng,
row_digest: &[F],
is_merge_case: bool,
) -> Result<ProofWithVK> {
let pi = random_rows_tree_pi(rng, row_digest, is_merge_case);
let pi = random_rows_tree_pi(rng, is_merge_case);

let proof = self
.rows_tree_set
Expand All @@ -358,20 +359,23 @@ mod tests {

Ok(ProofWithVK::from((proof[0].clone(), vk)))
}

fn generate_leaf_proof(
&self,
rng: &mut ThreadRng,
block_id: F,
block_number: U256,
) -> Result<ProofWithVK> {
let row_digest = Point::sample(rng).to_weierstrass().to_fields();
let rows_tree_proof = self.generate_rows_tree_proof(rng, true)?;
let rows_tree_pi =
row_tree::PublicInputs::from_slice(&rows_tree_proof.proof.public_inputs);
let final_digest = compute_final_digest(true, &rows_tree_pi)
.to_weierstrass()
.to_fields();
let extraction_proof =
self.generate_extraction_proof(rng, block_number, &row_digest, true)?;
let rows_tree_proof = self.generate_rows_tree_proof(rng, &row_digest, true)?;
self.generate_extraction_proof(rng, block_number, &final_digest, true)?;
let extraction_pi =
extraction::test::PublicInputs::from_slice(&extraction_proof.proof.public_inputs);
let rows_tree_pi =
row_tree::PublicInputs::from_slice(&rows_tree_proof.proof.public_inputs);

let input = CircuitInput::new_leaf(
block_id.to_canonical_u64(),
Expand Down Expand Up @@ -438,8 +442,12 @@ mod tests {
}
// Check new node digest
{
let exp_digest =
compute_expected_set_digest(block_id, block_number.to_vec(), rows_tree_pi);
let exp_digest = compute_expected_set_digest(
true,
block_id,
block_number.to_vec(),
rows_tree_pi,
);
assert_eq!(pi.new_value_set_digest_point(), exp_digest.to_weierstrass());
}

Expand All @@ -458,14 +466,16 @@ mod tests {
left_child: HashOut<F>,
right_child: HashOut<F>,
) -> Result<ProofWithVK> {
let row_digest = Point::sample(rng).to_weierstrass().to_fields();
let rows_tree_proof = self.generate_rows_tree_proof(rng, false)?;
let rows_tree_pi =
row_tree::PublicInputs::from_slice(&rows_tree_proof.proof.public_inputs);
let final_digest = compute_final_digest(false, &rows_tree_pi)
.to_weierstrass()
.to_fields();
let extraction_proof =
self.generate_extraction_proof(rng, block_number, &row_digest, false)?;
let rows_tree_proof = self.generate_rows_tree_proof(rng, &row_digest, false)?;
self.generate_extraction_proof(rng, block_number, &final_digest, false)?;
let extraction_pi =
extraction::test::PublicInputs::from_slice(&extraction_proof.proof.public_inputs);
let rows_tree_pi =
row_tree::PublicInputs::from_slice(&rows_tree_proof.proof.public_inputs);

let old_rows_tree_hash =
HashOut::from_vec(random_vector::<u32>(NUM_HASH_OUT_ELTS).to_fields());
Expand Down Expand Up @@ -553,8 +563,12 @@ mod tests {
}
// Check new node digest
{
let exp_digest =
compute_expected_set_digest(block_id, block_number.to_vec(), rows_tree_pi);
let exp_digest = compute_expected_set_digest(
false,
block_id,
block_number.to_vec(),
rows_tree_pi,
);
assert_eq!(pi.new_value_set_digest_point(), exp_digest.to_weierstrass());
}

Expand Down
Loading
Loading