diff --git a/bandersnatch/bandersnatch/Fr.cpp b/bandersnatch/bandersnatch/Fr.cpp index 06fefe7..6f793cd 100644 --- a/bandersnatch/bandersnatch/Fr.cpp +++ b/bandersnatch/bandersnatch/Fr.cpp @@ -28,10 +28,10 @@ Fr::Fr(const uint64_t a[4]) blst_fr_from_uint64(&m_val, a); } -Fr::Fr(const uint8_t* msg, size_t len) +Fr::Fr(const uint8_t* msg, size_t nbits) { blst_scalar scalar; - blst_scalar_from_le_bytes(&scalar, msg, (len+7)/8); + blst_scalar_from_le_bytes(&scalar, msg, (nbits+7)/8); blst_fr_from_scalar(&m_val, &scalar); } diff --git a/bandersnatch/bandersnatch/Fr.h b/bandersnatch/bandersnatch/Fr.h index cca2c57..49178f9 100644 --- a/bandersnatch/bandersnatch/Fr.h +++ b/bandersnatch/bandersnatch/Fr.h @@ -17,7 +17,7 @@ class Fr // deserialize Fr(const uint64_t a[4]); - Fr(const byte* msg, size_t len); + Fr(const byte* msg, size_t nbits); static Fr zero(); static Fr random(); diff --git a/bandersnatch/test/unittests/FrTest.cpp b/bandersnatch/test/unittests/FrTest.cpp index 48812ff..0cf8945 100644 --- a/bandersnatch/test/unittests/FrTest.cpp +++ b/bandersnatch/test/unittests/FrTest.cpp @@ -18,8 +18,8 @@ BOOST_AUTO_TEST_CASE(testFromBytes) 0x8a, 0xdd, 0x2d, 0x50, 0x08, 0x7b, 0xda, 0x61, 0xe2, 0xf7, 0x02, 0xa0, 0x3c, 0xb8, 0xf1, 0x3e }; - bandersnatch::Fr t1(a, 32); - bandersnatch::Fr t2(a, 32); + bandersnatch::Fr t1(a, 256); + bandersnatch::Fr t2(a, 256); BOOST_ASSERT(t1 == t2); } diff --git a/ipa/CMakelists.txt b/ipa/CMakeLists.txt similarity index 100% rename from ipa/CMakelists.txt rename to ipa/CMakeLists.txt diff --git a/ipa/ipa/Transcript.cpp b/ipa/ipa/Transcript.cpp index 4a82235..07cd7de 100644 --- a/ipa/ipa/Transcript.cpp +++ b/ipa/ipa/Transcript.cpp @@ -20,40 +20,39 @@ void Transcript::appendLabel(SeperateLabel label) void Transcript::appendScalar(const bandersnatch::Fr& scalar, SeperateLabel label) { + appendLabel(label); + uint64_t out[4]; scalar.serialize(out); - appendLabel(label); - for (size_t i = 0; i < 4; ++i) - { - m_buffer << out[i]; - } + m_buffer.write(reinterpret_cast(out), sizeof(out)); } void Transcript::appendPoint(const bandersnatch::Element& point, SeperateLabel label) { + appendLabel(label); + byte out[96]; point.serialize(out); - appendLabel(label); - for (size_t i = 0; i < 96; ++i) - { - m_buffer << out[i]; - } + m_buffer.write(reinterpret_cast(out), sizeof(out)); } verkle::bandersnatch::Fr Transcript::generateChallenge(SeperateLabel label) { - // fetch buffer and reset it appendLabel(label); - auto str = m_buffer.str(); - auto combined = str.c_str(); + + // fetch buffer and reset it + auto len = m_buffer.rdbuf()->in_avail(); + byte* combined = new byte[len]; + m_buffer.read(reinterpret_cast(combined), len); m_buffer.str(""); m_buffer.clear(); // use buffer hash to generate a challenge + m_state.update(combined, len); + delete[] combined; unsigned char hash[32]; - m_state.update(combined, strlen(combined)); m_state.finalize(hash); - bandersnatch::Fr ret(hash, sizeof(hash)); + bandersnatch::Fr ret(hash, 32*8*sizeof(unsigned char)); // add the new challenge to the state // which "summarises" the previous state before we cleared it diff --git a/ipa/test/CMakelists.txt b/ipa/test/CMakeLists.txt similarity index 100% rename from ipa/test/CMakelists.txt rename to ipa/test/CMakeLists.txt diff --git a/ipa/test/TranscriptTest.cpp b/ipa/test/TranscriptTest.cpp index f131dc6..83e1438 100644 --- a/ipa/test/TranscriptTest.cpp +++ b/ipa/test/TranscriptTest.cpp @@ -25,10 +25,10 @@ BOOST_AUTO_TEST_CASE(testChallengeScalar) challenge.serialize(out); uint64_t raw[4] = { - 0xe9f26c96d15bb807, - 0x6ad84e7137609152, - 0xe193b5e1ce7d01c4, - 0x27625b6a622c0518, + 0x9c8ca8bdb2785ef8, + 0x7f5c036bd7f1b221, + 0x69d73d9566c4ea5d, + 0x46d9ee65bc2da30d, }; bandersnatch::Fr exp(raw); BOOST_ASSERT(challenge == exp); @@ -54,10 +54,10 @@ BOOST_AUTO_TEST_CASE(testAppendScalar) challenge.serialize(out); uint64_t raw2[4] = { - 0xe9f26c96d15bb807, - 0x6ad84e7137609152, - 0xe193b5e1ce7d01c4, - 0x27625b6a622c0518, + 0xa1458f845d64bdc, + 0x6240c0aaf1ccf2c1, + 0xaa1ea719e83b622c, + 0x50a286aee96a8e8c, }; bandersnatch::Fr exp(raw2); BOOST_ASSERT(challenge == exp); @@ -77,10 +77,10 @@ BOOST_AUTO_TEST_CASE(testAppendPoint) challenge.serialize(out); uint64_t raw2[4] = { - 0xe9f26c96d15bb807, - 0x6ad84e7137609152, - 0xe193b5e1ce7d01c4, - 0x27625b6a622c0518, + 0x3d7bcf250fbbd4ca, + 0x2e34916fb1c77331, + 0x342beba88f4caf1b, + 0x165ec3c487655fc3, }; bandersnatch::Fr exp(raw2); BOOST_ASSERT(challenge == exp); @@ -102,10 +102,10 @@ BOOST_AUTO_TEST_CASE(testAppendLabel) challenge.serialize(out); uint64_t raw2[4] = { - 0xe9f26c96d15bb807, - 0x6ad84e7137609152, - 0xe193b5e1ce7d01c4, - 0x27625b6a622c0518, + 0x65d00a1fd12c7f1a, + 0x3fa282eb46713efa, + 0x7103e2c705f23b7c, + 0xe26e3e9526d325d, }; bandersnatch::Fr exp(raw2); BOOST_ASSERT(challenge == exp); diff --git a/utilities/verkleutils/Hash.cpp b/utilities/verkleutils/Hash.cpp index db6d7bb..e52d2a6 100644 --- a/utilities/verkleutils/Hash.cpp +++ b/utilities/verkleutils/Hash.cpp @@ -21,4 +21,6 @@ void Hash::finalize(unsigned char out[32]) { unsigned int _len; EVP_DigestFinal_ex(m_ctx, out, &_len); + EVP_MD_CTX_reset(m_ctx); + EVP_DigestInit_ex(m_ctx, EVP_sha256(), NULL); } \ No newline at end of file