|
| 1 | +// This test contains parts imported from cairo-lang |
| 2 | +// https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/builtins/segment_arena/segment_arena_test.cairo |
| 3 | + |
| 4 | +from starkware.cairo.common.alloc import alloc |
| 5 | + |
| 6 | +// Represents the information about a single segment allocated by the arena. |
| 7 | +struct SegmentInfo { |
| 8 | + // A pointer to the first element of this segment. |
| 9 | + start: felt*, |
| 10 | + // A pointer to the end of this segment (the first unused element). |
| 11 | + end: felt*, |
| 12 | + // A sequential id, assigned to the segment when it is finalized. |
| 13 | + // This value is used to guarantee that 'end' is not assigned twice. |
| 14 | + finalization_index: felt, |
| 15 | +} |
| 16 | + |
| 17 | +// Represents the status of the segment arena. |
| 18 | +struct SegmentArenaBuiltin { |
| 19 | + // A pointer to a list of SegmentInfo. infos[i] contains information about the i-th segment |
| 20 | + // (ordered by construction). |
| 21 | + // The value is fixed during the execution of an entry point. |
| 22 | + infos: SegmentInfo*, |
| 23 | + // The number of segments that were created so far. |
| 24 | + n_segments: felt, |
| 25 | + // The number of segments that were finalized so far. |
| 26 | + n_finalized: felt, |
| 27 | +} |
| 28 | + |
| 29 | +// Constructs a new segment for the segment arena builtin and initializes it with an empty instance |
| 30 | +// of `SegmentArenaBuiltin`. |
| 31 | +func new_arena() -> SegmentArenaBuiltin* { |
| 32 | + let (segment_arena: SegmentArenaBuiltin*) = alloc(); |
| 33 | + assert segment_arena[0] = SegmentArenaBuiltin( |
| 34 | + infos=cast(nondet %{ segments.add() %}, SegmentInfo*), n_segments=0, n_finalized=0 |
| 35 | + ); |
| 36 | + return &segment_arena[1]; |
| 37 | +} |
| 38 | + |
| 39 | +// Validates the segment arena builtin. |
| 40 | +// |
| 41 | +// In particular, relocates the temporary segments such that the start of segment i is strictly |
| 42 | +// larger than the end of segment i+1. |
| 43 | +func validate_segment_arena(segment_arena: SegmentArenaBuiltin*) { |
| 44 | + tempvar n_segments = segment_arena.n_segments; |
| 45 | + tempvar n_finalized = segment_arena.n_finalized; |
| 46 | + // The following line should follow from the fact that every allocated segment |
| 47 | + // must be finalized exactly once. |
| 48 | + // We keep it both as a sanity check and since Sierra compilation is not proven yet. |
| 49 | + assert n_segments = n_finalized; |
| 50 | +
|
| 51 | + if (n_segments == 0) { |
| 52 | + return (); |
| 53 | + } |
| 54 | +
|
| 55 | + // The following call also implies that n_segments > 0. |
| 56 | + _verify_continuity(infos=segment_arena.infos, n_segments_minus_one=n_segments - 1); |
| 57 | + return (); |
| 58 | +} |
| 59 | + |
| 60 | +// Helper function for validate_segment_arena. |
| 61 | +func _verify_continuity(infos: SegmentInfo*, n_segments_minus_one: felt) { |
| 62 | + if (n_segments_minus_one == 0) { |
| 63 | + // If there is only one segment left, there is no need to check anything. |
| 64 | + return (); |
| 65 | + } |
| 66 | +
|
| 67 | + // Enforce an empty cell between two consecutive segments so that the start of a segment |
| 68 | + // is strictly bigger than the end of the previous segment. |
| 69 | + // This is required for proving the soundness of this construction, in the case where a segment |
| 70 | + // has length zero. |
| 71 | +
|
| 72 | + // Note: the following code was copied from relocate_segment() for efficiency reasons. |
| 73 | + let src_ptr = infos[1].start; |
| 74 | + let dest_ptr = infos[0].end + 1; |
| 75 | + %{ memory.add_relocation_rule(src_ptr=ids.src_ptr, dest_ptr=ids.dest_ptr) %} |
| 76 | + assert src_ptr = dest_ptr; |
| 77 | +
|
| 78 | + return _verify_continuity(infos=&infos[1], n_segments_minus_one=n_segments_minus_one - 1); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// Creates a new segment using the segment arena. |
| 83 | +func new_segment{segment_arena: SegmentArenaBuiltin*}() -> felt* { |
| 84 | + let prev_segment_arena = &segment_arena[-1]; |
| 85 | + tempvar n_segments = prev_segment_arena.n_segments; |
| 86 | + tempvar infos = prev_segment_arena.infos; |
| 87 | + |
| 88 | + %{ |
| 89 | + if 'segment_index_to_arena_index' not in globals(): |
| 90 | + # A map from the relocatable value segment index to the index in the arena. |
| 91 | + segment_index_to_arena_index = {} |
| 92 | +
|
| 93 | + # The segment is placed at the end of the arena. |
| 94 | + index = ids.n_segments |
| 95 | +
|
| 96 | + # Create a segment or a temporary segment. |
| 97 | + start = segments.add_temp_segment() if index > 0 else segments.add() |
| 98 | +
|
| 99 | + # Update 'SegmentInfo::start' and 'segment_index_to_arena_index'. |
| 100 | + ids.prev_segment_arena.infos[index].start = start |
| 101 | + segment_index_to_arena_index[start.segment_index] = index |
| 102 | + %} |
| 103 | + assert segment_arena[0] = SegmentArenaBuiltin( |
| 104 | + infos=infos, n_segments=n_segments + 1, n_finalized=prev_segment_arena.n_finalized |
| 105 | + ); |
| 106 | + let segment_arena = &segment_arena[1]; |
| 107 | + return infos[n_segments].start; |
| 108 | +} |
| 109 | + |
| 110 | +// Finalizes a given segment and returns the corresponding start. |
| 111 | +func finalize_segment{segment_arena: SegmentArenaBuiltin*}(segment_end: felt*) -> felt* { |
| 112 | + let prev_segment_arena = &segment_arena[-1]; |
| 113 | + tempvar n_segments = prev_segment_arena.n_segments; |
| 114 | + tempvar n_finalized = prev_segment_arena.n_finalized; |
| 115 | + |
| 116 | + // Guess the index of the segment. |
| 117 | + tempvar index = nondet %{ segment_index_to_arena_index[ids.segment_end.segment_index] %}; |
| 118 | + |
| 119 | + // Write segment_end in the manager. |
| 120 | + tempvar infos: SegmentInfo* = prev_segment_arena.infos; |
| 121 | + tempvar segment_info: SegmentInfo* = &infos[index]; |
| 122 | + // Writing n_finalized to 'finalization_index' guarantees 'segment_info.end' was not assigned |
| 123 | + // a value before. |
| 124 | + assert segment_info.finalization_index = n_finalized; |
| 125 | + assert segment_info.end = segment_end; |
| 126 | + |
| 127 | + assert segment_arena[0] = SegmentArenaBuiltin( |
| 128 | + infos=infos, n_segments=n_segments, n_finalized=n_finalized + 1 |
| 129 | + ); |
| 130 | + |
| 131 | + let segment_arena = &segment_arena[1]; |
| 132 | + return segment_info.start; |
| 133 | +} |
| 134 | + |
| 135 | +func test_segment_arena() -> (felt*, SegmentInfo*) { |
| 136 | + alloc_locals; |
| 137 | +
|
| 138 | + local segment_arena_start: SegmentArenaBuiltin* = new_arena(); |
| 139 | + let segment_arena = segment_arena_start; |
| 140 | +
|
| 141 | + with segment_arena { |
| 142 | + let segment0 = new_segment(); |
| 143 | + let segment1 = new_segment(); |
| 144 | + let segment2 = new_segment(); |
| 145 | +
|
| 146 | + assert segment0[0] = 1; |
| 147 | + assert segment0[1] = 2; |
| 148 | +
|
| 149 | + assert segment1[0] = 3; |
| 150 | + assert segment1[1] = 4; |
| 151 | +
|
| 152 | + assert segment2[0] = 5; |
| 153 | +
|
| 154 | + assert finalize_segment(segment0 + 2) = segment0; |
| 155 | + assert finalize_segment(segment1 + 2) = segment1; |
| 156 | +
|
| 157 | + let segment3 = new_segment(); |
| 158 | +
|
| 159 | + assert segment3[0] = 6; |
| 160 | + assert segment3[1] = 7; |
| 161 | +
|
| 162 | + assert finalize_segment(segment3 + 2) = segment3; |
| 163 | + assert finalize_segment(segment2 + 1) = segment2; |
| 164 | + } |
| 165 | + validate_segment_arena(segment_arena=&segment_arena[-1]); |
| 166 | + return (segment0, segment_arena_start[-1].infos); |
| 167 | +} |
| 168 | + |
| 169 | +func main{}() { |
| 170 | + let (_segment0, _infos) = test_segment_arena(); |
| 171 | + ret; |
| 172 | +} |
0 commit comments