Skip to content

perf(rsema1d): reuse SHA256 hasher in coefficient derivation#7064

Merged
walldiss merged 3 commits intomainfrom
perf/rsema1d-sha256-reuse
Apr 14, 2026
Merged

perf(rsema1d): reuse SHA256 hasher in coefficient derivation#7064
walldiss merged 3 commits intomainfrom
perf/rsema1d-sha256-reuse

Conversation

@walldiss
Copy link
Copy Markdown
Member

@walldiss walldiss commented Apr 13, 2026

Summary

Reuses a single sha256 hasher with Reset() between iterations in deriveCoefficients, instead of calling sha256.Sum256() (which re-initializes from scratch each time).

Motivation

Benchmarking the verification hot path shows deriveCoefficients is the most expensive component of VerifyRowWithContext65% of total time for 128MB blobs. It calls SHA256 once per GF16 symbol: 65,536 hashes for 128KB rows.

Component breakdown (128MB blob, per-row):

deriveCoefficients:    4.6ms  (65%)  ← 65K SHA256 hashes
computeRLC:            2.4ms  (34%)  ← 65K × 8 GF16 multiplications
ComputeRootFromProof:  0.06ms  (1%)
commitmentHash:        0.00ms  (<1%)

Reusing the hasher saves ~12% on the SHA256 portion (~0.6ms for 128MB blobs).

Changes

  • pkg/rsema1d/commitment.go: deriveCoefficients now uses h.Reset() + h.Write() + h.Sum(digest[:0]) instead of sha256.Sum256()
  • pkg/rsema1d/verify_breakdown_bench_test.go: New benchmark breaking down each component of VerifyRowWithContext at 1MB/8MB/128MB blob sizes

Benchmarks

SHA256_x_all_symbols (Sum256):       3,747 µs
SHA256_reuse_x_all_symbols (Reset):  3,182 µs  (-15%)

Note: a further 55% reduction is possible by deriving 2 coefficients per hash (using both 16-byte halves independently instead of XOR-folding), but that requires a protocol/spec change.

Test plan

  • go test ./pkg/rsema1d/... passes
  • CI passes

Closes https://linear.app/celestia/issue/PROTOCO-1497

🤖 Generated with Claude Code


Open with Devin

Reuse a single sha256 hasher with Reset() between iterations in
deriveCoefficients instead of calling sha256.Sum256() per symbol.
This avoids re-initializing the internal digest state from scratch
each time, yielding ~12% speedup on the SHA256 portion of
coefficient derivation.

Also add verify_breakdown_bench_test.go that isolates each component
of VerifyRowWithContext for profiling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@walldiss walldiss requested a review from a team as a code owner April 13, 2026 18:47
@walldiss walldiss requested review from ninabarbakadze and removed request for a team April 13, 2026 18:47
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@Wondertan
Copy link
Copy Markdown
Member

Wondertan commented Apr 13, 2026

Handle unchecked error returns from rand.Read and merkle.ComputeRootFromProof,
and fix gofumpt comment alignment to satisfy CI golangci-lint checks.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread pkg/rsema1d/verify_breakdown_bench_test.go Outdated
Existing BenchmarkDeriveCoefficients already covers the optimized path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@walldiss walldiss added this pull request to the merge queue Apr 14, 2026
Merged via the queue into main with commit 5aed8d9 Apr 14, 2026
31 checks passed
@walldiss walldiss deleted the perf/rsema1d-sha256-reuse branch April 14, 2026 14:44
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.

2 participants