|
| 1 | +// Copyright (c) 2019-2025 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 console::{ |
| 17 | + network::MainnetV0, |
| 18 | + prelude::*, |
| 19 | + program::{ArrayType, LiteralType, Locator, PlaintextType, U32}, |
| 20 | +}; |
| 21 | +use snarkvm_synthesizer_process::{Process, Stack}; |
| 22 | +use snarkvm_synthesizer_program::{Program, types_equivalent}; |
| 23 | + |
| 24 | +type CurrentNetwork = MainnetV0; |
| 25 | + |
| 26 | +// ---------- Helper ---------- |
| 27 | +fn sample_stack(program_text: &str) -> Result<Stack<CurrentNetwork>> { |
| 28 | + let program = Program::from_str(program_text)?; |
| 29 | + let stack = Stack::new(&Process::load()?, &program)?; |
| 30 | + Ok(stack) |
| 31 | +} |
| 32 | + |
| 33 | +// ---------- 1️⃣ Literal equivalence ---------- |
| 34 | +#[test] |
| 35 | +fn test_literal_equivalence() -> Result<()> { |
| 36 | + let stack = sample_stack("program p_lit.aleo; function main:")?; |
| 37 | + |
| 38 | + let u32_ty = PlaintextType::Literal(LiteralType::U32); |
| 39 | + let u64_ty = PlaintextType::Literal(LiteralType::U64); |
| 40 | + |
| 41 | + assert!(types_equivalent(&stack, &u32_ty, &stack, &u32_ty)?); |
| 42 | + assert!(!types_equivalent(&stack, &u32_ty, &stack, &u64_ty)?); |
| 43 | + |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +// ---------- 2️⃣ Struct equivalence (same program) ---------- |
| 48 | +#[test] |
| 49 | +fn test_struct_equivalence_same_program() -> Result<()> { |
| 50 | + let program_text = r" |
| 51 | + program p_struct.aleo; |
| 52 | + struct Foo: x as u32; y as u32; |
| 53 | + struct Bar: x as u32; y as u32; z as u32; |
| 54 | + function main: |
| 55 | + "; |
| 56 | + |
| 57 | + let stack = sample_stack(program_text)?; |
| 58 | + let foo_ty = PlaintextType::Struct("Foo".try_into()?); |
| 59 | + let bar_ty = PlaintextType::Struct("Bar".try_into()?); |
| 60 | + |
| 61 | + assert!(types_equivalent(&stack, &foo_ty, &stack, &foo_ty)?); |
| 62 | + assert!(!types_equivalent(&stack, &foo_ty, &stack, &bar_ty)?); |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +// ---------- 3️⃣ Array equivalence ---------- |
| 68 | +#[test] |
| 69 | +fn test_array_equivalence() -> Result<()> { |
| 70 | + let stack = sample_stack("program p_array.aleo; function main:")?; |
| 71 | + |
| 72 | + let u32_ty = PlaintextType::Literal(LiteralType::U32); |
| 73 | + |
| 74 | + let array1 = PlaintextType::Array(ArrayType::new(*Box::new(u32_ty.clone()), vec![U32::new(3)])?); |
| 75 | + let array2 = PlaintextType::Array(ArrayType::new(*Box::new(u32_ty.clone()), vec![U32::new(3)])?); |
| 76 | + let array3 = PlaintextType::Array(ArrayType::new(*Box::new(u32_ty.clone()), vec![U32::new(4)])?); |
| 77 | + |
| 78 | + assert!(types_equivalent(&stack, &array1, &stack, &array2)?); |
| 79 | + assert!(!types_equivalent(&stack, &array1, &stack, &array3)?); |
| 80 | + |
| 81 | + Ok(()) |
| 82 | +} |
| 83 | + |
| 84 | +// ---------- 4️⃣ Cross-program struct equivalence ---------- |
| 85 | +#[test] |
| 86 | +fn test_cross_program_structs_equivalence() -> Result<()> { |
| 87 | + // ---------- Sample stacks ---------- |
| 88 | + let s1 = sample_stack("program p1.aleo; struct Foo: x as u32; y as u32; function main:")?; |
| 89 | + let s2 = sample_stack("program p2.aleo; struct Foo: x as u32; y as u32; function main:")?; |
| 90 | + let s3 = sample_stack("program p3.aleo; struct Foo: x as u32; y as u32; z as u32; function main:")?; |
| 91 | + let s4 = sample_stack("program p4.aleo; struct Bar: x as u32; y as u32; function main:")?; |
| 92 | + let s5 = sample_stack( |
| 93 | + "program p5.aleo; struct Inner: a as u32; struct Outer: inner as Inner; b as u32; function main:", |
| 94 | + )?; |
| 95 | + let s6 = sample_stack( |
| 96 | + "program p6.aleo; struct Inner: a as u32; struct Outer: inner as Inner; b as u32; function main:", |
| 97 | + )?; |
| 98 | + let s7 = sample_stack( |
| 99 | + "program p7.aleo; struct Inner: a as u32; struct Outer: inner as Inner; c as u32; function main:", |
| 100 | + )?; |
| 101 | + |
| 102 | + // ---------- Define types ---------- |
| 103 | + let foo_ty = PlaintextType::Struct("Foo".try_into()?); |
| 104 | + let foo_ty_diff = PlaintextType::Struct("Foo".try_into()?); // will use s3 to test different fields |
| 105 | + let bar_ty = PlaintextType::Struct("Bar".try_into()?); |
| 106 | + let outer_ty = PlaintextType::Struct("Outer".try_into()?); |
| 107 | + let outer_ty_diff = PlaintextType::Struct("Outer".try_into()?); |
| 108 | + |
| 109 | + // ---------- Assertions ---------- |
| 110 | + // Same name, same fields |
| 111 | + assert!(types_equivalent(&s1, &foo_ty, &s2, &foo_ty)?); |
| 112 | + |
| 113 | + // Same name, different fields |
| 114 | + assert!(!types_equivalent(&s1, &foo_ty, &s3, &foo_ty_diff)?); |
| 115 | + |
| 116 | + // Different names, same fields |
| 117 | + assert!(!types_equivalent(&s1, &foo_ty, &s4, &bar_ty)?); |
| 118 | + |
| 119 | + // Nested structs, same fields |
| 120 | + assert!(types_equivalent(&s5, &outer_ty, &s6, &outer_ty)?); |
| 121 | + |
| 122 | + // Nested structs, different fields in inner |
| 123 | + assert!(!types_equivalent(&s5, &outer_ty, &s7, &outer_ty_diff)?); |
| 124 | + |
| 125 | + Ok(()) |
| 126 | +} |
| 127 | + |
| 128 | +// ---------- 5️⃣ External vs Local struct ---------- |
| 129 | +#[test] |
| 130 | +fn test_external_vs_local_struct_equivalence() -> Result<()> { |
| 131 | + // Create a single process to hold both programs |
| 132 | + let mut process = Process::<CurrentNetwork>::load()?; |
| 133 | + |
| 134 | + // 1️⃣ External program |
| 135 | + let external_program = Program::from_str( |
| 136 | + r"program external.aleo; |
| 137 | + struct Foo: x as u32; y as u32; |
| 138 | + function main:", |
| 139 | + )?; |
| 140 | + process.add_program(&external_program)?; |
| 141 | + |
| 142 | + // 2️⃣ Local program that imports external |
| 143 | + let local_program = Program::from_str( |
| 144 | + r"import external.aleo; |
| 145 | + program local.aleo; |
| 146 | + struct Foo: x as u32; y as u32; |
| 147 | + function main:", |
| 148 | + )?; |
| 149 | + process.add_program(&local_program)?; |
| 150 | + |
| 151 | + // 3️⃣ Retrieve the stack for the local program |
| 152 | + let s_local = process.get_stack(local_program.id())?; |
| 153 | + |
| 154 | + let local_ty = PlaintextType::<CurrentNetwork>::Struct("Foo".try_into()?); |
| 155 | + let external_ty = |
| 156 | + PlaintextType::<CurrentNetwork>::ExternalStruct(Locator::new("external.aleo".try_into()?, "Foo".try_into()?)); |
| 157 | + |
| 158 | + // Local vs External |
| 159 | + assert!(types_equivalent(&*s_local, &local_ty, &*s_local, &external_ty)?); |
| 160 | + |
| 161 | + // External vs Local |
| 162 | + assert!(types_equivalent(&*s_local, &external_ty, &*s_local, &local_ty)?); |
| 163 | + |
| 164 | + // External vs External |
| 165 | + assert!(types_equivalent(&*s_local, &external_ty, &*s_local, &external_ty)?); |
| 166 | + |
| 167 | + Ok(()) |
| 168 | +} |
| 169 | + |
| 170 | +#[test] |
| 171 | +fn test_external_and_array_struct_equivalence() -> Result<()> { |
| 172 | + use console::program::{ArrayType, LiteralType, Locator, PlaintextType, U32}; |
| 173 | + use snarkvm_synthesizer_program::types_equivalent; |
| 174 | + |
| 175 | + // ---------- 1️⃣ Create a single process ---------- |
| 176 | + let mut process = Process::<CurrentNetwork>::load()?; |
| 177 | + |
| 178 | + // ---------- 2️⃣ External program ---------- |
| 179 | + let external_program = Program::from_str( |
| 180 | + r"program external.aleo; |
| 181 | + struct Foo: x as u32; y as u32; |
| 182 | + struct Bar: a as u32; b as u32; |
| 183 | + function main:", |
| 184 | + )?; |
| 185 | + process.add_program(&external_program)?; |
| 186 | + |
| 187 | + // ---------- 3️⃣ Local program importing external ---------- |
| 188 | + let local_program = Program::from_str( |
| 189 | + r"import external.aleo; |
| 190 | + program local.aleo; |
| 191 | + struct Foo: x as u32; y as u32; |
| 192 | + struct Baz: f as Foo; g as u32; |
| 193 | + function main:", |
| 194 | + )?; |
| 195 | + process.add_program(&local_program)?; |
| 196 | + |
| 197 | + // ---------- 4️⃣ Retrieve the stack ---------- |
| 198 | + let s_local = process.get_stack(local_program.id())?; |
| 199 | + |
| 200 | + // ---------- 5️⃣ Define types ---------- |
| 201 | + let local_foo = PlaintextType::Struct("Foo".try_into()?); |
| 202 | + let external_foo = PlaintextType::ExternalStruct(Locator::new("external.aleo".try_into()?, "Foo".try_into()?)); |
| 203 | + |
| 204 | + let local_baz = PlaintextType::Struct("Baz".try_into()?); |
| 205 | + |
| 206 | + // Arrays of literals |
| 207 | + let u32_lit = PlaintextType::Literal(LiteralType::U32); |
| 208 | + let array_3 = PlaintextType::Array(ArrayType::new(*Box::new(u32_lit.clone()), vec![U32::new(3)])?); |
| 209 | + let array_4 = PlaintextType::Array(ArrayType::new(*Box::new(u32_lit.clone()), vec![U32::new(4)])?); |
| 210 | + |
| 211 | + // Arrays of structs |
| 212 | + let array_foo_2 = PlaintextType::Array(ArrayType::new(*Box::new(local_foo.clone()), vec![U32::new(2)])?); |
| 213 | + let array_external_foo_2 = |
| 214 | + PlaintextType::Array(ArrayType::new(*Box::new(external_foo.clone()), vec![U32::new(2)])?); |
| 215 | + |
| 216 | + // ---------- 6️⃣ Assertions ---------- |
| 217 | + |
| 218 | + // Local vs External struct |
| 219 | + assert!(types_equivalent(&*s_local, &local_foo, &*s_local, &external_foo)?); |
| 220 | + assert!(types_equivalent(&*s_local, &external_foo, &*s_local, &local_foo)?); |
| 221 | + |
| 222 | + // External vs External |
| 223 | + assert!(types_equivalent(&*s_local, &external_foo, &*s_local, &external_foo)?); |
| 224 | + |
| 225 | + // Arrays of literals |
| 226 | + assert!(types_equivalent(&*s_local, &array_3, &*s_local, &array_3)?); |
| 227 | + assert!(!types_equivalent(&*s_local, &array_3, &*s_local, &array_4)?); |
| 228 | + |
| 229 | + // Arrays of structs |
| 230 | + assert!(types_equivalent(&*s_local, &array_foo_2, &*s_local, &array_external_foo_2)?); |
| 231 | + |
| 232 | + // Nested struct inside local |
| 233 | + assert!(types_equivalent(&*s_local, &local_baz, &*s_local, &local_baz)?); |
| 234 | + |
| 235 | + Ok(()) |
| 236 | +} |
0 commit comments