Make a host in Go, step 4: prove it exact
Your host shows what the reference shows. This step proves that it agrees to the last bit, with a yes-or-no test: the state hash. It adds state.go to the package.
The state serialization
Between frames, a machine's whole state is one string of bytes; its BLAKE3-256 hash is the state hash. Two hosts given the same program and input agree exactly when their hashes agree after every frame.
package machine
func (m *Machine) State(id [32]byte,
grants, terminal []byte) []byte {
state := append([]byte("terse-state-1"), id[:]...)
state = le.AppendUint32(state, m.PC)
for _, x := range m.X[1:] {
state = le.AppendUint32(state, x)
}
state = le.AppendUint32(state, m.Frame)
fault, faultPC := Fault{}, uint32(0)
if m.Fault != nil {
fault, faultPC = *m.Fault, m.PC
}
state = append(state, m.Stopped, m.ExitCode, fault.Kind)
state = le.AppendUint32(state, fault.Encoding)
state = le.AppendUint32(state, faultPC)
state = le.AppendUint32(state, fault.Address)
state = le.AppendUint32(state, uint32(m.Budget))
state = le.AppendUint32(state, uint32(len(m.RAM)))
state = append(append(state, m.RAM...), grants...)
state = append(state, make([]byte, 37)...)
if terminal == nil {
return append(state, 0, 0, 0, 0)
}
state = append(append(state, 1), terminal...)
return append(state, 0, 0, 0)
}
In order: terse-state-1 and the image ID; pc and the registers x1 to x31; the frame counter, how the machine stopped (0 running, 1 exited, 2 faulted), the exit code, and a fault's kind, encoding, pc and address; the budget carried into the next frame; the RAM; one byte per optional device, 1 if granted. Then the System device: 37 zero bytes here (the time, 0 in a test; an empty event queue; four controllers, none connected). Then each device's section in a fixed order, from its driver's STATE: the terminal's with a byte 1 before it, or a 0 for a machine without one; then a 0 each for the vector display, content and the page, which this host never has.
The program
package main
import (
"fmt"
"os"
"strconv"
"host/machine"
"lukechampine.com/blake3"
)
func main() {
file, err := os.ReadFile(os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
frames, _ := strconv.Atoi(os.Args[2])
cpu, devices, err := machine.Load(file)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
m := &machine.Machine{CPU: cpu}
var bus *machine.Bus
if len(devices) > 0 {
bus = machine.Start(os.Args[3], m, file, devices)
m.Devices = bus.Call
}
for m.Stopped == machine.Running && int(m.Frame) < frames {
if bus != nil {
m.Budget -= bus.Begin(m.Frame)
}
m.RunFrame()
if bus != nil {
bus.End()
}
}
var grants, terminal []byte
if bus != nil {
grants, terminal = bus.Grants, bus.State(2)
bus.Close()
}
state := m.State(blake3.Sum256(file), grants, terminal)
fmt.Printf("%x\n", blake3.Sum256(state))
if m.Fault != nil {
fmt.Fprintln(os.Stderr, m.Fault)
os.Exit(1)
}
os.Exit(int(m.ExitCode))
}
It starts the driver only for a program that lists devices, and prints the hash after the last frame.
Check it
From examples/host/go, after go build -o bin/ ./..., with step 1's exit.terse:
bin/step4 exit.terse 0 bin/step4 exit.terse 1
prints the hash at reset, then after the frame in which it exits:
9cbfa0da982561dd1c5dbaf1d5fde2121dee78c097d754aa0c2a350584bbeb5a 3272a879392d8179997dc398726430bf92bbd413b89653be8c9af4472656fe9b
the two STATE.md section 3 gives. And hello after 30 frames:
bin/step4 ../../../programs/hello/hello.terse 30 \
../../../target/release/terse-dev-terminal
819432152b38626f8fc54b43f50d5d4c501dc99e6904315b61dc78b796c611e2
which is what terse run programs/hello/hello.terse --frames 30 --hash prints. A program that faults must agree too: native/core-c/tests has one for each kind of fault, and the sources' tests compare their hashes with the reference's.
Random programs find what the tests do not. native/core-c/tests/fuzz.py writes them: random 16-bit and 32-bit encodings, many of them faulting. This prints the name of any whose hash after 3 frames is not the reference's, and with this host prints nothing:
python3 ../../../native/core-c/tests/fuzz.py fuzz 200
for f in fuzz/*.terse; do
a=$(bin/step4 $f 3 2>/dev/null)
b=$(terse run $f --frames 3 --hash 2>/dev/null)
[ "$a" = "$b" ] || echo $f
done
What is left out
This host runs hello and proves it, nothing more. The rest of a complete host, in the order to add it:
- Input: the event queue and the events file of a test (STATE.md section 6.2), so that
helloechoes typed text:programs/hello/hello.eventsmust giveprograms/hello/expected-30-frames.txt. - The other System calls:
unix_time,random,controller,open,argumentanddevice. - More drivers: the 2D vector display,
terse-dev-vector, asked for slots 3 and 9 together. Thenprograms/conform-programwith its events must show itsexpected-screen.txt, 22 PASS lines, and give the 20 hashes ofexpected-hashes.txt. - Snapshots: the same string, read back section by section (RESTORE gives each driver its section).
tcore (native/core-c) has all of it but snapshots in about 1,580 lines of C: its serialization is 46 lines, the events file about 160.
What you have now
A host that runs Terse32 programs exactly as the reference does, and proves it: state.go, 28 lines, step4/main.go, 52; 900 lines in all. Next, write a device as a separate program: Make a device.
- The same step in Rust: Make a host in Rust, step 4: prove it exact.
- The rules: State, replay and conformance, sections "The state serialization", "The hash", "Snapshots" and "The conformance image"; the input in Events and input.