Skip to content

fix(checksums): rolling checksum sign-extends bytes to match upstream schar - #3560

Merged
oferchen merged 3 commits into
masterfrom
fix/rolling-checksum-signed-bytes
May 2, 2026
Merged

oferchen merged 3 commits into
masterfrom
fix/rolling-checksum-signed-bytes

Conversation

@oferchen

@oferchen oferchen commented May 2, 2026

Copy link
Copy Markdown
Owner

Summary

Upstream rsync's checksum.c:get_checksum1() casts bytes via schar *buf = (schar *)buf1, treating each byte as signed (-128..127). oc-rsync was treating bytes as unsigned (0..255), causing per-byte divergence on any byte >= 0x80. This produced wrong rolling checksums on any binary basis file with high bytes, breaking the standalone:delta-stats interop test against upstream rsync.

Root-caused from pcap evidence captured against a basis file containing high bytes; oc-rsync's rolling sums diverged from upstream's exactly where bytes >= 0x80 first appeared.

Changes

All rolling-checksum code paths now sign-extend bytes before accumulation:

  • Scalar 4-byte unrolled loop (sign_extend_byte helper)
  • update_byte fast path
  • roll() outgoing/incoming bytes
  • roll_many() outgoing/incoming bytes
  • AVX2: _mm256_cvtepi8_epi16 direct widening + _mm256_madd_epi16
  • SSE2: _mm_cmplt_epi8 sign-mask + _mm_unpacklo/hi_epi8 + _mm_madd_epi16
  • NEON: vreinterpretq_s8_u8 + vmovl_s8 + vaddlvq_s16 widening reduction (avoids vaddvq_s16 i16 overflow risk)

Property-test reference (rolling_simd_parity.rs) and goldens (tests.rs, rsync_rolling_compat.rs) updated to match upstream signed semantics:

Test Old New Notes
all_ff (block_length=16) 0x8778_0ff0 0xff78_fff0 0xFF == -1 signed; s1 = -16 = 0xfff0, s2 = -136 = 0xff78
block_length=700 0xe2ea_5c96 0x01ea_fe96 Mixed bytes, includes high bytes
block_length=4096 0x2000_f800 0x2000_f800 Unchanged - data bytes all < 0x80

Test plan

  • CI fmt + clippy passes
  • CI nextest (Linux stable) passes - exercises AVX2/SSE2 SIMD paths
  • CI Windows (stable) passes - exercises AVX2/SSE2 SIMD paths
  • CI macOS (stable) passes - exercises NEON SIMD path
  • CI Linux musl (stable) passes
  • CI Interop validation passes - standalone:delta-stats should now pass
  • Property tests in rolling_simd_parity.rs confirm SIMD vs scalar parity over the full byte range

Upstream rsync's checksum.c:get_checksum1() casts bytes via
`schar *buf = (schar *)buf1`, treating each byte as signed (-128..127).
oc-rsync was treating bytes as unsigned (0..255), causing per-byte
divergence for any byte >= 0x80. This produced wrong rolling checksums
on any binary basis file with high bytes, breaking delta-stats interop
against upstream rsync.

Fix all rolling-checksum code paths to sign-extend before accumulation:

- Scalar 4-byte unrolled loop (sign_extend_byte helper)
- update_byte fast path
- roll() outgoing/incoming bytes
- roll_many() outgoing/incoming bytes
- AVX2: _mm256_cvtepi8_epi16 direct widening + _mm256_madd_epi16
- SSE2: _mm_cmplt_epi8 sign-mask + _mm_unpacklo/hi_epi8 + _mm_madd_epi16
- NEON: vreinterpretq_s8_u8 + vmovl_s8 + vaddlvq_s16 (widening reduction
  avoids vaddvq_s16 i16 overflow)

Property-test reference (rolling_simd_parity.rs) and golden values
(tests.rs, rsync_rolling_compat.rs) updated to match upstream signed
semantics:
  - all_ff golden: 0xff78_fff0 (was 0x8778_0ff0); 0xFF == -1 signed,
    s1 = -16 = 0xfff0, s2 = -136 = 0xff78
  - block_length_700 golden: 0x01ea_fe96 (was 0xe2ea_5c96)
  - block_length_4096 golden: 0x2000_f800 (unchanged; bytes <0x80 only)

