Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore discriminants in Hash for AV + ArrayValue + nix len-prefix for PV #1057

Merged
merged 1 commit into from
Apr 24, 2024

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Apr 9, 2024

Description of Changes

Fixes #1048.

  • Hash for AV does not include Discriminant<AV> anymore.
  • Hash for PV is no longer length-prefixed.
  • Hash for ArrayValue does not include Discriminant<ArrayValue> anymore.

In spacetimedb, we only compare hashes between homogeneously-typed values. Therefore, this PR does less work and does in fact not weaken the hash function. Rather, by avoiding to feed in known-equal data into the hasher, we can reduce the rate of conflicts, as we're now packing the same number of "interesting" bits alongside fewer "boring" bits into a hash.

API and ABI breaking changes

Semantics in the APIs above are broken by this PR.

Expected complexity level and risk

2

@Centril Centril requested review from kazimuth and gefjon and removed request for kazimuth April 9, 2024 17:42
@Centril Centril changed the title Weaken Hash for AlgebraicValue to ignore dicriminants Weaken Hash for AlgebraicValue to ignore discriminants Apr 9, 2024
gefjon
gefjon previously requested changes Apr 10, 2024
Copy link
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

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

Does this make BFLATN hashing consistent with ProductValue::hash? I suspect not until we merge #1063 . If that's the case, I'd like to hold off on merging this until after that PR, and then add a (prop)test to this PR that the hash functions are indeed consistent.

I'd also like to see a comment on SumValue about what constraints need to be upheld by its Hash impl, and the fact that derive(Hash) does indeed uphold those constraints.

Otherwise, code looks good. I'll mention that, for our case of comparing hashes only between homogeneously-typed values, this does not actually weaken the hash function at all - in fact, avoiding feeding known-equal data into the hasher should improve the hashes, as we're now packing the same number of "interesting" bits alongside fewer "boring" bits into a hash.

@bfops bfops added release-any To be landed in any release window and removed release-0.9 labels Apr 15, 2024
@Centril Centril force-pushed the centril/weaker-av-hash branch from fa485cb to fb56a9b Compare April 18, 2024 11:37
@Centril
Copy link
Contributor Author

Centril commented Apr 18, 2024

Does this make BFLATN hashing consistent with ProductValue::hash? I suspect not until we merge #1063 . If that's the case, I'd like to hold off on merging this until after that PR, and then add a (prop)test to this PR that the hash functions are indeed consistent.

This does not, but is a stepping stone towards that goal. The full implementation is provided in #1107 with proptests and Hash + Eq for BSATN + RelValue. However, in the interests of smaller PRs for review, I'd like to merge these piece-meal.

Note however that #1063, now merged, has no bearing on this or #1107.

I'd also like to see a comment on SumValue about what constraints need to be upheld by its Hash impl, and the fact that derive(Hash) does indeed uphold those constraints.

Added a larger comment in algebraic_value_hash.rs instead as I felt that provided better context.

I'll mention that, for our case of comparing hashes only between homogeneously-typed values, this does not actually weaken the hash function at all - in fact, avoiding feeding known-equal data into the hasher should improve the hashes, as we're now packing the same number of "interesting" bits alongside fewer "boring" bits into a hash.

Yep, that's definitely true. I'll adjust the PR title and description to capture this.

@Centril Centril changed the title Weaken Hash for AlgebraicValue to ignore discriminants Ignore discriminants in Hash for AV + ArrayValue + nix len-prefix for PV Apr 18, 2024
@Centril Centril requested a review from gefjon April 18, 2024 11:45
- Nix len-prefixing in `Hash for ProductValue`
- Ignore discriminants in `Hash for ArrayValue`
@Centril Centril added this pull request to the merge queue Apr 24, 2024
Merged via the queue into master with commit 8b12d3f Apr 24, 2024
6 checks passed
@Centril Centril deleted the centril/weaker-av-hash branch April 24, 2024 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weaken AlgebraicValue's Hash function to ignore enum discriminants
4 participants