Sitelet https://github.com/dojoengine/origami/pull/131
Skip to content

feat(hexmap): random walk generator (L6) - #131

Merged
bal7hazar merged 2 commits into
mainfrom
feat/hexmap-walker
Sep 27, 2026
Merged

bal7hazar merged 2 commits into
mainfrom
feat/hexmap-walker

Conversation

@bal7hazar

Copy link
Copy Markdown
Collaborator

Summary

Implements Walker::generate(width, height, steps, seed) in origami_hexmap (lot L6): start on a seed-selected interior tile, then steps moves in a loop. Each move draws one of the 6 directions, moves if the target is interior (stays otherwise) and opens the tile. steps == 0 returns only the start tile.

Gas (17x14): 4.8k-5.3k per step, under the 6k target.

Steps Gas Per step
50 301_845 5_307
200 1_001_849 4_827
500 2_467_497 4_862

For reference, origami_map's walker test (18x14, 500 steps) reports 29.9M.

Design (details in GAS.md, section L6)

  • Random source: one pool division by 216 per three moves, with the digits read from a 216-arm table that is called, not inlined. The pool is refilled by a counter every 12 draws, so there is no per-draw pool < 2^32 test.
  • Move: doubled column c = 2x + (y & 1), row, and one-hot position 2^i, all felts. Bound tests are felt equalities with no parity branch. The state carries the NorthEast factor for both row parities and swaps them on vertical moves, so a move is one field product.
  • Grid: the positions of three moves are summed. Only the first and third positions can coincide, so one equality check is enough. The sum is ORed into a u256 once, and converted to a felt once at the end.
  • Loop: 18 moves per iteration, plus a short tail loop.

The losing variants required by the brief are kept test-only in bench_walker.cairo, with their numbers in GAS.md:

  • random source: next_below, 3-bit digits with rejection, pair table
  • move: (x, y) with a parity bool, index plus mask test, one-hot plus AND
  • grid accumulation: OR each step, test-then-add, low-limb OR
  • loop shape: 3, 6, 12 and 36 moves per iteration, table inlined or not

Tests

  • Exact grids on fixed seeds for 17x14, 7x7, 19x13 and 3x3.
  • Equality with a scalar reference (neighbor plus bit test and set, on the same draws):
    • 11 sizes, from 3x3 to 83x3 and 3x83;
    • every step count from 0 to 79;
    • 20 seeds.
  • Border ring closed, determinism, and panics on invalid dimensions.

🤖 Generated with Claude Code

bal7hazar and others added 2 commits September 27, 2026 14:35
Walker::generate: seed-selected interior start, `steps` moves in a loop,
each move draws one of the 6 directions, moves if the target is interior
and opens the tile. 4.8k-5.3k gas per step on 17x14 (origami_map: ~60k).

- one pool division by 216 per three moves, 216-arm table (called once),
  counter-based pool refill
- doubled column `2x + (y & 1)` and one-hot position: felt-only bound
  tests and moves, no parity branch
- positions of three moves summed (one equality) and ORed once
- 18 moves per loop iteration

Benchmarks for the winner and the losing variants (random source, move,
grid accumulation, loop shape), with budgets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bal7hazar
bal7hazar merged commit e20fb9f into main Sep 27, 2026
9 checks passed
@bal7hazar
bal7hazar deleted the feat/hexmap-walker branch September 27, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant