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

Parameterize programs #100

Merged

Conversation

uncomputable
Copy link
Collaborator

Refactor the code that handles witness types / values / modules. Add parameterized values to Simfony which are instantiated at commitment time.

Simfony programs with parameters are templates for a family of Simfony programs. A template is instantiated by providing arguments to its parameters. Arguments can be provided via JSON or via code mod param { ... }.

The existing API needed to be updated to handle parameters. This turned out to be more work than expected :) While most changes are mechanical, there were some interesting engineering problems, such as handling parameters inside fuzz tests.

This commit introduces a bunch of changes to the map types WitnessTypes
and WitnessValues. Instead of splitting these changes into a dozen
commits, I combined them all into one commit. The goal of these changes
is to make WitnessTypes and WitnessValues more ergonomic to use.

Move WitnessTypes into witness.rs.

Use HashMap instead of BTreeMap for better asymptotics. I used to
require a sorted list of keys, but that is no longer the case.

Make both WitnessTypes and WitnessValues immutable. Instead of building
a mutation API for these maps, we can simply construct a HashMap with
its rich API. All values of HashMap<WitnessName, ResolvedType> are valid
for WitnessTypes and similarly for WitnessValues.

Wrap the inner map in an Arc<T> for cheap cloning. This means that we
can pass owned WitnessValues, which simplifies some code.

Wrap the implementation of WitnessTypes and of WitnessValues inside
macros, so we can reuse the code for other kinds of maps. I will add
Parameters and Arguments, which will use these macros.
Allow for different kinds of modules that each define a list of
constants. Each module has a name, such as the witness module.

Currently the only module is `mod witness { ... }`. In the following
commits, we will add a `mod param { ... }` module for defining
commitment-time values such as public keys.

Most changes in this commit are renamings or moved code.
Many Simfony programs differ only in hardcoded values such as public
keys. This commit introduces parameters of the form `param::PUBLIC_KEY`
which can be used as variables that resolve to a Simfony value at
commitment time.

Simfony programs with parameters are templates which can be instantiated
as concrete Simfony programs by providing arguments for the template's
parameters.

CompiledProgram::new(s, Arguments::default()) can be used to parse and
compile a Simfony program without parameters. However, I expect most
programs to contain parameters. It remains an open question if
parameters are a good abstraction for this use case. If they turn out as
not useful, we can remove them.

I had to adapt the fuzz tests to handle arguments. Interestingly, we can
use simfony::ArbitraryOfType to create a random list of arguments for a
given list of parameters. Very nice!
Extend the unit test and integration test harnesses to handle
parameterized programs.

Parameterize the public key value in the P2PK example.
Copy link
Contributor

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK c5ea02a; successfully ran local tests; yeah, this seems like a good direction

@apoelstra apoelstra merged commit 869a0b2 into BlockstreamResearch:master Nov 6, 2024
12 checks passed
@uncomputable uncomputable deleted the 2024-11-parameters branch November 6, 2024 21:34
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.

2 participants