Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hint fix in progress #7

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
venv
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.9.18
22 changes: 10 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
[package]
name = "cairo-bootloader"
version = "0.1.0"
edition = "2021"
[workspace]
members = ["runner","bootloader"]
resolver = "2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "05352b1c67859a4d8cd128575c1e68ca7e300341", features = ["extensive_hints"] }
[workspace.dependencies]
cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", features = [
"extensive_hints",
"mod_builtin",
] }
num-traits = "0.2.19"
serde = { version = "1.0.202", features = ["derive"] }
serde_json = "1.0.117"
starknet-crypto = "0.6.2"
starknet-crypto = "0.7.0"
starknet-types-core = "0.1.2"
thiserror = "1.0.61"
thiserror-no-std = "2.0.2"

[dev-dependencies]
assert_matches = "1.5.0"
rstest = "0.19.0"

bincode = "2.0.0-rc.3"
19 changes: 19 additions & 0 deletions bootloader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "bootloader"
version = "0.1.0"
edition = "2021"

[dependencies]
cairo-vm.workspace = true
num-traits.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet-crypto.workspace = true
starknet-types-core.workspace = true
thiserror.workspace = true
thiserror-no-std.workspace = true
bincode.workspace = true

[dev-dependencies]
assert_matches = "1.5.0"
rstest = "0.19.0"
6 changes: 3 additions & 3 deletions src/bootloaders.rs → bootloader/src/bootloaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use cairo_vm::types::program::Program;

pub use crate::hints::*;

const BOOTLOADER_V0_13_0: &[u8] = include_bytes!("../resources/bootloader-0.13.0.json");

const _BOOTLOADER_V0_13_0: &[u8] = include_bytes!("../../resources/bootloader-0.13.0.json");
const BOOTLOADER_V0_13_2: &[u8] = include_bytes!("../../resources/bootloader-0.13.2.json");
/// Loads the bootloader and returns it as a Cairo VM `Program` object.
pub fn load_bootloader() -> Result<Program, ProgramError> {
Program::from_bytes(BOOTLOADER_V0_13_0, Some("main"))
Program::from_bytes(BOOTLOADER_V0_13_2, Some("main"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ use crate::hints::vars;
/// %}
/// ```
pub fn prepare_simple_bootloader_output_segment(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
// Python: bootloader_input = BootloaderInput.Schema().load(program_input)
// -> Assert that the bootloader input has been loaded when setting up the VM
let _bootloader_input: &BootloaderInput = exec_scopes.get_ref(vars::BOOTLOADER_INPUT)?;

Ok(())
}
pub fn bootloader_runner_output_segment(
vm: &mut VirtualMachine,
ids_data: &HashMap<String, HintReference>,
ap_tracking: &ApTracking,
exec_scopes: &mut ExecutionScopes,
) -> Result<(), HintError> {
// Python: ids.simple_bootloader_output_start = segments.add()
let new_segment_base = vm.add_memory_segment();
insert_value_from_var_name(
Expand All @@ -70,7 +74,6 @@ pub fn prepare_simple_bootloader_output_segment(
ids_data,
ap_tracking,
)?;

Ok(())
}

Expand Down Expand Up @@ -498,10 +501,7 @@ mod tests {

exec_scopes.insert_value(vars::BOOTLOADER_INPUT, bootloader_input);
prepare_simple_bootloader_output_segment(
&mut vm,
&mut exec_scopes,
&ids_data,
&ap_tracking,
)
.expect("Hint failed unexpectedly");

Expand Down Expand Up @@ -537,10 +537,7 @@ mod tests {
let ap_tracking = ApTracking::default();

let result = prepare_simple_bootloader_output_segment(
&mut vm,
&mut exec_scopes,
&ids_data,
&ap_tracking,
);
let hint_error =
result.expect_err("Hint should fail, the bootloader input variable is not set");
Expand Down
19 changes: 6 additions & 13 deletions src/hints/codes.rs → bootloader/src/hints/codes.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
pub const BOOTLOADER_PREPARE_SIMPLE_BOOTLOADER_OUTPUT_SEGMENT: &str =
"from starkware.cairo.bootloaders.bootloader.objects import BootloaderInput
bootloader_input = BootloaderInput.Schema().load(program_input)

ids.simple_bootloader_output_start = segments.add()

# Change output builtin state to a different segment in preparation for calling the
# simple bootloader.
output_builtin_state = output_builtin.get_state()
output_builtin.new_state(base=ids.simple_bootloader_output_start)";
bootloader_input = BootloaderInput.Schema().load(program_input)";
pub const BOOTLOADER_RUNNER_OUTPUT_SEGMENT: &str =
"ids.simple_bootloader_output_start = segments.add()\n\n# Change output builtin state to a different segment in preparation for calling the\n# simple bootloader.\noutput_builtin_state = output_builtin.get_state()\noutput_builtin.new_state(base=ids.simple_bootloader_output_start)";

pub const BOOTLOADER_PREPARE_SIMPLE_BOOTLOADER_INPUT: &str =
"simple_bootloader_input = bootloader_input";
Expand Down Expand Up @@ -117,6 +112,8 @@ task = simple_bootloader_input.tasks[task_id].load_task()";
// Appears as nondet %{ 0 %} in the code.
pub const SIMPLE_BOOTLOADER_ZERO: &str = "memory[ap] = to_felt_or_relocatable(0)";

pub const SIMPLE_BOOTLOADER_USE_POSEIDON: &str =
"memory[ap] = to_felt_or_relocatable(1 if task.use_poseidon else 0)";
pub const EXECUTE_TASK_ALLOCATE_PROGRAM_DATA_SEGMENT: &str =
"ids.program_data_ptr = program_data_base = segments.add()";

Expand All @@ -129,11 +126,7 @@ program_address, program_data_size = load_program(
builtins_offset=ids.ProgramHeader.builtin_list)
segments.finalize(program_data_base.segment_index, program_data_size)";

pub const EXECUTE_TASK_VALIDATE_HASH: &str = "# Validate hash.
from starkware.cairo.bootloaders.hash_program import compute_program_hash_chain

assert memory[ids.output_ptr + 1] == compute_program_hash_chain(task.get_program()), \\
'Computed hash does not match input.'";
pub const EXECUTE_TASK_VALIDATE_HASH: &str = "# Validate hash.\nfrom starkware.cairo.bootloaders.hash_program import compute_program_hash_chain\n\nassert memory[ids.output_ptr + 1] == compute_program_hash_chain(\n program=task.get_program(),\n use_poseidon=bool(ids.use_poseidon)), 'Computed hash does not match input.'";

pub const EXECUTE_TASK_ASSERT_PROGRAM_ADDRESS: &str = "# Sanity check.
assert ids.program_address == program_address";
Expand Down
Loading