Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zk): implement LogUp scheme #420

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Conversation

Insun35
Copy link
Contributor

@Insun35 Insun35 commented May 20, 2024

Implement logarithmic derivative lookup scheme.
Logarithmic derivatives translate products of linear factors into sums of their reciprocals, turning zeroes into simple poles of same multiplicity. It reduces computation cost using grand sum argument instead of grand product argument.
See Paper 2022/1530

Referenced Scroll Halo2 implementation.
We named Logup not MVLookup because this implementation uses univariate polynomial commitment scheme.

@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch 3 times, most recently from feb2a27 to e0f3423 Compare May 20, 2024 12:44
@Insun35 Insun35 marked this pull request as draft May 21, 2024 05:06
@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch 3 times, most recently from 35d5ddf to 8a314d3 Compare May 26, 2024 09:04
@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch 4 times, most recently from d144562 to 6f6be38 Compare May 30, 2024 04:23
@Insun35 Insun35 marked this pull request as ready for review May 30, 2024 04:35
@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch 3 times, most recently from 71cf1d6 to ca0d474 Compare May 30, 2024 06:55
// clang-format off
// (1 - (l_last + l_blind)) * Z(X) * (A_compressed(X) + β) * (S_compressed(X) + γ)
// clang-format on
size_t{2} + max_input_degree + max_table_degree);
}

private:
static size_t GetExprDegree(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about renaming it to GetExprMaxDegree()

Comment on lines 178 to 197
std::vector<std::vector<std::unique_ptr<Expression<F>>>> inputs_expressions_ =
{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<std::vector<std::unique_ptr<Expression<F>>>> inputs_expressions_ =
{};
std::vector<std::vector<std::unique_ptr<Expression<F>>>> inputs_expressions_;

return lookup_pairs;
});

EXPECT_EQ(constraint_system.lookups().size() - 1, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPECT_EQ(constraint_system.lookups().size() - 1, 0);
EXPECT_EQ(constraint_system.lookups().size() , 1);

return lookup_pairs;
});

EXPECT_EQ(constraint_system.lookups().size() - 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPECT_EQ(constraint_system.lookups().size() - 1, 1);
EXPECT_EQ(constraint_system.lookups().size(), 2);

return lookup_pairs;
});

