Stygian
Status: built, not deployed, and not published yet.
Stygian is a language for programs. A Stygian file is Python with a few keywords. Compiling it gives a module of the Zuring IR, a text format with the program's tests built in. The back end, the zuring-ir tool, checks the module, assembles it into a program for Styx's engine, and runs its tests on an interpreter and on the assembled program, which must agree.
A program
This one counts a note's payments in its ψ. Change that stays at the address carries the count forward, one more each time, up to a limit, and payments that leave are capped:
# coding: stygian
home = Address(41, 31)
payee = Address(21, 11)
PARAM ACCOUNT : Field SUCH AS 777
PARAM CAP : Amount SUCH AS 2_0000_0000
PARAM MAX_PAYMENTS : U[10] SUCH AS 3
STATE payments : U[10] SUCH AS 0, 2, 1
BUT NOT 3 ON 1
CONTEXT spender SUCH AS 777
CONTEXT spent.address SUCH AS home
CONTEXT spent.value SUCH AS 5_0000_0000
CONTEXT output.value SUCH AS 4_0000_0000, 3_0000_0000, 1_0000_0000
BUT NOT 3_0000_0000 ON 2
CONTEXT output.address SUCH AS home, home, payee
REQUIRE spender == ACCOUNT
with output.address == spent.address:
REQUIRE payments < MAX_PAYMENTS
STATE_OUT payments + 1
with output.address != spent.address:
REQUIRE output.value <= CAP
FINISH
PARAMdeclares the program's parameters. Each set of parameters makes a different program.STATEdeclares a field of ψ, andSTATE_OUTsets the output's.SUCH ASgives test values, one per test row, andBUT NOTmakes a failing test:BUT NOT 3 ON 1says row 1 must fail withpaymentsset to 3.CONTEXTgives the ports' test values.withgates what's inside it: it binds only where the condition holds.
The count is per note, since ψ is chosen by whoever makes the note. Like every program today, it binds the spender only towards the recipients who recognise it (Who a program binds).
Running it
From the stygian folder of the zuring_ir crate:
python3 -m stygian compile examples/payment_count.zr # the IR, on stdout
python3 -m stygian test examples/payment_count.zr # its tests
python3 -m stygian build examples/payment_count.zr # assemble it
python3 -m stygian transform examples/payment_count.zr # the Python it becomes
test, build and the sinsemilla fixtures call the zuring-ir tool. Point ZURING_IR at it, or put it on the PATH. The tool works on IR files directly too: fmt, digest, check, build, list, test, and hash for test values.
The language
Declarations
| Declaration | What it declares |
|---|---|
PARAM NAME : Type SUCH AS … | A parameter. PARAM NAME[n] declares an array, NAME.0 to NAME.(n-1). |
DECLARE name : Type SUCH AS … BUT NOT … | A private input, read once |
STATE name : U[bits] SUCH AS … BUT NOT … | A field of ψ, of 10 to 60 bits in whole 10-bit words |
CONTEXT path SUCH AS … BUT NOT … | Test values for a port |
Types are Field, Bool, Amount (64 bits), Height (32 bits), U[n] (1 to 64 bits), Enum[A, B, …] (each name a constant, from 0), and Address, a pair of field elements (Address(gd, pkd) makes a test address). The ports are spent.value, output.value, spent.address, output.address (each address with .gd and .pkd), spender (the x-coordinate of ak), psi_old and psi_new.
Statements
| Statement | Meaning |
|---|---|
REQUIRE condition | Holds wherever the gates around it are on |
LET name = expression | Set under gates; it keeps its earlier value where they're off |
with condition:, with !condition: | A gate: what's inside binds only where it's on |
ACCEPT | Inside a gate, an accepting path. If there are any, exactly one is on. |
REJECT | Inside a gate, the gate must be off. At the top level, the end of the paths. |
STATE_OUT a, b, … | ψ of the output note: these fields, packed as STATE lays them out |
FINISH | The end |
Expressions
+,-and*work in the field.==and!=compare anything, addresses included.<,<=,>and>=compare integers whose range is known: values, amounts, integer inputs and state, parameters, and sums, differences and products of them. Field values such as hashes and keys don't compare;cast(x, bits)gives one a range, checked.&,|and!combine conditions. Python binds&and|tighter than comparisons, so a comparison next to them needs parentheses:(a == b) & c.a IN listanda NOT IN listwork at the top of aREQUIRE,LETorwith, andisin(a, list)andnotin(a, list)anywhere. Lists hold constants.Is(x, NAME)tests an enum,select(c, a, b)picks a value, andsinsemilla(domain, a, b) == targetchecks a hash, in aREQUIRE, at most once.
Python's own if, and, or, not and in don't work on circuit values: they would decide at compile time. Plain Python around them does work: fixtures, loops, helper functions.
The corpus
The corpus has seven programs, each with its tests:
| Program | What it requires |
|---|---|
limits.zr | The output's value is between a minimum and a maximum |
fee_cap.zr | The spent value minus the output's is at most a fee cap |
min_share.zr | The output keeps at least a fixed share of the spent value |
tiered.zr | The output's value is under a limit picked by two lists of recipients |
invoice.zr | The payment pays an amount to a merchant, and a private id and salt hash to the invoice |
escrow.zr | The payment pays an agent, or the spender knows the secret that hashes to a release value |
vault.zr | Per-withdrawal and lifetime caps and a withdrawal count in ψ, with change staying home |
vault.zr doesn't assemble yet: the compiler runs out of registers. Rules on an account's own funds, like the vault's or examples/capped_payees.zr's, bind the spender only once the chain enforces programs (Contracts).