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
19dc675
commit 29c1ae4
Showing
26 changed files
with
391 additions
and
68 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,42 @@ | ||
// | ||
// Created by Zhengxuan Guo on 2024/8/30. | ||
// | ||
#include "PrecomputedElements.h" | ||
|
||
using namespace verkle::bandersnatch; | ||
|
||
PrecomputedElements::PrecomputedElements(Element::ElementListPtr const& points, size_t window) | ||
: m_window(window), | ||
m_numPoints(points->size()), | ||
m_table(std::make_shared<blst_p1_affine[]>([window, this]() { | ||
auto const bits = blst_p1s_mult_wbits_precompute_sizeof(window, m_numPoints); | ||
return bits / sizeof(blst_p1_affine); | ||
}())) | ||
{ | ||
blst_p1_affine affinePoints[m_numPoints]; | ||
for (size_t i = 0; i < m_numPoints; ++i) | ||
{ | ||
blst_p1_to_affine(&affinePoints[i], &points->at(i).m_point); | ||
} | ||
|
||
const blst_p1_affine* pointsArg[2] = {affinePoints, nullptr}; | ||
blst_p1s_mult_wbits_precompute(m_table.get(), m_window, pointsArg, m_numPoints); | ||
} | ||
|
||
Element PrecomputedElements::msm(Fr::FrListPtr const& scalars) const | ||
{ | ||
size_t sz = blst_p1s_mult_wbits_scratch_sizeof(m_numPoints); | ||
limb_t scratch[sz/sizeof(limb_t)]; | ||
|
||
blst_scalar baseScalars[m_numPoints]; | ||
for (size_t i = 0; i < m_numPoints; ++i) | ||
{ | ||
blst_scalar_from_fr(&baseScalars[i], &scalars->at(i).m_val); | ||
} | ||
|
||
Element ret; | ||
const byte* scalarsArg[2] = {reinterpret_cast<byte*>(baseScalars), nullptr}; | ||
blst_p1s_mult_wbits(&ret.m_point, m_table.get(), m_window, m_numPoints, scalarsArg, 255, scratch); | ||
|
||
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,21 @@ | ||
// | ||
// Created by Zhengxuan Guo on 2024/8/30. | ||
// | ||
#pragma once | ||
#include "Element.h" | ||
#include "Fr.h" | ||
|
||
namespace verkle::bandersnatch | ||
{ | ||
class PrecomputedElements { | ||
public: | ||
using Ptr = std::shared_ptr<PrecomputedElements>; | ||
explicit PrecomputedElements(Element::ElementListPtr const& points, size_t window); | ||
[[nodiscard]] Element msm(Fr::FrListPtr const& scalars) const; | ||
|
||
private: | ||
size_t m_window; | ||
size_t m_numPoints; | ||
std::shared_ptr<blst_p1_affine[]> m_table; | ||
}; | ||
} |
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,42 @@ | ||
// | ||
// Created by Zhengxuan Guo on 2024/8/30. | ||
// | ||
#include <testutils/TestPromptFixture.h> | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include "bandersnatch/Fr.h" | ||
#include "bandersnatch/Element.h" | ||
#include "bandersnatch/PrecomputedElements.h" | ||
|
||
using verkle::bandersnatch::Fr; | ||
using verkle::bandersnatch::Element; | ||
using verkle::bandersnatch::PrecomputedElements; | ||
|
||
namespace verkle::ipa::test | ||
{ | ||
BOOST_FIXTURE_TEST_SUITE(ElementTest, verkle::test::TestPromptFixture) | ||
|
||
BOOST_AUTO_TEST_CASE(testPrecomputedMSM) | ||
{ | ||
auto scalars = std::make_shared<std::vector<Fr>>(256); | ||
auto points = std::make_shared<std::vector<Element>>(256); | ||
auto exp = Element::zero(); | ||
for (size_t i = 0; i < 256; ++i) | ||
{ | ||
auto randomFr = bandersnatch::Fr::random(); | ||
auto randomPoint = Element::generator().mult(randomFr); | ||
|
||
// naive sum of multiple multiplication | ||
exp.add(Element::mult(randomFr, randomPoint)); | ||
|
||
scalars->at(i) = randomFr; | ||
points->at(i) = randomPoint; | ||
} | ||
|
||
auto const precomputed = PrecomputedElements(points, 8); | ||
auto const res = precomputed.msm(scalars); | ||
BOOST_ASSERT(res == 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
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
Oops, something went wrong.