snarkvm_synthesizer_program/logic/finalize_global_state/
mod.rs

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
16use console::network::prelude::*;
17
18#[derive(Copy, Clone, Debug, PartialEq, Eq)]
19pub struct FinalizeGlobalState {
20    /// The block round.
21    block_round: u64,
22    /// The block height.
23    block_height: u32,
24    /// The block timestamp.
25    block_timestamp: Option<i64>, // TODO (raychu86): Consider adding the entire Metadata here instead.
26    /// The block-specific random seed.
27    random_seed: [u8; 32],
28}
29
30impl FinalizeGlobalState {
31    /// Initializes a new genesis global state.
32    #[inline]
33    pub fn new_genesis<N: Network>() -> Result<Self> {
34        // Initialize the parameters.
35        let block_round = 0;
36        let block_height = 0;
37        let block_cumulative_weight = 0;
38        let block_cumulative_proof_target = 0;
39        let previous_block_hash = N::BlockHash::default();
40        // Return the new global state.
41        Self::new::<N>(
42            block_round,
43            block_height,
44            None,
45            block_cumulative_weight,
46            block_cumulative_proof_target,
47            previous_block_hash,
48        )
49    }
50
51    /// Initializes a new global state from the given inputs.
52    #[inline]
53    pub fn new<N: Network>(
54        block_round: u64,
55        block_height: u32,
56        block_timestamp: Option<i64>,
57        block_cumulative_weight: u128,
58        block_cumulative_proof_target: u128,
59        previous_block_hash: N::BlockHash,
60    ) -> Result<Self> {
61        // Initialize the preimage, optionally including the block timestamp.
62        let preimage = to_bits_le![
63            block_round,
64            block_height,
65            block_cumulative_weight,
66            block_cumulative_proof_target,
67            (*previous_block_hash); 605
68        ]
69        .into_iter()
70        .chain(block_timestamp.into_iter().flat_map(|ts| to_bits_le![ts]))
71        .collect::<Vec<_>>();
72
73        // Hash the preimage to get the random seed.
74        let seed = N::hash_bhp768(&preimage)?.to_bytes_le()?;
75        // Ensure the seed is 32-bytes.
76        ensure!(seed.len() == 32, "Invalid seed length for finalize global state.");
77
78        // Convert the seed into a 32-byte array.
79        let mut random_seed = [0u8; 32];
80        random_seed.copy_from_slice(&seed[..32]);
81
82        Ok(Self { block_round, block_height, block_timestamp, random_seed })
83    }
84
85    /// Initializes a new global state.
86    #[inline]
87    pub const fn from(
88        block_round: u64,
89        block_height: u32,
90        block_timestamp: Option<i64>,
91        random_seed: [u8; 32],
92    ) -> Self {
93        Self { block_round, block_height, block_timestamp, random_seed }
94    }
95
96    /// Returns the block round.
97    #[inline]
98    pub const fn block_round(&self) -> u64 {
99        self.block_round
100    }
101
102    /// Returns the block height.
103    #[inline]
104    pub const fn block_height(&self) -> u32 {
105        self.block_height
106    }
107
108    /// Returns the random seed.
109    #[inline]
110    pub const fn random_seed(&self) -> &[u8; 32] {
111        &self.random_seed
112    }
113
114    /// Returns the block timestamp.
115    #[inline]
116    pub const fn block_timestamp(&self) -> Option<i64> {
117        self.block_timestamp
118    }
119}