Skip to content

Commit

Permalink
fix hash finalize & Fr constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhengxuan committed Aug 27, 2024
1 parent 279c2b1 commit bb47de2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions bandersnatch/bandersnatch/Fr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion bandersnatch/bandersnatch/Fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions bandersnatch/test/unittests/FrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
File renamed without changes.
29 changes: 14 additions & 15 deletions ipa/ipa/Transcript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(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<const char*>(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<char*>(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
Expand Down
File renamed without changes.
32 changes: 16 additions & 16 deletions ipa/test/TranscriptTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions utilities/verkleutils/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit bb47de2

Please sign in to comment.