forked from FISCO-BCOS/verkle-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30cffd0
commit 279c2b1
Showing
15 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
project(ipa) | ||
|
||
file(GLOB_RECURSE SRCS ipa/*.cpp) | ||
file(GLOB_RECURSE HEADERS ipa/*.h) | ||
|
||
add_library(ipa ${SRCS} ${HEADERS}) | ||
add_dependencies(ipa bandersnatch utilities) | ||
target_include_directories(ipa PUBLIC ../bandersnatch ipa) | ||
target_link_libraries(ipa PUBLIC bandersnatch utilities) | ||
|
||
if (TESTS) | ||
enable_testing() | ||
set(CTEST_OUTPUT_ON_FAILURE TRUE) | ||
add_subdirectory(test) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
#include "bandersnatch/Fr.h" | ||
#include "bandersnatch/Element.h" | ||
|
||
|
||
namespace verkle::ipa | ||
{ | ||
class IPAConfig | ||
{ | ||
public: | ||
bandersnatch::Element commitToPoly(const std::vector<bandersnatch::Fr>& scalars); | ||
private: | ||
std::vector<bandersnatch::Element> m_srs; | ||
bandersnatch::Element q; | ||
uint32_t rounds; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "Transcript.h" | ||
|
||
using namespace verkle::ipa; | ||
|
||
Transcript::Transcript(SeperateLabel label) | ||
{ | ||
auto lv = getLabelValue(label); | ||
m_state.update(lv, strlen(lv)); | ||
} | ||
|
||
const char* Transcript::getLabelValue(const SeperateLabel& label) | ||
{ | ||
return labelValues.at(label); | ||
} | ||
|
||
void Transcript::appendLabel(SeperateLabel label) | ||
{ | ||
m_buffer << getLabelValue(label); | ||
} | ||
|
||
void Transcript::appendScalar(const bandersnatch::Fr& scalar, SeperateLabel label) | ||
{ | ||
uint64_t out[4]; | ||
scalar.serialize(out); | ||
appendLabel(label); | ||
for (size_t i = 0; i < 4; ++i) | ||
{ | ||
m_buffer << out[i]; | ||
} | ||
} | ||
|
||
void Transcript::appendPoint(const bandersnatch::Element& point, SeperateLabel label) | ||
{ | ||
byte out[96]; | ||
point.serialize(out); | ||
appendLabel(label); | ||
for (size_t i = 0; i < 96; ++i) | ||
{ | ||
m_buffer << out[i]; | ||
} | ||
} | ||
|
||
verkle::bandersnatch::Fr Transcript::generateChallenge(SeperateLabel label) | ||
{ | ||
// fetch buffer and reset it | ||
appendLabel(label); | ||
auto str = m_buffer.str(); | ||
auto combined = str.c_str(); | ||
m_buffer.str(""); | ||
m_buffer.clear(); | ||
|
||
// use buffer hash to generate a challenge | ||
unsigned char hash[32]; | ||
m_state.update(combined, strlen(combined)); | ||
m_state.finalize(hash); | ||
bandersnatch::Fr ret(hash, sizeof(hash)); | ||
|
||
// add the new challenge to the state | ||
// which "summarises" the previous state before we cleared it | ||
appendScalar(ret, label); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
#include "bandersnatch/Fr.h" | ||
#include "bandersnatch/Element.h" | ||
#include "verkleutils/Hash.h" | ||
#include <sstream> | ||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace verkle::ipa | ||
{ | ||
enum SeperateLabel | ||
{ | ||
LABEL_IPA, | ||
LABEL_COMMITMENT, | ||
LABEL_INPUT_POINT, | ||
LABEL_OUTPPUT_POINT, | ||
LABEL_RESCALING, | ||
LABEL_LEFT, | ||
LABEL_RIGHT, | ||
LABEL_X, | ||
}; | ||
|
||
// The transcript is for challenge scalars generation. | ||
class Transcript | ||
{ | ||
private: | ||
std::stringstream m_buffer; | ||
utils::Hash m_state; | ||
static const std::unordered_map<SeperateLabel, const char*> labelValues; | ||
static const char* getLabelValue(const SeperateLabel& label); | ||
|
||
public: | ||
Transcript(SeperateLabel label); | ||
void appendLabel(SeperateLabel label); | ||
void appendScalar(const bandersnatch::Fr& scalar, SeperateLabel label); | ||
void appendPoint(const bandersnatch::Element& point, SeperateLabel label); | ||
|
||
bandersnatch::Fr generateChallenge(SeperateLabel label); | ||
}; | ||
|
||
inline const std::unordered_map<SeperateLabel, const char*> Transcript::labelValues = { | ||
{LABEL_IPA, "ipa"}, | ||
{LABEL_COMMITMENT, "commitment"}, | ||
{LABEL_INPUT_POINT, "input point"}, | ||
{LABEL_OUTPPUT_POINT, "output point"}, | ||
{LABEL_RESCALING, "rescaling"}, | ||
{LABEL_LEFT, "left"}, | ||
{LABEL_RIGHT, "right"}, | ||
{LABEL_X, "x"} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
file(GLOB_RECURSE TEST_SRCS "*.cpp") | ||
|
||
add_executable(ipa-test ${TEST_SRCS}) | ||
target_include_directories(ipa-test PRIVATE . ..) | ||
|
||
find_package(boost_unit_test_framework CONFIG REQUIRED) | ||
|
||
target_link_libraries(ipa-test PRIVATE utilities ipa Boost::unit_test_framework) | ||
add_test(NAME test-bandersnatch WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND ipa-test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// Created by Zhengxuan Guo on 2024/8/15. | ||
// | ||
#include "ipa/Transcript.h" | ||
#include "testutils/TestPromptFixture.h" | ||
#include <boost/test/unit_test.hpp> | ||
|
||
namespace verkle::test | ||
{ | ||
BOOST_FIXTURE_TEST_SUITE(TransCriptTest, TestPromptFixture) | ||
|
||
BOOST_AUTO_TEST_CASE(testCallChallengeScalarTwice) | ||
{ | ||
ipa::Transcript tr(ipa::LABEL_IPA); | ||
auto challenge_1 = tr.generateChallenge(ipa::LABEL_X); | ||
auto challenge_2 = tr.generateChallenge(ipa::LABEL_X); | ||
BOOST_ASSERT(challenge_1 != challenge_2); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(testChallengeScalar) | ||
{ | ||
ipa::Transcript tr(ipa::LABEL_IPA); | ||
auto challenge = tr.generateChallenge(ipa::LABEL_X); | ||
uint64_t out[4]; | ||
challenge.serialize(out); | ||
|
||
uint64_t raw[4] = { | ||
0xe9f26c96d15bb807, | ||
0x6ad84e7137609152, | ||
0xe193b5e1ce7d01c4, | ||
0x27625b6a622c0518, | ||
}; | ||
bandersnatch::Fr exp(raw); | ||
BOOST_ASSERT(challenge == exp); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(testAppendScalar) | ||
{ | ||
uint64_t raw1[4] = { | ||
0xe9f26c96d15bb807, | ||
0x6ad84e7137609152, | ||
0xe193b5e1ce7d01c4, | ||
0x27625b6a622c0518, | ||
}; | ||
bandersnatch::Fr base(raw1); | ||
ipa::Transcript tr(ipa::LABEL_LEFT); | ||
|
||
// append scalar twice | ||
tr.appendScalar(base, ipa::LABEL_RESCALING); | ||
tr.appendScalar(base, ipa::LABEL_RESCALING); | ||
|
||
auto challenge = tr.generateChallenge(ipa::LABEL_X); | ||
uint64_t out[4]; | ||
challenge.serialize(out); | ||
|
||
uint64_t raw2[4] = { | ||
0xe9f26c96d15bb807, | ||
0x6ad84e7137609152, | ||
0xe193b5e1ce7d01c4, | ||
0x27625b6a622c0518, | ||
}; | ||
bandersnatch::Fr exp(raw2); | ||
BOOST_ASSERT(challenge == exp); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(testAppendPoint) | ||
{ | ||
auto g = bandersnatch::Element::generator(); | ||
ipa::Transcript tr(ipa::LABEL_LEFT); | ||
|
||
// append point twice | ||
tr.appendPoint(g, ipa::LABEL_INPUT_POINT); | ||
tr.appendPoint(g, ipa::LABEL_OUTPPUT_POINT); | ||
|
||
auto challenge = tr.generateChallenge(ipa::LABEL_X); | ||
uint64_t out[4]; | ||
challenge.serialize(out); | ||
|
||
uint64_t raw2[4] = { | ||
0xe9f26c96d15bb807, | ||
0x6ad84e7137609152, | ||
0xe193b5e1ce7d01c4, | ||
0x27625b6a622c0518, | ||
}; | ||
bandersnatch::Fr exp(raw2); | ||
BOOST_ASSERT(challenge == exp); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(testAppendLabel) | ||
{ | ||
auto g = bandersnatch::Element::generator(); | ||
ipa::Transcript tr(ipa::LABEL_LEFT); | ||
|
||
// append point and some labels | ||
tr.appendPoint(g, ipa::LABEL_INPUT_POINT); | ||
tr.appendLabel(ipa::LABEL_COMMITMENT); | ||
tr.appendPoint(g, ipa::LABEL_OUTPPUT_POINT); | ||
tr.appendLabel(ipa::LABEL_RIGHT); | ||
|
||
auto challenge = tr.generateChallenge(ipa::LABEL_X); | ||
uint64_t out[4]; | ||
challenge.serialize(out); | ||
|
||
uint64_t raw2[4] = { | ||
0xe9f26c96d15bb807, | ||
0x6ad84e7137609152, | ||
0xe193b5e1ce7d01c4, | ||
0x27625b6a622c0518, | ||
}; | ||
bandersnatch::Fr exp(raw2); | ||
BOOST_ASSERT(challenge == exp); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#define BOOST_TEST_MODULE VERKLE_Tests | ||
#define BOOST_TEST_MAIN | ||
|
||
#include <boost/test/included/unit_test.hpp> | ||
#include <boost/test/unit_test.hpp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
project(utilities) | ||
|
||
find_package(OpenSSL CONFIG REQUIRED) | ||
find_package(boost_unit_test_framework CONFIG REQUIRED) | ||
find_package(boost_filesystem CONFIG REQUIRED) | ||
|
||
add_library(utilities INTERFACE) | ||
target_include_directories(utilities INTERFACE .) | ||
file(GLOB_RECURSE SRCS *.cpp) | ||
file(GLOB_RECURSE HEADERS *.h) | ||
|
||
add_library(utilities ${SRCS} ${HEADERS}) | ||
target_include_directories(utilities PUBLIC .) | ||
target_link_libraries(utilities PUBLIC OpenSSL::Crypto) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "Hash.h" | ||
|
||
using namespace verkle::utils; | ||
|
||
Hash::Hash() : m_ctx(EVP_MD_CTX_new()) | ||
{ | ||
EVP_DigestInit_ex(m_ctx, EVP_sha256(), NULL); | ||
} | ||
|
||
Hash::~Hash() | ||
{ | ||
EVP_MD_CTX_free(m_ctx); | ||
} | ||
|
||
void Hash::update(const void* data, size_t len) | ||
{ | ||
EVP_DigestUpdate(m_ctx, data, len); | ||
} | ||
|
||
void Hash::finalize(unsigned char out[32]) | ||
{ | ||
unsigned int _len; | ||
EVP_DigestFinal_ex(m_ctx, out, &_len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
#include <openssl/evp.h> | ||
|
||
namespace verkle::utils | ||
{ | ||
class Hash | ||
{ | ||
private: | ||
EVP_MD_CTX* m_ctx; | ||
public: | ||
Hash(); | ||
~Hash(); | ||
void update(const void* data, size_t len); | ||
void finalize(unsigned char out[32]); | ||
}; | ||
} |
Oops, something went wrong.