Skip to content

Commit

Permalink
Merge a9c1037 into 9a9cedb
Browse files Browse the repository at this point in the history
  • Loading branch information
emlowe committed Jun 16, 2023
2 parents 9a9cedb + a9c1037 commit 7cfd580
Show file tree
Hide file tree
Showing 12 changed files with 666 additions and 534 deletions.
344 changes: 168 additions & 176 deletions .github/workflows/build-wheels.yml

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "CodeQL"

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: "35 9 * * 1"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ javascript, python, cpp ]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: After Prepare (cpp)
if: ${{ matrix.language == 'cpp' }}
run: |
mkdir custom_cmake
wget --quiet -O - "https://cmake.org/files/v3.16/cmake-3.16.3-Linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C custom_cmake
export PATH=$(pwd)/custom_cmake/bin:${PATH} && echo "PATH=$PATH" >> $GITHUB_ENV
cd $GITHUB_WORKSPACE/
export CMAKE_INCLUDE_PATH=$GITHUB_WORKSPACE/include:${CMAKE_INCLUDE_PATH} && echo "CMAKE_INCLUDE_PATH=$CMAKE_INCLUDE_PATH" >> $GITHUB_ENV
export CMAKE_LIBRARY_PATH=$GITHUB_WORKSPACE/lib:${CMAKE_LIBRARY_PATH} && echo "CMAKE_LIBRARY_PATH=$CMAKE_LIBRARY_PATH" >> $GITHUB_ENV
mkdir $GITHUB_WORKSPACE/_lgtm_build_dir
cd $GITHUB_WORKSPACE/_lgtm_build_dir
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ include(FetchContent)

FetchContent_Declare(Sodium
GIT_REPOSITORY https://github.com/AmineKhaldi/libsodium-cmake.git

# Latest commit at the moment this was added here
# Anchored to libsodium v1.0.18
GIT_TAG f73a3fe1afdc4e37ac5fe0ddd401bf521f6bba65
Expand Down
2 changes: 1 addition & 1 deletion emsdk_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ rm -rf js_build
mkdir -p js_build
cd js_build

emcmake cmake -G "Unix Makefiles" ..
emcmake cmake -G "Unix Makefiles" -DBUILD_BLS_TESTS=0 -DBUILD_BLS_BENCHMARKS=0 ..
emmake make
674 changes: 358 additions & 316 deletions js-bindings/package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions js-bindings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@
"cryptography"
],
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^11.15.18",
"@types/mocha": "^10.0.1",
"@types/node": "^20.1.5",
"assert": "^2.0.0",
"babel-polyfill": "^6.26.0",
"buffer": "^6.0.3",
"crypto-browserify": "^3.12.0",
"karma": "^6.3.4",
"karma-firefox-launcher": "^1.3.0",
"karma": "^6.4.2",
"karma-firefox-launcher": "^2.1.2",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-webpack": "^5.0.0",
"mime": "1.4.1",
"mime": "3.0.0",
"mocha": "^10.2.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"typescript": "^3.6.5",
"webpack": "^5.39.0"
"webpack": "^5.82.1",
"typescript": "^5.0.4"
},
"dependencies": {
"binascii": "0.0.2"
Expand Down
2 changes: 1 addition & 1 deletion js_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ git submodule update --init --recursive
mkdir js_build
cd js_build

cmake ../ -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $(which emcc)))/cmake/Modules/Platform/Emscripten.cmake
cmake ../ -DBUILD_BLS_TESTS=0 -DBUILD_BLS_BENCHMARKS=0 -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $(which emcc)))/cmake/Modules/Platform/Emscripten.cmake
cmake --build . --
2 changes: 1 addition & 1 deletion python-bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.10.0
GIT_TAG v2.10.0
)
FetchContent_MakeAvailable(pybind11 blst)

Expand Down
19 changes: 4 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/python3
import os
import platform
import re
import subprocess
import sys
from distutils.version import LooseVersion

from setuptools import Extension, setup, setuptools
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext


Expand All @@ -19,21 +16,14 @@ def __init__(self, name, sourcedir=""):
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(["cmake", "--version"])
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build"
+ " the following extensions: "
+ ", ".join(e.name for e in self.extensions)
)

if platform.system() == "Windows":
cmake_version = LooseVersion(
re.search(r"version\s*([\d.]+)", out.decode()).group(1)
)
if cmake_version < "3.1.0":
raise RuntimeError("CMake >= 3.1.0 is required on Windows")

for ext in self.extensions:
self.build_extension(ext)

Expand All @@ -42,6 +32,8 @@ def build_extension(self, ext):
cmake_args = [
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
"-DPYTHON_EXECUTABLE=" + sys.executable,
"-DBUILD_BLS_TESTS=0",
"-DBUILD_BLS_BENCHMARKS=0",
]

cfg = "Debug" if self.debug else "Release"
Expand All @@ -51,9 +43,6 @@ def build_extension(self, ext):
cmake_args += [
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir)
]
if sys.maxsize > 2 ** 32:
cmake_args += ["-A", "x64"]
build_args += ["--", "/m"]
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
build_args += ["--", "-j", "6"]
Expand Down
27 changes: 16 additions & 11 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ add_compile_options(-fno-builtin)
add_compile_options(-fPIC)
add_compile_options(-Wall)
add_compile_options(-Wextra)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
add_compile_options(-mno-avx)
endif ()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)|(amd64)|(AMD64)")
add_compile_options(-mno-avx)
endif()

add_compile_options(-D__BLST_PORTABLE__)

