Skip to content

Commit

Permalink
Merge updates needed for SHA with lookups. (#196)
Browse files Browse the repository at this point in the history
This is highly unoptimized, for now.
  • Loading branch information
alex-ozdemir authored Jun 19, 2024
1 parent aa318e5 commit 2cdc019
Show file tree
Hide file tree
Showing 60 changed files with 2,300 additions and 330 deletions.
12 changes: 10 additions & 2 deletions circ_fields/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,22 @@ impl PartialOrd for FieldV {
impl Ord for FieldV {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.full_cow().cmp(&other.full_cow())
if self.is_full() || other.is_full() {
self.full_cow().cmp(&other.full_cow())
} else {
self.inline_i64().cmp(&other.inline_i64())
}
}
}

impl PartialEq for FieldV {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.full_cow().eq(&other.full_cow())
if self.is_full() || other.is_full() {
self.full_cow().eq(&other.full_cow())
} else {
self.inline_i64().eq(&other.inline_i64())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion circ_hc/src/hashconsing/example_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl crate::Table<u8> for Table {
"hashconsing"
}

fn for_each(f: impl FnMut(&u8, &[Self::Node])) {
fn for_each(_f: impl FnMut(&u8, &[Self::Node])) {
panic!()
}

Expand Down
2 changes: 1 addition & 1 deletion circ_hc/src/hashconsing/macro_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ macro_rules! generate_hashcons_hashconsing {
"hashconsing"
}

fn for_each(f: impl FnMut(&$Op, &[Self::Node])) {
fn for_each(_f: impl FnMut(&$Op, &[Self::Node])) {
panic!()
}

Expand Down
2 changes: 1 addition & 1 deletion circ_hc/src/hashconsing/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl crate::Table<TemplateOp> for Table {
"hashconsing"
}

fn for_each(f: impl FnMut(&TemplateOp, &[Self::Node])) {
fn for_each(_f: impl FnMut(&TemplateOp, &[Self::Node])) {
panic!()
}

Expand Down
41 changes: 24 additions & 17 deletions circ_hc/src/rc/example_u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ use fxhash::FxHashMap as HashMap;

use crate::Id;
use log::trace;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::atomic::AtomicU64;
use std::thread_local;

#[allow(dead_code)]
struct NodeData {
op: u8,
hash: AtomicU64,
cs: Box<[Node]>,
}

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

#[derive(Clone)]
pub struct Node {
data: Rc<NodeData>,
Expand Down Expand Up @@ -158,6 +156,7 @@ impl Manager {
let mut table = self.table.borrow_mut();
let data = Rc::new(NodeData {
op: op.clone(),
hash: Default::default(),
cs: children.into(),
});

Expand Down Expand Up @@ -218,10 +217,10 @@ impl Manager {
let duration = start.elapsed();
self.in_gc.set(false);
trace!(
"GC: {} terms -> {} terms in {} us",
"GC: {} terms -> {} terms in {} ns",
collected,
new_size,
duration.as_micros()
duration.as_nanos()
);
collected
} else {
Expand Down Expand Up @@ -296,37 +295,45 @@ impl crate::Weak<u8> for Weak {
}

mod hash {
use super::{Node, NodeData, NodeDataRef, Weak};
use std::borrow::Borrow;
use super::{Node, NodeData, Weak};
use fxhash::FxHasher;
use std::hash::{Hash, Hasher};
use std::sync::atomic::Ordering::SeqCst;

impl Hash for Node {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for Weak {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for NodeData {
fn hash<H: Hasher>(&self, state: &mut H) {
self.op.hash(state);
impl NodeData {
fn rehash(&self) -> u64 {
let mut hasher = FxHasher::default();
self.op.hash(&mut hasher);
for c in self.cs.iter() {
c.hash(state);
c.hash(&mut hasher);
}
let current_hash = hasher.finish();
self.hash.store(current_hash, SeqCst);
current_hash
}
}

impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
impl Hash for NodeData {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
for c in self.1.borrow().iter() {
c.hash(state);
let mut current_hash: u64 = self.hash.load(SeqCst);
if current_hash == 0 {
current_hash = self.rehash();
}
state.write_u64(current_hash);
}
}
}
Expand Down
41 changes: 24 additions & 17 deletions circ_hc/src/rc/macro_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ macro_rules! generate_hashcons_rc {
use fxhash::FxHashMap as HashMap;

use log::trace;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::atomic::AtomicU64;
use std::thread_local;
use $crate::Id;

#[allow(dead_code)]
struct NodeData {
op: $Op,
hash: AtomicU64,
cs: Box<[Node]>,
}

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

#[derive(Clone)]
pub struct Node {
data: Rc<NodeData>,
Expand Down Expand Up @@ -161,6 +159,7 @@ macro_rules! generate_hashcons_rc {
let mut table = self.table.borrow_mut();
let data = Rc::new(NodeData {
op: op.clone(),
hash: Default::default(),
cs: children.into(),
});

Expand Down Expand Up @@ -221,10 +220,10 @@ macro_rules! generate_hashcons_rc {
let duration = start.elapsed();
self.in_gc.set(false);
trace!(
"GC: {} terms -> {} terms in {} us",
"GC: {} terms -> {} terms in {} ns",
collected,
new_size,
duration.as_micros()
duration.as_nanos()
);
collected
} else {
Expand Down Expand Up @@ -299,37 +298,45 @@ macro_rules! generate_hashcons_rc {
}

mod hash {
use super::{Node, NodeData, NodeDataRef, Weak};
use std::borrow::Borrow;
use super::{Node, NodeData, Weak};
use fxhash::FxHasher;
use std::hash::{Hash, Hasher};
use std::sync::atomic::Ordering::SeqCst;

impl Hash for Node {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for Weak {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for NodeData {
fn hash<H: Hasher>(&self, state: &mut H) {
self.op.hash(state);
impl NodeData {
fn rehash(&self) -> u64 {
let mut hasher = FxHasher::default();
self.op.hash(&mut hasher);
for c in self.cs.iter() {
c.hash(state);
c.hash(&mut hasher);
}
let current_hash = hasher.finish();
self.hash.store(current_hash, SeqCst);
current_hash
}
}

impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
impl Hash for NodeData {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
for c in self.1.borrow().iter() {
c.hash(state);
let mut current_hash: u64 = self.hash.load(SeqCst);
if current_hash == 0 {
current_hash = self.rehash();
}
state.write_u64(current_hash);
}
}
}
Expand Down
40 changes: 24 additions & 16 deletions circ_hc/src/rc/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ use fxhash::FxHashMap as HashMap;

use crate::Id;
use log::trace;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::net::SocketAddrV6 as TemplateOp;
use std::rc::Rc;
use std::sync::atomic::AtomicU64;
use std::thread_local;

#[allow(dead_code)]
struct NodeData {
op: TemplateOp,
hash: AtomicU64,
cs: Box<[Node]>,
}

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

#[derive(Clone)]
pub struct Node {
data: Rc<NodeData>,
Expand Down Expand Up @@ -158,6 +156,7 @@ impl Manager {
let mut table = self.table.borrow_mut();
let data = Rc::new(NodeData {
op: op.clone(),
hash: Default::default(),
cs: children.into(),
});

Expand Down Expand Up @@ -218,10 +217,10 @@ impl Manager {
let duration = start.elapsed();
self.in_gc.set(false);
trace!(
"GC: {} terms -> {} terms in {} us",
"GC: {} terms -> {} terms in {} ns",
collected,
new_size,
duration.as_micros()
duration.as_nanos()
);
collected
} else {
Expand Down Expand Up @@ -296,37 +295,46 @@ impl crate::Weak<TemplateOp> for Weak {
}

mod hash {
use super::{Node, NodeData, NodeDataRef, Weak};
use std::borrow::Borrow;
use super::{Node, NodeData, Weak};
use fxhash::FxHasher;
use std::hash::{Hash, Hasher};
use std::sync::atomic::Ordering::SeqCst;

impl Hash for Node {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for Weak {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state)
}
}

impl Hash for NodeData {
fn hash<H: Hasher>(&self, state: &mut H) {
self.op.hash(state);
impl NodeData {
fn rehash(&self) -> u64 {
let mut hasher = FxHasher::default();
self.op.hash(&mut hasher);
for c in self.cs.iter() {
c.hash(state);
c.hash(&mut hasher);
}
let current_hash = hasher.finish();
self.hash.store(current_hash, SeqCst);
current_hash
}
}

impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
impl Hash for NodeData {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
for c in self.1.borrow().iter() {
c.hash(state);
let mut current_hash: u64 = self.hash.load(SeqCst);
if current_hash == 0 {
current_hash = self.rehash();
}
state.write_u64(current_hash);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions circ_opt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ pub struct IrOpt {
default_value = "true"
)]
pub fits_in_bits_ip: bool,
/// Time operator evaluations
#[arg(
long = "ir-time-eval-ops",
env = "IR_TIME_EVAL_OPS",
action = ArgAction::Set,
default_value = "false"
)]
pub time_eval_ops: bool,
}

impl Default for IrOpt {
Expand All @@ -202,6 +210,7 @@ impl Default for IrOpt {
field_to_bv: Default::default(),
frequent_gc: Default::default(),
fits_in_bits_ip: true,
time_eval_ops: false,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ZoKrates/pf/2024_05_24_benny_bug.zok.vin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(set_default_modulus 52435875175126190479447740508185965837690552500527637822603658699938581184513
(let (
(x #f6)
(return #f6)
(return #f0)
) false ; ignored
))

1 change: 1 addition & 0 deletions examples/ZoKrates/pf/hash/sha256lookup/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This directory contains a SHA256 implementation by Anna Woo.
Loading

0 comments on commit 2cdc019

Please sign in to comment.