From 2cd875b6803354e0375cd9e6ba0aff1b873992ef Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 23 Sep 2024 13:39:09 +0200 Subject: [PATCH 1/4] fix mismatch in number of zk rows in error message --- kimchi/src/prover.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kimchi/src/prover.rs b/kimchi/src/prover.rs index 14e043be07..41557a26e6 100644 --- a/kimchi/src/prover.rs +++ b/kimchi/src/prover.rs @@ -210,9 +210,9 @@ where .ok_or(ProverError::NoRoomForZkInWitness)?; let zero_knowledge_limit = zk_rows_strict_lower_bound(num_chunks); - if (index.cs.zk_rows as usize) < zero_knowledge_limit { + if (index.cs.zk_rows as usize) <= zero_knowledge_limit { return Err(ProverError::NotZeroKnowledge( - zero_knowledge_limit, + zero_knowledge_limit + 1, index.cs.zk_rows as usize, )); } From 424810a8455e3c8bd0a79208ce3bd406bd871312 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 23 Sep 2024 14:36:56 +0200 Subject: [PATCH 2/4] constants for default values --- kimchi/src/circuits/constraints.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kimchi/src/circuits/constraints.rs b/kimchi/src/circuits/constraints.rs index f8f2f20555..a5dd718db7 100644 --- a/kimchi/src/circuits/constraints.rs +++ b/kimchi/src/circuits/constraints.rs @@ -639,6 +639,10 @@ impl ConstraintSystem { } } +// +pub const NUM_CHUNKS_BY_DEFAULT: usize = 1; +pub const ZK_ROWS_BY_DEFAULT: u64 = 3; + pub fn zk_rows_strict_lower_bound(num_chunks: usize) -> usize { (2 * (PERMUTS + 1) * num_chunks - 2) / PERMUTS } From f585647a3dc712143e6931cc34977a037017f083 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 30 Sep 2024 14:17:33 +0200 Subject: [PATCH 3/4] adding comments for better understanding of the strict lower bound on zk rows --- kimchi/src/circuits/constraints.rs | 14 +++++++++++++- kimchi/src/prover.rs | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/kimchi/src/circuits/constraints.rs b/kimchi/src/circuits/constraints.rs index a5dd718db7..917eb4169f 100644 --- a/kimchi/src/circuits/constraints.rs +++ b/kimchi/src/circuits/constraints.rs @@ -639,10 +639,22 @@ impl ConstraintSystem { } } -// +/// The default number of chunks in a circuit is one (< 2^16 rows) pub const NUM_CHUNKS_BY_DEFAULT: usize = 1; + +/// The number of rows required for zero knowledge in circuits with one single chunk pub const ZK_ROWS_BY_DEFAULT: u64 = 3; +/// This function computes a strict lower bound in the number of rows required +/// for zero knowledge in circuits with `num_chunks` chunks. This means that at +/// least one needs 1 more row than the result of this function to achieve zero +/// knowledge. +/// Example: +/// for 1 chunk, this function returns 2, but at least 3 rows are needed +/// Note: +/// the number of zero knowledge rows is usually computed across the codebase +/// as the formula `(16 * num_chunks + 5) / 7`, which is precisely the formula +/// in this function plus one. pub fn zk_rows_strict_lower_bound(num_chunks: usize) -> usize { (2 * (PERMUTS + 1) * num_chunks - 2) / PERMUTS } diff --git a/kimchi/src/prover.rs b/kimchi/src/prover.rs index 41557a26e6..1c61e42936 100644 --- a/kimchi/src/prover.rs +++ b/kimchi/src/prover.rs @@ -210,6 +210,14 @@ where .ok_or(ProverError::NoRoomForZkInWitness)?; let zero_knowledge_limit = zk_rows_strict_lower_bound(num_chunks); + // Because the lower bound is strict, the result of the function above + // is not a sufficient number of zero knowledge rows, so the error must + // be raised anytime the number of zero knowledge rows is not greater + // than the strict lower bound. + // Example: + // for 1 chunk, `zero_knowledge_limit` is 2, and we need at least 3, + // thus the error should be raised and the message should say that the + // expected number of zero knowledge rows is 3 (hence the + 1). if (index.cs.zk_rows as usize) <= zero_knowledge_limit { return Err(ProverError::NotZeroKnowledge( zero_knowledge_limit + 1, From 0f1718b0b3065c812962470d2546fad28ebd7ae3 Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 30 Sep 2024 15:36:09 +0200 Subject: [PATCH 4/4] cargo fmt --- kimchi/src/circuits/constraints.rs | 8 ++++---- kimchi/src/prover.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kimchi/src/circuits/constraints.rs b/kimchi/src/circuits/constraints.rs index 917eb4169f..0e9d61faeb 100644 --- a/kimchi/src/circuits/constraints.rs +++ b/kimchi/src/circuits/constraints.rs @@ -648,13 +648,13 @@ pub const ZK_ROWS_BY_DEFAULT: u64 = 3; /// This function computes a strict lower bound in the number of rows required /// for zero knowledge in circuits with `num_chunks` chunks. This means that at /// least one needs 1 more row than the result of this function to achieve zero -/// knowledge. -/// Example: +/// knowledge. +/// Example: /// for 1 chunk, this function returns 2, but at least 3 rows are needed -/// Note: +/// Note: /// the number of zero knowledge rows is usually computed across the codebase /// as the formula `(16 * num_chunks + 5) / 7`, which is precisely the formula -/// in this function plus one. +/// in this function plus one. pub fn zk_rows_strict_lower_bound(num_chunks: usize) -> usize { (2 * (PERMUTS + 1) * num_chunks - 2) / PERMUTS } diff --git a/kimchi/src/prover.rs b/kimchi/src/prover.rs index 1c61e42936..95221b23bc 100644 --- a/kimchi/src/prover.rs +++ b/kimchi/src/prover.rs @@ -212,10 +212,10 @@ where let zero_knowledge_limit = zk_rows_strict_lower_bound(num_chunks); // Because the lower bound is strict, the result of the function above // is not a sufficient number of zero knowledge rows, so the error must - // be raised anytime the number of zero knowledge rows is not greater + // be raised anytime the number of zero knowledge rows is not greater // than the strict lower bound. // Example: - // for 1 chunk, `zero_knowledge_limit` is 2, and we need at least 3, + // for 1 chunk, `zero_knowledge_limit` is 2, and we need at least 3, // thus the error should be raised and the message should say that the // expected number of zero knowledge rows is 3 (hence the + 1). if (index.cs.zk_rows as usize) <= zero_knowledge_limit {