EXPECT_EQ(constraint_system.lookups().size() - 1, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPECT_EQ(constraint_system.lookups().size() - 1, 0);
EXPECT_EQ(constraint_system.lookups().size(), 1);

Comment on lines 11 to 14
#include <memory>
#include <ostream>
#include <string>
#include <vector>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include <ostream>
#include <string>

Comment on lines 37 to 49
template <typename LookupTracker>
class RustDebugStringifier<absl::btree_map<std::string, LookupTracker>> {
public:
static std::ostream& AppendToStream(
std::ostream& os, RustFormatter& fmt,
const absl::btree_map<std::string, LookupTracker>& lookups_map) {
DebugMap debug_map = fmt.DebugMap();
for (const auto& [key, value] : lookups_map) {
debug_map.Pair(key, value);
}
return os << debug_map.Finish();
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be defined as RustDebugStringifier<absl::btree_map<K, V>> and moved to somewhere else.

Comment on lines 16 to 20
#include "absl/container/btree_map.h"

#include "tachyon/base/strings/rust_stringifier.h"
#include "tachyon/zk/plonk/constraint_system/lookup_tracker.h"
#include "tachyon/zk/plonk/halo2/stringifiers/expression_stringifier.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please match deps with header and apply these changes to all.

@@ -0,0 +1,53 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this blank


using Prover = lookup::logup::Prover<Poly, Evals>;

static constexpr Type type = Type::kLogUp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minor , but please use constexpr first!

Suggested change
static constexpr Type type = Type::kLogUp;
constexpr static Type type = Type::kLogUp;

@chokobole
Copy link
Contributor

Please remove this statement from the commit body of feat(zk): add constraint_system lookup logic for LookupsMap

Also add kInvalid type to avoid using Lookup() and LookupAny()
without setting a lookup type.

@chokobole
Copy link
Contributor

chokobole commented May 30, 2024

Please add links to the rust reference code to every single commit!

@chokobole
Copy link
Contributor

You need to add license of Geometry Research. Does Scroll take this code from here, right?

@chokobole
Copy link
Contributor

chokobole commented May 30, 2024

Please change the type of fix(zk): fix wrong iterator access, feat(zk): add circuit test flags for LogUp and feat(zk): implement MultiLookupCircuit to test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update all the comments in RequiredDegree().

// τ(X) = t(X) + β
// LHS = τ(X) * Π(φᵢ(X)) * (ϕ(ω * X) - ϕ(X))
// = max_table_degree + max_input_degree + 1
// RHS = τ(X) * Π(φᵢ(X)) * ((Σ 1/φᵢ(X)) - m(X) / τ(X))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// RHS = τ(X) * Π(φᵢ(X)) * ((Σ 1/φᵢ(X)) - m(X) / τ(X))
// RHS = τ(X) * Π(φᵢ(X)) * ((Σ 1/φᵢ(X)) - m(X) / τ(X))
// = max_input_degree + 1

@@ -108,16 +137,13 @@ class Argument {
// (1 - (l_last(X) + l_blind(X))) * (A′(X) − S′(X)) * (A′(X) − A′(ω⁻¹ * X)) = 0
// clang-format on
size_t max_input_degree = std::accumulate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size_t max_input_degree = std::accumulate(
size_t max_input_degree_sum = std::accumulate(

});
lookup::Pairs<std::unique_ptr<Expression<F>>, LookupTableColumn> pairs =
std::move(callback).Run(cells);
if (lookup_type_ == lookup::Type::kHalo2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use switch statement.

@@ -565,6 +713,39 @@ class ConstraintSystem {
FRIEND_TEST(ConstraintSystemTest, Lookup);
FRIEND_TEST(ConstraintSystemTest, LookupAny);

template <typename VirtualCells>
std::unique_ptr<Expression<F>> MakeTable(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving this to VirtualCells and changing the function name to QueryLookupTable ?

"lhs", config_.a(), 0, [&values]() { return values[0]; });
ret.push_back(lhs);

region.AssignAdvice("lhs^4", config_.d(), 0, [&values]() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
region.AssignAdvice("lhs^4", config_.d(), 0, [&values]() {
region.AssignAdvice("lhs", config_.d(), 0, [&values]() {

"rhs", config_.b(), 0, [&values]() { return values[1]; });
ret.push_back(rhs);

region.AssignAdvice("rhs^4", config_.e(), 0, [&values]() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
region.AssignAdvice("rhs^4", config_.e(), 0, [&values]() {
region.AssignAdvice("rhs", config_.e(), 0, [&values]() {


AssignedCell<F> rhs = region.AssignAdvice(
"rhs", config_.b(), 0, [&values]() { return values[1]; });
ret.push_back(rhs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret.push_back(rhs);
ret.push_back(std::move(rhs));

ret.push_back(rhs);

region.AssignAdvice("rhs^4", config_.e(), 0, [&values]() {
return values[1].SquareImpl().SquareImpl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return values[1].SquareImpl().SquareImpl();
return values[1].Pow(4);


AssignedCell<F> out = region.AssignAdvice(
"out", config_.c(), 0, [&values]() { return values[2]; });
ret.push_back(out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret.push_back(out);
ret.push_back(std::move(out));

Change input expression interface in lookup argument to support Logup scheme.
Change return type of `Lookup()` and `LookupAny()` from size_t (size of
lookups) to void, because it is redundant.
CloneExpressions function clones a vector of
`std::unique_ptr<Expression>`.
It is needed to handle both lookups and lookups_map in LogUp.
@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch 2 times, most recently from 62bf7f2 to 56a3412 Compare June 3, 2024 08:09
Insun35 added 19 commits June 3, 2024 17:13
Add `Lookup()`, `LookupAny()`, and `ChunkLookup()` for LogUp scheme.
Constraint system select a logic dynamically using lookup type.

- LookupTracker: holds table and input expressions with its name
- LookupsMap: btree_map with table_expressions_identifier as a key and
  LookupTracker as values.
Add btree_map_stringifier and lookup_tracker_stringifier.
MultiLookupCircuit to be introduced in the following commit requires 2^8
kMaxExtendedDomainSize.
@Insun35 Insun35 force-pushed the feat/implement-logup-scheme branch from 56a3412 to aae25a7 Compare June 3, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants