Skip to content

Commit

Permalink
just for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
neithanmo committed Mar 18, 2024
1 parent 084881d commit ecab35d
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 227 deletions.
2 changes: 1 addition & 1 deletion app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ rust:
rust_clean:
cd rust && CARGO_HOME="$(CURDIR)/rust/.cargo" cargo clean

clean: rust_clean
clean:

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

Expand Down
1 change: 1 addition & 0 deletions app/bls12_381
Submodule bls12_381 added at 34dab7
138 changes: 11 additions & 127 deletions app/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ crate-type = ["staticlib"]

[dependencies]
no-std-compat = { version = "0.4.1" }
bls_signature = { git = "https://github.com/dfinity/verify-bls-signatures.git", branch = "jack/add-alloc-feature", package = "ic-verify-bls-signature", default-features = false }
# bls_signature = { git = "https://github.com/dfinity/verify-bls-signatures.git", branch = "jack/add-alloc-feature", package = "ic-verify-bls-signature", default-features = false }
# bls_signature = { git = "https://github.com/Zondax/verify-bls-signatures.git", branch = "debug_pic", package = "ic-verify-bls-signature", default-features = false }
bls_signature = { path = "../verify-bls-signatures/", package = "ic-verify-bls-signature", default-features = false }


[dev-dependencies]
no-std-compat = { version = "0.4.1", features = ["std"] }
Expand All @@ -32,7 +35,7 @@ no-std-compat = { version = "0.4.1", features = ["std"] }
[profile.release]
lto = false
codegen-units = 1
debug = false
debug = true
opt-level = "z"

[profile.dev]
Expand Down
2 changes: 1 addition & 1 deletion app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include "parser_txdef.h"

// Signature must be 48 bytes and key 96 bytes
uint16_t verify_bls_sign(const uint8_t *msg, uint16_t msg_len, const uint8_t *sk, uint8_t *sig);
uint16_t verify_bls_sign(const uint8_t *sig, const uint8_t *msg, uint16_t msg_len, const uint8_t *sk );


53 changes: 48 additions & 5 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
#![no_builtins]
#![macro_use]
#![allow(dead_code)]
#![deny(unused_crate_dependencies)]
// #![deny(unused_crate_dependencies)]

extern crate no_std_compat as std;

use bls_signature::verify_bls_signature;

const BLS_SIGNATURE_SIZE: usize = 96;
const BLS_PUBLIC_KEY_SIZE: usize = 48;
// The signature must be exactly 48 bytes (compressed G1 element)
// The key must be exactly 96 bytes (compressed G2 element)
const BLS_SIGNATURE_SIZE: usize = 48;
const BLS_PUBLIC_KEY_SIZE: usize = 96;

fn debug(_msg: &str) {}

Expand All @@ -22,18 +24,59 @@ fn panic(_info: &PanicInfo) -> ! {
loop {}
}

extern "C" {
fn zemu_log_stack(s: *const u8);
fn check_canary();
}

/// The signature must be exactly 48 bytes (compressed G1 element)
/// The key must be exactly 96 bytes (compressed G2 element)
#[no_mangle]
pub unsafe extern "C" fn verify_bls_sign(
sig: *const u8,
msg: *const u8,
msg_len: u16,
key: *const u8,
sig: *const u8,
) -> u8 {
let msg = std::slice::from_raw_parts(msg, msg_len as usize);
let key = std::slice::from_raw_parts(key, BLS_PUBLIC_KEY_SIZE);
let sig = std::slice::from_raw_parts(sig, BLS_SIGNATURE_SIZE);

verify_bls_signature(sig, msg, key).is_ok() as u8
// let res = verify_bls_signature(sig, msg, key);
// res.is_ok() as u8
unsafe {
check_canary();
}
verify_bls(sig, msg, key).is_ok() as u8
}

#[inline(never)]
fn verify_bls(sig: &[u8], msg: &[u8], key: &[u8]) -> Result<(), ()> {
if verify_bls_signature(sig, msg, key).is_ok() {
return Ok(());
}
Err(())
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn check_signature() {
let s = [
139, 118, 54, 85, 203, 37, 178, 69, 140, 15, 123, 156, 250, 54, 205, 107, 22, 254, 170,
98, 234, 151, 252, 248, 245, 75, 211, 209, 237, 75, 135, 119, 53, 244, 174, 38, 241,
127, 34, 154, 2, 92, 174, 10, 73, 110, 128, 24,
];
let key = [
136, 241, 121, 119, 242, 65, 192, 110, 129, 119, 65, 77, 158, 13, 150, 144, 28, 235,
33, 208, 173, 221, 78, 19, 60, 123, 224, 65, 6, 100, 121, 203, 211, 101, 20, 169, 44,
125, 233, 145, 41, 91, 200, 233, 176, 158, 87, 101, 14, 124, 251, 239, 197, 63, 193,
29, 63, 169, 173, 27, 106, 244, 66, 35, 18, 131, 154, 12, 85, 56, 162, 240, 100, 125,
155, 115, 241, 135, 95, 223, 191, 44, 141, 140, 9, 202, 43, 152, 228, 117, 44, 46, 126,
194, 128, 157,
];
let m = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100];
verify_bls_signature(&s, &m, &key).unwrap();
}
}
34 changes: 34 additions & 0 deletions app/src/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "actions.h"
#include "formatting.h"
#include "zxformat.h"
#include "rslib.h"

zxerr_t addr_getNumItems(uint8_t *num_items) {
zemu_log_stack("addr_getNumItems");
Expand All @@ -34,10 +35,36 @@ zxerr_t addr_getNumItems(uint8_t *num_items) {
return zxerr_ok;
}

uint8_t counter = 0;

zxerr_t addr_getItem(int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount) {
// Assuming the signature is 48 bytes long based on the provided values
uint8_t s[48] = {
139, 118, 54, 85, 203, 37, 178, 69, 140, 15, 123, 156, 250, 54, 205, 107,
22, 254, 170, 98, 234, 151, 252, 248, 245, 75, 211, 209, 237, 75, 135, 119,
53, 244, 174, 38, 241, 127, 34, 154, 2, 92, 174, 10, 73, 110, 128, 24
};

// Assuming the public key is 96 bytes long based on the provided values
uint8_t pub[96] = {
136, 241, 121, 119, 242, 65, 192, 110, 129, 119, 65, 77, 158, 13, 150, 144,
28, 235, 33, 208, 173, 221, 78, 19, 60, 123, 224, 65, 6, 100, 121, 203,
211, 101, 20, 169, 44, 125, 233, 145, 41, 91, 200, 233, 176, 158, 87, 101,
14, 124, 251, 239, 197, 63, 193, 29, 63, 169, 173, 27, 106, 244, 66, 35,
18, 131, 154, 12, 85, 56, 162, 240, 100, 125, 155, 115, 241, 135, 95, 223,
191, 44, 141, 140, 9, 202, 43, 152, 228, 117, 44, 46, 126, 194, 128, 157
};

// Assuming the message is 11 bytes long based on the provided values
uint8_t message[11] = {
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100
};

CHECK_APP_CANARY();

char buffer[50] = {0};
snprintf(buffer, sizeof(buffer), "addr_getItem %d/%d", displayIdx, pageIdx);
zemu_log_stack(buffer);
Expand All @@ -49,11 +76,18 @@ zxerr_t addr_getItem(int8_t displayIdx,
switch (displayIdx) {
case 0:
snprintf(outKey, outKeyLen, "Address ");
if (counter == 0 && verify_bls_sign(s, message, 11, pub) == 1) {
zemu_log_stack("\n OK!!***\n");
} else {
zemu_log_stack("\nERR!!***\n");
}
counter += 1;
err = page_hexstring_with_delimiters(G_io_apdu_buffer + VIEW_ADDRESS_OFFSET_TEXT, DFINITY_SUBACCOUNT_LEN,
outVal, outValLen, pageIdx, pageCount);
if (err != parser_ok) {
return zxerr_unknown;
}
CHECK_APP_CANARY();
return zxerr_ok;

case 1:
Expand Down
Loading

0 comments on commit ecab35d

Please sign in to comment.