Skip to content

Commit

Permalink
refactor: Use BIP66 Library (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepdefic1t authored and faustbrian committed May 24, 2019
1 parent 453f87f commit 3f20868
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 387 deletions.
1 change: 1 addition & 0 deletions .circleci/script_arduino.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ mkdir -p ~/Arduino/libraries/cpp-crypto/
mv ~/project/* ~/Arduino/libraries/cpp-crypto

arduino-cli lib install "ArduinoJson@6.10.0"
arduino-cli lib install "BIP66"

arduino-cli compile --output temp.bin -b esp32:esp32:esp32 ~/Arduino/libraries/cpp-crypto/examples/arduino/ESP32/ESP32.ino --debug
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "src/lib/ArduinoJson"]
path = src/lib/ArduinoJson
url = https://github.com/bblanchon/ArduinoJson
[submodule "src/lib/BIP66"]
path = src/lib/BIP66
url = https://github.com/sleepdefic1t/BIP66
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- changed to BIP66 lib for DER ser/des. ([#88])
- updated vendorField to support 255 bytes in Core v2.4 ([#84])
- updated ArduinoJson package to version v.6.10.0 ([#76])
- updated tests to use Core fixtures ([#74])
- improved Windows support ([#83])

### Fixed

- properly handle 0 ARKtoshi Transaction amounts.
- properly handle 0 ARKtoshi Transaction amounts. ([#85])

## [0.3.1] - 2019-02-19

Expand Down
5 changes: 3 additions & 2 deletions extras/ARDUINO_IDE.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ if [[ -d ${INCLUDE_DIR} ]]; then
mv ${INCLUDE_ENUMS_DIR}/types.h ${SRC_ENUMS_DIR}

echo -e "Moving 'helpers' headers.\n"
mv ${INCLUDE_HELPERS_DIR}/encoding/der.h ${SRC_HELPERS_DIR}/encoding
mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_HELPERS_DIR}/encoding
mkdir ${SRC_ENCODING_DIR}
mv ${INCLUDE_HELPERS_DIR}/encoding/hex.h ${SRC_ENCODING_DIR}

## 'bip39' library is not supported in Arduino
echo -e "Backing up and removing 'mnemonic.h'.\n"
Expand Down Expand Up @@ -192,6 +192,7 @@ else

echo -e "Moving 'helpers/encoding' headers.\n"
mv ${SRC_ENCODING_DIR}/hex.h ${INCLUDE_ENCODING_DIR}
rm ${SRC_ENCODING_DIR}

echo -e "Moving 'identities' headers.\n"
mv ${SRC_IDENTITIES_DIR}/address.h ${INCLUDE_IDENTITIES_DIR}
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ description = "A simple Cryptography Implementation in C++ for the ARK Blockchai

[common]
lib_ldf_mode = off
lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0
lib_deps = micro-ecc, bip39@^1.1, ArduinoJson@6.10.0, BIP66
build_flags = -I./src/ -I./src/lib -I./src/include/cpp-crypto
src_filter = +<*> -<.git/> -<examples/> -<lib/ArduinoJson> -<lib/bip39> -<lib/uECC> -<CMakeFiles>
src_filter = +<*> -<.git/> -<examples/> -<lib/ArduinoJson> -<lib/bip39> -<lib/uECC> -<CMakeFiles> -<lib/BIP66>
upload_speed = 921600

[env:esp8266]
Expand Down
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ project(Ark-Cpp-Crypto-lib C CXX)

add_subdirectory(lib/bip39/src)

set(BIP66_SRC
${PROJECT_SOURCE_DIR}/lib/BIP66/src/bip66.cpp
)

set(BCL_SRC
lib/bcl/Base58Check.cpp
lib/bcl/CurvePoint.cpp
Expand All @@ -26,7 +30,6 @@ set(COMMON_SRC
configuration/fee.cpp
configuration/network.cpp
helpers/crypto.cpp
helpers/encoding/der.cpp
identities/address.cpp
identities/mnemonic.cpp
identities/privatekey.cpp
Expand All @@ -45,6 +48,7 @@ add_library(${PROJECT_NAME}
STATIC
${BCL_SRC}
${uECC_SRC}
${BIP66_SRC}
${COMMON_SRC}
)

Expand All @@ -59,6 +63,7 @@ include_directories(${PROJECT_SOURCE_DIR}/lib/bcl)
include_directories(${PROJECT_SOURCE_DIR}/lib/bip39)
include_directories(${PROJECT_SOURCE_DIR}/lib/rfc6979)
include_directories(${PROJECT_SOURCE_DIR}/lib/uECC)
include_directories(${PROJECT_SOURCE_DIR}/lib/BIP66/src)

target_include_directories( ${PROJECT_NAME}
PUBLIC ${cpp_crypto_build_include_dirs}
Expand Down
22 changes: 10 additions & 12 deletions src/helpers/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "bcl/Ecdsa.hpp"
#include "bcl/Sha256.hpp"
#include "bcl/Uint256.hpp"
#include "helpers/encoding/der.h"
#include "helpers/crypto_helpers.h"
#include "rfc6979/rfc6979.h"
#include "uECC.h"
#include "bip66.h"

void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey, std::vector<uint8_t>& signature) {
Uint256 r;
Expand All @@ -19,13 +19,11 @@ void cryptoSign(Sha256Hash hash, Ark::Crypto::Identities::PrivateKey privateKey,
auto ret = Ecdsa::sign(Uint256(privateKey.toBytes()), hash, Uint256(nonce32), r, s);
assert(ret);

std::vector<uint8_t> r_der(PRIVATEKEY_SIZE);
r.getBigEndianBytes(&r_der[0]);
std::vector<uint8_t> rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE);
r.getBigEndianBytes(&rValue[0]);
s.getBigEndianBytes(&sValue[0]);

std::vector<uint8_t> s_der(PRIVATEKEY_SIZE);
s.getBigEndianBytes(&s_der[0]);

encodeDER(toDER(r_der), toDER(s_der), signature);
BIP66::encode(rValue, sValue, signature);
}

bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash, std::vector<uint8_t>& signature) {
Expand All @@ -52,12 +50,12 @@ bool cryptoVerify(Ark::Crypto::Identities::PublicKey publicKey, Sha256Hash hash,
CurvePoint curvePoint(x, y);

/* Decode signature from DER into r & s buffers */
std::vector<uint8_t> r; // create r-value buffer
std::vector<uint8_t> s; // create s-value buffer
decodeDER(signature, r, s);
std::vector<uint8_t> rValue(PRIVATEKEY_SIZE), sValue(PRIVATEKEY_SIZE);

BIP66::decode(signature, rValue, sValue);

Uint256 r256(r.data()); // create Uint256/BigNumber from r-value buffer
Uint256 s256(s.data()); // create Uint256/BigNumber from s-value buffer
Uint256 r256(rValue.data()); // create Uint256/BigNumber from r-value buffer
Uint256 s256(sValue.data()); // create Uint256/BigNumber from s-value buffer

/* Verify */
return Ecdsa::verify(curvePoint, hash, r256, s256);
Expand Down
111 changes: 0 additions & 111 deletions src/helpers/encoding/der.cpp

This file was deleted.

35 changes: 0 additions & 35 deletions src/include/cpp-crypto/helpers/encoding/der.h

This file was deleted.

1 change: 1 addition & 0 deletions src/lib/BIP66
Submodule BIP66 added at 5f024b
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ set (TEST_SRC
${PROJECT_SOURCE_DIR}/configuration/network.cpp
${PROJECT_SOURCE_DIR}/enums/fees.cpp
${PROJECT_SOURCE_DIR}/enums/types.cpp
${PROJECT_SOURCE_DIR}/helpers/encoding/der.cpp
${PROJECT_SOURCE_DIR}/identities/address.cpp
${PROJECT_SOURCE_DIR}/identities/mnemonic.cpp
${PROJECT_SOURCE_DIR}/identities/privatekey.cpp
Expand Down
Loading

0 comments on commit 3f20868

Please sign in to comment.