Skip to content

Commit 1af5d01

Browse files
committed
finish merge
1 parent d5443e8 commit 1af5d01

39 files changed

+738
-245
lines changed

circ_fields/src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,22 @@ impl PartialOrd for FieldV {
371371
impl Ord for FieldV {
372372
#[inline]
373373
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
374-
self.full_cow().cmp(&other.full_cow())
374+
if self.is_full() || other.is_full() {
375+
self.full_cow().cmp(&other.full_cow())
376+
} else {
377+
self.inline_i64().cmp(&other.inline_i64())
378+
}
375379
}
376380
}
377381

378382
impl PartialEq for FieldV {
379383
#[inline]
380384
fn eq(&self, other: &Self) -> bool {
381-
self.full_cow().eq(&other.full_cow())
385+
if self.is_full() || other.is_full() {
386+
self.full_cow().eq(&other.full_cow())
387+
} else {
388+
self.inline_i64().eq(&other.inline_i64())
389+
}
382390
}
383391
}
384392

circ_hc/src/hashconsing/example_u8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl crate::Table<u8> for Table {
4949
"hashconsing"
5050
}
5151

52-
fn for_each(f: impl FnMut(&u8, &[Self::Node])) {
52+
fn for_each(_f: impl FnMut(&u8, &[Self::Node])) {
5353
panic!()
5454
}
5555

circ_hc/src/hashconsing/macro_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ macro_rules! generate_hashcons_hashconsing {
5252
"hashconsing"
5353
}
5454

55-
fn for_each(f: impl FnMut(&$Op, &[Self::Node])) {
55+
fn for_each(_f: impl FnMut(&$Op, &[Self::Node])) {
5656
panic!()
5757
}
5858

circ_hc/src/hashconsing/template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl crate::Table<TemplateOp> for Table {
5050
"hashconsing"
5151
}
5252

53-
fn for_each(f: impl FnMut(&TemplateOp, &[Self::Node])) {
53+
fn for_each(_f: impl FnMut(&TemplateOp, &[Self::Node])) {
5454
panic!()
5555
}
5656

circ_hc/src/rc/example_u8.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ use fxhash::FxHashMap as HashMap;
33

44
use crate::Id;
55
use log::trace;
6-
use std::borrow::Borrow;
76
use std::cell::{Cell, RefCell};
87
use std::rc::Rc;
8+
use std::sync::atomic::AtomicU64;
99
use std::thread_local;
1010

1111
#[allow(dead_code)]
1212
struct NodeData {
1313
op: u8,
14+
hash: AtomicU64,
1415
cs: Box<[Node]>,
1516
}
1617

17-
#[allow(dead_code)]
18-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a u8, &'a Q);
19-
2018
#[derive(Clone)]
2119
pub struct Node {
2220
data: Rc<NodeData>,
@@ -158,6 +156,7 @@ impl Manager {
158156
let mut table = self.table.borrow_mut();
159157
let data = Rc::new(NodeData {
160158
op: op.clone(),
159+
hash: Default::default(),
161160
cs: children.into(),
162161
});
163162

@@ -296,37 +295,45 @@ impl crate::Weak<u8> for Weak {
296295
}
297296

298297
mod hash {
299-
use super::{Node, NodeData, NodeDataRef, Weak};
300-
use std::borrow::Borrow;
298+
use super::{Node, NodeData, Weak};
299+
use fxhash::FxHasher;
301300
use std::hash::{Hash, Hasher};
301+
use std::sync::atomic::Ordering::SeqCst;
302302

303303
impl Hash for Node {
304+
#[inline]
304305
fn hash<H: Hasher>(&self, state: &mut H) {
305306
self.id.hash(state)
306307
}
307308
}
308309

309310
impl Hash for Weak {
311+
#[inline]
310312
fn hash<H: Hasher>(&self, state: &mut H) {
311313
self.id.hash(state)
312314
}
313315
}
314316

315-
impl Hash for NodeData {
316-
fn hash<H: Hasher>(&self, state: &mut H) {
317-
self.op.hash(state);
317+
impl NodeData {
318+
fn rehash(&self) -> u64 {
319+
let mut hasher = FxHasher::default();
320+
self.op.hash(&mut hasher);
318321
for c in self.cs.iter() {
319-
c.hash(state);
322+
c.hash(&mut hasher);
320323
}
324+
let current_hash = hasher.finish();
325+
self.hash.store(current_hash, SeqCst);
326+
current_hash
321327
}
322328
}
323-
324-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
329+
impl Hash for NodeData {
330+
#[inline]
325331
fn hash<H: Hasher>(&self, state: &mut H) {
326-
self.0.hash(state);
327-
for c in self.1.borrow().iter() {
328-
c.hash(state);
332+
let mut current_hash: u64 = self.hash.load(SeqCst);
333+
if current_hash == 0 {
334+
current_hash = self.rehash();
329335
}
336+
state.write_u64(current_hash);
330337
}
331338
}
332339
}

circ_hc/src/rc/macro_.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ macro_rules! generate_hashcons_rc {
55
use fxhash::FxHashMap as HashMap;
66

77
use log::trace;
8-
use std::borrow::Borrow;
98
use std::cell::{Cell, RefCell};
109
use std::rc::Rc;
10+
use std::sync::atomic::AtomicU64;
1111
use std::thread_local;
1212
use $crate::Id;
1313

1414
#[allow(dead_code)]
1515
struct NodeData {
1616
op: $Op,
17+
hash: AtomicU64,
1718
cs: Box<[Node]>,
1819
}
1920

20-
#[allow(dead_code)]
21-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a $Op, &'a Q);
22-
2321
#[derive(Clone)]
2422
pub struct Node {
2523
data: Rc<NodeData>,
@@ -161,6 +159,7 @@ macro_rules! generate_hashcons_rc {
161159
let mut table = self.table.borrow_mut();
162160
let data = Rc::new(NodeData {
163161
op: op.clone(),
162+
hash: Default::default(),
164163
cs: children.into(),
165164
});
166165

@@ -299,37 +298,45 @@ macro_rules! generate_hashcons_rc {
299298
}
300299

301300
mod hash {
302-
use super::{Node, NodeData, NodeDataRef, Weak};
303-
use std::borrow::Borrow;
301+
use super::{Node, NodeData, Weak};
302+
use fxhash::FxHasher;
304303
use std::hash::{Hash, Hasher};
304+
use std::sync::atomic::Ordering::SeqCst;
305305

306306
impl Hash for Node {
307+
#[inline]
307308
fn hash<H: Hasher>(&self, state: &mut H) {
308309
self.id.hash(state)
309310
}
310311
}
311312

312313
impl Hash for Weak {
314+
#[inline]
313315
fn hash<H: Hasher>(&self, state: &mut H) {
314316
self.id.hash(state)
315317
}
316318
}
317319

318-
impl Hash for NodeData {
319-
fn hash<H: Hasher>(&self, state: &mut H) {
320-
self.op.hash(state);
320+
impl NodeData {
321+
fn rehash(&self) -> u64 {
322+
let mut hasher = FxHasher::default();
323+
self.op.hash(&mut hasher);
321324
for c in self.cs.iter() {
322-
c.hash(state);
325+
c.hash(&mut hasher);
323326
}
327+
let current_hash = hasher.finish();
328+
self.hash.store(current_hash, SeqCst);
329+
current_hash
324330
}
325331
}
326-
327-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
332+
impl Hash for NodeData {
333+
#[inline]
328334
fn hash<H: Hasher>(&self, state: &mut H) {
329-
self.0.hash(state);
330-
for c in self.1.borrow().iter() {
331-
c.hash(state);
335+
let mut current_hash: u64 = self.hash.load(SeqCst);
336+
if current_hash == 0 {
337+
current_hash = self.rehash();
332338
}
339+
state.write_u64(current_hash);
333340
}
334341
}
335342
}

circ_hc/src/rc/template.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use fxhash::FxHashMap as HashMap;
22

33
use crate::Id;
44
use log::trace;
5-
use std::borrow::Borrow;
65
use std::cell::{Cell, RefCell};
76
use std::net::SocketAddrV6 as TemplateOp;
87
use std::rc::Rc;
8+
use std::sync::atomic::AtomicU64;
99
use std::thread_local;
1010

1111
#[allow(dead_code)]
1212
struct NodeData {
1313
op: TemplateOp,
14+
hash: AtomicU64,
1415
cs: Box<[Node]>,
1516
}
1617

17-
#[allow(dead_code)]
18-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a TemplateOp, &'a Q);
19-
2018
#[derive(Clone)]
2119
pub struct Node {
2220
data: Rc<NodeData>,
@@ -158,6 +156,7 @@ impl Manager {
158156
let mut table = self.table.borrow_mut();
159157
let data = Rc::new(NodeData {
160158
op: op.clone(),
159+
hash: Default::default(),
161160
cs: children.into(),
162161
});
163162

@@ -296,37 +295,46 @@ impl crate::Weak<TemplateOp> for Weak {
296295
}
297296

298297
mod hash {
299-
use super::{Node, NodeData, NodeDataRef, Weak};
300-
use std::borrow::Borrow;
298+
use super::{Node, NodeData, Weak};
299+
use fxhash::FxHasher;
301300
use std::hash::{Hash, Hasher};
301+
use std::sync::atomic::Ordering::SeqCst;
302302

303303
impl Hash for Node {
304+
#[inline]
304305
fn hash<H: Hasher>(&self, state: &mut H) {
305306
self.id.hash(state)
306307
}
307308
}
308309

309310
impl Hash for Weak {
311+
#[inline]
310312
fn hash<H: Hasher>(&self, state: &mut H) {
311313
self.id.hash(state)
312314
}
313315
}
314316

315-
impl Hash for NodeData {
316-
fn hash<H: Hasher>(&self, state: &mut H) {
317-
self.op.hash(state);
317+
impl NodeData {
318+
fn rehash(&self) -> u64 {
319+
let mut hasher = FxHasher::default();
320+
self.op.hash(&mut hasher);
318321
for c in self.cs.iter() {
319-
c.hash(state);
322+
c.hash(&mut hasher);
320323
}
324+
let current_hash = hasher.finish();
325+
self.hash.store(current_hash, SeqCst);
326+
current_hash
321327
}
322328
}
323329

324-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
330+
impl Hash for NodeData {
331+
#[inline]
325332
fn hash<H: Hasher>(&self, state: &mut H) {
326-
self.0.hash(state);
327-
for c in self.1.borrow().iter() {
328-
c.hash(state);
333+
let mut current_hash: u64 = self.hash.load(SeqCst);
334+
if current_hash == 0 {
335+
current_hash = self.rehash();
329336
}
337+
state.write_u64(current_hash);
330338
}
331339
}
332340
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(set_default_modulus 52435875175126190479447740508185965837690552500527637822603658699938581184513
22
(let (
33
(x #f6)
4-
(return #f6)
4+
(return #f0)
55
) false ; ignored
66
))
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from "test_sha256_adv" import test_sha256
2+
3+
const u32 N = 1
4+
const u32 NL = 3 // Number of limbs
5+
6+
def main(field[8] expected_hash, private field[N][16][NL] padded_message) -> bool:
7+
return test_sha256::<N, NL>(expected_hash, padded_message)
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
def main(field x) -> field:
22
transcript field[25] A = [0; 25]
33
for field counter in 0..30 do
4-
bool oob = counter < x
5-
cond_store(A, if oob then counter else 0 fi, x, oob)
4+
bool inbound = counter < x
5+
cond_store(A, if inbound then counter else 0 fi, x, inbound)
66
endfor
77

88
return A[x]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(set_default_modulus 52435875175126190479447740508185965837690552500527637822603658699938581184513
22
(let (
33
(x #f6)
4-
(return #f6)
4+
(return #f0)
55
) false ; ignored
66
))
77

examples/ZoKrates/pf/mem/sparse4.zok

+7-11
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,14 @@ def normalize_sum_4(field s) -> Dual:
109109
//do a bitwise AND.
110110
def main(private field dense_x, private field dense_y) -> field:
111111
Dual z = dense_to_dual_4(0)
112-
Dual x = dense_to_dual_4(dense_x) // 1010 (10)
113-
Dual y = dense_to_dual_4(dense_y) // 1001 (9)
114-
Dual a = and_4(x, y) // 1000 (8)
115-
for field i in 0..10 do
116-
a = and_4(a, y) // idempotent
117-
endfor
118-
Dual b = or_4(x, y) // 1011 (11)
119-
Dual s = normalize_sum_4(b.d + a.d) // 0011 (3)
112+
Dual x = dense_to_dual_4(dense_x) // 10001000 (136)
113+
Dual y = dense_to_dual_4(dense_y) // 10000001 (129)
114+
Dual a = and_4(x, y) // 10000000
115+
Dual b = or_4(x, y) // 10001001
116+
Dual c = xor_4(x, y, z) // 00001001
117+
Dual d = maj_4(x, y, c) // 10001001
118+
Dual s = normalize_sum_4(d.d + c.d + b.d + a.d) // 10011011 (128+27=155)
120119
return s.d
121-
// return reverse_lookup(D_TO_S_4, dense_x) * dense_y
122-
// return reverse_lookup(D_TO_S_4, dense_x) * reverse_lookup(D_TO_S_4, dense_y)
123-
// return dense_x * dense_y
124120

125121

126122

examples/circ.rs

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ fn main() {
266266
Mode::Proof | Mode::ProofOfHighValue(_) => {
267267
let mut opts = Vec::new();
268268

269+
opts.push(Opt::ConstantFold(Box::new([])));
269270
opts.push(Opt::DeskolemizeWitnesses);
270271
opts.push(Opt::ScalarizeVars);
271272
opts.push(Opt::Flatten);

scripts/ram_test.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ transcript_type_test ./examples/ZoKrates/pf/mem/two_level_ptr.zok "covering ROM"
7171
# A=400; N=20; L=2; expected cost ~= N + A(L+1) = 1220
7272
cs_count_test ./examples/ZoKrates/pf/mem/rom.zok 1230
7373

74-
ram_test ./examples/ZoKrates/pf/mem/2024_05_31_benny_bug_tr.zok mirage ""
7574
ram_test ./examples/ZoKrates/pf/mem/2024_05_24_benny_bug_tr.zok mirage ""
75+
ram_test ./examples/ZoKrates/pf/mem/2024_05_31_benny_bug_tr.zok mirage ""
7676
ram_test ./examples/ZoKrates/pf/mem/two_level_ptr.zok groth16 "--ram-permutation waksman --ram-index sort --ram-range bit-split"
7777
ram_test ./examples/ZoKrates/pf/mem/volatile.zok groth16 "--ram-permutation waksman --ram-index sort --ram-range bit-split"
7878
# waksman is broken for non-scalar array values

0 commit comments

Comments
 (0)