Skip to content

Commit 0122cf3

Browse files
aszepieniecSword-Smith
authored andcommitted
style: Check sanity of size indicators
Closes Neptune-Crypto#276. Co-authored-by: Thorkil Schmidiger <thor@neptune.cash>
1 parent f83a0b3 commit 0122cf3

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/models/blockchain/type_scripts/native_currency.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tasm_lib::memory::encode_to_memory;
1414
use tasm_lib::memory::FIRST_NON_DETERMINISTICALLY_INITIALIZED_MEMORY_ADDRESS;
1515
use tasm_lib::prelude::Library;
1616
use tasm_lib::prelude::TasmObject;
17+
use tasm_lib::structure::tasm_object::DEFAULT_MAX_DYN_FIELD_SIZE;
1718
use tasm_lib::structure::verify_nd_si_integrity::VerifyNdSiIntegrity;
1819
use tasm_lib::triton_vm::prelude::*;
1920
use tasm_lib::twenty_first::math::b_field_element::BFieldElement;
@@ -43,6 +44,9 @@ const BAD_SALTED_UTXOS_ERROR: i128 = 1_000_031;
4344
const NO_INFLATION_VIOLATION: i128 = 1_000_032;
4445
const BAD_STATE_SIZE_ERROR: i128 = 1_000_033;
4546
const COINBASE_TIMELOCK_INSUFFICIENT: i128 = 1_000_034;
47+
const UTXO_SIZE_TOO_LARGE_ERROR: i128 = 1_000_035;
48+
const TOO_BIG_COIN_FIELD_SIZE_ERROR: i128 = 1_000_036;
49+
const STATE_LENGTH_FOR_TIME_LOCK_NOT_ONE_ERROR: i128 = 1_000_037;
4650

4751
/// `NativeCurrency` is the type script that governs Neptune's native currency,
4852
/// Neptune coins.
@@ -661,7 +665,6 @@ impl ConsensusProgram for NativeCurrency {
661665
hint coins_j_si = stack[8]
662666
hint j = stack[9]
663667
hint emm = stack[10]
664-
break
665668

666669
push 0 push 0 push 0 push 0
667670
push 0
@@ -679,7 +682,8 @@ impl ConsensusProgram for NativeCurrency {
679682
pick 7 pick 7 pick 7 pick 7
680683
// _ N i *utxos[i]_si M M *coins[M]_si [amount'] [timelocked_amount']
681684

682-
dup 12 push 1 add
685+
// prepare next iteration
686+
dup 12 addi 1
683687
// _ N i *utxos[i]_si M M *coins[M]_si [amount'] [timelocked_amount'] (i+1)
684688

685689
swap 13 pop 1
@@ -688,6 +692,11 @@ impl ConsensusProgram for NativeCurrency {
688692
dup 11 read_mem 1 push 2 add
689693
// _ N (i+1) *utxos[i]_si M M *coins[M]_si [amount'] [timelocked_amount'] size(utxos[i]) *utxos[i]
690694

695+
push {DEFAULT_MAX_DYN_FIELD_SIZE}
696+
dup 2
697+
lt
698+
assert error_id {UTXO_SIZE_TOO_LARGE_ERROR}
699+
691700
add swap 12 pop 1
692701
// _ N (i+1) *utxos[i+1]_si M M *coins[M]_si [amount'] [timelocked_amount']
693702

@@ -776,6 +785,13 @@ impl ConsensusProgram for NativeCurrency {
776785
dup 13 read_mem 1 push 2 add
777786
// _ M (j+1) *coins[j]_si [amount] [timelocked_amount] [utxo_amount] utxo_is_timelocked size(coins[j]) *coins[j]
778787

788+
/* Range-check on size */
789+
push {DEFAULT_MAX_DYN_FIELD_SIZE}
790+
dup 2
791+
lt
792+
assert error_id {TOO_BIG_COIN_FIELD_SIZE_ERROR}
793+
// _ M (j+1) *coins[j]_si [amount] [timelocked_amount] [utxo_amount] utxo_is_timelocked size(coins[j]) *coins[j]
794+
779795
add
780796
// _ M (j+1) *coins[j]_si [amount] [timelocked_amount] [utxo_amount] utxo_is_timelocked *coins[j+1]_si
781797

@@ -801,7 +817,7 @@ impl ConsensusProgram for NativeCurrency {
801817
// _ M j *coins[j]_si [amount] [timelocked_amount] [utxo_amount'] utxo_is_timelocked state[0] state.len()
802818

803819
// time lock states must encode exactly one element
804-
assert
820+
assert error_id {STATE_LENGTH_FOR_TIME_LOCK_NOT_ONE_ERROR}
805821
// _ M j *coins[j]_si [amount] [timelocked_amount] [utxo_amount'] utxo_is_timelocked utxo_release_date
806822

807823
split

0 commit comments

Comments
 (0)