Skip to content

Commit 07db836

Browse files
committed
handle CStore in linearization pass
1 parent 7f6d0a0 commit 07db836

File tree

11 files changed

+62
-11
lines changed

11 files changed

+62
-11
lines changed

circ_hc/src/rc/example_u8.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct NodeData {
1414
cs: Box<[Node]>,
1515
}
1616

17+
#[allow(dead_code)]
1718
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a u8, &'a Q);
1819

1920
#[derive(Clone)]

circ_hc/src/rc/macro_.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ macro_rules! generate_hashcons_rc {
1717
cs: Box<[Node]>,
1818
}
1919

20+
#[allow(dead_code)]
2021
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a $Op, &'a Q);
2122

2223
#[derive(Clone)]

circ_hc/src/rc/template.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct NodeData {
1414
cs: Box<[Node]>,
1515
}
1616

17+
#[allow(dead_code)]
1718
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a TemplateOp, &'a Q);
1819

1920
#[derive(Clone)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def main(field x) -> field:
2+
field[25] A = [0; 25]
3+
for field counter in 0..30 do
4+
cond_store(A, counter, x, counter < x)
5+
endfor
6+
7+
return A[x]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def main(field x) -> field:
2+
transcript field[25] A = [0; 25]
3+
for field counter in 0..30 do
4+
cond_store(A, counter, x, counter < x)
5+
endfor
6+
7+
return A[x]

scripts/zokrates_test.zsh

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ r1cs_test ./third_party/ZoKrates/zokrates_stdlib/stdlib/utils/casts/bool_128_to_
8686
r1cs_test ./third_party/ZoKrates/zokrates_stdlib/stdlib/ecc/edwardsScalarMult.zok
8787
r1cs_test ./third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/mimc7/mimc7R20.zok
8888
r1cs_test ./third_party/ZoKrates/zokrates_stdlib/stdlib/hashes/pedersen/512bit.zok
89+
r1cs_test ./examples/ZoKrates/pf/2024_05_24_benny_bug.zok
90+
r1cs_test ./examples/ZoKrates/pf/2024_05_24_benny_bug_tr.zok
8991

9092
pf_test_only_pf sha_temp1
9193
pf_test_only_pf sha_rot

src/circify/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,6 @@ struct ScopeIdx {
110110
lex: usize,
111111
}
112112

113-
#[derive(Hash, PartialEq, Eq)]
114-
struct VerVar {
115-
name: VarName,
116-
ver: Version,
117-
}
118-
119113
#[derive(Debug)]
120114
struct LexEntry<Ty> {
121115
ver: Version,

src/ir/opt/mem/lin.rs

+40-3
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ impl RewritePass for Linearizer {
5252
let cs = rewritten_children();
5353
let idx = &cs[1];
5454
let tup = &cs[0];
55-
if let Sort::Array(key_sort, _, size) = check(&orig.cs()[0]) {
55+
if let Sort::Array(key_sort, val_sort, size) = check(&orig.cs()[0]) {
5656
assert!(size > 0);
5757
if idx.is_const() {
5858
let idx_usize = extras::as_uint_constant(idx).unwrap().to_usize().unwrap();
59-
Some(term![Op::Field(idx_usize); tup.clone()])
59+
if idx_usize < size {
60+
Some(term![Op::Field(idx_usize); tup.clone()])
61+
} else {
62+
Some(val_sort.default_term())
63+
}
6064
} else {
6165
let mut fields = (0..size).map(|idx| term![Op::Field(idx); tup.clone()]);
6266
let first = fields.next().unwrap();
@@ -77,7 +81,11 @@ impl RewritePass for Linearizer {
7781
assert!(size > 0);
7882
if idx.is_const() {
7983
let idx_usize = extras::as_uint_constant(idx).unwrap().to_usize().unwrap();
80-
Some(term![Op::Update(idx_usize); tup.clone(), val.clone()])
84+
if idx_usize < size {
85+
Some(term![Op::Update(idx_usize); tup.clone(), val.clone()])
86+
} else {
87+
Some(tup.clone())
88+
}
8189
} else {
8290
let mut updates =
8391
(0..size).map(|idx| term![Op::Update(idx); tup.clone(), val.clone()]);
@@ -90,6 +98,35 @@ impl RewritePass for Linearizer {
9098
unreachable!()
9199
}
92100
}
101+
Op::CStore => {
102+
let cs = rewritten_children();
103+
let tup = &cs[0];
104+
let idx = &cs[1];
105+
let val = &cs[2];
106+
let cond = &cs[3];
107+
if let Sort::Array(key_sort, _, size) = check(&orig.cs()[0]) {
108+
assert!(size > 0);
109+
if idx.is_const() {
110+
let idx_usize = extras::as_uint_constant(idx).unwrap().to_usize().unwrap();
111+
if idx_usize < size {
112+
Some(
113+
term![Op::Ite; cond.clone(), term![Op::Update(idx_usize); tup.clone(), val.clone()], tup.clone()],
114+
)
115+
} else {
116+
Some(tup.clone())
117+
}
118+
} else {
119+
let mut updates =
120+
(0..size).map(|idx| term![Op::Update(idx); tup.clone(), val.clone()]);
121+
let first = updates.next().unwrap();
122+
Some(key_sort.elems_iter().take(size).skip(1).zip(updates).fold(first, |acc, (idx_c, update)| {
123+
term![Op::Ite; term![AND; term![Op::Eq; idx.clone(), idx_c], cond.clone()], update, acc]
124+
}))
125+
}
126+
} else {
127+
unreachable!()
128+
}
129+
}
93130
// ITEs and EQs are correctly rewritten by default.
94131
_ => None,
95132
}

src/ir/opt/mem/ram/volatile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl RewritePass for Extactor {
275275
"Bad ram term: {}",
276276
t
277277
);
278-
debug_assert!(self.graph.children.get(t).is_some());
278+
debug_assert!(self.graph.children.contains_key(t));
279279
debug_assert_eq!(1, self.graph.children.get(t).unwrap().len());
280280
// Get dependency's RAM
281281
let child = self.graph.children.get(t).unwrap()[0].clone();

src/ir/opt/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub trait RewritePass {
102102

103103
/// An analysis pass that repeated sweeps all terms, visiting them, untill a pass makes no more
104104
/// progress.
105+
#[allow(dead_code)]
105106
pub trait ProgressAnalysisPass {
106107
/// The visit function. Returns whether progress was made.
107108
fn visit(&mut self, term: &Term) -> bool;

src/ir/term/dist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Distributions over terms (useful for fuzz testing)
2-
2+
#![allow(dead_code)]
33
use super::*;
44

55
use circ_fields::{FieldT, FieldV};

0 commit comments

Comments
 (0)