Skip to content

Commit

Permalink
Cg/poc base case partial (#72)
Browse files Browse the repository at this point in the history
* Lots of work in the now-extinct #65.
* Composer tests shows memory issue
* NOT working; TwoGates too trivial.
* Zero out univariate accumulator (hack).
* Init relation works.
* Add failure tests.
* Added Sage notebook.
*Cleanup.
  • Loading branch information
codygunton authored and dbanks12 committed Jan 27, 2023
1 parent 41187fe commit b5528ab
Show file tree
Hide file tree
Showing 27 changed files with 849 additions and 338 deletions.
279 changes: 279 additions & 0 deletions cpp/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "b1c5160a",
"metadata": {},
"source": [
"# Barretenberg Notebook\n",
"\n",
"This is a start on a Sage notebook for checking one's work. I think we shouldn't build this out systematically, but periodically update it as we find the need to build some new functionality."
]
},
{
"cell_type": "markdown",
"id": "0a1dead8-3d16-4175-a17b-217249302996",
"metadata": {},
"source": [
"# General Utilities"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1510ae41-d51c-4230-bbe5-24f3a5556b85",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{0x6c7301b49d85a46c, 0x44311531e39c64f6, 0xb13d66d8d6c1a24c, 0x04410c360230a295}'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Convert a 256-bit hex string. Outputs four 64-bit limbs that can be used to construct a native field element.\n",
"# Idea: s == barretenberg::fr(hex_to_field(s)).to_montgomery_form() is true as hex strings if s was in Montgomery form.\n",
"def hex_to_field(s):\n",
" s = s[2:] # slice off leading 0x\n",
" data = [s[48:64], s[32:48], s[16:32], s[0:16]]\n",
" data = [\"0x\" + d for d in data]\n",
" out = ', '.join(data)\n",
" return \"{\" + out + \"}\"\n",
"\n",
"hex_to_field('0x04410c360230a295b13d66d8d6c1a24c44311531e39c64f66c7301b49d85a46c')"
]
},
{
"cell_type": "markdown",
"id": "2bf8f9d7-8b17-4711-a52d-3da21337bfd4",
"metadata": {},
"source": [
"# Basic Structures"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1785930b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order of BN254 is: 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001\n",
"21888242871839275222246405745257275088548364400416034343698204186575808495615\n",
"X^2 + 21888242871839275222246405745257275088548364400416034343698204186575808495616\n"
]
}
],
"source": [
"# The BN254 prime.\n",
"prime_r = 21888242871839275222246405745257275088548364400416034343698204186575808495617\n",
"Fr = GF(prime_r)\n",
"Polys.<X> = PolynomialRing(Fr)\n",
"print(\"Order of BN254 is: \", hex(prime_r))\n",
"print(Fr(-2))\n",
"print((X + 1) * (X - 1))"
]
},
{
"cell_type": "markdown",
"id": "c28762ec",
"metadata": {},
"source": [
"# Sumcheck Utilities\n",
"Here we generate generate a two-gate example of a sumcheck calculation. The output of `get_values` is the expected `Univariate` produced after the first round."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "796ab188",
"metadata": {},
"outputs": [],
"source": [
"def get_values(poly):\n",
" return [poly(i) for i in range(4)]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "335dbb65-d2e3-4003-a8e9-576bd131a148",
"metadata": {},
"outputs": [],
"source": [
"def convert_to_polys(edge_list):\n",
" return [edge[0] * (1-X) + edge[1] * X for edge in edge_list]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1dfb5ac7-bcdf-4341-87e6-6377ac1ba176",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['0x0',\n",
" '0x0',\n",
" '0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffffff',\n",
" '0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffffb']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"edge_list = [[0, 1], [0, 1], [0, 2], [0, 0], [1, 1], [0, 1], [0, -1], [0, 0]]\n",
"w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c = convert_to_polys(edge_list)\n",
"[hex(val) for val in get_values(q_m * w_l * w_r + q_l * w_l + q_r * w_r + q_o * w_o + q_c)]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "715f394b-e9f6-4311-8960-c13826070d08",
"metadata": {},
"outputs": [],
"source": [
"# edge_list = [[0,0] for _ in range(8)]\n",
"# w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c = convert_to_polys(edge_list)\n",
"# get_values(q_m * w_l * w_r + q_l * w_l + q_r * w_r + q_o * w_o + q_c)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "810f52e0-6f5e-4f91-b0b8-e8f6878dfe66",
"metadata": {},
"outputs": [],
"source": [
"u_2 = 0x2cb413b29041834eb4c859d50a771208a5e3dec2cd9bb2a1a13e5a57c6160691"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6fb88b21-efb7-447b-93b9-50627286965d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[20219933756566846493536316042331920548925056837893251409966172577112016684689, 0], [20219933756566846493536316042331920548925056837893251409966172577112016684689, 0], [18551624641294417764826226339406566009301749275370468476234140967648224873761, 0], [0, 0], [1, 0], [20219933756566846493536316042331920548925056837893251409966172577112016684689, 0], [1668309115272428728710089702925354539623307562522782933732031609463791810928, 0], [0, 0]]\n"
]
}
],
"source": [
"folded = [[poly(u_2), 0] for poly in [w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c]]\n",
"print(folded)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "40667a20-c063-4d51-b35b-5f62ea7d8890",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"folded == [[u_2, 0], [u_2, 0], [2*u_2, 0], [0, 0], [1, 0], [u_2, 0], [-u_2, 0], [0, 0]]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ecd54ffa-b3b4-4f5b-81c4-de1c3250a595",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['0x2cb413b29041834eb4c859d50a771208a5e3dec2cd9bb2a1a13e5a57c6160691',\n",
" '0x2cb413b29041834eb4c859d50a771208a5e3dec2cd9bb2a1a13e5a57c6160691',\n",
" '0x2903d8f23f516673b1406df3936ccbb42393d53d217df4b1fe9abf1b9c2c0d21',\n",
" '0x0',\n",
" '0x1',\n",
" '0x2cb413b29041834eb4c859d50a771208a5e3dec2cd9bb2a1a13e5a57c6160691',\n",
" '0x3b03ac050f01cdb0387ebe1770a465482500985ac1dbdefa2a39b3c29e9f970',\n",
" '0x0']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[hex(Fr(first)) for [first, second] in folded]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "8e00fb7f-0085-45e0-af29-79ee9784842e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['0x18ea803098b5a8b9d02047be7a4f24cf4db4c25696323b82b34625f98827c179',\n",
" '0x0',\n",
" '0x18ea803098b5a8b9d02047be7a4f24cf4db4c25696323b82b34625f98827c179',\n",
" '0x2e163dca0736293cfe0938ce639e282e66b38c965560ce84554acbe409f05e2']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c = convert_to_polys(folded)\n",
"[hex(val) for val in get_values(q_m * w_l * w_r + q_l * w_l + q_r * w_r + q_o * w_o + q_c)]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 9.7",
"language": "sage",
"name": "sagemath"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <proof_system/flavor/flavor.hpp>

namespace honk {
enum StandardSelectors { QM, QC, Q1, Q2, Q3, NUM };
enum StandardSelectors { QM, Q1, Q2, Q3, QC, NUM };
inline std::vector<std::string> standard_selector_names()
{
std::vector<std::string> result{ "q_m", "q_c", "q_1", "q_2", "q_3" };
std::vector<std::string> result{ "q_m", "q_1", "q_2", "q_3", "q_c" };
return result;
}

Expand All @@ -27,6 +27,8 @@ class StandardCircuitConstructor : public CircuitConstructorBase<STANDARD_HONK_W
w_l.reserve(size_hint);
w_r.reserve(size_hint);
w_o.reserve(size_hint);
// To effieciently constrain wires to zero, we set the first value of w_1 to be 0, and use copy constraints for
// all future zero values.
zero_idx = put_constant_variable(barretenberg::fr::zero());
};

Expand Down
31 changes: 12 additions & 19 deletions cpp/src/aztec/honk/composer/composer_helper/composer_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ std::shared_ptr<waffle::proving_key> ComposerHelper<CircuitConstructor>::compute
selector_values.emplace_back(fr::zero());
}

// TODO: Now that we can't accomodate this, what do we do?
// // TODO(Cody): We used to use a nonzero value to avoid the zero selector case.
// selector_values.emplace_back(i + 1);
selector_values.emplace_back(fr::zero());

// Compute selector vector
polynomial selector_poly_lagrange(subgroup_size);
Expand Down Expand Up @@ -267,15 +268,15 @@ std::shared_ptr<waffle::verification_key> ComposerHelper<CircuitConstructor>::co
* @return The verifier.
* */
template <typename CircuitConstructor>
waffle::Verifier ComposerHelper<CircuitConstructor>::create_verifier(CircuitConstructor& circuit_constructor)
StandardVerifier ComposerHelper<CircuitConstructor>::create_verifier(CircuitConstructor& circuit_constructor)
{
compute_verification_key(circuit_constructor);
// TODO figure out types, actuallt
auto verification_key = compute_verification_key(circuit_constructor);
// TODO figure out types, actually
// circuit_verification_key->composer_type = type;

// TODO: initialize verifier according to manifest and key
// Verifier output_state(circuit_verification_key, create_manifest(public_inputs.size()));
waffle::Verifier output_state;
StandardVerifier output_state;
// TODO: Do we need a commitment scheme defined here?
// std::unique_ptr<KateCommitmentScheme<standard_settings>> kate_commitment_scheme =
// std::make_unique<KateCommitmentScheme<standard_settings>>();
Expand All @@ -286,14 +287,15 @@ waffle::Verifier ComposerHelper<CircuitConstructor>::create_verifier(CircuitCons
}

template <typename CircuitConstructor>
waffle::UnrolledVerifier ComposerHelper<CircuitConstructor>::create_unrolled_verifier(
StandardUnrolledVerifier ComposerHelper<CircuitConstructor>::create_unrolled_verifier(
CircuitConstructor& circuit_constructor)
{
compute_verification_key(circuit_constructor);
// UnrolledVerifier output_state(circuit_verification_key,
// create_unrolled_manifest(circuit_constructor.n,
// circuit_constructor.public_inputs.size()));
waffle::UnrolledVerifier output_state;
StandardUnrolledVerifier output_state(
circuit_verification_key,
honk::StandardHonk::create_unrolled_manifest(circuit_constructor.public_inputs.size(),
numeric::get_msb(circuit_verification_key->n)));
// StandardUnrolledVerifier output_state;

// TODO: Deal with commitments
// std::unique_ptr<KateCommitmentScheme<unrolled_standard_settings>> kate_commitment_scheme =
Expand All @@ -317,15 +319,6 @@ StandardUnrolledProver ComposerHelper<CircuitConstructor>::create_unrolled_prove
auto manifest = Flavor::create_unrolled_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds);
StandardUnrolledProver output_state(circuit_proving_key, manifest);

// TODO: Initialize constraints
// std::unique_ptr<ProverPermutationWidget<3, false>> permutation_widget =
// std::make_unique<ProverPermutationWidget<3, false>>(circuit_proving_key.get());
// std::unique_ptr<ProverArithmeticWidget<unrolled_standard_settings>> arithmetic_widget =
// std::make_unique<ProverArithmeticWidget<unrolled_standard_settings>>(circuit_proving_key.get());

// output_state.random_widgets.emplace_back(std::move(permutation_widget));
// output_state.transition_widgets.emplace_back(std::move(arithmetic_widget));

// TODO(Cody): This should be more generic
std::unique_ptr<pcs::kzg::CommitmentKey> kate_commitment_key =
std::make_unique<pcs::kzg::CommitmentKey>(circuit_proving_key->n, "../srs_db/ignition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ template <typename CircuitConstructor> class ComposerHelper {
{
compute_witness_base<program_width>(circuit_constructor);
}
waffle::Verifier create_verifier(CircuitConstructor& circuit_constructor);

StandardVerifier create_verifier(CircuitConstructor& circuit_constructor);
/**
* Preprocess the circuit. Delegates to create_prover.
*
Expand All @@ -55,7 +56,7 @@ template <typename CircuitConstructor> class ComposerHelper {
StandardProver preprocess(CircuitConstructor& circuit_constructor) { return create_prover(circuit_constructor); };
StandardProver create_prover(CircuitConstructor& circuit_constructor);

waffle::UnrolledVerifier create_unrolled_verifier(CircuitConstructor& circuit_constructor);
StandardUnrolledVerifier create_unrolled_verifier(CircuitConstructor& circuit_constructor);

template <typename Flavor> StandardUnrolledProver create_unrolled_prover(CircuitConstructor& circuit_constructor);

Expand Down
Loading

0 comments on commit b5528ab

Please sign in to comment.