Ejemplo n.º 1
0
def test_cairo_pie_serialize_deserialize():
    program = compile_cairo(
        code=[('%builtins output pedersen range_check ecdsa\nmain:\n[ap] = [ap]\n', '')],
        prime=DEFAULT_PRIME)
    metadata = CairoPieMetadata(
        program=program.stripped(),
        program_segment=SegmentInfo(0, 10),
        execution_segment=SegmentInfo(1, 20),
        ret_fp_segment=SegmentInfo(6, 12),
        ret_pc_segment=SegmentInfo(7, 21),
        builtin_segments={
            'a': SegmentInfo(4, 15),
        },
        extra_segments=[],
    )
    memory = MemoryDict({
        1: 2,
        RelocatableValue(3, 4): RelocatableValue(6, 7),
    })
    additional_data = {'c': ['d', 3]}
    execution_resources = ExecutionResources(
        n_steps=10,
        n_memory_holes=7,
        builtin_instance_counter={
            'output': 6,
            'pedersen': 3,
        }
    )
    cairo_pie = CairoPie(
        metadata=metadata,
        memory=memory,
        additional_data=additional_data,
        execution_resources=execution_resources
    )

    fileobj = io.BytesIO()
    cairo_pie.to_file(fileobj)
    actual_cairo_pie = CairoPie.from_file(fileobj)

    assert cairo_pie == actual_cairo_pie
Ejemplo n.º 2
0
 def get_builtin_segments_info(self):
     builtin_segments: Dict[str, SegmentInfo] = {}
     for builtin in self.builtin_runners.values():
         for name, segment_addresses in builtin.get_memory_segment_addresses(
                 self).items():
             begin_addr = segment_addresses.begin_addr
             assert isinstance(begin_addr, RelocatableValue), \
                 f'{name} segment begin_addr is not a RelocatableValue {begin_addr}.'
             assert begin_addr.offset == 0, \
                 f'Unexpected {name} segment begin_addr {begin_addr.offset}.'
             assert segment_addresses.stop_ptr is not None, f'{name} segment stop ptr is None.'
             segment_index = begin_addr.segment_index
             segment_size = segment_addresses.stop_ptr - begin_addr
             assert name not in builtin_segments, f'Builtin segment name collision: {name}.'
             builtin_segments[name] = SegmentInfo(index=segment_index,
                                                  size=segment_size)
     return builtin_segments
Ejemplo n.º 3
0
    def get_cairo_pie(self) -> CairoPie:
        """
        Constructs and returns a CairoPie representing the current VM run.
        """
        builtin_segments = self.get_builtin_segments_info()
        known_segment_indices = WriteOnceDict()
        for segment_info in builtin_segments.values():
            known_segment_indices[segment_info.index] = None

        # Note that n_used_builtins might be smaller then len(builtin_segments).
        n_used_builtins = len(self.program.builtins)
        ret_fp, ret_pc = (self.vm_memory[self.execution_base +
                                         n_used_builtins + i]
                          for i in range(2))

        assert isinstance(
            ret_fp,
            RelocatableValue), f'Expecting a relocatable value got {ret_fp}.'
        assert isinstance(
            ret_pc,
            RelocatableValue), f'Expecting a relocatable value got {ret_pc}.'

        assert self.segments.segment_sizes[ret_fp.segment_index] == 0, \
            f'Unexpected ret_fp_segment size {self.segments.segment_sizes[ret_fp.segment_index]}'
        assert self.segments.segment_sizes[ret_pc.segment_index] == 0, \
            f'Unexpected ret_pc_segment size {self.segments.segment_sizes[ret_pc.segment_index]}'

        for addr in self.program_base, self.execution_base, ret_fp, ret_pc:
            assert addr.offset == 0, 'Expecting a 0 offset.'
            known_segment_indices[addr.segment_index] = None

        # Put all the remaining segments in extra_segments.
        extra_segments = [
            SegmentInfo(idx, size)
            for idx, size in sorted(self.segments.segment_sizes.items())
            if idx not in known_segment_indices
        ]

        execution_size = self.vm.run_context.ap - self.execution_base
        cairo_pie_metadata = CairoPieMetadata(
            program=self.program.stripped(),
            program_segment=SegmentInfo(index=self.program_base.segment_index,
                                        size=len(self.program.data)),
            execution_segment=SegmentInfo(
                index=self.execution_base.segment_index, size=execution_size),
            ret_fp_segment=SegmentInfo(ret_fp.segment_index, 0),
            ret_pc_segment=SegmentInfo(ret_pc.segment_index, 0),
            builtin_segments=builtin_segments,
            extra_segments=extra_segments,
        )

        execution_resources = self.get_execution_resources()

        return CairoPie(
            metadata=cairo_pie_metadata,
            memory=self.vm.run_context.memory,
            additional_data={
                name: builtin.get_additional_data()
                for name, builtin in self.builtin_runners.items()
            },
            execution_resources=execution_resources,
        )