A Go library implementing the Library of Babel algorithm for arbitrary binary data.
Every possible page of bytes exists in the library. Given any sequence of bytes, ouija can find an address for it; given any address, ouija returns the page stored there. The mapping is deterministic and reversible.
go get code.dwrz.net/ouija
package main
import (
"fmt"
"code.dwrz.net/ouija"
)
func main() {
// Find a page containing some data.
data := []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f}
addr, err := ouija.Search(data)
if err != nil {
panic(err)
}
fmt.Println("Address:", addr)
// Retrieve the page.
page, err := ouija.GetPage(addr)
if err != nil {
panic(err)
}
fmt.Printf("Page length: %d\n", len(page))
// The data appears somewhere on the page.
// ...
}The text subpackage provides a convenience layer for source code. It compresses text with boardgame before storing it in the library.
package main
import (
"fmt"
"code.dwrz.net/ouija/text"
)
func main() {
src := []byte("package main\n\nfunc main() {\n\tfmt.Println(1 + 2)\n}\n")
addr, err := text.Search(src)
if err != nil {
panic(err)
}
fmt.Printf("Address: %s\n", addr)
// Only the address is needed to recover the original text.
recovered, err := text.Recover(addr)
if err != nil {
panic(err)
}
fmt.Printf("Recovered: %s\n", recovered)
}HexAddr:Wall:Shelf:Volume:Page
| Field | Range | Description |
|---|---|---|
| HexAddr | base-36 string | Encodes the page content |
| Wall | 0–3 | Wall in the hexagonal room |
| Shelf | 0–4 | Shelf on the wall |
| Volume | 00–31 | Book on the shelf |
| Page | 000–409 | Page in the book |
The example/ directory contains a program that computes the Library of Babel
addresses of ouija.go itself, using the text subpackage to compress source
with boardgame before searching. The addresses alone are sufficient to recover
the original source — they are kept in example/data.txt. A portable
pre-commit hook and CI step ensure these addresses stay current whenever the
source changes.
To enable the pre-commit hook:
git config core.hooksPath hooks
See example/README.md for details.
See docs/algorithm.md for a detailed description.
See LICENSE.