cairo_vm/vm/errors/
memory_errors.rs

1// The `(*.0).0` syntax of thiserror falsely triggers this clippy warning
2#![allow(clippy::explicit_auto_deref)]
3
4use crate::stdlib::prelude::*;
5use crate::types::builtin_name::BuiltinName;
6
7use thiserror_no_std::Error;
8
9use crate::Felt252;
10
11use crate::types::{
12    errors::math_errors::MathError,
13    relocatable::{MaybeRelocatable, Relocatable},
14};
15
16#[derive(Debug, PartialEq, Error)]
17pub enum MemoryError {
18    #[error(transparent)]
19    Math(#[from] MathError),
20    #[error(transparent)]
21    InsufficientAllocatedCells(#[from] InsufficientAllocatedCellsError),
22    #[error("Can't insert into segment #{}; memory only has {} segment", (*.0).0, (*.0).1)]
23    UnallocatedSegment(Box<(usize, usize)>),
24    #[error("Memory addresses must be relocatable")]
25    AddressNotRelocatable,
26    #[error("Range-check validation failed, number {} is out of valid range [0, {}]", (*.0).0, (*.0).1)]
27    RangeCheckNumOutOfBounds(Box<(Felt252, Felt252)>),
28    #[error("Range-check validation failed, encountered non-int value at address {0}")]
29    RangeCheckFoundNonInt(Box<Relocatable>),
30    #[error("Inconsistent memory assignment at address {:?}. {:?} != {:?}", (*.0).0, (*.0).1, (*.0).2)]
31    InconsistentMemory(Box<(Relocatable, MaybeRelocatable, MaybeRelocatable)>),
32    #[error("Inconsistent Relocation")]
33    Relocation,
34    #[error("Could not cast arguments")]
35    WriteArg,
36    #[error("Memory addresses mustn't be in a TemporarySegment, segment: {0}")]
37    AddressInTemporarySegment(isize),
38    #[error("Memory addresses must be in a TemporarySegment, segment: {0}")]
39    AddressNotInTemporarySegment(isize),
40    #[error("Temporary segment found while relocating (flattening), segment: {0}")]
41    TemporarySegmentInRelocation(isize),
42    #[error("The TemporarySegment: {0} doesn't have a relocation address")]
43    NonZeroOffset(usize),
44    #[error("Attempt to overwrite a relocation rule, segment: {0}")]
45    DuplicatedRelocation(isize),
46    #[error("Segment effective sizes haven't been calculated.")]
47    MissingSegmentUsedSizes,
48    #[error("Found a memory gap when calling get_continuous_range with base:{} and size: {}", (*.0).0, (*.0).1)]
49    GetRangeMemoryGap(Box<(Relocatable, usize)>),
50    #[error("Error calculating builtin memory units")]
51    ErrorCalculatingMemoryUnits,
52    #[error("Missing memory cells for {0}")]
53    MissingMemoryCells(Box<BuiltinName>),
54    #[error("Missing memory cells for {}: {:?}", (*.0).0, (*.0).1)]
55    MissingMemoryCellsWithOffsets(Box<(BuiltinName, Vec<usize>)>),
56    #[error("ErrorInitializing Verifying Key from public key: {0:?}")]
57    InitializingVerifyingKey(Box<Vec<u8>>),
58    #[error(
59        "Signature {}, is invalid, with respect to the public key {}, 
60    and the message hash {}.", (*.0).0, (*.0).1, (*.0).2
61    )]
62    InvalidSignature(Box<(String, Felt252, Felt252)>),
63    #[error(
64        "Signature hint is missing for ECDSA builtin at address {0}.
65    Add it using 'ecdsa_builtin.add_signature'."
66    )]
67    SignatureNotFound(Box<Relocatable>),
68    #[error("Could not create pubkey from: {0:?}")]
69    ErrorParsingPubKey(Box<str>),
70    #[error("Could not retrieve message from: {0:?}")]
71    ErrorRetrievingMessage(Box<str>),
72    #[error("Error verifying given signature")]
73    ErrorVerifyingSignature,
74    #[error("Couldn't obtain a mutable accessed offset")]
75    CantGetMutAccessedOffset,
76    #[error("ECDSA builtin: Expected public key at address {0} to be an integer")]
77    PubKeyNonInt(Box<Relocatable>),
78    #[error("ECDSA builtin: Expected message hash at address {0} to be an integer")]
79    MsgNonInt(Box<Relocatable>),
80    #[error("Failed to convert String: {0} to FieldElement")]
81    FailedStringToFieldElementConversion(Box<str>),
82    #[error("Failed to fetch {} return values, ap is only {}", (*.0).0, (*.0).1)]
83    FailedToGetReturnValues(Box<(usize, Relocatable)>),
84    #[error("Segment {} has {} amount of accessed addresses but its size is only {}.", (*.0).0, (*.0).1, (*.0).2)]
85    SegmentHasMoreAccessedAddressesThanSize(Box<(usize, usize, usize)>),
86    #[error("gen_arg: found argument of invalid type.")]
87    GenArgInvalidType,
88    // Memory.get() errors
89    #[error("Expected integer at address {0}")]
90    ExpectedInteger(Box<Relocatable>),
91    #[error("Expected relocatable at address {0}")]
92    ExpectedRelocatable(Box<Relocatable>),
93    #[error("Unknown memory cell at address {0}")]
94    UnknownMemoryCell(Box<Relocatable>),
95    // SegmentArenaBuiltin
96    #[error("segment_arena_builtin: assert used >= INITIAL_SEGMENT_SIZE")]
97    InvalidUsedSizeSegmentArena,
98    #[error("Vector capacity exceeded")]
99    VecCapacityExceeded,
100    #[error("Memory wasn't relocated")]
101    UnrelocatedMemory,
102    #[error("Malformed public memory")]
103    MalformedPublicMemory,
104}
105
106#[derive(Debug, PartialEq, Eq, Error)]
107pub enum InsufficientAllocatedCellsError {
108    #[error("Number of steps must be at least {} for the {}.", (*.0).0, (*.0).1)]
109    MinStepNotReached(Box<(usize, BuiltinName)>),
110    #[error("The {} used {} cells but the capacity is {}.", (*.0).0, (*.0).1, (*.0).2)]
111    BuiltinCells(Box<(BuiltinName, usize, usize)>),
112    #[error("There are only {} cells to fill the range checks holes, but potentially {} are required.", (*.0).0, (*.0).1)]
113    RangeCheckUnits(Box<(usize, usize)>),
114    #[error("There are only {} cells to fill the diluted check holes, but potentially {} are required.", (*.0).0, (*.0).1)]
115    DilutedCells(Box<(usize, usize)>),
116    #[error("There are only {} cells to fill the memory address holes, but {} are required.", (*.0).0, (*.0).1)]
117    MemoryAddresses(Box<(u32, usize)>),
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    // Test to catch possible enum size regressions
126    fn test_memory_error_size() {
127        let size = crate::stdlib::mem::size_of::<MemoryError>();
128        assert!(size <= 24, "{size}")
129    }
130
131    #[test]
132    // Test to catch possible enum size regressions
133    fn test_insufficient_allocated_cells_error_size() {
134        let size = crate::stdlib::mem::size_of::<InsufficientAllocatedCellsError>();
135        assert!(size <= 16, "{size}")
136    }
137}