Skip to content

Commit

Permalink
refac(zk): show data ownership clearer
Browse files Browse the repository at this point in the history
To show the motivation of the function to accept argument to be moved, this
commit changes a argument type `T` to `T&&` for this purpose.
  • Loading branch information
chokobole committed Dec 4, 2023
1 parent c92eafb commit c1f00ef
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 76 deletions.
4 changes: 2 additions & 2 deletions tachyon/zk/base/blinded_polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class BlindedPolynomial {
using F = typename Poly::Field;

BlindedPolynomial() = default;
BlindedPolynomial(const Poly& poly, const F& blind)
: poly_(poly), blind_(blind) {}
BlindedPolynomial(Poly&& poly, const F& blind)
: poly_(std::move(poly)), blind_(blind) {}
BlindedPolynomial(Poly&& poly, F&& blind)
: poly_(std::move(poly)), blind_(std::move(blind)) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KZGCommitmentSchemeExtension

KZGCommitmentSchemeExtension() = default;
explicit KZGCommitmentSchemeExtension(
crypto::KZGCommitmentScheme<G1PointTy, G2PointTy, MaxDegree, Commitment>
crypto::KZGCommitmentScheme<G1PointTy, G2PointTy, MaxDegree, Commitment>&&
kzg)
: kzg_(std::move(kzg)) {}

Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/base/entities/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Entity {
using Poly = typename PCSTy::Poly;
using Commitment = typename PCSTy::Commitment;

Entity(PCSTy pcs, std::unique_ptr<Domain> domain,
Entity(PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<Transcript<Commitment>> transcript)
: pcs_(std::move(pcs)),
Expand Down
4 changes: 2 additions & 2 deletions tachyon/zk/base/entities/prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class Prover : public Entity<PCSTy> {
using Poly = typename PCSTy::Poly;
using Commitment = typename PCSTy::Commitment;

Prover(PCSTy pcs, std::unique_ptr<Domain> domain,
Prover(PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptWriter<Commitment>> writer,
Blinder<PCSTy> blinder)
Blinder<PCSTy>&& blinder)
: Entity<PCSTy>(std::move(pcs), std::move(domain),
std::move(extended_domain), std::move(writer)),
blinder_(std::move(blinder)) {
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/base/entities/verifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Verifier : public Entity<PCSTy> {
using ExtendedDomain = typename PCSTy::ExtendedDomain;
using Commitment = typename PCSTy::Commitment;

Verifier(PCSTy pcs, std::unique_ptr<Domain> domain,
Verifier(PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptReader<Commitment>> transcript)
: Entity<PCSTy>(std::move(pcs), std::move(domain),
Expand Down
11 changes: 6 additions & 5 deletions tachyon/zk/base/halo2/halo2_prover.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Halo2Prover : public Prover<PCSTy> {
using Commitment = typename PCSTy::Commitment;

static Halo2Prover CreateFromRandomSeed(
PCSTy pcs, std::unique_ptr<Domain> domain,
PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptWriter<Commitment>> writer,
size_t blinding_factors) {
Expand All @@ -37,7 +37,7 @@ class Halo2Prover : public Prover<PCSTy> {
}

static Halo2Prover CreateFromSeed(
PCSTy pcs, std::unique_ptr<Domain> domain,
PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptWriter<Commitment>> writer, uint8_t seed[16],
size_t blinding_factors) {
Expand All @@ -49,7 +49,7 @@ class Halo2Prover : public Prover<PCSTy> {
}

static Halo2Prover CreateFromRNG(
PCSTy pcs, std::unique_ptr<Domain> domain,
PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptWriter<Commitment>> writer,
std::unique_ptr<crypto::XORShiftRNG> rng, size_t blinding_factors) {
Expand All @@ -64,10 +64,11 @@ class Halo2Prover : public Prover<PCSTy> {
Halo2RandomFieldGenerator<F>* generator() { return generator_.get(); }

private:
Halo2Prover(PCSTy pcs, std::unique_ptr<Domain> domain,
Halo2Prover(PCSTy&& pcs, std::unique_ptr<Domain> domain,
std::unique_ptr<ExtendedDomain> extended_domain,
std::unique_ptr<TranscriptWriter<Commitment>> writer,
Blinder<PCSTy> blinder, std::unique_ptr<crypto::XORShiftRNG> rng,
Blinder<PCSTy>&& blinder,
std::unique_ptr<crypto::XORShiftRNG> rng,
std::unique_ptr<Halo2RandomFieldGenerator<F>> generator)
: Prover<PCSTy>(std::move(pcs), std::move(domain),
std::move(extended_domain), std::move(writer),
Expand Down
7 changes: 3 additions & 4 deletions tachyon/zk/lookup/lookup_committed.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class LookupCommitted {
public:
using F = typename Poly::Field;

LookupCommitted() = default;
LookupCommitted(BlindedPolynomial<Poly> permuted_input_poly,
BlindedPolynomial<Poly> permuted_table_poly,
BlindedPolynomial<Poly> product_poly)
LookupCommitted(BlindedPolynomial<Poly>&& permuted_input_poly,
BlindedPolynomial<Poly>&& permuted_table_poly,
BlindedPolynomial<Poly>&& product_poly)
: permuted_input_poly_(std::move(permuted_input_poly)),
permuted_table_poly_(std::move(permuted_table_poly)),
product_poly_(std::move(product_poly)) {}
Expand Down
7 changes: 3 additions & 4 deletions tachyon/zk/lookup/lookup_evaluated.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ class LookupEvaluated {
public:
using F = typename Poly::Field;

LookupEvaluated() = default;
LookupEvaluated(BlindedPolynomial<Poly> permuted_input_poly,
BlindedPolynomial<Poly> permuted_table_poly,
BlindedPolynomial<Poly> product_poly)
LookupEvaluated(BlindedPolynomial<Poly>&& permuted_input_poly,
BlindedPolynomial<Poly>&& permuted_table_poly,
BlindedPolynomial<Poly>&& product_poly)
: permuted_input_poly_(std::move(permuted_input_poly)),
permuted_table_poly_(std::move(permuted_table_poly)),
product_poly_(std::move(product_poly)) {}
Expand Down
9 changes: 4 additions & 5 deletions tachyon/zk/lookup/lookup_permuted.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ class LookupPermuted {
public:
using F = typename Poly::Field;

LookupPermuted() = default;
LookupPermuted(LookupPair<Evals> compressed_evals_pair,
LookupPair<Evals> permuted_evals_pair,
BlindedPolynomial<Poly> permuted_input_poly,
BlindedPolynomial<Poly> permuted_table_poly)
LookupPermuted(LookupPair<Evals>&& compressed_evals_pair,
LookupPair<Evals>&& permuted_evals_pair,
BlindedPolynomial<Poly>&& permuted_input_poly,
BlindedPolynomial<Poly>&& permuted_table_poly)
: compressed_evals_pair_(std::move(compressed_evals_pair)),
permuted_evals_pair_(std::move(permuted_evals_pair)),
permuted_input_poly_(std::move(permuted_input_poly)),
Expand Down
6 changes: 3 additions & 3 deletions tachyon/zk/plonk/circuit/assembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Assembly : public Assignment<typename PCSTy::Field> {
using AssignCallback = typename Assignment<F>::AssignCallback;

Assembly() = default;
Assembly(uint32_t k, std::vector<RationalEvals> fixed_columns,
PermutationAssembly<PCSTy> permutation,
std::vector<std::vector<bool>> selectors,
Assembly(uint32_t k, std::vector<RationalEvals>&& fixed_columns,
PermutationAssembly<PCSTy>&& permutation,
std::vector<std::vector<bool>>&& selectors,
base::Range<size_t> usable_rows)
: k_(k),
fixed_columns_(std::move(fixed_columns)),
Expand Down
3 changes: 3 additions & 0 deletions tachyon/zk/plonk/circuit/assigned_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define TACHYON_ZK_PLONK_CIRCUIT_ASSIGNED_CELL_H_

#include <string>
#include <utility>

#include "tachyon/zk/base/value.h"
#include "tachyon/zk/plonk/circuit/cell.h"
Expand All @@ -24,6 +25,8 @@ class AssignedCell {
AssignedCell() = default;
AssignedCell(const Cell& cell, const Value<F>& value)
: cell_(cell), value_(value) {}
AssignedCell(const Cell& cell, Value<F>&& value)
: cell_(cell), value_(std::move(value)) {}

const Cell& cell() const { return cell_; }
const Value<F>& value() const { return value_; }
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/plonk/circuit/circuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Circuit {

virtual std::unique_ptr<Circuit> WithoutWitness() const = 0;

virtual void Synthesize(Config config, Layouter<Field>* layouter) = 0;
virtual void Synthesize(Config&& config, Layouter<Field>* layouter) = 0;
};

} // namespace tachyon::zk
Expand Down
26 changes: 10 additions & 16 deletions tachyon/zk/plonk/circuit/examples/simple_circuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ class FieldConfig {
const Selector& s_mul() const { return s_mul_; }

private:
FieldConfig(std::array<AdviceColumnKey, 2> advice, InstanceColumnKey instance,
Selector s_mul)
: advice_(std::move(advice)),
instance_(std::move(instance)),
s_mul_(std::move(s_mul)) {}
FieldConfig(std::array<AdviceColumnKey, 2>&& advice,
const InstanceColumnKey& instance, const Selector& s_mul)
: advice_(std::move(advice)), instance_(instance), s_mul_(s_mul) {}

// For this chip, we will use two advice columns to implement our
// instructions. These are also the columns through which we communicate with
Expand All @@ -58,12 +56,10 @@ class FieldConfig {
template <typename F>
class FieldChip {
public:
FieldChip() = default;

static FieldConfig<F> Configure(ConstraintSystem<F>& meta,
std::array<AdviceColumnKey, 2> advice,
InstanceColumnKey instance,
FixedColumnKey constant) {
std::array<AdviceColumnKey, 2>&& advice,
const InstanceColumnKey& instance,
const FixedColumnKey& constant) {
meta.EnableEquality(instance);
meta.EnableConstant(constant);
for (const AdviceColumnKey& column : advice) {
Expand Down Expand Up @@ -107,8 +103,7 @@ class FieldChip {
(std::move(lhs) * std::move(rhs) - std::move(out))});
});

return FieldConfig<F>(std::move(advice), std::move(instance),
std::move(sel));
return FieldConfig<F>(std::move(advice), instance, sel);
}

AssignedCell<F> LoadPrivate(Layouter<F>* layouter,
Expand Down Expand Up @@ -168,7 +163,7 @@ class FieldChip {
private:
friend class SimpleCircuit<F>;

explicit FieldChip(FieldConfig<F> config) : config_(std::move(config)) {}
explicit FieldChip(FieldConfig<F>&& config) : config_(std::move(config)) {}

FieldConfig<F> config_;
};
Expand All @@ -195,11 +190,10 @@ class SimpleCircuit : public Circuit<FieldConfig<F>> {
// Create a fixed column to load constants.
FixedColumnKey constant = meta.CreateFixedColumn();

return FieldChip<F>::Configure(meta, std::move(advice), std::move(instance),
std::move(constant));
return FieldChip<F>::Configure(meta, std::move(advice), instance, constant);
}

void Synthesize(FieldConfig<F> config, Layouter<F>* layouter) override {
void Synthesize(FieldConfig<F>&& config, Layouter<F>* layouter) override {
FieldChip<F> field_chip(std::move(config));

// Load our private values into the circuit.
Expand Down
5 changes: 3 additions & 2 deletions tachyon/zk/plonk/circuit/floor_planner/simple_floor_planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class SimpleFloorPlanner {
public:
template <typename F, typename CircuitTy, typename Config>
static void Synthesize(Assignment<F>* assignment, CircuitTy& circuit,
Config config, std::vector<FixedColumnKey> constants) {
SingleChipLayouter layouter(assignment, std::move(constants));
Config&& config,
const std::vector<FixedColumnKey>& constants) {
SingleChipLayouter layouter(assignment, constants);
circuit.Synthesize(std::move(config));
}
};
Expand Down
4 changes: 2 additions & 2 deletions tachyon/zk/plonk/circuit/floor_planner/single_chip_layouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ class SingleChipLayouter : public Layouter<F> {
friend class Region;

SingleChipLayouter(Assignment<F>* assignment,
std::vector<FixedColumnKey> constants)
: assignment_(assignment), constants_(std::move(constants)) {}
const std::vector<FixedColumnKey>& constants)
: assignment_(assignment), constants_(constants) {}

// not owned
Assignment<F>* const assignment_;
Expand Down
10 changes: 5 additions & 5 deletions tachyon/zk/plonk/circuit/gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ template <typename F>
class Gate {
public:
Gate() = default;
Gate(std::string name, std::vector<std::string> constraint_names,
std::vector<std::unique_ptr<Expression<F>>> polys,
std::vector<Selector> queried_selectors,
std::vector<VirtualCell> queried_cells)
: name_(std::move(name)),
Gate(std::string_view name, std::vector<std::string>&& constraint_names,
std::vector<std::unique_ptr<Expression<F>>>&& polys,
std::vector<Selector>&& queried_selectors,
std::vector<VirtualCell>&& queried_cells)
: name_(std::string(name)),
constraint_names_(std::move(constraint_names)),
polys_(std::move(polys)),
queried_selectors_(std::move(queried_selectors)),
Expand Down
14 changes: 7 additions & 7 deletions tachyon/zk/plonk/circuit/selector_compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class SelectorCompressor {
std::vector<SelectorAssignment<F>> assignments;

Result() = default;
Result(std::vector<std::vector<F>> polys,
std::vector<SelectorAssignment<F>> assignments)
Result(std::vector<std::vector<F>>&& polys,
std::vector<SelectorAssignment<F>>&& assignments)
: polys(std::move(polys)), assignments(std::move(assignments)) {}
};

Expand All @@ -55,7 +55,7 @@ class SelectorCompressor {
// substitutions to the constraint system.
//
// This function is completely deterministic.
static Result Process(const std::vector<std::vector<bool>>& selectors_in,
static Result Process(std::vector<std::vector<bool>>&& selectors_in,
const std::vector<size_t>& degrees, size_t max_degree,
AllocateFixedColumnCallback callback) {
if (selectors_in.empty()) return {};
Expand All @@ -70,10 +70,10 @@ class SelectorCompressor {
auto zipped = base::Zipped(selectors_in, degrees);
std::vector<SelectorDescription> selectors = base::Map(
zipped.begin(), zipped.end(),
[](size_t selector_index,
const std::tuple<std::vector<bool>, size_t>& e) {
const auto& [activations, max_degree] = e;
return SelectorDescription(selector_index, activations, max_degree);
[](size_t selector_index, std::tuple<std::vector<bool>, size_t>& e) {
auto& [activations, max_degree] = e;
return SelectorDescription(selector_index, std::move(activations),
max_degree);
});

std::vector<F> combination_assignments;
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/plonk/circuit/selector_description.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace tachyon::zk {
class TACHYON_EXPORT SelectorDescription {
public:
SelectorDescription() = default;
SelectorDescription(size_t selector_index, std::vector<bool> activations,
SelectorDescription(size_t selector_index, std::vector<bool>&& activations,
size_t max_degree)
: selector_index_(selector_index),
activations_(std::move(activations)),
Expand Down
8 changes: 4 additions & 4 deletions tachyon/zk/plonk/constraint_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@ class ConstraintSystem {
}
CHECK(!polys.empty()) << "Gates must contain at least one constraint.";

gates_.emplace_back(std::string(name), std::move(constraint_names),
std::move(polys), std::move(queried_selectors),
std::move(queried_cells));
gates_.emplace_back(name, std::move(constraint_names), std::move(polys),
std::move(queried_selectors), std::move(queried_cells));
}

// This will compress selectors together depending on their provided
Expand Down Expand Up @@ -299,7 +298,8 @@ class ConstraintSystem {
std::vector<FixedColumnKey> new_columns;
typename SelectorCompressor<F>::Result result =
SelectorCompressor<F>::Process(
selectors, degrees, ComputeDegree(), [this, &new_columns]() {
std::move(selectors), degrees, ComputeDegree(),
[this, &new_columns]() {
FixedColumnKey column = CreateFixedColumn();
new_columns.push_back(column);
return ExpressionFactory<F>::Fixed(
Expand Down
6 changes: 3 additions & 3 deletions tachyon/zk/plonk/keys/proving_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class ProvingKey {
using Evals = typename PCSTy::Evals;

ProvingKey() = default;
ProvingKey(VerifyingKey<PCSTy> verifying_key, Poly l0, Poly l_last,
Poly l_active_row, Evals fixed_values, Evals fixed_polys,
PermutationProvingKey<Poly, Evals> permutation_proving_key)
ProvingKey(VerifyingKey<PCSTy>&& verifying_key, Poly&& l0, Poly&& l_last,
Poly&& l_active_row, Evals&& fixed_values, Evals&& fixed_polys,
PermutationProvingKey<Poly, Evals>&& permutation_proving_key)
: verifying_key_(std::move(verifying_key)),
l0_(std::move(l0)),
l_last_(std::move(l_last)),
Expand Down
6 changes: 3 additions & 3 deletions tachyon/zk/plonk/keys/verifying_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class VerifyingKey {
using Commitments = std::vector<Commitment>;

VerifyingKey() = default;
VerifyingKey(const Domain* domain, Commitments fixed_commitments,
PermutationVerifyingKey<PCSTy> permutation_verifying_key,
ConstraintSystem<F> constraint_system)
VerifyingKey(const Domain* domain, Commitments&& fixed_commitments,
PermutationVerifyingKey<PCSTy>&& permutation_verifying_key,
ConstraintSystem<F>&& constraint_system)
: domain_(domain),
fixed_commitments_(std::move(fixed_commitments)),
permutation_verifying_Key_(std::move(permutation_verifying_key)),
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/plonk/permutation/cycle_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TACHYON_EXPORT CycleStore {
class Table {
public:
Table() = default;
explicit Table(std::vector<std::vector<T>> values)
explicit Table(std::vector<std::vector<T>>&& values)
: values_(std::move(values)) {}

T& operator[](const Label& l) { return values_[l.col][l.row]; }
Expand Down
2 changes: 1 addition & 1 deletion tachyon/zk/plonk/permutation/permutation_committed.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <typename Poly>
class PermutationCommitted {
public:
explicit PermutationCommitted(
std::vector<BlindedPolynomial<Poly>> product_polys)
std::vector<BlindedPolynomial<Poly>>&& product_polys)
: product_polys_(std::move(product_polys)) {}

std::vector<BlindedPolynomial<Poly>>&& product_polys() && {
Expand Down
3 changes: 1 addition & 2 deletions tachyon/zk/plonk/permutation/permutation_evaluated.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ namespace tachyon::zk {
template <typename Poly>
class PermutationEvaluated {
public:
PermutationEvaluated() = default;
explicit PermutationEvaluated(
std::vector<BlindedPolynomial<Poly>> product_polys)
std::vector<BlindedPolynomial<Poly>>&& product_polys)
: product_polys_(std::move(product_polys)) {}

const std::vector<BlindedPolynomial<Poly>>& product_polys() const {
Expand Down

0 comments on commit c1f00ef

Please sign in to comment.