Skip to content

feat: add SHA256 chunk hash verification for CRS downloads#21087

Closed
AztecBot wants to merge 7 commits intonextfrom
claudebox/crs-hash-verification
Closed

feat: add SHA256 chunk hash verification for CRS downloads#21087
AztecBot wants to merge 7 commits intonextfrom
claudebox/crs-hash-verification

Conversation

@AztecBot
Copy link
Collaborator

@AztecBot AztecBot commented Mar 4, 2026

Summary

Adds integrity verification for BN254 CRS data downloaded over HTTP, addressing the concern raised in AztecProtocol/barretenberg#1616 without the complexity of adding SSL/TLS support.

Instead of HTTPS, we embed SHA256 hashes of 8MB chunks of the CRS file directly in the binary. Downloads are rounded up to 8MB boundaries so every downloaded chunk can be fully verified.

Changes

  • bn254_crs_hashes.hpp (new): 257 SHA256 hashes covering the full 2^25+1 point CRS file (~53KB header), plus an inline verify_bn254_crs_integrity() function
  • get_bn254_crs.cpp: Rounds up downloads to 8MB chunk boundaries; calls hash verification after download; retains first-element check as fast sanity check for small downloads
  • sha256.cpp: Adds std::span<const uint8_t> template instantiation needed by the verification function
  • crs_factory.test.cpp: New Bn254HashVerification test that requests 200k points (non-aligned), verifies the download rounds up to 2×8MB (262144 points), validates hash checking passes, and confirms corruption detection works
  • generate_crs_hashes.sh (new): Script to regenerate the hash header from a CRS file or by downloading from the CDN

Design decisions

  • 8MB chunk size: 257 hashes in ~53KB — compact enough to embed, large enough for efficient verification
  • Aligned downloads: Downloads are rounded up to 8MB boundaries so every chunk is fully hash-verified (at most 8MB extra data, which benefits future requests via caching)
  • Download-time only: Cached CRS loads skip verification since the data was already verified when first downloaded

Test plan

  • CrsFactory.bn254 — existing test passes
  • CrsFactory.Bn254Fallback — existing fallback test passes
  • CrsFactory.Bn254HashVerification — new test: downloads 16MB (aligned from 200k points), verifies hash integrity, validates corruption detection

Adds integrity verification for BN254 CRS data downloaded over HTTP.
Instead of requiring SSL/TLS, the downloaded data is verified against
SHA256 hashes of 1MB chunks embedded in the binary.

- Embeds 2049 SHA256 hashes covering the full 2^25+1 point CRS (2GB)
- Verifies all complete 1MB chunks on network download (not cached loads)
- Retains first-element check as fast sanity check for all download sizes
- Replaces the second-element-only check with comprehensive hash coverage
- Adds test that validates hash checking and corruption detection
- Includes script to regenerate hashes from CRS source data

Addresses AztecProtocol/barretenberg#1616
@AztecBot AztecBot added the claudebox Owned by claudebox. it can push to this PR. label Mar 4, 2026
AztecBot added 2 commits March 4, 2026 03:56
…nloads

- Use 8MB chunks instead of 1MB (257 hashes vs 2049, ~53KB vs ~400KB header)
- Round up downloads to 8MB boundaries so every chunk is fully verified
- Update test to verify alignment: requesting 200k points downloads 262144 (2×8MB)
- Update generation script to match
Use parallel_for with ThreadChunk to verify SHA256 chunk hashes across
all available cores. Includes early-exit on first mismatch.
@AztecBot AztecBot force-pushed the claudebox/crs-hash-verification branch from 0c6572a to 5125aad Compare March 4, 2026 04:18
AztecBot added 4 commits March 4, 2026 04:28
verify_bn254_crs_integrity now throws if data size is not a multiple of
the 8MB chunk size, since this function is designed for data downloaded
in aligned 8MB chunks.
- Cap aligned download to 256 full chunks to avoid requesting past EOF
  (full CRS has 256 full 8MB chunks + 64-byte remainder)
- verify_bn254_crs_integrity now skips trailing bytes < chunk size
  instead of erroring, since the CRS tail is inherently non-aligned
- Fix misleading CAS comment ("smallest failing index" -> "first CAS wins")
- Sync generation script with header changes
@ludamad ludamad marked this pull request as ready for review March 4, 2026 04:53
@ludamad ludamad closed this Mar 4, 2026
ludamad pushed a commit that referenced this pull request Mar 4, 2026
…#21113)

Adds SHA-256 chunk hash verification for BN254 CRS downloads, combining
the best of #21087 and #20864.

- **8MB chunks** (131K points each, 257 hashes embedded at compile time)
- **Parallel verification** with atomic early-exit on first mismatch
- **`std::span`-based** hashing (zero per-chunk allocation)
- Retains fast first-element sanity check before full hash verification
- Covers partial last chunk (64-byte tail) so every downloaded byte is
verified
- Primary/fallback CDN URLs with HTTP Range requests

---------

Co-authored-by: Jonathan Hao <jonathan@aztec-labs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claudebox Owned by claudebox. it can push to this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants