Programs

Status: on Arctic: every payment runs a program and carries Δ. Wallets use only the no-op program so far. The Rust API's source isn't published yet.

Every Styx payment runs a program. The Zuring execution engine proves, in the payment's one proof, that the bundle follows Ironwood's rules and that the program held in every action. The payment carries Δ, a hiding commitment to the program. The chain checks every payment's proof with one verifying key, whatever program it ran, so it learns that the proof is valid, not which program ran.

A wallet that wants nothing more runs the no-op program, which every plain payment shares. A payment that runs it can't be told from another wallet's.

What a program sees

A program runs once in each action of the bundle, dummies included, and sees only its own action. It reads nine values from the action, its ports, and any private inputs the prover gives it:

PortValue
VOldThe spent note's value
VNewThe output note's value
GdOldX, PkdOldXThe spent note's address, its owner's: the x-coordinates of gd and pkd
GdNewX, PkdNewXThe output note's address, its recipient's, in the same way
AkXThe x-coordinate of ak: the spender's account, the same for all its addresses
PsiOldψ of the spent note: the program's state coming in
PsiNewψ of the output note: the program's state going out

A note's ψ travels in its memo (Tagged memos), so a program can choose an output's ψ and read the spent note's: state carried from note to note.

If the program fails in any action, the payment can't be proved.

What a program can do

A program is a sequence of operations on four registers, R0 to R3, which start at zero. In Rust it's built with orchard::zuring::Asm:

OperationWhat it does
set(r, e)Rr ← the expression e
assert_zero(e)Requires e = 0
assert_le(e, bound), assert_ge(e, bound)Requires e ≤ bound, or e ≥ bound, for 64-bit values
assert_nonneg(e)Requires 0 ≤ e < 270: for 64-bit operands, e ≥ 0
range(e, words, taps)Requires 0 ≤ e < 210·words, writing running sums into registers on the way
member(e, list), not_member(e, list)Requires e to be in, or not in, a list of constants
mul(r)Rr ← R0·R1
iszero(r)Rr ← 1 if R0 = 0, else 0
cmpbit()R3 ← 1 if R0 ≥ 0, else 0, for R0 strictly between −264 and 264
product(list, r)Rr ← the product of R0 − a over the list: zero exactly when R0 is in it
input(r)Rr ← the next private input
hash(domain, target)Hashes the final R0 and R1 with Sinsemilla in domain. With a target, requires the hash to equal it, unless the final R2 is zero.

An expression is a constant, plus multiples of registers, of one port, and of R0·R1. c(v), r(i) and port(p) make the terms, which add, subtract, and multiply by constants. Values are elements of Pallas's base field.

A program has a fixed size budget. Asm::finish returns the program, or the first operation that didn't fit.

For example, a program that requires every output of the bundle, change included, to be at most 100,000,000 zatoshis:

#![allow(unused)]
fn main() {
use orchard::zuring::{port, Asm, Port};

let mut asm = Asm::new();
asm.assert_le(port(Port::VNew.index()), 100_000_000);
let program = asm.finish()?;
}

Paying with a program

The Styx builder takes the program when it builds the transaction:

#![allow(unused)]
fn main() {
let built = builder.build(
    rng,
    &program,
    |_metadata, actions| ActionInputs::none(actions),
    &[spend_authorizing_key],
    &TransparentSigningSet::new(),
)?;
}

The closure returns the private inputs, as ActionInputs: none(actions) for a program that reads none, same(actions, inputs) for the same inputs on every action, or per_action(vectors) for one vector per action. Actions are shuffled, so the metadata says which action holds which spend and output.

Before proving, the builder checks that the program holds on every action. check_action(program, ports, inputs) runs that check on one action's values without proving, for tests. The builder computes Δ before signing: Δ's digest is part of the transaction ID, so the signatures cover the program's commitment, and nobody can swap it.

Who a program binds

The spender chooses each payment's program. Nothing on the chain says which program an address's funds must run, so a program binds the spender only towards those who check it: the payment's recipients, who can recognise the program. A recipient who recognises it knows every action of the bundle met its requirements: that the payment carries an invoice's amount and reference, for example.

Rules on an account's own funds, such as a spending cap or a list of payees, need the chain to enforce a program for an address. That's what contracts would do, and they're undecided.

Writing programs

  • The Rust API above.
  • Stygian, a language that compiles to it. Built, not deployed.
  • The experimental tools, for a different machine.