Skip to content
Open
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"crates/vm-core",
"crates/zk-core",
"crates/zk",
"crates/predicate",
]
exclude = ["crates/lpn-estimator"]
resolver = "2"
Expand Down Expand Up @@ -63,6 +64,7 @@ mpz-memory-core = { path = "crates/memory-core" }
mpz-vm-core = { path = "crates/vm-core" }
mpz-zk-core = { path = "crates/zk-core" }
mpz-zk = { path = "crates/zk" }
mpz-predicate = { path = "crates/predicate" }
clmul = { path = "crates/clmul" }
matrix-transpose = { path = "crates/matrix-transpose" }
rangeset = "0.4"
Expand Down
102 changes: 102 additions & 0 deletions crates/circuits-core/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,108 @@ pub fn inv<const N: usize>(builder: &mut CircuitBuilder, a: [Node<Feed>; N]) ->
std::array::from_fn(|n| builder.add_inv_gate(a[n]))
}

/// Returns 1 if all input bits are 1, otherwise 0.
pub fn all(builder: &mut CircuitBuilder, inputs: &[Node<Feed>]) -> Node<Feed> {
assert!(!inputs.is_empty(), "all requires at least one input");
inputs
.iter()
.copied()
.reduce(|acc, x| builder.add_and_gate(acc, x))
.unwrap()
}

/// Returns 1 if any input bit is 1, otherwise 0.
pub fn any(builder: &mut CircuitBuilder, inputs: &[Node<Feed>]) -> Node<Feed> {
assert!(!inputs.is_empty(), "any requires at least one input");
inputs
.iter()
.copied()
.reduce(|acc, x| {
// OR = (A ⊕ B) ⊕ (A ^ B)
let a_xor_b = builder.add_xor_gate(acc, x);
let a_and_b = builder.add_and_gate(acc, x);
builder.add_xor_gate(a_xor_b, a_and_b)
})
.unwrap()
}

/// Returns 1 if two nbit values are equal, otherwise 0.
pub fn eq<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// Two values are equal if all XOR bits are 0, i.e., NOR of all XORs
let xors: Vec<_> = a
.iter()
.zip(b.iter())
.map(|(a, b)| builder.add_xor_gate(*a, *b))
.collect();

// All XORs must be 0 for equality
let any_diff = any(builder, &xors);
builder.add_inv_gate(any_diff)
}

/// Returns 1 if two nbit values are not equal, otherwise 0.
pub fn neq<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// Two values are not equal if any XOR bit is 1
let xors: Vec<_> = a
.iter()
.zip(b.iter())
.map(|(a, b)| builder.add_xor_gate(*a, *b))
.collect();

any(builder, &xors)
}

/// Returns 1 if a < b (unsigned), otherwise 0.
pub fn lt<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// a < b iff (a - b) underflows
let (_diff, underflow) = wrapping_sub(builder, &a, &b);
underflow
}

/// Returns 1 if a <= b (unsigned), otherwise 0.
pub fn lte<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// a <= b iff NOT (a > b) iff NOT (b < a)
let b_lt_a = lt(builder, b, a);
builder.add_inv_gate(b_lt_a)
}

/// Returns 1 if a > b (unsigned), otherwise 0.
pub fn gt<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// a > b iff b < a
lt(builder, b, a)
}

/// Returns 1 if a >= b (unsigned), otherwise 0.
pub fn gte<const N: usize>(
builder: &mut CircuitBuilder,
a: [Node<Feed>; N],
b: [Node<Feed>; N],
) -> Node<Feed> {
// a >= b iff NOT (a < b)
let a_lt_b = lt(builder, a, b);
builder.add_inv_gate(a_lt_b)
}

#[cfg(test)]
mod tests {
use std::array::from_fn;
Expand Down
21 changes: 21 additions & 0 deletions crates/predicate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "mpz-predicate"
version = "0.1.0-alpha.14-pre"
edition = "2021"

[lints]
workspace = true

[dependencies]
bytes = { workspace = true }
serde = { version = "1", features = ["derive"] }
rand = { workspace = true }
rand_core = { workspace = true }
rand_chacha = { workspace = true }
thiserror = { workspace = true }
rangeset = { workspace = true, features = ["serde"] }
mpz-circuits = { workspace = true }
serde_json = { version = "*", features = ["raw_value"] }

[dev-dependencies]
rstest = { workspace = true }
Loading
Loading