-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit f3abd0e
Showing
10 changed files
with
586 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "microecc"] | ||
path = microecc | ||
url = https://github.com/kmackay/micro-ecc |
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,46 @@ | ||
language: python | ||
python: | ||
- "3.8" | ||
|
||
jobs: | ||
include: | ||
# perform a linux build | ||
- services: docker | ||
|
||
# and a mac build | ||
- os: osx | ||
osx_image: xcode10.1 | ||
language: shell | ||
# and a windows build | ||
- os: windows | ||
language: shell | ||
before_install: | ||
- choco install python --version 3.8.6 | ||
- export PATH="/c/Python38:/c/Python38/Scripts:$PATH" | ||
- ln -s /c/Python38/python.exe /c/Python38/python3.exe | ||
# Update root certificates to fix SSL error | ||
- powershell "md C:\temp\certs; CertUtil -generateSSTFromWU C:\temp\certs\RootStore.sst; Get-ChildItem -Path C:\\temp\certs\Rootstore.sst | Import-Certificate -CertStoreLocation Cert:\\LocalMachine\\Root\\ | out-null" | ||
|
||
env: | ||
global: | ||
- CIBW_BEFORE_BUILD_LINUX="python3 -m pip install cmake" | ||
- CIBW_BUILD="cp37-macosx_x86_64 cp37-manylinux_x86_64 cp37-win_amd64 cp38-macosx_x86_64 cp38-manylinux_x86_64 cp38-win_amd64" | ||
- HOMEBREW_NO_INSTALL_CLEANUP=1 | ||
|
||
install: | ||
- bash -c 'echo $TRAVIS_OS_NAME' | ||
- python3 -m pip install cibuildwheel==1.5.5 | ||
|
||
script: | ||
# build the wheels, put them into './wheelhouse' | ||
- python3 -m cibuildwheel --output-dir wheelhouse | ||
|
||
deploy: | ||
provider: releases | ||
api_key: $TRAVIS_GH_DEPLOY | ||
file_glob: true | ||
file: wheelhouse/*.whl | ||
skip_cleanup: true | ||
draft: true | ||
overwrite: true | ||
repo: ixje/neo3crypto |
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 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
project(neo3crypto-core) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
include(FetchContent) | ||
FetchContent_Declare( | ||
pybind11 | ||
GIT_REPOSITORY https://github.com/pybind/pybind11.git | ||
GIT_TAG v2.6.0 | ||
) | ||
FetchContent_MakeAvailable(pybind11) | ||
|
||
pybind11_add_module(neo3crypto python-bindings.cpp ecc.h ecc.cpp microecc/uECC.h microecc/uECC.c) | ||
if(MSVC) | ||
target_link_libraries(neo3crypto PRIVATE advapi32) | ||
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,32 @@ | ||
Copyright (c) 2019-present COZ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
|
||
micro-ecc code license | ||
====================== | ||
Copyright (c) 2014, Kenneth MacKay | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,43 @@ | ||
.. image:: https://raw.githubusercontent.com/CityOfZion/visual-identity/develop/_CoZ%20Branding/_Logo/_Logo%20icon/_PNG%20200x178px/CoZ_Icon_DARKBLUE_200x178px.png | ||
:alt: CoZ logo | ||
|
||
|
||
NEO3VM | ||
------ | ||
C++ implementations of cryptographic functions used in the NEO3 Blockchain with bindings for Python 3.7 & 3.8. | ||
|
||
The current version only supports EllipticCurve functions by wrapping `micro-ecc <https://github.com/kmackay/micro-ecc>`_) | ||
and exposing helper classes. ``SECP256R1`` (a.k.a ``NIST256P``) and ``SECP256K1`` are the only curves exposed, but others can easily | ||
be enabled if needed. | ||
|
||
Installation | ||
~~~~~~~~~~~~ | ||
:: | ||
|
||
pip install neo3crypto | ||
|
||
Or download the wheels from the Github releases page. | ||
|
||
Usage | ||
~~~~~ | ||
|
||
:: | ||
|
||
import hashlib | ||
import os | ||
from neo3crypto import ECCCurve, ECPoint, sign, verify | ||
|
||
|
||
curve = ECCCurve.SECP256R1 | ||
private_key = os.urandom(32) | ||
public_key = ECPoint(private_key, curve) | ||
|
||
signature = sign(private_key, b'message', curve, hashlib.sha256) | ||
assert ecdsa.verify(signature, b'message', public_key, hashlib.sha256) == True | ||
|
||
Any hashlib hashing function can be used. Further documentation on the classes can be queried from the extension module | ||
using ``help(neo3crypto)``. | ||
|
||
Building wheels | ||
~~~~~~~~~~~~~~~ | ||
Make sure to have ``wheel`` and ``CMake`` installed. Then call ``python setup.py bdist_wheel``. |
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,168 @@ | ||
#include "ecc.h" | ||
#include <utility> | ||
#include <unordered_map> | ||
#include "microecc/uECC.h" | ||
|
||
namespace neo3crypto { | ||
std::vector<unsigned char> to_vector(const pybind11::bytes& input) { | ||
auto size = static_cast<size_t>(PYBIND11_BYTES_SIZE(input.ptr())); | ||
const auto *data = reinterpret_cast<const unsigned char*>(PYBIND11_BYTES_AS_STRING(input.ptr())); | ||
return std::vector<unsigned char>(data, data + size); | ||
} | ||
|
||
static uECC_Curve get_uecc_curve(const ECCCURVE curve) { | ||
switch (curve) { | ||
case ECCCURVE::secp256r1: | ||
return uECC_secp256r1(); | ||
case ::neo3crypto::ECCCURVE::secp256k1: | ||
return uECC_secp256k1(); | ||
default: | ||
throw ECCException("Unsupported curve"); | ||
} | ||
} | ||
|
||
ECPoint::ECPoint(std::vector<unsigned char> public_key, ECCCURVE curve_, bool validate) : curve{curve_} { | ||
if (public_key.empty()) | ||
throw ECCException("Public key has no data"); | ||
auto internal_curve = get_uecc_curve(curve_); | ||
auto curve_size = uECC_curve_private_key_size(internal_curve); | ||
|
||
value = std::vector<unsigned char>(curve_size * 2, 0); | ||
|
||
if (public_key.size() == 1 && public_key[0] == 0) { | ||
_is_infinity = true; | ||
value_compressed = std::vector<unsigned char>(curve_size * 2, 0); | ||
return; | ||
} | ||
|
||
if (public_key[0] == 0x2 || public_key[0] == 0x3) { | ||
if (public_key.size() != (curve_size + 1)) { | ||
throw ECCException("Incorrect public key length for specified curve."); | ||
} | ||
value_compressed = std::move(public_key); | ||
|
||
uECC_decompress(value_compressed.data(), value.data(), internal_curve); | ||
} else if (public_key[0] == 0x4) { | ||
// key is in uncompressed format, store it without the prefix | ||
std::copy(public_key.begin() + 1, public_key.end(), value.begin()); | ||
value_compressed = std::vector<unsigned char>(curve_size + 1, 0); | ||
uECC_compress(value.data(), value_compressed.data(), internal_curve); | ||
} | ||
|
||
if (validate) { | ||
if (!uECC_valid_public_key(value.data(), internal_curve)) { | ||
throw ECCException("Failed public key validation"); | ||
} | ||
} | ||
} | ||
|
||
ECPoint::ECPoint(const std::vector<unsigned char>& private_key, ECCCURVE curve_) : curve{curve_} { | ||
auto internal_curve = get_uecc_curve(curve); | ||
int curve_size = uECC_curve_private_key_size(internal_curve); | ||
|
||
if (curve_size != private_key.size()) { | ||
throw ECCException("Incorrect private key length for specified curve"); | ||
} | ||
|
||
value = std::vector<unsigned char>(curve_size * 2); | ||
|
||
if (!uECC_compute_public_key(private_key.data(), value.data() , internal_curve)) { | ||
throw ECCException("Failed public key computation"); | ||
} | ||
|
||
value_compressed = std::vector<unsigned char>(curve_size + 1); | ||
uECC_compress(value.data(), value_compressed.data(), internal_curve); | ||
} | ||
|
||
int ECPoint::compare_to(const ECPoint& other) const { | ||
auto half = value.size() / 2; | ||
auto x_smaller = std::lexicographical_compare(value.begin(), value.begin() + half, other.value.begin(), other.value.begin() + half); | ||
if (x_smaller) | ||
return -1; | ||
auto x_bigger = std::lexicographical_compare(other.value.begin(), other.value.begin() + half, value.begin(), value.begin() + half); | ||
if (x_bigger) | ||
return 1; | ||
// x is equal, so check y | ||
auto y_smaller = std::lexicographical_compare(value.begin() + half, value.end(), other.value.begin() + half, other.value.end()); | ||
if (y_smaller) | ||
return -1; | ||
auto y_bigger = std::lexicographical_compare(other.value.begin() + half, other.value.end(), value.begin() + half, value.end()); | ||
if (y_bigger) | ||
return 1; | ||
return 0; | ||
} | ||
|
||
std::vector<unsigned char> ECPoint::encode_point(bool compressed) { | ||
if (_is_infinity) { | ||
return std::vector<unsigned char>(1,0); | ||
} | ||
if (compressed) | ||
return value_compressed; | ||
std::vector<unsigned char> uncompressed(value.size() + 1); | ||
uncompressed[0] = 0x4; | ||
std::copy(value.begin(), value.end(), uncompressed.begin()+1); | ||
return uncompressed; | ||
} | ||
|
||
void ECPoint::from_bytes(std::vector<unsigned char> public_key, ECCCURVE curve_, bool validate) { | ||
if (public_key.empty()) | ||
throw ECCException("Public key has no data"); | ||
|
||
auto internal_curve = get_uecc_curve(curve_); | ||
auto curve_size = uECC_curve_private_key_size(internal_curve); | ||
|
||
value = std::vector<unsigned char>(curve_size * 2, 0); | ||
|
||
if (public_key.size() == 1 && public_key[0] == 0) { | ||
_is_infinity = true; | ||
value_compressed = std::vector<unsigned char>(curve_size * 2, 0); | ||
return; | ||
} | ||
|
||
_is_infinity = false; | ||
if (public_key[0] == 0x2 || public_key[0] == 0x3) { | ||
if (public_key.size() != (curve_size + 1)) { | ||
throw ECCException("Incorrect public key length for specified curve."); | ||
} | ||
value_compressed = std::move(public_key); | ||
|
||
uECC_decompress(value_compressed.data(), value.data(), internal_curve); | ||
} else if (public_key[0] == 0x4) { | ||
// key is in uncompressed format, store it without the prefix | ||
std::copy(public_key.begin() + 1, public_key.end(), value.begin()); | ||
value_compressed = std::vector<unsigned char>(curve_size + 1, 0); | ||
uECC_compress(value.data(), value_compressed.data(), internal_curve); | ||
} | ||
|
||
if (validate) { | ||
if (!uECC_valid_public_key(value.data(), internal_curve)) { | ||
throw ECCException("Failed public key validation"); | ||
} | ||
} | ||
} | ||
|
||
std::vector<unsigned char> sign(const std::vector<unsigned char>& private_key, const std::vector<unsigned char>& message_hash, ECCCURVE curve) { | ||
auto internal_curve = get_uecc_curve(curve); | ||
auto curve_size = uECC_curve_private_key_size(internal_curve); | ||
if (private_key.size() != curve_size) | ||
throw ECCException("Incorrect private key length for specified curve."); | ||
|
||
std::vector<unsigned char> signature(curve_size * 2); | ||
uECC_sign(private_key.data(), message_hash.data(), message_hash.size(), signature.data(), internal_curve); | ||
return signature; | ||
} | ||
|
||
bool verify(const std::vector<unsigned char>& signature, const std::vector<unsigned char>& message_hash, | ||
ECPoint public_key) { | ||
auto internal_curve = get_uecc_curve(public_key.curve); | ||
auto curve_size = uECC_curve_private_key_size(internal_curve); | ||
if (signature.size() != curve_size * 2) | ||
throw ECCException("Incorrect signature length for specified curve."); | ||
auto result = uECC_verify(public_key.value.data(), | ||
message_hash.data(), | ||
message_hash.size(), | ||
signature.data(), | ||
internal_curve); | ||
return static_cast<bool>(result); | ||
} | ||
} |
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,61 @@ | ||
#pragma once | ||
#include <utility> | ||
#include <vector> | ||
#include <string> | ||
#include <exception> | ||
#include <unordered_map> | ||
#include "microecc/uECC.h" | ||
#include <pybind11/pybind11.h> | ||
|
||
namespace neo3crypto { | ||
class ECCException: public std::exception { | ||
private: | ||
std::string _message; | ||
public: | ||
explicit ECCException(std::string message) : _message(std::move(message)) {}; | ||
const char* what() const noexcept override { | ||
return _message.c_str(); | ||
} | ||
}; | ||
|
||
enum class ECCCURVE : unsigned char { | ||
secp256r1 = 0x0, | ||
secp256k1 = 0x1 | ||
}; | ||
|
||
class ECPoint { | ||
public: | ||
ECPoint() = default; | ||
ECPoint(std::vector<unsigned char> public_key, ECCCURVE curve, bool validate); | ||
ECPoint(const std::vector<unsigned char>& private_key, ECCCURVE curve); | ||
|
||
void from_bytes(std::vector<unsigned char> compressed_public_key, ECCCURVE curve, bool validate); | ||
std::vector<unsigned char> encode_point(bool compressed = true); | ||
std::vector<unsigned char> value; | ||
std::vector<unsigned char> value_compressed; | ||
ECCCURVE curve = ECCCURVE::secp256r1; | ||
|
||
friend bool operator<(const ECPoint& lhs, const ECPoint& rhs) { return lhs.compare_to(rhs) < 0; } | ||
|
||
friend bool operator>(const ECPoint& lhs, const ECPoint& rhs) { return rhs < lhs; } | ||
|
||
friend bool operator<=(const ECPoint& lhs, const ECPoint& rhs) { return !(lhs > rhs); } | ||
|
||
friend bool operator>=(const ECPoint& lhs, const ECPoint& rhs) { return !(lhs < rhs); } | ||
|
||
friend bool operator==(const ECPoint& lhs, const ECPoint& rhs) { return lhs.compare_to(rhs) == 0; } | ||
|
||
friend bool operator!=(const ECPoint& lhs, const ECPoint& rhs) { return lhs.compare_to(rhs) != 0; } | ||
|
||
[[nodiscard]] bool is_infinity() const { return _is_infinity; } | ||
private: | ||
[[nodiscard]] int compare_to(const ECPoint& other) const; | ||
bool _is_infinity = false; | ||
}; | ||
|
||
std::vector<unsigned char> to_vector(const pybind11::bytes& input); | ||
|
||
std::vector<unsigned char> sign(const std::vector<unsigned char>& private_key, const std::vector<unsigned char>& message_hash, ECCCURVE curve); | ||
bool verify(const std::vector<unsigned char>& signature, const std::vector<unsigned char>& message_hash, ECPoint public_key); | ||
|
||
} |
Oops, something went wrong.