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

Add procedures to read and verify data from the advice stack #1312

Open
hackaugusto opened this issue Apr 25, 2024 · 3 comments
Open

Add procedures to read and verify data from the advice stack #1312

hackaugusto opened this issue Apr 25, 2024 · 3 comments
Labels
stdlib Related to Miden standard library

Comments

@hackaugusto
Copy link
Contributor

We often have to write code that does the following:

  • initialize the stack with the hasher state
  • read N words from the advice stack to memory, and run a permute after each read
  • extract the digest
  • validate the data against a known value

I think steps 1 through 3 should have a procedure, and step the another one for steps 1 through 4.

@hackaugusto hackaugusto added the stdlib Related to Miden standard library label Apr 25, 2024
@hackaugusto
Copy link
Contributor Author

Well, I just realized we have some of this functionality already:

#! Moves an arbitrary number of words from the advice stack to memory and asserts it matches the commitment.
#!
#! Input: [num_words, write_ptr, COM, ...]
#! Output: [write_ptr', ...]
#! Cycles:
#! even num_words: 58 + 9 * num_words / 2
#! odd num_words: 75 + 9 * round_down(num_words / 2)
export.pipe_preimage_to_memory.0

But we never use it because that is less efficient, because the counter goes to the stack, and we usually write code with a hardcoded constants. I guess we have a few alternatives:

  • Write multiple procedures, e.g. pipe_to_memory_2 / pipe_to_memory_4 / pipe_to_memory_6, where the number in the procedure is the number of words read from the advice stack to memory
  • Extend the assembly syntax to allow for constant arguments to procedures, something like:
export.pipe_to_memory_even(count)
  repead.@count
    adv_pipe hperm
  end
end

Where the @count would be replaced by the assembler, similar to a preprocessor.

@hackaugusto
Copy link
Contributor Author

The idea of using constant seems to have an issue of its own: #297

@bobbinth
Copy link
Contributor

  • Extend the assembly syntax to allow for constant arguments to procedures, something like:
export.pipe_to_memory_even(count)
  repead.@count
    adv_pipe hperm
  end
end

This is a bit tricky because for every value of @count, we'd have a different MAST root. This means that we won't be able to compile this function to MAST without knowing the calling context. The way the assembler is structured now, I believe something like that won't be possible, and also it won't be possible once we transition to MAST-based libraries. But overall, I agree, this would be a nice way to avoid some code duplication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Related to Miden standard library
Projects
None yet
Development

No branches or pull requests

2 participants