|
| 1 | +// Copyright (c) 2019-2026 Provable Inc. |
| 2 | +// This file is part of the snarkVM library. |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at: |
| 7 | + |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +use super::*; |
| 17 | + |
| 18 | +use crate::Stack; |
| 19 | + |
| 20 | +use console::network::prelude::*; |
| 21 | + |
| 22 | +/// Builds a VM advanced to V19 height and runs `check_program_plaintext_sizes` |
| 23 | +/// against `program_text` using the V19 bit budget. |
| 24 | +fn run_check_at_v19(program_text: &str) -> Result<()> { |
| 25 | + let rng = &mut TestRng::default(); |
| 26 | + let vm = sample_vm_at_height(CurrentNetwork::CONSENSUS_HEIGHT(ConsensusVersion::V19).unwrap(), rng); |
| 27 | + |
| 28 | + let program = Program::<CurrentNetwork>::from_str(program_text).unwrap(); |
| 29 | + let stack = Stack::new(vm.process(), &program).unwrap(); |
| 30 | + let max_bits = CurrentNetwork::LATEST_MAX_PLAINTEXT_TYPE_SIZE_IN_BITS(); |
| 31 | + check_program_plaintext_sizes(&program, &stack, max_bits) |
| 32 | +} |
| 33 | + |
| 34 | +/// A triply-nested 2048x2048x2048 bool array, the largest array type the element limit permits. |
| 35 | +/// It is rejected on the type AST, without sampling any leaf. |
| 36 | +#[test] |
| 37 | +fn test_deeply_nested_array_input_rejected() { |
| 38 | + let result = run_check_at_v19( |
| 39 | + r" |
| 40 | +program nested_array.aleo; |
| 41 | +
|
| 42 | +function f: |
| 43 | + input r0 as [[[boolean; 2048u32]; 2048u32]; 2048u32].private; |
| 44 | + output r0 as [[[boolean; 2048u32]; 2048u32]; 2048u32].private; |
| 45 | +
|
| 46 | +constructor: |
| 47 | + assert.eq true true; |
| 48 | +", |
| 49 | + ); |
| 50 | + let err = result.expect_err("over-cap nested array type must be rejected"); |
| 51 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 52 | +} |
| 53 | + |
| 54 | +/// A program whose function input fits well under the cap is accepted. |
| 55 | +#[test] |
| 56 | +fn test_under_cap_function_input_accepted() { |
| 57 | + run_check_at_v19( |
| 58 | + r" |
| 59 | +program small.aleo; |
| 60 | +
|
| 61 | +function f: |
| 62 | + input r0 as [u64; 100u32].private; |
| 63 | + output r0 as [u64; 100u32].private; |
| 64 | +
|
| 65 | +constructor: |
| 66 | + assert.eq true true; |
| 67 | +", |
| 68 | + ) |
| 69 | + .expect("under-cap program must pass"); |
| 70 | +} |
| 71 | + |
| 72 | +/// A struct whose member exceeds the per-type cap is rejected. |
| 73 | +#[test] |
| 74 | +fn test_over_cap_struct_member_rejected() { |
| 75 | + let err = run_check_at_v19( |
| 76 | + r" |
| 77 | +program big_struct.aleo; |
| 78 | +
|
| 79 | +struct big: |
| 80 | + huge as [[u64; 2048u32]; 9u32]; |
| 81 | +
|
| 82 | +function f: |
| 83 | + input r0 as u64.private; |
| 84 | + output r0 as u64.private; |
| 85 | +
|
| 86 | +constructor: |
| 87 | + assert.eq true true; |
| 88 | +", |
| 89 | + ) |
| 90 | + .expect_err("over-cap struct member must be rejected"); |
| 91 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 92 | +} |
| 93 | + |
| 94 | +/// A record entry that exceeds the per-type cap is rejected. |
| 95 | +#[test] |
| 96 | +fn test_over_cap_record_entry_rejected() { |
| 97 | + let err = run_check_at_v19( |
| 98 | + r" |
| 99 | +program big_record.aleo; |
| 100 | +
|
| 101 | +record big: |
| 102 | + owner as address.private; |
| 103 | + huge as [[u64; 2048u32]; 9u32].private; |
| 104 | +
|
| 105 | +function f: |
| 106 | + input r0 as u64.private; |
| 107 | + output r0 as u64.private; |
| 108 | +
|
| 109 | +constructor: |
| 110 | + assert.eq true true; |
| 111 | +", |
| 112 | + ) |
| 113 | + .expect_err("over-cap record entry must be rejected"); |
| 114 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 115 | +} |
| 116 | + |
| 117 | +/// A mapping value that exceeds the per-type cap is rejected. |
| 118 | +#[test] |
| 119 | +fn test_over_cap_mapping_value_rejected() { |
| 120 | + let err = run_check_at_v19( |
| 121 | + r" |
| 122 | +program big_mapping.aleo; |
| 123 | +
|
| 124 | +mapping m: |
| 125 | + key as u64.public; |
| 126 | + value as [[u64; 2048u32]; 9u32].public; |
| 127 | +
|
| 128 | +function f: |
| 129 | + input r0 as u64.private; |
| 130 | + output r0 as u64.private; |
| 131 | +
|
| 132 | +constructor: |
| 133 | + assert.eq true true; |
| 134 | +", |
| 135 | + ) |
| 136 | + .expect_err("over-cap mapping value must be rejected"); |
| 137 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 138 | +} |
| 139 | + |
| 140 | +/// A closure input that exceeds the per-type cap is rejected. |
| 141 | +#[test] |
| 142 | +fn test_over_cap_closure_input_rejected() { |
| 143 | + let err = run_check_at_v19( |
| 144 | + r" |
| 145 | +program big_closure.aleo; |
| 146 | +
|
| 147 | +closure c: |
| 148 | + input r0 as [[u64; 2048u32]; 9u32]; |
| 149 | + is.eq r0 r0 into r1; |
| 150 | + output r1 as boolean; |
| 151 | +
|
| 152 | +function f: |
| 153 | + input r0 as u64.private; |
| 154 | + output r0 as u64.private; |
| 155 | +
|
| 156 | +constructor: |
| 157 | + assert.eq true true; |
| 158 | +", |
| 159 | + ) |
| 160 | + .expect_err("over-cap closure input must be rejected"); |
| 161 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 162 | +} |
| 163 | + |
| 164 | +/// A finalize argument that exceeds the per-type cap is rejected. |
| 165 | +/// Since async arguments and finalize inputs must agree, the over-cap type also appears as |
| 166 | +/// a function input; the function-input check fires first, but the program is still rejected. |
| 167 | +/// The legacy `check_future_argument_bit_size` runs only before V14 and permits up to |
| 168 | +/// `u16::MAX` bits; this check applies from V19 with a tighter budget. |
| 169 | +#[test] |
| 170 | +fn test_over_cap_finalize_input_rejected() { |
| 171 | + let err = run_check_at_v19( |
| 172 | + r" |
| 173 | +program big_finalize.aleo; |
| 174 | +
|
| 175 | +function f: |
| 176 | + input r0 as u64.public; |
| 177 | + input r1 as [[u64; 2048u32]; 9u32].public; |
| 178 | + async f r0 r1 into r2; |
| 179 | + output r2 as big_finalize.aleo/f.future; |
| 180 | +
|
| 181 | +finalize f: |
| 182 | + input r0 as u64.public; |
| 183 | + input r1 as [[u64; 2048u32]; 9u32].public; |
| 184 | + assert.eq r0 r0; |
| 185 | +
|
| 186 | +constructor: |
| 187 | + assert.eq true true; |
| 188 | +", |
| 189 | + ) |
| 190 | + .expect_err("over-cap finalize input must be rejected"); |
| 191 | + assert!(err.to_string().contains("exceeds the maximum allowed size in bits"), "unexpected error: {err}"); |
| 192 | +} |
0 commit comments