Aleo for Agents is offline at present

blog·2026-11-16·10 min read

Project of the Week: Build an Aleo Hash-to-Group Consensus Sentinel

Introduction

A cryptographic test can pass while every deterministic output changes.

Suppose a field-arithmetic refactor changes the console Elligator2 implementation. The circuit implementation receives the same change. Random encode-decode tests still pass, and console-versus-circuit checks still agree. Both layers now produce a different point for the same field element, but the suite remains green.

That is dangerous in consensus-sensitive code. Agreement between two implementations proves that they match now. It does not prove that they still match the outputs accepted by earlier releases.

snarkVM PR #3770 addresses this problem by adding known-answer vectors for Elligator2. Each vector fixes an input field element and its expected encoded group point. An output change becomes a visible test failure instead of quietly becoming the new baseline.

There is one qualification. Aleo's curve does not have matching external Elligator2 vectors, and RFC 9380 covers other curves. The PR records outputs from the existing snarkVM implementation. These vectors are regression pins, not independent proof that the formulas are correct.

We will carry the same policy through Leo. The sentinel will run the snarkVM vectors at a pinned commit, compile a Leo fixture that calls Poseidon2::hash_to_group, check that the compiler emitted a group-valued Poseidon instruction, and compare fixed inputs against reviewed output snapshots.

Random tests still belong in the suite. They cover broad classes of inputs and invariants. Fixed vectors catch a different failure: two related implementations changing together.

Repository layout

The finished repository has this structure:

