Skip to content

Commit

Permalink
add spend and output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Nov 18, 2024
1 parent afd1261 commit a32eadd
Show file tree
Hide file tree
Showing 15 changed files with 486 additions and 93 deletions.
2 changes: 2 additions & 0 deletions app/rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub const DETECTION_DATA_QTY: usize = 16;
pub const ACTION_DATA_QTY: usize = 16;
pub const MAX_CLUE_SUBKEYS: usize = 10;

pub const EFFECT_HASH_LEN: usize = 64;

// Nonces:
pub const NONCE_LEN: usize = 12;
pub const NONCE_NOTE: &[u8; NONCE_LEN] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
Expand Down
16 changes: 8 additions & 8 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#![no_std]
// #![no_std]
#![no_builtins]
#![allow(dead_code)]
#![deny(unused_crate_dependencies)]
Expand Down Expand Up @@ -44,14 +44,14 @@ pub(crate) use utils::prf::{expand_fq, expand_fr};

fn debug(_msg: &str) {}

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
use core::panic::PanicInfo;
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// use core::panic::PanicInfo;

#[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
// #[cfg(all(not(test), not(feature = "clippy"), not(feature = "fuzzing")))]
// #[panic_handler]
// fn panic(_info: &PanicInfo) -> ! {
// loop {}
// }

extern "C" {
fn check_app_canary();
Expand Down
3 changes: 2 additions & 1 deletion app/rust/src/parser/plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::effect_hash::EffectHash;
use crate::parser::bytes::BytesC;
use crate::parser::parameters::TransactionParametersC;
use crate::ParserError;
use crate::constants::EFFECT_HASH_LEN;

#[repr(C)]
#[cfg_attr(any(feature = "derive-debug", test), derive(Debug))]
Expand Down Expand Up @@ -68,7 +69,7 @@ pub unsafe extern "C" fn rs_compute_transaction_plan(
crate::zlog("rs_compute_transaction_plan\x00");
let output = std::slice::from_raw_parts_mut(output, output_len);

if output.len() < 200 {
if output.len() < EFFECT_HASH_LEN {
return ParserError::UnexpectedData as u32;
}

Expand Down
5 changes: 0 additions & 5 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>

typedef struct {
uint8_t *ptr;
uint16_t len;
} bytes_t;

#define KEY_LEN 32
#define DIVERSIFIER_KEY_LEN 16
#define OUTGOING_VIEWING_KEY_LEN KEY_LEN
Expand Down
31 changes: 15 additions & 16 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static bool decode_detection_data(pb_istream_t *stream, const pb_field_t *field,
static uint16_t actions_qty = 0;
static uint16_t detection_data_qty = 0;

void print_buffer(Bytes_t *buffer, const char *title) {
void print_buffer(bytes_t *buffer, const char *title) {
#if defined(LEDGER_SPECIFIC)
ZEMU_LOGF(50, "%s\n", title);
char print[1000] = {0};
Expand All @@ -53,7 +53,7 @@ void print_string(const char *str) {
#endif
}

parser_error_t decode_output_plan(const Bytes_t *data, output_plan_t *output) {
parser_error_t decode_output_plan(const bytes_t *data, output_plan_t *output) {
penumbra_core_component_shielded_pool_v1_OutputPlan output_plan =
penumbra_core_component_shielded_pool_v1_OutputPlan_init_default;

Expand Down Expand Up @@ -86,7 +86,7 @@ parser_error_t decode_output_plan(const Bytes_t *data, output_plan_t *output) {
return parser_ok;
}

parser_error_t decode_delegate_plan(const Bytes_t *data, delegate_plan_t *delegate) {
parser_error_t decode_delegate_plan(const bytes_t *data, delegate_plan_t *delegate) {
penumbra_core_component_stake_v1_Delegate delegate_plan =
penumbra_core_component_stake_v1_Delegate_init_default;

Expand Down Expand Up @@ -115,7 +115,7 @@ parser_error_t decode_delegate_plan(const Bytes_t *data, delegate_plan_t *delega
return parser_ok;
}

parser_error_t decode_undelegate_plan(const Bytes_t *data, undelegate_plan_t *undelegate) {
parser_error_t decode_undelegate_plan(const bytes_t *data, undelegate_plan_t *undelegate) {
penumbra_core_component_stake_v1_Undelegate undelegate_plan =
penumbra_core_component_stake_v1_Undelegate_init_default;

Expand Down Expand Up @@ -160,7 +160,7 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
return false;
}

Bytes_t action_data;
bytes_t action_data;
action_data.ptr = stream->state + 3;
action_data.len = stream->bytes_left - 3;

Expand Down Expand Up @@ -223,7 +223,7 @@ bool decode_detection_data(pb_istream_t *stream, const pb_field_t *field, void *
}

parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
Bytes_t data;
bytes_t data;
action_t actions_plan[ACTIONS_QTY];
data.ptr = c->buffer;
data.len = c->bufferLen;
Expand Down Expand Up @@ -305,20 +305,19 @@ parser_error_t _read(parser_context_t *c, parser_tx_t *v) {
print_buffer(&v->plan.memo.plaintext.return_address.alt_bech32m, "real memo return address alt bech32m");

for (uint16_t i = 0; i < actions_qty; i++) {
switch (actions_plan[i].action_type) {
case penumbra_core_transaction_v1_ActionPlan_spend_tag:
compute_spend_action_hash(&actions_plan[i].action.spend, &v->plan.actions.hashes[i]);
break;
case penumbra_core_transaction_v1_ActionPlan_output_tag:
compute_output_action_hash(&actions_plan[i].action.output, &v->plan.actions.hashes[i]);
break;
}
compute_action_hash(&actions_plan[i].action.spend, actions_plan[i].action_type, &v->plan.actions.hashes[i]);
}
v->plan.actions.qty = actions_qty;

compute_transaction_plan(&v->plan);
compute_transaction_plan(&v->plan, v->effect_hash, sizeof(v->effect_hash));

return parser_unexpected_error;
// TODO: only for testing
bytes_t effect_hash;
effect_hash.ptr = v->effect_hash;
effect_hash.len = sizeof(v->effect_hash);
print_buffer(&effect_hash, "effect_hash");

return parser_ok;
}

const char *parser_getErrorDescription(parser_error_t err) {
Expand Down
50 changes: 19 additions & 31 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "keys_def.h"
#include "rslib.h"
#include "zxformat.h"
#include "protobuf/penumbra/core/transaction/v1/transaction.pb.h"

void print_buffer_interface(uint8_t *buffer, size_t len, const char *title) {
#if defined(LEDGER_SPECIFIC)
Expand All @@ -36,21 +37,17 @@ void print_buffer_interface(uint8_t *buffer, size_t len, const char *title) {
#endif
}

parser_error_t compute_transaction_plan(transaction_plan_t *plan) {
if (plan == NULL) return parser_unexpected_error;
parser_error_t compute_transaction_plan(transaction_plan_t *plan, uint8_t *effect_hash, uint16_t effect_hash_len) {
if (plan == NULL || effect_hash == NULL) return parser_unexpected_error;

uint8_t output[300] = {0};
if (rs_compute_transaction_plan(plan, output, sizeof(output)) != parser_ok) {
if (rs_compute_transaction_plan(plan, effect_hash, effect_hash_len) != parser_ok) {
return parser_unexpected_error;
}

// TODO: only for testing
print_buffer_interface(output, 300, "output_bytes");

return parser_ok;
}

parser_error_t compute_spend_action_hash(spend_plan_t *plan, action_hash_t *output) {
parser_error_t compute_action_hash(spend_plan_t *plan, uint8_t action_type, action_hash_t *output) {
if (plan == NULL || output == NULL)
return parser_unexpected_error;

Expand All @@ -60,33 +57,24 @@ parser_error_t compute_spend_action_hash(spend_plan_t *plan, action_hash_t *outp
0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc
};

if (rs_spend_action_hash(&sk_bytes, plan, (uint8_t *)output, 64) != parser_ok) {
return parser_unexpected_error;
bytes_t memo = {0};
switch (action_type) {
case penumbra_core_transaction_v1_ActionPlan_spend_tag:
if (rs_spend_action_hash(&sk_bytes, plan, (uint8_t *)output, 64) != parser_ok) {
return parser_unexpected_error;
}
break;
case penumbra_core_transaction_v1_ActionPlan_output_tag:
if (rs_output_action_hash(&sk_bytes, plan, &memo, (uint8_t *)output, 64) != parser_ok) {
return parser_unexpected_error;
}
break;
default:
return parser_unexpected_error;
}

// TODO: only for testing
print_buffer_interface((uint8_t *)output, 64, "spend action hash");

return parser_ok;
}

parser_error_t compute_output_action_hash(output_plan_t *plan, action_hash_t *output) {
if (plan == NULL || output == NULL)
return parser_unexpected_error;

// TODO: we need to get the spend key
spend_key_bytes_t sk_bytes = {
0xa1, 0xff, 0xba, 0x0c, 0x37, 0x93, 0x1f, 0x0a, 0x62, 0x61, 0x37, 0x52, 0x0d, 0xa6, 0x50, 0x63,
0x2d, 0x35, 0x85, 0x3b, 0xf5, 0x91, 0xb3, 0x6b, 0xb4, 0x28, 0x63, 0x0a, 0x4d, 0x87, 0xc4, 0xdc
};
Bytes_t memo = {0};

if (rs_output_action_hash(&sk_bytes, plan, &memo, (uint8_t *)output, 64) != parser_ok) {
return parser_unexpected_error;
}

// TODO: only for testing
print_buffer_interface((uint8_t *)output, 64, "output action hash");

return parser_ok;
}
5 changes: 2 additions & 3 deletions app/src/parser_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ extern "C" {
#include "zxerror.h"
#include "zxmacros.h"

parser_error_t compute_transaction_plan(transaction_plan_t *plan);
parser_error_t compute_spend_action_hash(spend_plan_t *plan, action_hash_t *output);
parser_error_t compute_output_action_hash(output_plan_t *plan, action_hash_t *output);
parser_error_t compute_transaction_plan(transaction_plan_t *plan, uint8_t *effect_hash, uint16_t effect_hash_len);
parser_error_t compute_action_hash(spend_plan_t *plan, uint8_t action_type, action_hash_t *output);

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/parser_pb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ bool decode_variable_field(pb_istream_t *stream, const pb_field_t *field, void *
return true;
}

void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size) {
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, bytes_t *bytes, uint16_t expected_size) {
arg->bytes = bytes;
arg->expected_size = expected_size;
callback->funcs.decode = &decode_fixed_field;
callback->arg = arg;
}

void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, Bytes_t *bytes) {
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, bytes_t *bytes) {
arg->bytes = bytes;
callback->funcs.decode = &decode_variable_field;
callback->arg = arg;
}

parser_error_t extract_data_from_tag(Bytes_t *in, Bytes_t *out, uint32_t tag) {
parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag) {
const uint8_t *start = NULL;
const uint8_t *end = NULL;
bool eof = false;
Expand Down
10 changes: 5 additions & 5 deletions app/src/parser_pb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ extern "C" {
#include "zxtypes.h"

typedef struct {
Bytes_t *bytes;
bytes_t *bytes;
uint16_t expected_size;
} fixed_size_field_t;

typedef struct {
Bytes_t *bytes;
bytes_t *bytes;
} variable_size_field_t;

// Callback to parse binding fields in spend plans. all those fields are just
Expand All @@ -46,9 +46,9 @@ typedef struct {
bool decode_fixed_field(pb_istream_t *stream, const pb_field_t *field, void **arg);
bool decode_variable_field(pb_istream_t *stream, const pb_field_t *field, void **arg);

void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, Bytes_t *bytes, uint16_t expected_size);
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, Bytes_t *bytes);
parser_error_t extract_data_from_tag(Bytes_t *in, Bytes_t *out, uint32_t tag);
void setup_decode_fixed_field(pb_callback_t *callback, fixed_size_field_t *arg, bytes_t *bytes, uint16_t expected_size);
void setup_decode_variable_field(pb_callback_t *callback, variable_size_field_t *arg, bytes_t *bytes);
parser_error_t extract_data_from_tag(bytes_t *in, bytes_t *out, uint32_t tag);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit a32eadd

Please sign in to comment.