diff --git a/barretenberg/cpp/src/barretenberg/transcript/transcript.hpp b/barretenberg/cpp/src/barretenberg/transcript/transcript.hpp index 032bc8472945..a3bf578fb22d 100644 --- a/barretenberg/cpp/src/barretenberg/transcript/transcript.hpp +++ b/barretenberg/cpp/src/barretenberg/transcript/transcript.hpp @@ -211,7 +211,7 @@ template class BaseTranscript { * @param label of the element sent * @param element_frs serialized */ - void consume_prover_element_frs(const std::string& label, std::span element_frs) + void add_element_frs_to_hash_buffer(const std::string& label, std::span element_frs) { if (use_manifest) { // Add an entry to the current round of the manifest @@ -350,11 +350,37 @@ template class BaseTranscript { return call_get_challenges(); } + /** + * @brief Adds an element to the transcript. + * @details Serializes the element to frs and adds it to the current_round_data buffer. Does NOT add the element to + * the proof. + * + * @param label Human-readable name for the challenge. + * @param element Element to be added. + */ + template void add_to_hash_buffer(const std::string& label, const T& element) + { + DEBUG_LOG(label, element); + + // TODO(Adrian): Ensure that serialization of affine elements (including point at infinity) is consistent. + // TODO(Adrian): Consider restricting serialization (via concepts) to types T for which sizeof(T) reliably + // returns the size of T in frs. (E.g. this is true for std::array but not for std::vector). + // convert element to field elements + auto element_frs = TranscriptParams::convert_to_bn254_frs(element); + +#ifdef LOG_INTERACTIONS + if constexpr (Loggable) { + info("consumed: ", label, ": ", element); + } +#endif + BaseTranscript::add_element_frs_to_hash_buffer(label, element_frs); + } + /** * @brief Adds a prover message to the transcript, only intended to be used by the prover. * * @details Serializes the provided object into `proof_data`, and updates the current round state in - * consume_prover_element_frs. + * add_element_frs_to_hash_buffer. * * @param label Description/name of the object being added. * @param element Serializable object that will be added to the transcript @@ -379,7 +405,7 @@ template class BaseTranscript { info("sent: ", label, ": ", element); } #endif - BaseTranscript::consume_prover_element_frs(label, element_frs); + BaseTranscript::add_element_frs_to_hash_buffer(label, element_frs); } /** @@ -397,7 +423,7 @@ template class BaseTranscript { auto element_frs = std::span{ proof_data }.subspan(num_frs_read, element_size); num_frs_read += element_size; - BaseTranscript::consume_prover_element_frs(label, element_frs); + BaseTranscript::add_element_frs_to_hash_buffer(label, element_frs); auto element = TranscriptParams::template convert_from_bn254_frs(element_frs); DEBUG_LOG(label, element); diff --git a/barretenberg/cpp/src/barretenberg/transcript/transcript.test.cpp b/barretenberg/cpp/src/barretenberg/transcript/transcript.test.cpp index 69e5c91df3c5..092d52320132 100644 --- a/barretenberg/cpp/src/barretenberg/transcript/transcript.test.cpp +++ b/barretenberg/cpp/src/barretenberg/transcript/transcript.test.cpp @@ -144,4 +144,18 @@ TEST(NativeTranscript, TwoProversTwoFields) EXPECT_TRUE(received_k.is_point_at_infinity()); EXPECT_EQ(received_k, elt_k); } -} \ No newline at end of file +} + +/** + * @brief Test the add_to_hash_buffer functionality + * + */ +TEST(NativeTranscript, ConsumeElement) +{ + Transcript prover_transcript, verifier_transcript; + prover_transcript.add_to_hash_buffer("a", Fr(1)); + verifier_transcript.add_to_hash_buffer("a", Fr(1)); + auto prover_chal = prover_transcript.get_challenge("alpha"); + auto verifier_chal = verifier_transcript.get_challenge("alpha"); + EXPECT_EQ(prover_chal, verifier_chal); +}