Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
kzg.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
14
15#include <memory>
16#include <utility>
17
18namespace bb {
19
20template <typename Curve_> class KZG {
21 public:
22 using Curve = Curve_;
25 using Fr = typename Curve::ScalarField;
27 using GroupElement = typename Curve::Element;
30
39 template <typename Transcript>
40 static void compute_opening_proof(const CK& ck,
41 const ProverOpeningClaim<Curve>& opening_claim,
42 const std::shared_ptr<Transcript>& prover_trancript)
43 {
44 Polynomial quotient = opening_claim.polynomial;
45 OpeningPair<Curve> pair = opening_claim.opening_pair;
46 Commitment quotient_commitment;
47
48 if (opening_claim.polynomial.is_empty()) {
49 // We treat the empty polynomial as the zero polynomial
50 quotient_commitment = Commitment::infinity();
51 } else {
52 quotient.at(0) = quotient[0] - pair.evaluation;
53 // Computes the coefficients for the quotient polynomial q(X) = (p(X) - v) / (X - r) through an FFT
54 quotient.factor_roots(pair.challenge);
55 quotient_commitment = ck.commit(quotient);
56 }
57
58 // TODO(#479): for now we compute the KZG commitment directly to unify the KZG and IPA interfaces but in the
59 // future we might need to adjust this to use the incoming alternative to work queue (i.e. variation of
60 // pthreads) or even the work queue itself
61 prover_trancript->send_to_verifier("KZG:W", quotient_commitment);
62 };
63
74 template <typename Transcript>
76 const std::shared_ptr<Transcript>& verifier_transcript)
77 {
78 auto quotient_commitment = verifier_transcript->template receive_from_prover<Commitment>("KZG:W");
79
80 // Note: The pairing check can be expressed naturally as
81 // e(C - v * [1]_1, [1]_2) = e([W]_1, [X - r]_2) where C =[p(X)]_1. This can be rearranged (e.g. see the plonk
82 // paper) as e(C + r*[W]_1 - v*[1]_1, [1]_2) * e(-[W]_1, [X]_2) = 1, or e(P_0, [1]_2) * e(P_1, [X]_2) = 1
83 GroupElement P_0;
84 if constexpr (Curve::is_stdlib_type) {
85 // Express operation as a batch_mul in order to use Goblinization if available
86 auto builder = quotient_commitment.get_context();
87 auto one = Fr(builder, 1);
88 std::vector<GroupElement> commitments = { claim.commitment,
89 quotient_commitment,
90 GroupElement::one(builder) };
91 std::vector<Fr> scalars = { one, claim.opening_pair.challenge, -claim.opening_pair.evaluation };
92 P_0 = GroupElement::batch_mul(commitments, scalars);
93
94 } else {
95 P_0 = claim.commitment;
96 P_0 += quotient_commitment * claim.opening_pair.challenge;
97 P_0 -= GroupElement::one() * claim.opening_pair.evaluation;
98 }
99
100 auto P_1 = -quotient_commitment;
101 return { P_0, P_1 };
102 };
103
121 template <typename Transcript>
123 const std::shared_ptr<Transcript>& transcript)
124 {
125 auto quotient_commitment = transcript->template receive_from_prover<Commitment>("KZG:W");
126
127 // The pairing check can be expressed as
128 // e(C + [W]₁ ⋅ z, [1]₂) * e(−[W]₁, [X]₂) = 1, where C = ∑ commitmentsᵢ ⋅ scalarsᵢ.
129 GroupElement P_0;
130 // Place the commitment to W to 'commitments'
131 batch_opening_claim.commitments.emplace_back(quotient_commitment);
132 // Update the scalars by adding the Shplonk evaluation challenge z
133 batch_opening_claim.scalars.emplace_back(batch_opening_claim.evaluation_point);
134 // Compute C + [W]₁ ⋅ z
135 if constexpr (Curve::is_stdlib_type) {
136 P_0 = GroupElement::batch_mul(batch_opening_claim.commitments,
137 batch_opening_claim.scalars,
138 /*max_num_bits=*/0,
139 /*with_edgecases=*/true);
140 } else {
141 P_0 = batch_mul_native(batch_opening_claim.commitments, batch_opening_claim.scalars);
142 }
143 auto P_1 = -quotient_commitment;
144
145 return { P_0, P_1 };
146 }
147};
148} // namespace bb
CommitmentKey object over a pairing group 𝔾₁.
typename Curve::AffineElement Commitment
Definition kzg.hpp:26
typename Curve::Element GroupElement
Definition kzg.hpp:27
std::array< GroupElement, 2 > VerifierAccumulator
Definition kzg.hpp:29
Curve_ Curve
Definition kzg.hpp:22
static VerifierAccumulator reduce_verify_batch_opening_claim(BatchOpeningClaim< Curve > batch_opening_claim, const std::shared_ptr< Transcript > &transcript)
Computes the input points for the pairing check needed to verify a KZG opening claim obtained from a ...
Definition kzg.hpp:122
static VerifierAccumulator reduce_verify(const OpeningClaim< Curve > &claim, const std::shared_ptr< Transcript > &verifier_transcript)
Computes the input points for the pairing check needed to verify a KZG opening claim of a single poly...
Definition kzg.hpp:75
typename Curve::ScalarField Fr
Definition kzg.hpp:25
static void compute_opening_proof(const CK &ck, const ProverOpeningClaim< Curve > &opening_claim, const std::shared_ptr< Transcript > &prover_trancript)
Computes the KZG commitment to an opening proof polynomial at a single evaluation point.
Definition kzg.hpp:40
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition claim.hpp:53
OpeningPair< Curve > opening_pair
Definition claim.hpp:62
Commitment commitment
Definition claim.hpp:64
Opening pair (r,v) for some witness polynomial p(X) such that p(r) = v.
Definition claim.hpp:19
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
bool is_empty() const
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
void factor_roots(const Fr &root)
Divides p(X) by (X-r) in-place. Assumes that p(rⱼ)=0 for all j.
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:34
Polynomial polynomial
Definition claim.hpp:39
OpeningPair< Curve > opening_pair
Definition claim.hpp:40
typename Group::element Element
Definition grumpkin.hpp:55
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:62
typename Group::affine_element AffineElement
Definition grumpkin.hpp:56
AluTraceBuilder builder
Definition alu.test.cpp:123
Entry point for Barretenberg command-line interface.
CommitmentKey< Curve > ck
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
An accumulator consisting of the Shplonk evaluation challenge and vectors of commitments and scalars.
Definition claim.hpp:169
Curve::ScalarField evaluation_point
Definition claim.hpp:172
std::vector< typename Curve::AffineElement > commitments
Definition claim.hpp:170
std::vector< typename Curve::ScalarField > scalars
Definition claim.hpp:171