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

Optimized transcript checking for covering ROMs #178

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions circ_hc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub trait Node<Op>: Sized + Clone + PartialEq + Eq + PartialOrd + Ord + Hash {
fn op(&self) -> &Op;
/// Get the children of this node.
fn cs(&self) -> &[Self];
/// Get the operator and children of this node.
fn parts(&self) -> (&Op, &[Self]) {
(self.op(), self.cs())
}
/// Get a token that does not retain this node during GC.
fn downgrade(&self) -> Self::Weak;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ZoKrates/pf/mem/arr_of_str_of_arr.zok
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct Pt {
field[INNER_LEN] x
field[INNER_LEN] y
}
const Pt [LEN] array = [Pt {x: [0; INNER_LEN], y: [5; INNER_LEN]}, ...[Pt {x: [1; INNER_LEN], y: [2; INNER_LEN]}; LEN - 1]]
const transcript Pt [LEN] array = [Pt {x: [0; INNER_LEN], y: [5; INNER_LEN]}, ...[Pt {x: [1; INNER_LEN], y: [2; INNER_LEN]}; LEN - 1]]

def main(private field[ACCESSES] idx) -> field:
field prod = 1
Expand Down
39 changes: 38 additions & 1 deletion scripts/ram_test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ disable -r time
# cargo build --release --features r1cs,smt,zok --example circ
# cargo build --features "zok smt bellman" --example circ --example zk

MODE=release # debug or release
MODE=release
# MODE=debug
BIN=./target/$MODE/examples/circ
ZK_BIN=./target/$MODE/examples/zk

Expand All @@ -22,6 +23,42 @@ function ram_test {
rm -rf P V pi
}

# Test for how many transcripts are extracted
function transcript_count_test {
ex_name=$1
ex_num=$2
rm -rf P V pi
found_num=$(RUST_LOG=circ::ir::opt::mem=debug $BIN $ex_name r1cs --action count |& grep -Eo 'Found [0-9]* transcripts' | grep -Eo '\b[0-9]+\b')
if [[ ! $ex_num == $found_num ]]
then
echo "expected $ex_num transcripts;\n found $found_num transcripts"
exit 2
fi
}

# Test for whether a particular type of transcript is found
function transcript_type_test {
ex_name=$1
ex_type=$2
rm -rf P V pi
output=$(RUST_LOG=circ::ir::opt::mem=debug $BIN $ex_name r1cs --action count |& cat)
echo $output
if (echo $output |& grep "Checking $ex_type") then;
else
echo "Did not find a transcript of type $ex_type"
exit 2
fi
}

transcript_count_test ./examples/ZoKrates/pf/mem/volatile.zok 1
transcript_count_test ./examples/ZoKrates/pf/mem/two_level_ptr.zok 1
transcript_count_test ./examples/ZoKrates/pf/mem/volatile_struct.zok 1
transcript_count_test ./examples/ZoKrates/pf/mem/arr_of_str.zok 1
transcript_count_test ./examples/ZoKrates/pf/mem/arr_of_str_of_arr.zok 1

transcript_type_test ./examples/ZoKrates/pf/mem/volatile_struct.zok "RAM"
transcript_type_test ./examples/ZoKrates/pf/mem/two_level_ptr.zok "covering ROM"

ram_test ./examples/ZoKrates/pf/mem/two_level_ptr.zok groth16 "--ram-permutation waksman --ram-index sort --ram-range bit-split"
ram_test ./examples/ZoKrates/pf/mem/volatile.zok groth16 "--ram-permutation waksman --ram-index sort --ram-range bit-split"
# waksman is broken for non-scalar array values
Expand Down
81 changes: 56 additions & 25 deletions src/ir/opt/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,63 @@ use crate::ir::term::*;

use super::visit::RewritePass;

/// Parse `ite` as a conditional store (arr, idx, val, guard)
fn parse_cond_store(ite: &Term) -> Option<ConditionalStore> {
if ite.op() == &Op::Ite && ite.cs()[1].op() == &Op::Store && ite.cs()[1].cs()[0] == ite.cs()[2]
{
// (ite COND (store ARR IDX VAL) ARR)
Some(ConditionalStore {
arr: ite.cs()[2].clone(),
idx: ite.cs()[1].cs()[1].clone(),
val: ite.cs()[1].cs()[2].clone(),
guard: ite.cs()[0].clone(),
})
} else if ite.op() == &Op::Ite
&& ite.cs()[2].op() == &Op::Store
&& ite.cs()[2].cs()[0] == ite.cs()[1]
{
// (ite COND ARR (store ARR IDX VAL))
Some(ConditionalStore {
arr: ite.cs()[1].clone(),
idx: ite.cs()[2].cs()[1].clone(),
val: ite.cs()[2].cs()[2].clone(),
guard: term![NOT; ite.cs()[0].clone()],
})
// could do things like: (store ARR IDX (ite COND VAL (select ARR IDX)))
} else {
None
/// Parse `ite` or `store` as a conditional store (arr, idx, val, guard)
fn parse_cond_store(term: &Term) -> Option<ConditionalStore> {
if let (&Op::Ite, [guard, true_, false_]) = term.parts() {
match true_.parts() {
(&Op::Store, [arr, idx, val]) if arr == false_ => {
// (ite COND (store ARR IDX VAL) ARR)
return Some(ConditionalStore {
arr: arr.clone(),
idx: idx.clone(),
val: val.clone(),
guard: guard.clone(),
});
}
_ => {}
}
match false_.parts() {
(&Op::Store, [arr, idx, val]) if arr == false_ => {
// (ite COND ARR (store ARR IDX VAL))
return Some(ConditionalStore {
arr: arr.clone(),
idx: idx.clone(),
val: val.clone(),
guard: term![NOT; guard.clone()],
});
}
_ => {}
}
}
if let (&Op::Store, [arr, idx, val]) = term.parts() {
if let (&Op::Ite, [guard, true_, false_]) = val.parts() {
match false_.parts() {
(&Op::Select, [arr2, idx2]) if arr == arr2 && idx == idx2 => {
// (store ARR IDX (ite COND VAL (select ARR IDX)))
return Some(ConditionalStore {
arr: arr.clone(),
idx: idx.clone(),
val: true_.clone(),
guard: guard.clone(),
});
}
_ => {}
}
match true_.parts() {
(&Op::Select, [arr2, idx2]) if arr == arr2 && idx == idx2 => {
// (store ARR IDX (ite COND (select ARR IDX) VAL))
return Some(ConditionalStore {
arr: arr.clone(),
idx: idx.clone(),
val: false_.clone(),
guard: term![NOT; guard.clone()],
});
}
_ => {}
}
}
}
None
}

#[derive(Debug)]
Expand Down
Loading
Loading