Skip to content

Commit

Permalink
add Jubjub
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Mar 12, 2024
1 parent 100c49b commit 88db1b2
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 374 deletions.
43 changes: 41 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#* limitations under the License.
#********************************************************************************
cmake_minimum_required(VERSION 3.0)
project(ledger-template VERSION 0.0.0)
project(ledger-ironfish VERSION 0.0.0)
enable_testing()

cmake_policy(SET CMP0025 NEW)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

option(ENABLE_FUZZING "Build with fuzzing instrumentation and build fuzz targets" OFF)
option(ENABLE_COVERAGE "Build with source code coverage instrumentation" OFF)
Expand Down Expand Up @@ -116,6 +116,11 @@ file(GLOB_RECURSE LIB_SRC
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
####
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref/blake2b-ref.c
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref/blake2s-ref.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand All @@ -125,9 +130,40 @@ target_include_directories(app_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
${CMAKE_CURRENT_SOURCE_DIR}/app/src/common
###
${CMAKE_CURRENT_SOURCE_DIR}/app/rust/include
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref

)

##############################################################
## Rust library
set(RUST_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/app/rust")
set(RUST_TARGET_DIR "${RUST_LIB_DIR}/target/aarch64-apple-darwin/release")

# Custom target for the Rust library
add_custom_target(RustLibClean
COMMAND cargo clean
WORKING_DIRECTORY ${RUST_LIB_DIR}
)
add_custom_target(RustLibBuild
COMMAND cargo build --release --target=aarch64-apple-darwin
WORKING_DIRECTORY ${RUST_LIB_DIR}
DEPENDS RustLibClean
)

# Assuming the Rust library outputs a file named librslib.a
set(RUST_LIB "${RUST_TARGET_DIR}/librslib.a")

# Ensure the Rust library is built before the C++ project
add_library(rslib STATIC IMPORTED)
set_property(TARGET rslib PROPERTY IMPORTED_LOCATION ${RUST_LIB})
add_dependencies(rslib RustLibBuild)

# Ensure your C++ targets depend on the Rust library being built first
# For example, for your app_lib static library:
add_dependencies(app_lib rslib)
##############################################################
# Tests
file(GLOB_RECURSE TESTS_SRC
Expand All @@ -141,11 +177,14 @@ target_include_directories(unittests PRIVATE
${CONAN_INCLUDE_DIRS_JSONCPP}
${CMAKE_CURRENT_SOURCE_DIR}/app/src
${CMAKE_CURRENT_SOURCE_DIR}/app/src/lib
###
${CMAKE_CURRENT_SOURCE_DIR}/deps/BLAKE2/ref
)

target_link_libraries(unittests PRIVATE
gtest_main
app_lib
rslib
CONAN_PKG::fmt
CONAN_PKG::jsoncpp)

Expand Down
25 changes: 5 additions & 20 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,38 +72,23 @@ endif

include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.platform
CFLAGS += -Wvla
# #{TODO} --> Need Rust?
# LDFLAGS += -z muldefs
# LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib
# APP_SOURCE_PATH += $(CURDIR)/rust/include
APP_CUSTOM_LINK_DEPENDENCIES = rust
LDLIBS += -Lrust/target/thumbv6m-none-eabi/release -lrslib
APP_SOURCE_PATH += $(CURDIR)/rust/include

# #{TODO} --> Need Rust?
.PHONY: rust
rust:
@echo "No rust code"
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release

# Before linking, we need to be sure rust lib is there
bin/app.elf: rust

.PHONY: rust_clean
rust_clean:
@echo "No rust code"
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo clean

clean: rust_clean

# .PHONY: rust
# rust:
# cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo build --target thumbv6m-none-eabi --release

# # Before linking, we need to be sure rust lib is there
# bin/app.elf: rust

# .PHONY: rust_clean
# rust_clean:
# cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo clean

# clean: rust_clean

include $(CURDIR)/../deps/ledger-zxlib/makefiles/Makefile.side_loading

# Import generic rules from the SDK
Expand Down
29 changes: 5 additions & 24 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
authors = ["Zondax AG <[email protected]>"]
name = "rslib"
version = "0.1.0"
version = "0.0.1"
edition = "2018"
readme = "README.md"

Expand All @@ -10,38 +10,19 @@ name = "rslib"
crate-type = ["staticlib"]

[dependencies]
rand={ version = "0.7.3", default-features = false}
merlin = {version = "2.0.0", default-features=false}
zeroize = {version = "1.1.1", default-features=false}

[target.'cfg(target_arch = "x86_64")'.dependencies]
getrandom = {version="0.1.14", default-features=false}

[dependencies.curve25519-dalek]
version = "3.0.0"
default-features = false
features=["u32_backend"]

[dependencies.schnorrkel]
version = "0.9.1"
default-features = false
features=["u32_backend"]

[dev-dependencies]
hex-literal = "0.2.1"
hex = "0.4.2"
env_logger = "0.7.1"
log = "0.4.8"
jubjub = { version = "0.10.0", default-features = false }

[target.thumbv6m-none-eabi.dev-dependencies]
panic-halt = "0.2.0"

[profile.release]
lto=false
lto = "fat"
codegen-units = 1
debug=true
opt-level = "s"
panic = "abort"

[profile.dev]
panic = "abort"
debug=true

9 changes: 5 additions & 4 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include <stdint.h>
#include "parser_common.h"
#include "keys_def.h"

void get_sr25519_sk(uint8_t *sk_ed25519_expanded);

void sign_sr25519_phase1(const uint8_t *sk_ed25519_expanded, const uint8_t *pk, const uint8_t *context_ptr, uint32_t context_len, const uint8_t *msg_ptr, uint32_t msg_len, uint8_t *sig_ptr);
void sign_sr25519_phase2(const uint8_t *sk_ed25519_expanded, const uint8_t *pk, const uint8_t *context_ptr, uint32_t context_len, const uint8_t *msg_ptr, uint32_t msg_len, uint8_t *sig_ptr);
/* Interface functions with jubjub crate */
parser_error_t from_bytes_wide(const uint8_t input[64], uint8_t output[32]);
parser_error_t scalar_multiplication(const uint8_t input[32], constant_key_t key, uint8_t output[32]);
84 changes: 0 additions & 84 deletions app/rust/src/bolos.rs

This file was deleted.

49 changes: 49 additions & 0 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

use jubjub::{AffineNielsPoint, AffinePoint, Fq};

pub const SPENDING_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([
0x47bf_4692_0a95_a753,
0xd5b9_a7d3_ef8e_2827,
0xd418_a7ff_2675_3b6a,
0x0926_d4f3_2059_c712,
]),
Fq::from_raw([
0x3056_32ad_aaf2_b530,
0x6d65_674d_cedb_ddbc,
0x53bb_37d0_c21c_fd05,
0x57a1_019e_6de9_b675,
]),
)
.to_niels();

pub const PROOF_GENERATION_KEY_GENERATOR: AffineNielsPoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([
0x3af2_dbef_b96e_2571,
0xadf2_d038_f2fb_b820,
0x7043_03f1_e890_6081,
0x1457_a502_31cd_e2df,
]),
Fq::from_raw([
0x467a_f9f7_e05d_e8e7,
0x50df_51ea_f5a1_49d2,
0xdec9_0184_0f49_48cc,
0x54b6_d107_18df_2a7a,
]),
)
.to_niels();
Loading

0 comments on commit 88db1b2

Please sign in to comment.