Selecting the chain (Elements or Bitcoin) in SimplicityHL source

I’m working on Bitcoin support in SimplicityHL. #437 adds the Bitcoin jets. it’s independent and ready for review. #436 (draft) lets programs be compiled for Bitcoin. It depends on rust-simplicity #385–#388, which add Bitcoin jet CMRs, costs and execution.

The open question is how a program says which chain it is for. The problem is that the same code is often valid on both chains, but it compiles to a different program. If the chain is chosen outside the source, compiling for the wrong one gives no error, just a program for the wrong chain.

There are two ways to solve this:

  1. A compiler option. Ivan’s #364 adds simc --target elements|core, and Bitcoin could be one more value. It needs no language change, but the source doesn’t say which chain it’s for, so the CMR depends on how the compiler was invoked. Tools that use the library directly, like Simplex, would also have to carry this setting for every contract.
  2. A header in the source (what #436 does now):
    target bitcoin;
    No header means Elements, so existing code doesn’t change. The compiler checks that the jet set matches the header, and Simplex can compile Bitcoin contracts without any changes.

I prefer the header, and the two can work together, because --target still makes sense for Core and custom jet sets, which aren’t chains.

Library files

In #436, only the entry file (with main function) has a header, and imported files are checked against whatever it says. That makes libraries implicitly chain-agnostic: a library can rely on Elements transaction rules and still be used in a Bitcoin program without any error.

So I’d like every file to declare which chains it supports, for example target bitcoin; for Bitcoin only, or target elements, bitcoin; for both. No header means elements, and every imported file must support the chain of the entry file, but we also can make it explicit with target elements;.

I’d be glad to hear your thoughts on these questions:

  • Which approach is better: a source header (#436), a compiler option (#364), or both?
  • Is target a good name? chain might be clearer, but #425 adds a chain! built-in.
  • Should the header only name chains (elements, bitcoin), or also core?
3 Likes

It would be cool to delegate the decision which target to use to the framework, so we wouldn’t have to deal with adding boilerplate.

We could just have this target specified in Simplex.toml.

It would look similar to how Rust ecosystem works: Cargo decides which target to use for the platform, If not specified otherwise, and it just passes it automaticly to rustcvia the --target CLI parameter.

Agreed that Simplex.toml should choose the chain, like Cargo chooses the target. But in Rust the source can still restrict it (for example with #[cfg(target_os = ...)]). I’d like the same in SimplicityHL: Simplex picks the chain, and a target header in the source restricts which chains the code supports, so the compiler can check that everything we build for Bitcoin actually supports Bitcoin.

Still another possibility is something that generates a nonfatal warning if the target is not the intended one (only relevant to code that compiles for multiple targets). “Warning: input file some.simf expects deployment on target bitcoin, but was compiled for target elements.”

Possibly related: @apoelstra and @stringhandler have been working on a “binary format” for representing compiled SimplicityHL programs together with additional data; that format can indicate which chain is targeted by a compiled program, so it can carry along this information into later deployment steps. That might be somewhat relevant and useful for this discussion.

Hm, good idea, I think we can do it, since we already have Severity in errors.rs

I have been using the same .simf files for bitcoin and elements. I don’t think there is a general need for specifying the chain, however there are some scenarios where you would want to restrict it.

In most cases, this is not the concern of the .simf source. The jets used alone determine which targets it can be compiled for. For example, the post quantum Shrincs jets can run on any target that has the core jets, but the .simf files used by simplicity_unchained cannot be compiled for any other target.

The one place I would say it’s necessary to restrict a .simf file, is when using a constant for validating timelocks. Because the block times are different, the .simf file changes meanings between compilations.

fn cancel_spend(sender_sig: Signature) {
    let timeout: Height = 1000;
    jet::check_lock_height(timeout);
    let sender_pk: Pubkey = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G
    checksig(sender_pk, sender_sig)
}

This example above has different meanings on bitcoin and elements, and having a compiler hint or restriction in the .simf file makes sense.

One statement I don’t agree with here:

The problem is that the same code is often valid on both chains, but it compiles to a different program.

This statement alone is not enough to justify adding statements to .simf syntax. We already have a case where compiling with debug symbols generates a different program, and in future, compilation of .simf to simplicity might change.

I would suggest this:

  1. if there is no compiler hint (e.g. target = bitcoin), then the compiler can attempt any target. Compilation fails for jets that are not known. NOTE: this is different from your suggestion that absence means “target=elements”
  2. If there is a compiler hint (e.g. target = bitcoin), the compiler SHOULD fail for attempts to compile to a different target. My suggestion is a compilation failure with the option to override.
2 Likes

This gives me a thought, although I think it’s a questionable thought because of its effect on backwards compatibility. My thought is that these could be distinguished with more specific types. If we had chain-specific types for Height then calling a jet on one chain with a value meant for the other chain would be a type error. For example, we could have let timeout: ElementsHeight = 1000; and then calling jet::check_lock_height(timeout); would be valid on Elements but not on Bitcoin, due to different type signatures.

(I think this is not true today as casting may be implicit, but I think it’s meant to be true with some version of the future type system.)

Are there any possible cases other than timelocks where there are identically-named jets with different semantics?

1 Like