Skip to content

Commit 6528914

Browse files
authored
chore(backend): implement more circuit op logic (#173)
* Add backend MerkleProof type * Add eval_not_contains * Remove print statement * Handle some edge cases * Add test * Add missing ? * Optimisation and stylistic changes * Code review
1 parent adad695 commit 6528914

File tree

10 files changed

+502
-48
lines changed

10 files changed

+502
-48
lines changed

src/backends/plonky2/circuits/common.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
use crate::backends::plonky2::basetypes::D;
44
use crate::backends::plonky2::mock::mainpod::Statement;
55
use crate::backends::plonky2::mock::mainpod::{Operation, OperationArg};
6+
use crate::backends::plonky2::primitives::merkletree::MerkleClaimAndProofTarget;
67
use crate::middleware::{
78
NativeOperation, NativePredicate, Params, Predicate, StatementArg, ToFields, Value,
8-
EMPTY_VALUE, F, HASH_SIZE, OPERATION_ARG_F_LEN, STATEMENT_ARG_F_LEN, VALUE_SIZE,
9+
EMPTY_VALUE, F, HASH_SIZE, OPERATION_ARG_F_LEN, OPERATION_AUX_F_LEN, STATEMENT_ARG_F_LEN,
10+
VALUE_SIZE,
911
};
1012
use anyhow::Result;
1113
use plonky2::field::extension::Extendable;
1214
use plonky2::field::types::{Field, PrimeField64};
13-
use plonky2::hash::hash_types::RichField;
15+
use plonky2::hash::hash_types::{HashOutTarget, RichField, NUM_HASH_OUT_ELTS};
1416
use plonky2::iop::target::{BoolTarget, Target};
1517
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
1618
use plonky2::plonk::circuit_builder::CircuitBuilder;
@@ -155,6 +157,7 @@ impl StatementTarget {
155157
pub struct OperationTarget {
156158
pub op_type: [Target; Params::operation_type_size()],
157159
pub args: Vec<[Target; OPERATION_ARG_F_LEN]>,
160+
pub aux: [Target; OPERATION_AUX_F_LEN],
158161
}
159162

160163
impl OperationTarget {
@@ -174,6 +177,7 @@ impl OperationTarget {
174177
{
175178
pw.set_target_arr(&self.args[i], &arg.to_fields(params))?;
176179
}
180+
pw.set_target_arr(&self.aux, &op.aux().to_fields(params))?;
177181
Ok(())
178182
}
179183

@@ -197,6 +201,56 @@ pub trait Flattenable {
197201
fn from_flattened(vs: &[Target]) -> Self;
198202
}
199203

204+
/// For the purpose of op verification, we need only look up the
205+
/// Merkle claim rather than the Merkle proof since it is verified
206+
/// elsewhere.
207+
pub struct MerkleClaimTarget {
208+
pub(crate) enabled: BoolTarget,
209+
pub(crate) root: HashOutTarget,
210+
pub(crate) key: ValueTarget,
211+
pub(crate) value: ValueTarget,
212+
pub(crate) existence: BoolTarget,
213+
}
214+
215+
impl From<MerkleClaimAndProofTarget> for MerkleClaimTarget {
216+
fn from(pf: MerkleClaimAndProofTarget) -> Self {
217+
Self {
218+
enabled: pf.enabled,
219+
root: pf.root,
220+
key: pf.key,
221+
value: pf.value,
222+
existence: pf.existence,
223+
}
224+
}
225+
}
226+
227+
impl Flattenable for MerkleClaimTarget {
228+
fn flatten(&self) -> Vec<Target> {
229+
[
230+
vec![self.enabled.target],
231+
self.root.elements.to_vec(),
232+
self.key.elements.to_vec(),
233+
self.value.elements.to_vec(),
234+
vec![self.existence.target],
235+
]
236+
.concat()
237+
}
238+
239+
fn from_flattened(vs: &[Target]) -> Self {
240+
Self {
241+
enabled: BoolTarget::new_unsafe(vs[0]),
242+
root: HashOutTarget::from_vec((&vs[1..1 + NUM_HASH_OUT_ELTS]).to_vec()),
243+
key: ValueTarget::from_slice(
244+
&vs[1 + NUM_HASH_OUT_ELTS..1 + NUM_HASH_OUT_ELTS + VALUE_SIZE],
245+
),
246+
value: ValueTarget::from_slice(
247+
&vs[1 + NUM_HASH_OUT_ELTS + VALUE_SIZE..1 + NUM_HASH_OUT_ELTS + 2 * VALUE_SIZE],
248+
),
249+
existence: BoolTarget::new_unsafe(vs[1 + NUM_HASH_OUT_ELTS + 2 * VALUE_SIZE]),
250+
}
251+
}
252+
}
253+
200254
impl Flattenable for StatementTarget {
201255
fn flatten(&self) -> Vec<Target> {
202256
self.predicate
@@ -290,6 +344,7 @@ impl CircuitBuilderPod<F, D> for CircuitBuilder<F, D> {
290344
args: (0..params.max_operation_args)
291345
.map(|_| self.add_virtual_target_arr())
292346
.collect(),
347+
aux: self.add_virtual_target_arr(),
293348
}
294349
}
295350

0 commit comments

Comments
 (0)