From 88db1b21d18447637b259e182cba2c19eff19ec1 Mon Sep 17 00:00:00 2001 From: ftheirs Date: Tue, 12 Mar 2024 09:47:40 -0300 Subject: [PATCH] add Jubjub --- CMakeLists.txt | 43 +++++- app/Makefile | 25 +--- app/rust/Cargo.toml | 29 +--- app/rust/include/rslib.h | 9 +- app/rust/src/bolos.rs | 84 ----------- app/rust/src/constants.rs | 49 +++++++ app/rust/src/lib.rs | 300 ++++++++------------------------------ 7 files changed, 165 insertions(+), 374 deletions(-) delete mode 100644 app/rust/src/bolos.rs create mode 100644 app/rust/src/constants.rs diff --git a/CMakeLists.txt b/CMakeLists.txt index a086e3c..296624d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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}) @@ -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 @@ -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) diff --git a/app/Makefile b/app/Makefile index 33cef67..da7ec35 100755 --- a/app/Makefile +++ b/app/Makefile @@ -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 diff --git a/app/rust/Cargo.toml b/app/rust/Cargo.toml index 08fcd9f..fceab5f 100644 --- a/app/rust/Cargo.toml +++ b/app/rust/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Zondax AG "] name = "rslib" -version = "0.1.0" +version = "0.0.1" edition = "2018" readme = "README.md" @@ -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 diff --git a/app/rust/include/rslib.h b/app/rust/include/rslib.h index 8e26766..72eca71 100644 --- a/app/rust/include/rslib.h +++ b/app/rust/include/rslib.h @@ -1,8 +1,9 @@ #pragma once #include +#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]); diff --git a/app/rust/src/bolos.rs b/app/rust/src/bolos.rs deleted file mode 100644 index f98ae76..0000000 --- a/app/rust/src/bolos.rs +++ /dev/null @@ -1,84 +0,0 @@ -/******************************************************************************* -* (c) 2018 - 2023 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. -********************************************************************************/ -//! Rust interfaces to Ledger SDK APIs. -#[cfg(test)] -use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; -use curve25519_dalek::scalar::Scalar; -#[cfg(test)] -#[cfg(target_arch = "x86_64")] -use getrandom::getrandom; -use merlin::TranscriptRng; -use rand::{CryptoRng, RngCore}; - -extern "C" { - fn cx_rng(buffer: *mut u8, len: u32); - fn zemu_log_stack(buffer: *const u8); - fn check_app_canary(); -} - -#[cfg(not(test))] -pub fn c_zemu_log_stack(s: &[u8]) { - unsafe { zemu_log_stack(s.as_ptr()) } -} - -#[cfg(test)] -pub fn c_zemu_log_stack(s: &[u8]) {} - -pub fn c_check_app_canary() { - unsafe { check_app_canary() } -} - -pub struct Trng; - -impl RngCore for Trng { - fn next_u32(&mut self) -> u32 { - let mut out = [0; 4]; - self.fill_bytes(&mut out); - u32::from_le_bytes(out) - } - - fn next_u64(&mut self) -> u64 { - let mut out = [0; 8]; - self.fill_bytes(&mut out); - u64::from_le_bytes(out) - } - - #[cfg(not(target_arch = "x86_64"))] - fn fill_bytes(&mut self, dest: &mut [u8]) { - c_zemu_log_stack(b"fill_bytes\x00".as_ref()); - - unsafe { - cx_rng(dest.as_mut_ptr(), dest.len() as u32); - } - } - - #[cfg(target_arch = "x86_64")] - #[cfg(test)] - fn fill_bytes(&mut self, dest: &mut [u8]) { - getrandom(dest); - } - - #[cfg(target_arch = "x86_64")] - #[cfg(not(test))] - fn fill_bytes(&mut self, _dest: &mut [u8]) {} - - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { - self.fill_bytes(dest); - Ok(()) - } -} - -impl CryptoRng for Trng {} diff --git a/app/rust/src/constants.rs b/app/rust/src/constants.rs new file mode 100644 index 0000000..a6057cd --- /dev/null +++ b/app/rust/src/constants.rs @@ -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(); diff --git a/app/rust/src/lib.rs b/app/rust/src/lib.rs index 7a5c2e5..ce684fb 100644 --- a/app/rust/src/lib.rs +++ b/app/rust/src/lib.rs @@ -1,5 +1,5 @@ /******************************************************************************* -* (c) 2018 - 2023 Zondax AG +* (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. @@ -14,268 +14,88 @@ * limitations under the License. ********************************************************************************/ #![no_std] +#![no_main] #![no_builtins] #![allow(dead_code, unused_imports)] -extern crate core; -#[cfg(test)] -#[macro_use] -extern crate hex_literal; - -use core::convert::TryInto; -use core::mem; use core::panic::PanicInfo; -use core::slice::{from_raw_parts, from_raw_parts_mut}; -use curve25519_dalek::scalar::Scalar; -use merlin::{Transcript, TranscriptRng, TranscriptRngBuilder}; -use rand::RngCore; -use schnorrkel::context::{SigningContext, SigningTranscript}; -use schnorrkel::{PublicKey, SecretKey}; -use zeroize::Zeroize; +use constants::{SPENDING_KEY_GENERATOR}; +mod constants; -use crate::bolos::*; - -mod bolos; +use jubjub::{Fr, AffinePoint, ExtendedPoint}; fn debug(_msg: &str) {} -#[cfg(not(test))] -#[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - loop {} +// ParserError should mirror parser_error_t from parser_common. +// At the moment, just implement OK or Error +#[repr(C)] +pub enum ParserError { + ParserOk = 0, + ParserUnexpectedError = 5, } -#[inline(never)] -fn mult_with_secret(k: &mut Scalar, sk: &[u8]) { - let mut skbytes = [0u8; 32]; - skbytes.copy_from_slice(&sk[0..32]); - let s = Scalar::from_bits(skbytes); - *k *= s; -} - -#[inline(never)] -fn add_witness(k: &mut Scalar, x: [u8; 32]) -> [u8; 32] { - let r = Scalar::from_bits(x); - *k += r; - k.to_bytes() -} - -#[inline(never)] -fn get_challenge_scalar(k: &mut Scalar, tr: &mut Transcript) { - let mut kbytes = [0u8; 64]; - tr.challenge_bytes(b"sign:c", &mut kbytes); - *k += Scalar::from_bytes_mod_order_wide(&kbytes); -} - -#[inline(never)] -fn get_witness_bytes_custom(br: &mut Transcript, nonce_seeds: &[&[u8]]) -> [u8; 32] { - c_zemu_log_stack(b"witness_bytes\x00".as_ref()); - let mut x = [0u8; 32]; - for ns in nonce_seeds { - br.append_message(b"nonce-bytes", ns); - } - { - let random_bytes = { - let mut bytes = [0u8; 32]; - Trng.fill_bytes(&mut bytes); - bytes - }; - br.append_message(b"rng", &random_bytes); - } - br.challenge_bytes(b"witness-bytes", &mut x); - br.zeroize(); - x +#[repr(C)] +pub enum ConstantKey { + SpendingKeyGenerator, + ProofGenerationKeyGenerator, } #[no_mangle] -pub extern "C" fn sign_sr25519_phase1( - sk_ristretto_expanded_ptr: *const u8, - pk_ptr: *const u8, - context_ptr: *const u8, - context_len: usize, - msg_ptr: *const u8, - msg_len: usize, - sig_ptr: *mut u8, -) { - c_zemu_log_stack(b"sign_sr25519\x00".as_ref()); - - let sk_ristretto_expanded = - unsafe { from_raw_parts(sk_ristretto_expanded_ptr as *const u8, 64) }; - let pk = unsafe { from_raw_parts(pk_ptr as *const u8, 32) }; - let context = unsafe { from_raw_parts(context_ptr as *const u8, context_len) }; - let message = unsafe { from_raw_parts(msg_ptr as *const u8, msg_len) }; - let signature = unsafe { from_raw_parts_mut(sig_ptr as *mut u8, 64) }; - - let mut signtranscript = Transcript::new(b"SigningContext"); - signtranscript.append_message(b"", context); - signtranscript.append_message(b"sign-bytes", message); - signtranscript.append_message(b"proto-name", b"Schnorr-sig"); //proto name - signtranscript.append_message(b"sign:pk", pk); //commitpoint: pk - - let x = get_witness_bytes_custom(&mut signtranscript, &[&sk_ristretto_expanded[32..]]); - signature[32..64].copy_from_slice(&x); +pub extern "C" fn from_bytes_wide(input: &[u8; 64], output: &mut [u8; 32]) -> ParserError { + let result = Fr::from_bytes_wide(input).to_bytes(); + output.copy_from_slice(&result[0..32]); + ParserError::ParserOk } #[no_mangle] -pub extern "C" fn sign_sr25519_phase2( - sk_ristretto_expanded_ptr: *const u8, - pk_ptr: *const u8, - context_ptr: *const u8, - context_len: usize, - msg_ptr: *const u8, - msg_len: usize, - sig_ptr: *mut u8, -) { - c_zemu_log_stack(b"sign_sr25519\x00".as_ref()); - - let sk_ristretto_expanded = - unsafe { from_raw_parts(sk_ristretto_expanded_ptr as *const u8, 64) }; - let pk = unsafe { from_raw_parts(pk_ptr as *const u8, 32) }; - let context = unsafe { from_raw_parts(context_ptr as *const u8, context_len) }; - let message = unsafe { from_raw_parts(msg_ptr as *const u8, msg_len) }; - let signature = unsafe { from_raw_parts_mut(sig_ptr as *mut u8, 64) }; - - let mut signtranscript = Transcript::new(b"SigningContext"); - signtranscript.append_message(b"", context); - signtranscript.append_message(b"sign-bytes", message); - signtranscript.append_message(b"proto-name", b"Schnorr-sig"); //proto name - signtranscript.append_message(b"sign:pk", pk); //commitpoint: pk - signtranscript.append_message(b"sign:R", &signature[0..32]); //commitpoint: pk - - let mut x = [0u8; 32]; - x.copy_from_slice(&signature[32..64]); - - let mut k = Scalar::zero(); - get_challenge_scalar(&mut k, &mut signtranscript); +pub extern "C" fn scalar_multiplication(input: &[u8; 32], key: ConstantKey, output: *mut [u8; 32]) -> ParserError { + let key_point = match key { + ConstantKey::SpendingKeyGenerator => constants::SPENDING_KEY_GENERATOR, + ConstantKey::ProofGenerationKeyGenerator => constants::PROOF_GENERATION_KEY_GENERATOR, + }; + + let extended_point = key_point.multiply_bits(input); + let result = AffinePoint::from(&extended_point); + + unsafe { + let output_slice = &mut *output; + output_slice.copy_from_slice(&result.to_bytes()); + } - mult_with_secret(&mut k, sk_ristretto_expanded); - signature[32..].copy_from_slice(&add_witness(&mut k, x)); - signature[63] |= 128; + ParserError::ParserOk } -#[no_mangle] -pub extern "C" fn get_sr25519_sk(sk_ed25519_expanded_ptr: *mut u8) { - let sk_ed25519_expanded = unsafe { from_raw_parts_mut(sk_ed25519_expanded_ptr as *mut u8, 64) }; - let secret: SecretKey = SecretKey::from_ed25519_bytes(&sk_ed25519_expanded[..]).unwrap(); - sk_ed25519_expanded.copy_from_slice(&secret.to_bytes()); +#[cfg(not(test))] +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} } + #[cfg(test)] mod tests { - use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; - use curve25519_dalek::edwards::EdwardsPoint; - use curve25519_dalek::scalar::Scalar; - use log::{debug, info}; - use schnorrkel::{context::*, Keypair, PublicKey, SecretKey, Signature}; + // use curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT; + // use curve25519_dalek::edwards::EdwardsPoint; + // use curve25519_dalek::scalar::Scalar; + // use log::{debug, info}; + // use schnorrkel::{context::*, Keypair, PublicKey, SecretKey, Signature}; + + // use crate::*; + // use core::ops::Mul; + + // fn init_logging() { + // let _ = env_logger::builder().is_test(true).try_init(); + // } + + // fn ristretto_scalarmult(sk: &[u8], pk: &mut [u8]) { + // let mut seckey = [0u8; 32]; + // seckey.copy_from_slice(&sk[0..32]); + // let pubkey = RISTRETTO_BASEPOINT_POINT + // .mul(Scalar::from_bits(seckey)) + // .compress() + // .0; + // pk.copy_from_slice(&pubkey); + // } - use crate::*; - use core::ops::Mul; - - fn init_logging() { - let _ = env_logger::builder().is_test(true).try_init(); - } - - fn ristretto_scalarmult(sk: &[u8], pk: &mut [u8]) { - let mut seckey = [0u8; 32]; - seckey.copy_from_slice(&sk[0..32]); - let pubkey = RISTRETTO_BASEPOINT_POINT - .mul(Scalar::from_bits(seckey)) - .compress() - .0; - pk.copy_from_slice(&pubkey); - } - - #[test] - fn test_sign_verify() { - let mut sk_ed25519_expanded = [ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, - 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, - 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, - 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - ]; - - let pk_expected = "b65abc66a8fdeac1197d03daa6c3791d0c0799a52db6b7127b1cd12d46e34364"; - - let secret = SecretKey::from_ed25519_bytes(&sk_ed25519_expanded).unwrap(); - - let mut pk = [0u8; 32]; - get_sr25519_sk(sk_ed25519_expanded.as_mut_ptr()); - - ristretto_scalarmult(&sk_ed25519_expanded, &mut pk); - - assert_eq!(hex::encode(pk), pk_expected); - - let context = b"good"; - let msg = b"test message"; - let mut signature = [0u8; 64]; - - sign_sr25519_phase1( - secret.to_bytes().as_ptr(), - pk.as_ptr(), - context.as_ptr(), - context.len(), - msg.as_ptr(), - msg.len(), - signature.as_mut_ptr(), - ); - - let mut x = [0u8; 32]; - x.copy_from_slice(&signature[32..64]); - - ristretto_scalarmult(&x, &mut signature[0..32]); - - sign_sr25519_phase2( - secret.to_bytes().as_ptr(), - pk.as_ptr(), - context.as_ptr(), - context.len(), - msg.as_ptr(), - msg.len(), - signature.as_mut_ptr(), - ); - - let keypair: Keypair = Keypair::from(secret); - - let mut sigledger = [0u8; 64]; - hex::decode_to_slice("48fdbe5cf3524bdd078ac711565d658a3053d10660749959883c4710f49d9948b2d5f829bea6800897dc6ea0150ca11075cc36b75bfcf3712aafb8e1bd10bf8f",&mut sigledger).expect("dec"); - - let self_sig = Signature::from_bytes(&signature).unwrap(); - let self_sig_ledger = Signature::from_bytes(&sigledger).unwrap(); - - let vers = signing_context(context); - - assert!( - keypair.verify(vers.bytes(msg), &self_sig).is_ok(), - "Verification of a valid signature failed!" - ); - assert!( - keypair.verify(vers.bytes(msg), &self_sig_ledger).is_ok(), - "Verification of a valid signature from ledger failed!" - ); - } - - #[test] - fn get_public_key_c() { - init_logging(); - - let mut sk_ed25519_expanded = [ - 0x00, 0x01, 0x02, 0x03, 04, 0x5, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 04, 0x5, 0x06, - 0x07, 0x00, 0x01, 0x02, 0x03, 04, 0x5, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 04, 0x5, - 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 04, 0x5, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 04, - 0x5, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 04, 0x5, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, - 04, 0x5, 0x06, 0x07, - ]; - - let pk_expected = "b65abc66a8fdeac1197d03daa6c3791d0c0799a52db6b7127b1cd12d46e34364"; - - let mut pk = [0u8; 32]; - get_sr25519_sk(sk_ed25519_expanded.as_mut_ptr()); - - ristretto_scalarmult(&sk_ed25519_expanded, &mut pk); - - info!("{:?}", hex::encode(pk)); - assert_eq!(hex::encode(pk), pk_expected); - } }