Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement bandersnatch library with unit tests #1

Merged
merged 13 commits into from
Aug 23, 2024
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ jobs:
- name: compile
run: cmake --build build -j2
- name: run test
run: cd build/bandersnatch && ctest
run: cd build/bandersnatch && ctest
77 changes: 77 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# java
*.class


# ignore some directory
build**
deps
deps/**

# vs
*.sln
*.vcxproj*
.vs/
.vscode/
.idea/

# clion
**/cmake-build-debug/

# macOS
.DS_Store

# log
*.log

# python
*.pyc

# vcpkg
vcpkg
CMakeUserPresets.json

# clangd file
compile_commands.json
.cache

# generated test
tools/BcosAirBuilder/nodes**
tools/BcosProBuilder/generated**
tools/BcosAirBuilder/*
*.log

test/*
/CMakeSettings.json
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.14)

project(verkle_tree)
set(CMAKE_CXX_STANDARD 20)

set(VERKLE_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
list(APPEND CMAKE_MODULE_PATH ${VERKLE_CMAKE_DIR})

include(ProjectBLST)

add_subdirectory(utilities)

add_subdirectory(bandersnatch)
23 changes: 23 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": 2,
"configurePresets": [
{
"name": "default",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
},
{
"name": "test",
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"CMAKE_BUILD_TYPE": "Debug",
"TESTS": "ON"
}
}
]
}
15 changes: 15 additions & 0 deletions bandersnatch/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.14)

project(bandersnatch)

file(GLOB_RECURSE SRCS bandersnatch/*.cpp)
file(GLOB_RECURSE HEADERS bandersnatch/*.h)

add_library(bandersnatch ${SRCS} ${HEADERS})
target_link_libraries(bandersnatch PUBLIC blst)

if (TESTS)
enable_testing()
set(CTEST_OUTPUT_ON_FAILURE TRUE)
add_subdirectory(test)
endif()
102 changes: 102 additions & 0 deletions bandersnatch/bandersnatch/Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// Created by Zhengxuan Guo on 2024/8/13.
//
#include "Element.h"

using namespace verkle::bandersnatch;

Element::Element() = default;

Element::Element(const Element& other) : m_point(other.m_point) {}

Element& Element::operator=(const Element& other)
{
if (this == &other)
{
return *this;
}
this->m_point = other.m_point;
return *this;
}

Element::Element(const byte* in, size_t len)
{
if (len == 0 || len != (in[0]&0x80 ? 48 : 96))
throw BLST_BAD_ENCODING;
blst_p1_affine a;
BLST_ERROR err = blst_p1_deserialize(&a, in);
if (err != BLST_SUCCESS)
throw err;
blst_p1_from_affine(&m_point, &a);
}

Element& Element::add(const Element& other)
{
blst_p1_add_or_double(&m_point, &m_point, &other.m_point);
return *this;
}

Element& Element::dbl()
{
blst_p1_double(&m_point, &m_point);
return *this;
}

Element& Element::mult(const Fr& fr)
{
blst_scalar scalar;
blst_scalar_from_fr(&scalar, &fr.m_val);
blst_p1_mult(&m_point, &m_point, scalar.b, 255);
return *this;
}

bool Element::operator==(const Element& other) const
{
return blst_p1_is_equal(&m_point, &other.m_point);
}

bool Element::operator!=(const Element& other) const
{
return !(*this == other);
}

Element Element::add(const Element& a, const Element& b)
{
Element ret;
blst_p1_add_or_double(&ret.m_point, &a.m_point, &b.m_point);
return ret;
}

Element Element::dbl(const Element& a)
{
Element ret;
blst_p1_double(&ret.m_point, &a.m_point);
return ret;
}

Element Element::mult(const Fr& fr, const Element& a)
{
Element ret;
blst_scalar scalar;
blst_scalar_from_fr(&scalar, &fr.m_val);
blst_p1_mult(&ret.m_point, &a.m_point, scalar.b, 255);
return ret;
}

Element Element::generator()
{
Element ret;
auto g = blst_p1_generator();
ret.m_point = *g;
return ret;
}

void Element::serialize(byte out[96]) const
{
blst_p1_serialize(out, &m_point);
}

void Element::compress(byte out[48]) const
{
blst_p1_compress(out, &m_point);
}
37 changes: 37 additions & 0 deletions bandersnatch/bandersnatch/Element.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Created by Zhengxuan Guo on 2024/8/13.
//
#pragma once
#include <blst.h>
#include "Fr.h"

namespace verkle::bandersnatch
{
class Element
{
public:
Element();
Element(const Element& other);
Element& operator=(const Element& other);

Element(const byte *in, size_t len);

Element& add(const Element& other);
Element& dbl();
Element& mult(const Fr& fr);
bool operator==(const Element& other) const;
bool operator!=(const Element& other) const;

static Element add(const Element& a, const Element& b);
static Element dbl(const Element& a);
static Element mult(const Fr& fr, const Element& a);

static Element generator();

void serialize(byte out[96]) const;
void compress(byte out[48]) const;

private:
blst_p1 m_point;
};
}
Loading
Loading