text
aleo-hash-group-sentinel/
|-- ci/
|   `-- check.sh
|-- hash_group_sentinel/
|   |-- program.json
|   `-- src/
|       `-- main.leo
|-- vectors/
|   `-- leo_poseidon2.txt
|-- .github/
|   `-- workflows/
|       `-- consensus-sentinel.yml
`-- snarkvm.rev

Four checks protect separate boundaries:

  • The snarkVM test runs the Elligator2 known-answer vectors introduced by PR #3770.
  • leo build confirms that the fixture compiles with the expected Leo version.
  • An instruction check confirms that Leo lowered the source to a group-valued Poseidon hash.
  • A snapshot check fixes the returned group literal for each selected input.

The Leo fixture does not expose Elligator2 as a direct public call. Poseidon2::hash_to_group hashes its input into field elements, maps them to curve points, and combines the points. The Rust vectors pin the underlying map, while the Leo vectors pin the integrated Poseidon-to-group path. Generated Aleo instructions form the compiler boundary between source and execution.

This split avoids writing a second Elligator2 implementation in Leo solely for testing. Such an implementation would introduce another place for mistakes and would not provide clean independence unless it came from a separately reviewed specification.

Prerequisites

Install these tools:

  • Leo 4.4.5.
  • Rust and Cargo.
  • Git and Bash.
  • A Unix environment with grep, awk, sed, and diff.

Check the installed versions:

bash
leo --version
rustc --version
cargo --version
git --version

The fixture targets Leo 4.4.5. In this language version, every program needs a constructor, entry points use fn, and public state updates run inside inline final { } blocks. The Leo program structure reference documents constructors and storage, while the cryptographic operators reference lists the Poseidon group-output operations.

Create the project:

bash
mkdir -p aleo-hash-group-sentinel/ci
mkdir -p aleo-hash-group-sentinel/vectors
mkdir -p aleo-hash-group-sentinel/.github/workflows
cd aleo-hash-group-sentinel
leo new hash_group_sentinel

Build the fixture

1. Add the Leo program

Replace hash_group_sentinel/src/main.leo with this complete program:

leo
program hash_group_sentinel.aleo {
    @noupgrade
    constructor() {}

    storage probe_count: u64;

    fn hash_poseidon2(public input: field) -> group {
        let output: group = Poseidon2::hash_to_group(input);
        return output;
    }

    fn record_probe(public input: field) -> (group, Final) {
        let output: group = Poseidon2::hash_to_group(input);
        return (output, final {
            let count: u64 = probe_count.unwrap_or(0u64);
            probe_count = count + 1u64;
        });
    }
}

hash_poseidon2 gives CI a narrow deterministic interface: one public field enters and one group value leaves. It can run locally without transaction construction or state access.

record_probe demonstrates how an application can attach the same calculation to public state. Its proof half computes the group point. The finalizer increments probe_count after successful on-chain execution. The counter is absent before the first finalized call, so unwrap_or(0u64) supplies its initial value.

The snapshot test calls hash_poseidon2, because leo run evaluates the function without executing an on-chain finalizer. Use leo execute record_probe 7field in a configured environment when testing proof generation and transaction construction.

The @noupgrade constructor makes a deployed copy immutable. Deployment is not required for this sentinel, but Leo still requires an explicit constructor policy.

2. Pin the manifest

Replace hash_group_sentinel/program.json with:

json
{
  "program": "hash_group_sentinel.aleo",
  "version": "0.1.0",
  "description": "Consensus sentinel for Aleo group-valued hashes",
  "license": "MIT",
  "leo": "4.4.5",
  "dependencies": null,
  "dev_dependencies": null
}

The leo field records the compiler expected by the fixture. A compiler upgrade can change source acceptance, emitted instructions, output formatting, or execution behavior. Handle any version change as a reviewed repository change.

Build and exercise the entry points:

bash
cd hash_group_sentinel
leo build
leo run hash_poseidon2 0field
leo run hash_poseidon2 1field
leo run hash_poseidon2 2field
leo run record_probe 7field
cd ..

3. Pin the snarkVM revision

Resolve the pull request head once and save its commit hash:

bash
git ls-remote https://github.com/ProvableHQ/snarkVM.git refs/pull/3770/head \
  | awk '{print $1}' \
  > snarkvm.rev

cat snarkvm.rev

Commit snarkvm.rev. Do not resolve the pull request head during normal CI runs. Pull request refs move when authors push commits, so fetching the ref on every run would create a floating dependency.

PR #3770 adds fixed expected values to the console Elligator2 tests. Existing round-trip tests ask whether an encoded point can be decoded. The fixed vectors ask whether one exact input still produces one exact output.

Console-versus-circuit tests answer another question. They detect disagreement between the console implementation and the constrained implementation. The known-answer vectors prevent both from changing together without an explicit snapshot change.

4. Add the check script

Create ci/check.sh:

bash
#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
LEO_DIR="$ROOT/hash_group_sentinel"
VECTOR_FILE="$ROOT/vectors/leo_poseidon2.txt"
ACTUAL_FILE="$(mktemp)"
SNARKVM_DIR="$ROOT/.cache/snarkvm"
trap 'rm -f "$ACTUAL_FILE"' EXIT

EXPECTED_LEO="4.4.5"
REV="$(tr -d '[:space:]' < "$ROOT/snarkvm.rev")"

test -n "$REV"
leo --version | grep -F "$EXPECTED_LEO" >/dev/null

mkdir -p "$(dirname "$SNARKVM_DIR")"
git init -q "$SNARKVM_DIR"
git -C "$SNARKVM_DIR" remote remove origin 2>/dev/null || true
git -C "$SNARKVM_DIR" remote add origin https://github.com/ProvableHQ/snarkVM.git
git -C "$SNARKVM_DIR" fetch -q --depth 1 origin "$REV"
git -C "$SNARKVM_DIR" checkout -q --detach FETCH_HEAD

cargo test \
  --manifest-path "$SNARKVM_DIR/Cargo.toml" \
  -p snarkvm-console-algorithms \
  elligator2

cd "$LEO_DIR"
leo build

grep -Eq 'hash\.psd2 .* into .* as group;' build/main.aleo

for input in 0field 1field 2field 7field; do
    output="$(leo run hash_poseidon2 "$input" \
      | grep -Eo '[0-9]+group' \
      | tail -n 1)"
    test -n "$output"
    printf '%s %s\n' "$input" "$output" >> "$ACTUAL_FILE"
done

if test "${UPDATE_VECTORS:-0}" = "1"; then
    cp "$ACTUAL_FILE" "$VECTOR_FILE"
    printf 'Updated %s\n' "$VECTOR_FILE"
    exit 0
fi

test -f "$VECTOR_FILE"
diff -u "$VECTOR_FILE" "$ACTUAL_FILE"

Make it executable:

bash
chmod +x ci/check.sh

The Cargo filter runs tests whose names contain elligator2, including the fixed vectors at the pinned revision. Keeping the filter broad retains nearby encode-decode coverage instead of selecting only one narrow test name.

The grep against build/main.aleo checks compiler lowering. A source-only check would not catch a compiler that accepted the call but emitted the wrong opcode or destination type. The expression tolerates generated register-name changes while still requiring hash.psd2 and as group.

The output parser retains only the returned group literal. Timings, headings, and other presentation text make poor snapshots because they can vary across machines or releases.

5. Generate the snapshot

Create the initial Leo snapshot with the pinned toolchain:

bash
UPDATE_VECTORS=1 ./ci/check.sh
cat vectors/leo_poseidon2.txt
git diff -- vectors/leo_poseidon2.txt snarkvm.rev

Review the generated file before committing it. The first snapshot establishes the accepted values, so generate it on a clean machine and compare it with a second clean installation when the sentinel protects a release branch.

Commit the fixture and its pins:

bash
git add hash_group_sentinel/program.json
git add hash_group_sentinel/src/main.leo
git add ci/check.sh vectors/leo_poseidon2.txt snarkvm.rev
git commit -m "add hash-to-group consensus sentinel"

Normal CI must not set UPDATE_VECTORS=1. If an output changes, the build should fail and show a readable diff. Regenerating the file is a review decision, not a routine response to a red build.

This follows the same rule described in giving the point at infinity exactly one encoding. Mathematical equality is not enough when serialized values, hashes, caches, or transcripts depend on one accepted representation. Here, the accepted objects are deterministic group outputs.

6. Wire GitHub Actions

Create .github/workflows/consensus-sentinel.yml:

yaml
name: consensus-sentinel

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  hash-to-group:
    runs-on: self-hosted
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v4
      - name: Check installed tools
        run: |
          leo --version
          cargo --version
      - name: Run sentinel
        run: ./ci/check.sh

A self-hosted runner keeps the Leo installation visible instead of placing it behind an unreviewed setup action. Pin and audit the runner image with the same care as the repository dependencies.

You can cache Cargo build artifacts if snarkVM compilation becomes slow. Keep the revision checkout and test execution mandatory. A cache may accelerate a build, but it must never decide which source revision is tested.

The workflow follows the method used by the branch-safe storage lab: compile the source, inspect generated Aleo instructions, and test behavior. Cryptographic output adds a historical requirement: deterministic values must remain reviewable across upgrades.

Test the sentinel

Run the full check twice:

bash
./ci/check.sh
./ci/check.sh

Both runs should exit with status zero. The second run confirms that repeating the fetch, detached checkout, build, and snapshot comparison does not change the result.

Next, change one digit in a group literal inside vectors/leo_poseidon2.txt and rerun the check:

bash
./ci/check.sh

diff should print the changed line and return a nonzero status. Restore the file afterward:

bash
git checkout -- vectors/leo_poseidon2.txt

Test the compiler boundary by replacing Poseidon2::hash_to_group with Poseidon4::hash_to_group. Leo may still compile and return a valid group point, but the generated-instruction assertion or output snapshot should fail. This is the coordinated-change case in a small form: a valid output is not automatically an accepted output.

Test the Rust pin by placing a different reachable commit in snarkvm.rev. The build should either fail to find the expected test coverage or run a different revision whose behavior requires review. Restore the approved commit after the experiment.

Do not regenerate snapshots merely because new group values appear random. Group literals are opaque by nature. Before accepting a change, inspect the upstream arithmetic patch, compare generated instructions, identify every moved vector, and document why the new values should replace the old ones.

Extend the checks

Add separate Poseidon4 and Poseidon8 entry points if the application uses those rates. Give each operation its own vector file so changes remain easy to attribute.

A release branch can also commit build/main.aleo as a reviewed artifact. Source, generated instructions, toolchain pins, and output vectors then change in one pull request. This consumes some repository space but gives reviewers a direct compiler diff.

For stronger independence, add a small Rust binary pinned to the accepted snarkVM revision and use it to calculate the same Poseidon group outputs supplied to Leo. Compare those results with Leo in CI, but retain the fixed snapshot. Live agreement between implementations still does not preserve historical continuity by itself.

Require an explicit approval label for pull requests that modify snarkvm.rev, files under vectors/, compiler versions, or fixture hash calls. Cryptographic goldens may change, but they should never change quietly.

Sources