Skip to content
Closed
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: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tracing = "0.1.41"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "ansi"] }
base64 = "0.22.1"
ruint = { version = "1.12.3", features = ["num-traits", "rand"] }
rand = "0.8.5"
rand = "0.9.0"
subtle = "2.6.1"
bytemuck = "1.19.0"
hex-literal = "0.4.1"
Expand Down
2 changes: 1 addition & 1 deletion merkle-hash-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ icicle-bn254 = { git = "https://github.com/ingonyama-zk/icicle.git", branch = "m
icicle-m31 = { git = "https://github.com/ingonyama-zk/icicle.git", branch = "main", optional = true }

# ZKHash
zkhash = { git = "https://extgit.isec.tugraz.at/krypto/zkfriendlyhashzoo.git", commit = "f252d2b2767bc88659615601ac13e19f52c2c11f", optional = true }
zkhash = { git = "https://extgit.isec.tugraz.at/krypto/zkfriendlyhashzoo.git", rev = "f252d2b2767bc88659615601ac13e19f52c2c11f", optional = true }

# Stwo
stwo-prover = { git = "https://github.com/starkware-libs/stwo.git", version = "0.1.1", optional = true }
8 changes: 6 additions & 2 deletions noir-r1cs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![doc = include_str!("../README.md")]
mod compiler;
mod r1cs_emitter;
mod sparse_matrix;
mod utils;

Expand Down Expand Up @@ -119,6 +120,7 @@ fn noir(args: NoirCmd) -> AnyResult<()> {
witness[2] = Some(FieldElement::from(5678_u32)); // b

// Solve constraints (this is how Noir expects it to be done, judging from ACVM)
info!("Solving for witness.");
for row in 0..r1cs.constraints {
let [a, b, c] =
[&r1cs.a, &r1cs.b, &r1cs.c].map(|mat| sparse_dot(mat.iter_row(row), &witness));
Expand All @@ -135,9 +137,10 @@ fn noir(args: NoirCmd) -> AnyResult<()> {
let Some((col, val)) = solve_dot(mat.iter_row(row), &witness, val) else {
panic!("Could not solve constraint {row}.")
};
eprintln!("Constraint {row}: Solved for witness[{col}] = {val}");
// eprintln!("Constraint {row}: Solved for witness[{col}] = {val}");
witness[col] = Some(val);
}
info!("Witness found.");

// Complete witness with entropy.
// TODO: Use better entropy source and proper sampling.
Expand All @@ -147,7 +150,7 @@ fn noir(args: NoirCmd) -> AnyResult<()> {
.map(|f| f.unwrap_or_else(|| FieldElement::from(rng.random::<u128>())))
.collect::<Vec<_>>();

dbg!(&witness);
// dbg!(&witness);

// Verify
let a = mat_mul(&r1cs.a, &witness);
Expand All @@ -160,6 +163,7 @@ fn noir(args: NoirCmd) -> AnyResult<()> {
.for_each(|(row, ((&a, &b), &c))| {
assert_eq!(a * b, c, "Constraint {row} failed");
});
info!("Witness verified.");

// dbg!(&a);
// dbg!(&b);
Expand Down
38 changes: 16 additions & 22 deletions noir-r1cs/src/compiler.rs → noir-r1cs/src/r1cs/acir_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
crate::SparseMatrix,
super::ConstraintSink,
acir::{
circuit::{Circuit, Opcode},
native_types::{Expression, Witness},
Expand All @@ -8,30 +8,24 @@ use {
std::collections::BTreeMap,
};

/// Represents a R1CS constraint system.
/// Represents a R1CS constraint source from ACIR.
#[derive(Debug, Clone)]
pub struct R1CS {
pub a: SparseMatrix<FieldElement>,
pub b: SparseMatrix<FieldElement>,
pub c: SparseMatrix<FieldElement>,

// Remapping of witness indices to the r1cs_witness array
pub witnesses: usize,
pub remap: BTreeMap<usize, usize>,

pub constraints: usize,
pub struct AcirSource<'a> {
circuit: &'a Circuit<FieldElement>,
witnesses: usize,
constraints: usize,
witness_map: Vec<usize>,
}

impl R1CS {
pub fn new() -> Self {
Self {
a: SparseMatrix::new(0, 1, FieldElement::zero()),
b: SparseMatrix::new(0, 1, FieldElement::zero()),
c: SparseMatrix::new(0, 1, FieldElement::zero()),
witnesses: 1,
remap: BTreeMap::new(),
constraints: 0,
}
impl<'a> AcirSource<'a> {
pub fn new(circuit: &'a Circuit<FieldElement>) -> Self {
let witness_map = vec![None; circuit.num_witnesses()];
// Do a preprocessing pass to compute the dimensions and witness map.
Self {}
}

pub fn emit(&self, sink: &mut impl ConstraintSink) {
todo!();
}

pub fn add_circuit(&mut self, circuit: &Circuit<FieldElement>) {
Expand Down
36 changes: 36 additions & 0 deletions noir-r1cs/src/r1cs/matrix_sink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// Represents a R1CS constraint system as Sparse Matrices.
#[derive(Debug, Clone)]
pub struct MatrixSink {
pub a: SparseMatrix<FieldElement>,
pub b: SparseMatrix<FieldElement>,
pub c: SparseMatrix<FieldElement>,
}

impl MatrixSink {
pub fn new(witnesses: usize, constraints: usize) -> Self {
Self {
a: SparseMatrix::new(constraints, witnesses, FieldElement::zero()),
b: SparseMatrix::new(constraints, witnesses, FieldElement::zero()),
c: SparseMatrix::new(constraints, witnesses, FieldElement::zero()),
}
}
}

impl ConstraintSink for MatrixSink {
fn add_constraint(
&mut self,
a: &[(FieldElement, usize)],
b: &[(FieldElement, usize)],
c: &[(FieldElement, usize)],
) {
for (c, col) in a.iter().copied() {
self.a.set(row, col, c)
}
for (c, col) in b.iter().copied() {
self.b.set(row, col, c)
}
for (c, col) in c.iter().copied() {
self.c.set(row, col, c)
}
}
}
9 changes: 9 additions & 0 deletions noir-r1cs/src/r1cs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub trait ConstraintSink {
/// Add an R1CS constraint.
fn add_constraint(
&mut self,
a: &[(FieldElement, usize)],
b: &[(FieldElement, usize)],
c: &[(FieldElement, usize)],
);
}
Loading