|
| 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::{network::Network, program::ValueType}; |
| 17 | +use snarkvm_synthesizer_program::Program; |
| 18 | + |
| 19 | +use anyhow::{Result, bail, ensure}; |
| 20 | + |
| 21 | +/// Verifies that the existing output register indices are not changed in a new version of the program. |
| 22 | +// Note. This function is public so that depednent crates can cleanly surface this error to users. |
| 23 | +pub fn check_output_register_indices_unchanged<N: Network>( |
| 24 | + old_program: &Program<N>, |
| 25 | + new_program: &Program<N>, |
| 26 | +) -> Result<()> { |
| 27 | + for (id, function) in old_program.functions() { |
| 28 | + // Get the corresponding function in the new program. |
| 29 | + let Ok(new_function) = new_program.get_function(id) else { bail!("Missing function '{id}'") }; |
| 30 | + // Ensure the record output registers match. |
| 31 | + let existing_output_registers = |
| 32 | + function.outputs().iter().filter(|output| matches!(output.value_type(), ValueType::Record(_))); |
| 33 | + let new_output_registers = |
| 34 | + new_function.outputs().iter().filter(|output| matches!(output.value_type(), ValueType::Record(_))); |
| 35 | + ensure!( |
| 36 | + existing_output_registers.eq(new_output_registers), |
| 37 | + "Function '{id}' has mismatched record output registers" |
| 38 | + ); |
| 39 | + } |
| 40 | + Ok(()) |
| 41 | +} |
0 commit comments