Refs: project_delta_stats_wire_evidence.md
@github-actions github-actions Bot added the bug Something isn't working label May 2, 2026
oferchen added 2 commits May 2, 2026 15:08
Upstream rsync's `checksum.c:get_checksum1()` uses `schar *buf =
(schar *)buf1`, sign-extending bytes to [-128,127]. For bytes >= 0x80
the contribution to s1/s2 is (byte - 256), not byte. The single-byte
expectation was using the old unsigned cast and failed for byte 128
(expected 8388736, got 4286644096).
@oferchen
oferchen merged commit 723a0a0 into master May 2, 2026
37 checks passed
@oferchen
oferchen deleted the fix/rolling-checksum-signed-bytes branch May 2, 2026 13:02
oferchen added a commit that referenced this pull request May 5, 2026
… schar (#3560)

* fix(checksums): sign-extend bytes in rolling checksum to match upstream

Upstream rsync's checksum.c:get_checksum1() casts bytes via
`schar *buf = (schar *)buf1`, treating each byte as signed (-128..127).
oc-rsync was treating bytes as unsigned (0..255), causing per-byte
divergence for any byte >= 0x80. This produced wrong rolling checksums
on any binary basis file with high bytes, breaking delta-stats interop
against upstream rsync.

Fix all rolling-checksum code paths to sign-extend before accumulation:

- Scalar 4-byte unrolled loop (sign_extend_byte helper)
- update_byte fast path
- roll() outgoing/incoming bytes
- roll_many() outgoing/incoming bytes
- AVX2: _mm256_cvtepi8_epi16 direct widening + _mm256_madd_epi16
- SSE2: _mm_cmplt_epi8 sign-mask + _mm_unpacklo/hi_epi8 + _mm_madd_epi16
- NEON: vreinterpretq_s8_u8 + vmovl_s8 + vaddlvq_s16 (widening reduction
  avoids vaddvq_s16 i16 overflow)

Property-test reference (rolling_simd_parity.rs) and golden values
(tests.rs, rsync_rolling_compat.rs) updated to match upstream signed
semantics:
  - all_ff golden: 0xff78_fff0 (was 0x8778_0ff0); 0xFF == -1 signed,
    s1 = -16 = 0xfff0, s2 = -136 = 0xff78
  - block_length_700 golden: 0x01ea_fe96 (was 0xe2ea_5c96)
  - block_length_4096 golden: 0x2000_f800 (unchanged; bytes <0x80 only)

Refs: project_delta_stats_wire_evidence.md

* style(checksums): cargo fmt import ordering in rolling x86.rs

* test(checksums): update single_byte_checksum for signed-byte semantics

Upstream rsync's `checksum.c:get_checksum1()` uses `schar *buf =
(schar *)buf1`, sign-extending bytes to [-128,127]. For bytes >= 0x80
the contribution to s1/s2 is (byte - 256), not byte. The single-byte
expectation was using the old unsigned cast and failed for byte 128
(expected 8388736, got 4286644096).
oferchen added a commit that referenced this pull request May 5, 2026
Construct adversarial pairs of 700-byte blocks that share an identical
rolling Adler digest (signed-byte interpretation) but have different
content. Verify that DeltaSignatureIndex::find_match_bytes rejects the
colliding window via the strong-checksum gate.

This is the safety property that future probabilistic prefilters
(e.g. a bithash) will depend on: even if the prefilter admits the
rolling key, the strong-checksum stage must catch the mismatch.

Includes a high-byte (>= 0x80) variant that exercises the signed-schar
path from upstream checksum.c:285, providing additional regression
coverage for the sign-extension fix in PR #3560.

Construction: swap last 4 bytes [a, b, c, d] -> [d, c, b, a] when
3*(a-d) == c-b. Two solutions used: low-byte [0x01, 0x00, 0x03, 0x00]
and high-byte [0x80, 0x86, 0x80, 0x82].
oferchen added a commit that referenced this pull request May 5, 2026
…3640)

Construct adversarial pairs of 700-byte blocks that share an identical
rolling Adler digest (signed-byte interpretation) but have different
content. Verify that DeltaSignatureIndex::find_match_bytes rejects the
colliding window via the strong-checksum gate.

This is the safety property that future probabilistic prefilters
(e.g. a bithash) will depend on: even if the prefilter admits the
rolling key, the strong-checksum stage must catch the mismatch.

Includes a high-byte (>= 0x80) variant that exercises the signed-schar
path from upstream checksum.c:285, providing additional regression
coverage for the sign-extension fix in PR #3560.

Construction: swap last 4 bytes [a, b, c, d] -> [d, c, b, a] when
3*(a-d) == c-b. Two solutions used: low-byte [0x01, 0x00, 0x03, 0x00]
and high-byte [0x80, 0x86, 0x80, 0x82].
oferchen added a commit that referenced this pull request May 5, 2026
#3723)

Add design note for the canonical zsync-style insertion adversary at
docs/design/zsync-shifted-insertion-test.md, pinning the public-API
guard for the four pipeline optimizations enumerated in
docs/design/zsync-inspired-matching.md (#2053). Documents the
fixture parameters, expected match output for aligned vs unaligned
regimes, the upstream rsync --stats comparison oracle, CI integration
gated by SIMD parity (#2077), and the failure modes (signed-byte
regression #2076 class, off-by-one in seek, bithash false negative
#2061-#2063 class, want_i hint off-by-one, rolling-window reset bugs,
strong-checksum truncation drift, ring-buffer wrap bugs).

Cites the existing fixture at crates/match/tests/shifted_insertion_fixture.rs,
the sibling rsum-collision fixture from #2078, and the rolling
sign-extension fix from PR #3560 (commit 723a0a0).
oferchen added a commit that referenced this pull request May 18, 2026
…3640)

Construct adversarial pairs of 700-byte blocks that share an identical
rolling Adler digest (signed-byte interpretation) but have different
content. Verify that DeltaSignatureIndex::find_match_bytes rejects the
colliding window via the strong-checksum gate.

This is the safety property that future probabilistic prefilters
(e.g. a bithash) will depend on: even if the prefilter admits the
rolling key, the strong-checksum stage must catch the mismatch.

Includes a high-byte (>= 0x80) variant that exercises the signed-schar
path from upstream checksum.c:285, providing additional regression
coverage for the sign-extension fix in PR #3560.

Construction: swap last 4 bytes [a, b, c, d] -> [d, c, b, a] when
3*(a-d) == c-b. Two solutions used: low-byte [0x01, 0x00, 0x03, 0x00]
and high-byte [0x80, 0x86, 0x80, 0x82].
oferchen added a commit that referenced this pull request May 18, 2026
#3723)

Add design note for the canonical zsync-style insertion adversary at
docs/design/zsync-shifted-insertion-test.md, pinning the public-API
guard for the four pipeline optimizations enumerated in
docs/design/zsync-inspired-matching.md (#2053). Documents the
fixture parameters, expected match output for aligned vs unaligned
regimes, the upstream rsync --stats comparison oracle, CI integration
gated by SIMD parity (#2077), and the failure modes (signed-byte
regression #2076 class, off-by-one in seek, bithash false negative
#2061-#2063 class, want_i hint off-by-one, rolling-window reset bugs,
strong-checksum truncation drift, ring-buffer wrap bugs).

Cites the existing fixture at crates/match/tests/shifted_insertion_fixture.rs,
the sibling rsum-collision fixture from #2078, and the rolling
sign-extension fix from PR #3560 (commit 723a0a0).
oferchen added a commit that referenced this pull request Aug 24, 2026
…3640)

Construct adversarial pairs of 700-byte blocks that share an identical
rolling Adler digest (signed-byte interpretation) but have different
content. Verify that DeltaSignatureIndex::find_match_bytes rejects the
colliding window via the strong-checksum gate.

This is the safety property that future probabilistic prefilters
(e.g. a bithash) will depend on: even if the prefilter admits the
rolling key, the strong-checksum stage must catch the mismatch.

Includes a high-byte (>= 0x80) variant that exercises the signed-schar
path from upstream checksum.c:285, providing additional regression
coverage for the sign-extension fix in PR #3560.

Construction: swap last 4 bytes [a, b, c, d] -> [d, c, b, a] when
3*(a-d) == c-b. Two solutions used: low-byte [0x01, 0x00, 0x03, 0x00]
and high-byte [0x80, 0x86, 0x80, 0x82].
oferchen added a commit that referenced this pull request Aug 24, 2026
#3723)

Add design note for the canonical zsync-style insertion adversary at
docs/design/zsync-shifted-insertion-test.md, pinning the public-API
guard for the four pipeline optimizations enumerated in
docs/design/zsync-inspired-matching.md (#2053). Documents the
fixture parameters, expected match output for aligned vs unaligned
regimes, the upstream rsync --stats comparison oracle, CI integration
gated by SIMD parity (#2077), and the failure modes (signed-byte
regression #2076 class, off-by-one in seek, bithash false negative
#2061-#2063 class, want_i hint off-by-one, rolling-window reset bugs,
strong-checksum truncation drift, ring-buffer wrap bugs).

Cites the existing fixture at crates/match/tests/shifted_insertion_fixture.rs,
the sibling rsum-collision fixture from #2078, and the rolling
sign-extension fix from PR #3560 (commit 723a0a0).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant