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

Public inputs refactor #676

Merged
merged 35 commits into from
Dec 6, 2023
Merged

Public inputs refactor #676

merged 35 commits into from
Dec 6, 2023

Conversation

juan518munoz
Copy link
Contributor

@juan518munoz juan518munoz commented Nov 9, 2023

Refactor Public Inputs

Description

Modify the MemorySegmentMap field of PublicInputs to be a HashMap with values of type Segment instead of Range.

This pr also updates the cairo-vm crate to it's latest main revision.

Type of change

  • Optimization

@codecov-commenter
Copy link

codecov-commenter commented Nov 9, 2023

Codecov Report

Attention: 14 lines in your changes are missing coverage. Please review.

Comparison is base (cd957b1) 96.23% compared to head (58a5d4e) 96.16%.

Files Patch % Lines
provers/cairo/src/air.rs 72.00% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #676      +/-   ##
==========================================
- Coverage   96.23%   96.16%   -0.07%     
==========================================
  Files         133      133              
  Lines       29801    29678     -123     
==========================================
- Hits        28678    28541     -137     
- Misses       1123     1137      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@juan518munoz
Copy link
Contributor Author

juan518munoz commented Nov 9, 2023

Initial implementation, the struct Segment replaces Range<u64> as the value of the MemorySegmentMap : HashMap.

Range<u64> is still used (and instanced like n..m) on some functions, for this reason From and Into has been implemented beetwen this types.

@juan518munoz
Copy link
Contributor Author

Should we implement Iterator for Segment in a similar way Range does?

@entropidelic
Copy link
Contributor

Should we implement Iterator for Segment in a similar way Range does?

@juan518munoz I think it is not necessary right now, is there any benefit on doing so?

@juan518munoz
Copy link
Contributor Author

juan518munoz commented Nov 9, 2023

Should we implement Iterator for Segment in a similar way Range does?

@juan518munoz I think it is not necessary right now, is there any benefit on doing so?

Not really, it would only save us from doing two clones() that are needed to cast into Range.

@juan518munoz juan518munoz marked this pull request as ready for review November 13, 2023 14:23
@juan518munoz juan518munoz requested review from schouhy, ajgara and a team as code owners November 13, 2023 14:23
Comment on lines 128 to 132
let codelen = runner.get_program().data_len();

let public_memory = (1..=codelen as u64)
.map(|i| (Felt252::from(i), *cairo_mem.get(&i).unwrap()))
.collect::<HashMap<Felt252, Felt252>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have the public inputs directly from the cairo vm, there is no need to ask for the codelen, and the public memory should be obtained from the public inputs, this was just a hacky solution until we had access to them.
The approach should be similar to the one you did with the memory segments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a result of this, the PublicInputs field codelen could be removed, since it can be deduced with the length of the Program memory segment

Copy link
Contributor Author

@juan518munoz juan518munoz Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only function that makes use of the codelen field is get_memory_holes which itself is only called inside build_main_trace. How should we handle this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach should be similar to the one you did with the memory segments

I'm having trouble doing an iteration over the vm public memory, doing something like this:

    let mut public_memory: HashMap<Felt252, Felt252> = HashMap::new();
    vm_public_inputs.public_memory.iter().for_each(|e| {
        let v = e.value.clone().unwrap().to_str_radix(16);
        public_memory.insert(
            Felt252::from((e.address + e.page) as u64),
            Felt252::from_hex_unchecked(&v),
        );
    });

Breaks some of our tests

image

Maybe I'm missing something?

@MauroToscano MauroToscano marked this pull request as draft November 27, 2023 13:43
@entropidelic entropidelic marked this pull request as ready for review December 5, 2023 18:10
provers/cairo/src/air.rs Outdated Show resolved Hide resolved
provers/cairo/src/air.rs Outdated Show resolved Hide resolved
Comment on lines 534 to 544
let mut pub_addrs = public_input.public_memory.keys();

// Iterate over addresses
for (i, a) in a_aux.iter_mut().enumerate() {
// When address `0` is found, it means it corresponds to a dummy access.
if a == &Felt252::zero() {
// While there are public memory addresses left, overwrite the dummy
// (addr, value) accesses with the real public memory pairs.
if let Some(pub_addr) = pub_addrs_iter.next() {
if let Some(pub_addr) = pub_addrs.next() {
*a = *pub_addr;
v_aux[i] = *public_input.public_memory.get(pub_addr).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just public_input.public_memory.iter() and then get the value and key together? It saves an unwrap.
Related question, is this logic dependent on the order of elements? Because public_memory seems to be a HashMap. Maybe a BTreeMap is more appropriate here?

@entropidelic entropidelic added this pull request to the merge queue Dec 6, 2023
Merged via the queue into main with commit 2828a9f Dec 6, 2023
7 checks passed
@entropidelic entropidelic deleted the PublicInputsRefactor branch December 6, 2023 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants