diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.hpp index 1ee7a101af80..b503bd49e8dd 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.hpp @@ -65,7 +65,9 @@ class TranscriptManifest { */ template class BaseTranscript { // TODO(Adrian): Make these tweakable + public: static constexpr size_t HASH_OUTPUT_SIZE = 32; + private: static constexpr size_t MIN_BYTES_PER_CHALLENGE = 128 / 8; // 128 bit challenges size_t round_number = 0; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/honk/transcript/trancript.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/honk/transcript/trancript.hpp index 95ad753b727e..c180e9afda47 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/honk/transcript/trancript.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/honk/transcript/trancript.hpp @@ -9,21 +9,18 @@ #include "barretenberg/stdlib/primitives/bigfield/bigfield.hpp" #include "barretenberg/stdlib/primitives/biggroup/biggroup.hpp" #include "barretenberg/stdlib/primitives/field/field.hpp" +#include "barretenberg/stdlib/utility/utility.hpp" //TODO(luke): this namespace will be sensible once stdlib is moved out of the plonk namespace namespace proof_system::plonk::stdlib::recursion::honk { template class Transcript { public: using field_pt = field_t; - using witness_pt = witness_t; - using fq_pt = bigfield; - using group_pt = element; using FF = barretenberg::fr; - using Commitment = barretenberg::g1::affine_element; using VerifierTranscript = proof_system::honk::VerifierTranscript; - template using Univariate = proof_system::honk::sumcheck::Univariate; + using Utility = utility::StdlibUtility; - static constexpr size_t HASH_OUTPUT_SIZE = 32; // WORKTODO: Duplicated from native transcript + static constexpr size_t HASH_OUTPUT_SIZE = VerifierTranscript::HASH_OUTPUT_SIZE; VerifierTranscript native_transcript; Builder* builder; @@ -53,7 +50,7 @@ template class Transcript { native_challenges = native_transcript.get_challenges(labels...); /* - * TODO(luke): Do stdlib hashing here. E.g., for the current pedersen/blake setup, we could write data into a + * TODO(#1351): Do stdlib hashing here. E.g., for the current pedersen/blake setup, we could write data into a * byte_array as it is received from prover, then compress via pedersen and apply blake3s. Not doing this now * since it's a pain and we'll be revamping our hashing anyway. For now, simply convert the native hashes to * stdlib types without adding any hashing constraints. @@ -77,7 +74,7 @@ template class Transcript { // Compute the indicated challenge from the native transcript auto native_challenge = native_transcript.get_challenge(label); - // TODO(luke): Stdlib hashing here... + // TODO(1351): Stdlib hashing here... return field_pt(native_challenge); } @@ -94,81 +91,8 @@ template class Transcript { // Extract the native element from the native transcript T element = native_transcript.template receive_from_prover(label); - // Add variables corresponding to the witness element and return the corresponding stdlib type - return stdlib_type_from_witness(element); - } - - private: - // Series of overloaded methods for converting native types extracted from the native transcript to the - // corresponding stdlib type for output. - // TODO(luke): Eventually these can also add data to a buffer (byte_array) to be hashed. - - /** - * @brief Construct stdlib field from uint32_t - * - * @param element - * @return field_pt - */ - field_pt stdlib_type_from_witness(uint32_t native_element) - { - auto element = witness_pt(builder, native_element); - - return element; - } - - /** - * @brief Construct stdlib field from native field type - * - * @param native_element - * @return field_pt - */ - field_pt stdlib_type_from_witness(FF native_element) - { - auto element = witness_pt(builder, native_element); - - return element; - } - - /** - * @brief Construct stdlib group from native affine group element type - * - * @param native_element - * @return field_pt - */ - group_pt stdlib_type_from_witness(Commitment native_element) - { - auto element = group_pt::from_witness(builder, native_element); - - return element; - } - - /** - * @brief Construct field_t array from native field array - * @param native_element Array of FF - * @return std::array - */ - template std::array stdlib_type_from_witness(std::array native_element) - { - std::array element; - for (size_t i = 0; i < LENGTH; ++i) { - element[i] = witness_pt(builder, native_element[i]); - } - return element; - } - - /** - * @brief Construct field_t array from native Univariate type - * TODO(luke): do we need a stdlib Univariate or is std::array good enough? - * @param native_element - * @return std::array - */ - template std::array stdlib_type_from_witness(Univariate native_element) - { - std::array element; - for (size_t i = 0; i < LENGTH; ++i) { - element[i] = witness_pt(builder, native_element.value_at(i)); - } - return element; + // Return the corresponding stdlib type + return Utility::from_witness(builder, element); } }; } // namespace proof_system::plonk::stdlib::recursion::honk diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/utility/utility.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/utility/utility.hpp new file mode 100644 index 000000000000..717672689fa3 --- /dev/null +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/utility/utility.hpp @@ -0,0 +1,99 @@ +#pragma once + +#include "barretenberg/ecc/curves/bn254/fq.hpp" +#include "barretenberg/ecc/curves/bn254/fr.hpp" +#include "barretenberg/ecc/curves/bn254/g1.hpp" +#include "barretenberg/honk/sumcheck/polynomials/univariate.hpp" +#include "barretenberg/honk/transcript/transcript.hpp" + +#include "barretenberg/stdlib/primitives/bigfield/bigfield.hpp" +#include "barretenberg/stdlib/primitives/biggroup/biggroup.hpp" +#include "barretenberg/stdlib/primitives/field/field.hpp" + +namespace proof_system::plonk::stdlib::recursion::utility { + +/** + * @brief Utility class for converting native types to corresponding stdlib types + * + * @details Used to facilitate conversion of various native types (uint32_t, field, group, Univarite, etc.) to + * corresponding stdlib types. Useful for example for obtaining stdlib types in the recursive trancript from native + * types upon deserialization from the native transcript. + * + * @todo Eliminate the need for these somehow? + * @tparam Builder + */ +template class StdlibUtility { + using field_ct = field_t; + using witness_ct = witness_t; + using fq_ct = bigfield; + using group_ct = element; + using FF = barretenberg::fr; + using Commitment = barretenberg::g1::affine_element; + template using Univariate = proof_system::honk::sumcheck::Univariate; + + public: + /** + * @brief Construct stdlib field from uint32_t + * + * @param element + * @return field_ct + */ + static field_ct from_witness(Builder* builder, uint32_t native_element) + { + return field_ct::from_witness(builder, native_element); + } + + /** + * @brief Construct stdlib field from native field type + * + * @param native_element + * @return field_ct + */ + static field_ct from_witness(Builder* builder, FF native_element) + { + return field_ct::from_witness(builder, native_element); + } + + /** + * @brief Construct stdlib group from native affine group element type + * + * @param native_element + * @return field_ct + */ + static group_ct from_witness(Builder* builder, Commitment native_element) + { + return group_ct::from_witness(builder, native_element); + } + + /** + * @brief Construct field_t array from native field array + * @param native_element Array of FF + * @return std::array + */ + template + static std::array from_witness(Builder* builder, std::array native_element) + { + std::array element; + for (size_t i = 0; i < LENGTH; ++i) { + element[i] = field_ct::from_witness(builder, native_element[i]); + } + return element; + } + + /** + * @brief Construct field_t array from native Univariate type + * TODO(luke): do we need a stdlib Univariate or is std::array good enough? + * @param native_element + * @return std::array + */ + template + static std::array from_witness(Builder* builder, Univariate native_element) + { + std::array element; + for (size_t i = 0; i < LENGTH; ++i) { + element[i] = field_ct::from_witness(builder, native_element.value_at(i)); + } + return element; + } +}; +} // namespace proof_system::plonk::stdlib::recursion::utility \ No newline at end of file