add_library(bls
Expand All @@ -22,18 +24,18 @@ add_library(bls

target_include_directories(bls
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${blst_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}
${blst_SOURCE_DIR}
)

target_compile_definitions(bls
PRIVATE
BLSALLOC_SODIUM=1
BLSALLOC_SODIUM=1
)

target_link_libraries(bls
PUBLIC
sodium
sodium
)

if(WITH_COVERAGE)
Expand All @@ -50,20 +52,23 @@ if(BUILD_BLS_TESTS)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.3.2
GIT_TAG v3.3.2
)
FetchContent_MakeAvailable(Catch2)
find_package(Threads REQUIRED)
add_executable(runtest test.cpp)

if(EMSCRIPTEN)
target_link_options(runtest PRIVATE "-sEXPORTED_FUNCTIONS=_malloc")
endif()

target_link_libraries(runtest
PRIVATE
bls
Catch2::Catch2
Threads::Threads
bls
Catch2::Catch2
Threads::Threads
)

if(WITH_COVERAGE)
target_compile_options(runtest PRIVATE --coverage)
target_link_options(runtest PRIVATE --coverage)
Expand Down
6 changes: 2 additions & 4 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ void G1Element::ToAffine(blst_p1_affine* output) const

G1Element G1Element::Negate() const
{
G1Element ans;
ans.FromNative(p);
G1Element ans = G1Element::FromNative(p);
blst_p1_cneg(&(ans.p), true);
return ans;
}
Expand Down Expand Up @@ -331,8 +330,7 @@ void G2Element::ToAffine(blst_p2_affine* output) const

G2Element G2Element::Negate() const
{
G2Element ans;
ans.FromNative(q);
G2Element ans = G2Element::FromNative(q);
blst_p2_cneg(&(ans.q), true);
return ans;
}
Expand Down
56 changes: 54 additions & 2 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TEST_CASE("class PrivateKey")
SECTION("(De)Serialization")
{
PrivateKey pk1 = PrivateKey::FromByteVector(getRandomSeed(), true);
REQUIRE_THROWS_AS(pk1.Serialize(nullptr), std::runtime_error);
pk1.Serialize(buffer);
REQUIRE(
memcmp(
Expand Down Expand Up @@ -402,16 +403,20 @@ TEST_CASE("Chia test vectors")
REQUIRE(pk1.GetFingerprint() == 0xb40dd58a);
REQUIRE(pk2.GetFingerprint() == 0xb839add1);

std::stringstream out;
out << sig1; // operator<< tests Serialize()
REQUIRE(
Util::HexStr(sig1.Serialize()) ==
out.str() ==
"b8faa6d6a3881c9fdbad803b170d70ca5cbf1e6ba5a586262df368c75acd1d1f"
"fa3ab6ee21c71f844494659878f5eb230c958dd576b08b8564aad2ee0992e85a"
"1e565f299cd53a285de729937f70dc176a1f01432129bb2b94d3d5031f8065a1");
REQUIRE(
Util::HexStr(sk1.Serialize()) ==
"4a353be3dac091a0a7e640620372f5e1e2e4401717c1e79cac6ffba8f6905604");
out.str("");
out << pk1;
REQUIRE(
Util::HexStr(pk1.Serialize()) ==
out.str() ==
"85695fcbc06cc4c4c9451f4dce21cbf8de3e5a13bf48f44cdbb18e2038ba7b8bb1"
"632d7911e"
"f1e2e08749bddbf165352");
Expand Down Expand Up @@ -1341,6 +1346,9 @@ TEST_CASE("CheckValid")
"8d5d0fb73b9c92df4eab4216e48c3e358578b4cc30f82c268bd6fef3bd34b55862"
"8daf1afef798d4c3b0fcd8b28c8973";

REQUIRE_THROWS_AS(
G1Element::FromBytesUnchecked(vector<uint8_t>(100, 0x05)),
std::invalid_argument);
// FromBytes throws
REQUIRE_THROWS(
G1Element::FromBytes(Bytes(Util::HexToBytes(badPointHex))));
Expand Down Expand Up @@ -1378,6 +1386,50 @@ TEST_CASE("CheckValid")
auto badSer = point.Serialize();

REQUIRE_THROWS(G2Element::FromByteVector(badSer));

REQUIRE_THROWS_AS(
G2Element::FromBytesUnchecked(vector<uint8_t>(100, 0x04)),
std::invalid_argument);
}
}

TEST_CASE("Element operations")
{
SECTION("G1Element")
{
auto pk1 =
PrivateKey::FromByteVector(getRandomSeed(), true).GetG1Element();
auto pk2 =
PrivateKey::FromByteVector(getRandomSeed(), true).GetG1Element();
auto res = pk1 + G1Element();
REQUIRE(res == pk1);
REQUIRE(pk1 + G1Element() == pk1);

REQUIRE(pk1 + pk2 == pk2 + pk1);
pk1 += pk1.Negate();
REQUIRE(pk1 == G1Element());

auto g = G1Element::Generator();
REQUIRE(g.IsValid() == true);
// assert g.is_on_curve()
// assert 2 * g == g + g
// assert (3 * g).is_on_curve()
// assert 3 * g == g + g + g
}
SECTION("G2Element")
{
auto sig1 =
PrivateKey::FromByteVector(getRandomSeed(), true).GetG2Element();
auto sig2 =
PrivateKey::FromByteVector(getRandomSeed(), true).GetG2Element();
auto res = sig1 + G2Element();
REQUIRE(res == sig1);
REQUIRE(sig1 + G2Element() == sig1);

REQUIRE(sig1 + sig2 == sig2 + sig1);
sig1 += sig1.Negate();
REQUIRE(sig1 == G2Element());
REQUIRE(G2Element::Generator().IsValid());
}
}

Expand Down

0 comments on commit 7cfd580

Please sign in to comment.