|
| 1 | +<p align="center"> |
| 2 | + <img src="assets/logo.png" width="200" alt="Sakura logo"> |
| 3 | +</p> |
| 4 | + |
| 5 | +# Sakura |
| 6 | + |
| 7 | +A dependency-free [Blossom](https://github.com/hzrd149/blossom) server written in Odin. |
| 8 | + |
| 9 | +## What it implements |
| 10 | + |
| 11 | +| BUD | Coverage | |
| 12 | +|-----|----------| |
| 13 | +| **BUD-01** | `GET`/`HEAD /<sha256>`, RFC 7233 range requests (`206`, `Content-Range`, `Accept-Ranges`), full CORS, `X-Reason` diagnostics, content sniffing | |
| 14 | +| **BUD-02** | `PUT /upload`, blob descriptors, `200`/`201` semantics, `X-SHA-256` check | |
| 15 | +| **BUD-04** | `PUT /mirror` (http sources; fetch, hash-verify, store) | |
| 16 | +| **BUD-06** | `HEAD /upload` upload-requirements probe (`X-SHA-256`/`X-Content-Length`/`X-Content-Type`) | |
| 17 | +| **BUD-11** | `Authorization: Nostr <base64url>` kind-24242 tokens: recomputes the event id, verifies the Schnorr signature, checks `kind`, `created_at`, `expiration`, `t`, `x` and `server` tags | |
| 18 | +| **BUD-12** | `GET /list/<pubkey>`, `DELETE /<sha256>` | |
| 19 | + |
| 20 | +## Performance notes |
| 21 | + |
| 22 | +- Reads are lock-free: `GET`/`HEAD` stat and stream blob files straight off disk |
| 23 | + in 64 KiB chunks, and the index mutex is only taken for mutations and metadata. |
| 24 | +- Worker threads (one per core) all accept on a single shared listening socket, |
| 25 | + with HTTP keep-alive and request pipelining. |
| 26 | +- Each request runs on an arena allocator that is reset afterwards, so parsing |
| 27 | + and response building cause no per-request heap churn. |
| 28 | +- secp256k1 verification is variable-time. It touches only public data, so |
| 29 | + constant time isn't needed, and dropping it buys speed. Field reduction |
| 30 | + exploits the `p = 2^256 - 2^32 - 977` shape; scalars and points use 4×`u64` |
| 31 | + limbs with `u128` intermediates. |
| 32 | +- SHA-256 is streaming and hashes directly out of the upload buffer. |
| 33 | + |
| 34 | +## Build |
| 35 | + |
| 36 | +```sh |
| 37 | +odin build . -out:sakura -o:speed |
| 38 | +``` |
| 39 | + |
| 40 | +## Run |
| 41 | + |
| 42 | +```sh |
| 43 | +SAKURA_PORT=3000 SAKURA_DATA=./data ./sakura |
| 44 | +``` |
| 45 | + |
| 46 | +Configuration (environment variables): |
| 47 | + |
| 48 | +| Variable | Default | Meaning | |
| 49 | +|----------|---------|---------| |
| 50 | +| `SAKURA_HOST` | `0.0.0.0` | bind address | |
| 51 | +| `SAKURA_PORT` | `3000` | listen port | |
| 52 | +| `SAKURA_DATA` | `./data` | blob + index directory | |
| 53 | +| `SAKURA_DOMAIN` | _(off)_ | domain for BUD-11 `server`-tag scoping | |
| 54 | +| `SAKURA_PUBLIC_URL` | _(derive from Host)_ | base URL in descriptors | |
| 55 | +| `SAKURA_MAX_MB` | `100` | max blob size (MB) | |
| 56 | +| `SAKURA_WORKERS` | `16` | worker threads | |
| 57 | +| `SAKURA_REQUIRE_AUTH` | `true` | require auth for upload/delete/mirror | |
| 58 | + |
| 59 | +## Tests |
| 60 | + |
| 61 | +```sh |
| 62 | +./sakura --selftest # in-binary known-answer tests (47 checks, exit code = result) |
| 63 | +./tests/run.sh # the above + SHA-256 differential + live HTTP end-to-end |
| 64 | +``` |
| 65 | + |
| 66 | +`--selftest` covers: |
| 67 | + |
| 68 | +- SHA-256: the canonical KATs (empty, `"abc"`, the two NIST multi-block |
| 69 | + vectors) plus the one-million-`'a'` streaming/padding stress. |
| 70 | +- secp256k1 units: `fe_inv`/`fe_sqrt`/`fe_neg` round-trips, the published |
| 71 | + x-coordinates of `2G`/`3G`, `(n-1)G == -G`, `2G+G == 3G`, `lift_x(Gx)`. |
| 72 | +- BIP-340 verify: all 19 vectors from the spec CSV, including every must-fail |
| 73 | + edge case (off-curve key, `has_even_y(R)` false, negated message/`s`, |
| 74 | + infinite `sG−eP` with `R.x` = 0 and 1, `sig[0:32]` off-curve / `== p`, |
| 75 | + `sig[32:64] == n`, pubkey `> p`) and the 0/1/17/100-byte message vectors. |
| 76 | +- BIP-340 sign: byte-exact signatures for the `aux_rand == 0` spec vectors |
| 77 | + (the deterministic case our minter uses). |
| 78 | + |
| 79 | +`tests/run.sh` also diffs `--sha256 <file>` against the system `sha256sum` |
| 80 | +across length boundaries (0,1,…55,56,…63,64,65,…127,128,… plus random sizes) |
| 81 | +and drives a live server through upload / re-upload / no-auth / wrong-x-tag / |
| 82 | +GET / HEAD / range / list / expiry / wrong-verb / delete / OPTIONS. |
| 83 | + |
| 84 | +## Tooling commands |
| 85 | + |
| 86 | +```sh |
| 87 | +./sakura --pubkey <seckey_hex> # derive x-only pubkey |
| 88 | +./sakura --make-token <seckey_hex> <verb> [hash] [exp_unix] # mint a kind-24242 token |
| 89 | +./sakura --sha256 <file> # hash a file (for diffing) |
| 90 | +``` |
| 91 | + |
| 92 | +Example end-to-end upload: |
| 93 | + |
| 94 | +```sh |
| 95 | +HASH=$(sha256sum file.png | cut -d' ' -f1) |
| 96 | +TOK=$(./sakura --make-token <seckey> upload "$HASH") |
| 97 | +curl -X PUT --data-binary @file.png -H "Authorization: Nostr $TOK" http://localhost:3000/upload |
| 98 | +``` |
| 99 | + |
| 100 | +## Docker |
| 101 | + |
| 102 | +Because the binary is dependency-free and links fully static, the image is |
| 103 | +`FROM scratch`: no base OS, no libc, no shell. The only non-zero layer is the |
| 104 | +~1.2 MB stripped binary; everything else is an empty `/data` dir and metadata. |
| 105 | + |
| 106 | +```sh |
| 107 | +docker build -t sakura . # downloads Odin, static-builds, runs --selftest |
| 108 | +docker run --rm -p 3000:3000 -v sakura-data:/data sakura |
| 109 | +``` |
| 110 | + |
| 111 | +- The container runs as non-root (`uid 65532`) and persists blobs in the |
| 112 | + `/data` volume; configure it with the same `SAKURA_*` env vars, e.g. |
| 113 | + `-e SAKURA_REQUIRE_AUTH=false`. |
| 114 | +- The build stage runs `--selftest` and fails the build if any vector fails. |
| 115 | + Pin the toolchain with `--build-arg ODIN_VERSION=dev-2026-05`. |
| 116 | + |
| 117 | +To test the live image, `tests/docker.sh` builds it, runs a container, drives |
| 118 | +the full Blossom flow, and checks persistence across container replacement. |
| 119 | +It mints tokens with the image's _own_ CLI, so it validates only the shipped |
| 120 | +artifact: |
| 121 | + |
| 122 | +```sh |
| 123 | +tests/docker.sh # build + black-box test the container |
| 124 | +SKIP_BUILD=1 tests/docker.sh # reuse an already-built image |
| 125 | +``` |
| 126 | + |
| 127 | +Resulting image is ~1.7 MB locally (≈0.5 MB compressed). To shrink the binary |
| 128 | +further you can `upx --best` it in the build stage (≈0.6 MB) at the cost of a |
| 129 | +small startup decompression; that's left out by default to keep cold-start |
| 130 | +instant. |
| 131 | + |
| 132 | +## Layout |
| 133 | + |
| 134 | +| File | Responsibility | |
| 135 | +|------|----------------| |
| 136 | +| `sha256.odin` | streaming SHA-256 | |
| 137 | +| `secp256k1.odin` | field/scalar/point math, BIP-340 verify + sign | |
| 138 | +| `codec.odin` | hex, Base64-URL | |
| 139 | +| `json.odin` | JSON parser + NIP-01 string escaping | |
| 140 | +| `nostr.odin` | event parsing, id recomputation, BUD-11 validation, token minting | |
| 141 | +| `store.odin` | sharded on-disk blob store + append-only index | |
| 142 | +| `http.odin` | request parser, response/streaming writer | |
| 143 | +| `http_client.odin` | minimal HTTP client for `/mirror` | |
| 144 | +| `server.odin` | routing and every endpoint | |
| 145 | +| `main.odin` | config, worker pool, self-test, CLI | |
| 146 | + |
| 147 | +## Limitations |
| 148 | + |
| 149 | +- `/mirror` fetches `http://` sources only; TLS is out of scope for a |
| 150 | + dependency-free build, so `https://` sources return `502`. |
| 151 | +- BUD-05 (media optimization), BUD-07 (payments), BUD-08/09/10 are not server |
| 152 | + endpoints and are intentionally out of scope. |
0 commit comments