Skip to content

Switch to the SipHash hash function#24

Closed
nicoonoclaste wants to merge 7 commits intoocaml:trunkfrom
nicoonoclaste:hash
Closed

Switch to the SipHash hash function#24
nicoonoclaste wants to merge 7 commits intoocaml:trunkfrom
nicoonoclaste:hash

Conversation

@nicoonoclaste
Copy link

The patchset changes the C API exposed in hash.h to make it digest-agnostic
(so as to make future changes simpler) and switches the hash function to
SipHash-2-4.

SipHash is a familly of cryptographically-strong hash functions which are
competitive (performance-wise) with MurmurHash, the hash function currently used.
SipHash-2-4 is the recommended version of SipHash.

Preliminary benchmarks look good. However, they were done using the functorized
version of Hashtbl, and this specific proposal hasn't been benchmarked yet.

Caveats:

Copy link
Member

Choose a reason for hiding this comment

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

there is a discrepancy between this comment and the uint64 declaration above; wouldn't it be correct to keep them int32 in this function?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, they could be casted to 64-bits just before shifting.
If this is clearer, I will change it.

@xavierleroy
Copy link
Contributor

Some high-level comments:

  • Some applications rely on persistent hash tables, i.e. hash tables that
    are saved to disk using output_value. Changing the hash function will
    cause the hash table read back with input_value to be unusable. When we
    switched to MurmurHash and seeded hash tables, I put ugly hacks in stdlib/
    hashtbl.ml so that "old" hashtables (created with the old hash function)
    would still be usable. I'm afraid those hacks cannot be extended to cover
    yet another hash function. Bottom line: if we change the hash function, we
    must outlaw this use of persistent hash tables and break some
    applications. (SpamOracle is one example.)
  • Passing the hash state by reference rather than by value (as an argument
    and a result) is a good design.
  • SipHash uses the type uint64_t from ISO C99, which is not guaranteed to
    be there in ISO C90. We've been thinking of using C99 rather than C90 as
    the baseline for the OCaml runtime system. This would simplify other
    things. But it means this patch should not be merged before this switch to
    C99 is completed.
  • Don't use alloca(), please. It is not standardized in any way, neither
    in ISO C nor in POSIX.

@gasche
Copy link
Member

gasche commented Mar 27, 2014

I think this patch would really benefit from having a dedicated testsuite. I would expect at least:

  • known-hash tests to make sure that this implementation really corresponds to SipHash
  • performance-stressing code that easily allows to reproduce performance evaluation on various datatypes; Besides performance regression (of course), I'd be curious about performance on 32-bit machines.

@nicoonoclaste
Copy link
Author

@gashe As mentioned in the caveats, I haven't written/fixed the testsuite yet.
I was indeed planning on using known hashes for strings (generated, for instance, with the reference implementation); this ties in with the change in length handling.
Regarding benchmarking, I have a draft benchmark that still needs some work.

@xavierleroy I used uint64 as it was already defined and used elsewhere, but it indeed wouldn't work when ARCH_INT64_TYPE isn't defined.
Is there a more appropriate type, or should we wait until the codebase switched over to C99?

Also, regarding the use of alloca(3) (which is indeed bad practice), should an automatic array be used (I'm not sure about its availability in C90), or should I just heap-allocate the state for the hash function (and pay the associated performance penalty) ?

@nicoonoclaste
Copy link
Author

@bobot The trivial collision occur before the integers are even fed to the hash function.
Basically, the higher and lower 32bits are xor-ed together (plus some fiddling with the sign bit).

Here is some quick-and-dirty code (for 64-bits machines) to generate universal collisions with a small integer:

let x0 = 123456 in (* We want many universal collisions with x0 *)
    assert(0 <= x0 && x0 < 1 lsl 32 && Sys.word_size = 64);
    for i=0 to 50_000 do (* The 50 000 bound is arbitrary *)
        let x = (x0 lxor i) lor (i lsl 32) in
        Printf.printf "%x hashes to %x\n" x @@ Hashtbl.hash x
    done

PS: The collisions are universal in the sense that, using the keyed hash function Hashtbl.seeded_hash, one gets a collision regardless of the hash function's key.

@bnoordhuis
Copy link

Also, regarding the use of alloca(3) (which is indeed bad practice), should an automatic array be used (I'm not sure about its availability in C90), or should I just heap-allocate the state for the hash function (and pay the associated performance penalty) ?

Perhaps I'm missing something obvious but when I looked at the patch yesterday, I was thinking to myself: "Why doesn't he just use a char a[CAML_HASH_T_SIZE] here?"

Choose a reason for hiding this comment

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

I would suggest appending ULL to the integer constants.

@bnoordhuis
Copy link

SipHash uses the type uint64_t from ISO C99, which is not guaranteed to be there in ISO C90.

Is that really an issue? Are there systems out there that ocaml supports that have neither a uint64_t or unsigned long long type? While purely anecdotal, I don't think I've seen any such system in the last 10 years, if not longer.

I would be more worried on the performance impact on 32 bits architectures. 32 bits ARM has instructions that let you do 64 bits arithmetic reasonably efficient but x86 probably has to go about it the long and slow way.

Then again, most operations here are very basic. I wouldn't be surprised if gcc at -m32 -O3 manages to crank out something that's close to optimal.

@xavierleroy
Copy link
Contributor

Are there systems out there that ocaml supports that have neither a
uint64_t or unsigned long long type?

I don't think so, and I'm willing to stop supporting them by removing the
64-bit integer emulation code in the OCaml runtime. My remark was just
that if we integrate this SipHash24-based code that uses native 64-bit
integers, it makes sense to do it while or after we remove this 64-bit
integer emulation code and switch to C99 in the runtime system.

I would be more worried on the performance impact on 32 bits architectures.

32 bits ARM has instructions that let you do 64 bits arithmetic reasonably
efficient but x86 probably has to go about it the long and slow way.

I use SipHash24 as a C benchmark in another context, and gcc generates very
reasonable x86-32 code for it, even at -O1. The only compiler that I saw
producing bad code for SipHash24 is gcc 4.2 on PowerPC 32-bits, for unknown
reasons.

@bobot
Copy link
Contributor

bobot commented Mar 28, 2014

A lot of effort are put into having the best hash functions for the polymorphic hash function of ocaml. Perhaps these hash functions should be accessible from ocaml (using external or better written in ocaml) in order to help the implementation of hash function in ocaml. Often people use combination function that must appear childish for specialist [0][1]:

let combine acc n = n * 65599 + acc

People can use an external library for that, but since the work is done in byterun/, it may be shared in stdlib/.

@c-cube
Copy link
Contributor

c-cube commented Mar 28, 2014

I second @bobot, it would be very interesting to have efficient, well-behaved built-in hash combinators for hashconsing-lovers (or other people who write their own hash functions).

@nicoonoclaste
Copy link
Author

In the spirit of @bobot's combine, it should be possible to expose the basic hash function:

  • type t an opaque for the state of the hash function
  • val init: Int32.t -> t builds the initial state from the key (I would be much happier with greater bit-width for keys and outputs, though)
  • val compress: t -> Int64.t -> unit the compression function
  • val final: t -> int (same issue with bit-width as init)

However, it is probably an orthogonal issue (though the refactoring of the API exposed in hash.h helps).

@bobot
Copy link
Contributor

bobot commented Mar 28, 2014

Yes, I should have said that it is orthogonal and shouldn't go in this merge request. But if someone want to write the code to make a new one, I expect it to be well received.

@nicoonoclaste
Copy link
Author

Here are some benchmarks I performed a few weeks ago; I still haven't benchmarked the impact on Hashtbl, as I couldn't find resources describing methodologies for benchmarking hash-tables.

As shown, the implementation needs some more work. The good news is that it was still fairly naive, and taking advantage of vectorization (either using hand-coded sipround() implementations and #ifdefs or more aggressive compiler optimizations, such as -O2 -ftree-vectorize) should help.

|          | SipHash            | 4.01.0              | Perf. hit |
| 32 chars | 17180522+- 25216/s | 37426014+- 80465/s  | 54.1%     |
| short    | 29820904+-954953/s | 62954641+- 96766/s  | 52.6%     |
| empty    | 30460935+-194229/s | 75118409+-206777/s  | 59.4%     |
|          |                    |                     |           |
| int      | 41318929+-127003/s | 116654005+-181840/s | 63.7%     |
| int64    | 33405769+-38009/s  | 76119185+-125293/s  | 56.1%     |
| int32    | 34526086+-18271/s  | 85051965+-149087/s  | 59.4%     |

PS: The benchmark was ran on my laptop (Core i5, amd64, Linux)

This preserves platform-invariance for integers < 2³¹ at a slight cost in performance
Hand-unroll loops (gcc -O misoptimises) and force inlining.
Halves the running-time on strings here (amd64 (Core i5), Linux, gcc 4.8.2)
This is in preparation of adding a variant of caml_hash for cases
where the initial state of the hash function are known
Also, add some string test vectors to check against
the C reference implementation.
@xavierleroy
Copy link
Contributor

This PR has been sleeping for a long time, sorry about that. In the meantime, a few things happened:

  • I mentioned a switch to ISO C99 standard integer types (uint64_t, etc) in the OCaml runtime system. This is now done in the trunk, so that's one less obstacle to putting Siphash there.
  • I convinced myself that we don't need to maintain backward compatibility for hash tables that are persistent (e.g. stored in files using output_value). It will take some explaining to users, and we should provide functions to help migrating hash tables to a new hash function. But the consequence is that we can consider a change of hashing algorithm serenely.
  • I did some benchmarking of my own on Siphash. Bottom line: yes, it is significantly slower than Murmur 3, especially on 32-bit architectures. What I measured is the speed of Hashtbl.seeded_hash_param, the core primitive.
    On x86-64 bits (Core i7): hashing time increases by 30% to 80% depending on data.
    On x86-32 bits (Core i7): +300% to +500% (ouch!).
    On ARMv7 32 bits (CubieTruck): +180% to +330%.
  • The issue with Murmur3 is not that it has collisions. With our small key space (30 bits), collisions are to be expected as soon as we have about 2^15 entries, whether we're using Siphash or Murmur3. The real issue, as demonstrated by Aumasson and Bernstein, is that Murmur3 has collisions that are independent of the seed, therefore bypassing the random seeding of hash tables and opening up a security hole. I wish someone would come up with a Murmur3-like hash function (i.e. small key size, fast even on 32-bit machines) that mixes the random seed better, to thwart this attack. But Web searches found nothing of that kind.

@funny-falcon
Copy link

Some thoughts:

  • siphash24 is overkill even if siphash is considered for usage. Even siphash13 gives enough avalanche and certainly safe for use in hash table.

    I asked Jean-Philippe Aumasson about, and he had confirmed:

your arguments make a lot of sense: we proposed SipHash-2-4 as a
(strong) PRF/MAC, and so far no attack whatsoever has been found,
although many competent people tried to break it. However, fewer
rounds may be sufficient and I would be very surprised if
SipHash-1-3 introduced weaknesses for hash tables.

@bluddy
Copy link
Contributor

bluddy commented Oct 27, 2015

Is there any way to have some global in Hashtbl that will set which hash function we want to use? Having hash_secure and hash_fast, one of which will alias to 'hash' based on a global, seems like a decent solution to me. I'd rather not incur a performance penalty on every hashtable, especially when this concerns only a small subset of applications.

@Drup
Copy link
Contributor

Drup commented Oct 27, 2015

Is there any way to have some global in Hashtbl that will set which hash function we want to use?

Please don't. Hashtbl.Make is made for that.

@bluddy
Copy link
Contributor

bluddy commented Oct 27, 2015

Fine. But should the default then be fast or secure? I could make the argument that if you're writing for domains where security is critical, you should be using Hashtbl.Make for that.

And can we still have access to the old, fast hash, since I use Hashtbl.hash in many places that don't have to do with hash tables? I would like to maintain that fast hash as Hashtbl.hash_fast

@xavierleroy
Copy link
Contributor

@funny-falcon: thanks a lot for those pointers. It could very well be that your 32-bit "Saphash" variant is fast enough and strong enough for our purposes. When I find the time I'll redo my measurements using Saphash and Siphash13 instead of Siphash24.

@bluddy: OCaml's historic hash function (the one we used before Murmur3) isn't that fast -- Murmur3 is faster on strings, if I remember correctly -- and really bad distribution-wise. I'd rather have a single hash function that is good enough and fast enough for most uses.

Gbury pushed a commit to Gbury/ocaml that referenced this pull request Jul 15, 2019
xavierleroy added a commit to xavierleroy/ocaml that referenced this pull request Jul 13, 2020
SipHash-1-3 is a seeded hash function that, unlike MurmurHash 3, has
no known seed-independent collisions.

This PR updates the Hashtbl implementation to use SipHash-1-3 as
the hash function for randomized hash tables, making them truly
resistant to hash flooding attacks.

Non-randomized hash tables still use MurmurHash 3, as it is faster
than SipHash-1-3, especially on 32-bit platforms, yet good enough,
statistically speaking, for a classic hash table.

Closes: ocaml#24
@xavierleroy
Copy link
Contributor

The resolution of this PR is near, see #9764 .

xavierleroy added a commit to xavierleroy/ocaml that referenced this pull request Jul 20, 2020
SipHash-1-3 is a seeded hash function that, unlike MurmurHash 3, has
no known seed-independent collisions.

This PR updates the Hashtbl implementation to use SipHash-1-3 as
the hash function for randomized hash tables, making them truly
resistant to hash flooding attacks.

Non-randomized hash tables still use MurmurHash 3, as it is faster
than SipHash-1-3, especially on 32-bit platforms, yet good enough,
statistically speaking, for a classic hash table.

Closes: ocaml#24
xavierleroy added a commit to xavierleroy/ocaml that referenced this pull request Aug 18, 2020
SipHash-1-3 is a seeded hash function that, unlike MurmurHash 3, has
no known seed-independent collisions.

This PR updates the Hashtbl implementation to use SipHash-1-3 as
the hash function for randomized hash tables, making them truly
resistant to hash flooding attacks.

Non-randomized hash tables still use MurmurHash 3, as it is faster
than SipHash-1-3, especially on 32-bit platforms, yet good enough,
statistically speaking, for a classic hash table.

This PR also adds an API for randomized hashing of custom blocks.
A "hash_ext" function is added to the operations that can be attached
to custom blocks.  Unlike the existing "hash" operation, which takes
a 32-bit hash state as argument and returns an updated state as result,
"hash_ext" takes a pointer to an opaque "caml_hash_state" struct, which
is updated in-place.

Closes: ocaml#24
xavierleroy added a commit to xavierleroy/ocaml that referenced this pull request Aug 19, 2020
SipHash-1-3 is a seeded hash function that, unlike MurmurHash 3, has
no known seed-independent collisions.

This PR updates the Hashtbl implementation to use SipHash-1-3 as
the hash function for randomized hash tables, making them truly
resistant to hash flooding attacks.

Non-randomized hash tables still use MurmurHash 3, as it is faster
than SipHash-1-3, especially on 32-bit platforms, yet good enough,
statistically speaking, for a classic hash table.

This PR also adds an API for randomized hashing of custom blocks.
A "hash_ext" function is added to the operations that can be attached
to custom blocks.  Unlike the existing "hash" operation, which takes
a 32-bit hash state as argument and returns an updated state as result,
"hash_ext" takes a pointer to an opaque "caml_hash_state" struct, which
is updated in-place.

Closes: ocaml#24
anmolsahoo25 pushed a commit to anmolsahoo25/ocaml that referenced this pull request Aug 25, 2020
lpw25 pushed a commit to lpw25/ocaml that referenced this pull request Oct 12, 2021
* Comballoc bugfix for local types.

When Comballoc does an actual allocation, the state of the other
allocation must must be preserved, as there are two independent
allocation combining sequences.

* Bugfix for type_function: check return modes of curried lambdas

* Print total local allocations under v=0x400

* Rudimentary GC scanning for local types
lpw25 pushed a commit to lpw25/ocaml that referenced this pull request Nov 12, 2021
* Comballoc bugfix for local types.

When Comballoc does an actual allocation, the state of the other
allocation must must be preserved, as there are two independent
allocation combining sequences.

* Bugfix for type_function: check return modes of curried lambdas

* Print total local allocations under v=0x400

* Rudimentary GC scanning for local types
stedolan added a commit to stedolan/ocaml that referenced this pull request May 24, 2022
173842c Merge flambda-backend changes
ed7eba2 Remove leading space from LINE. (oxcaml/oxcaml#484)
bd61170 Bump magic numbers (ocaml#5)
c50c47d Add CI builds with local allocations enabled
1412792 Move local allocations support behind '-extension local'
6d8e42a Better tail call behaviour in caml_applyN
c7dac3d Typemod: toplevel bindings escape even if no variables are bound
82d6c3e Several fixes for partial application and currying
d05c70c Pprintast support for new local syntax
e0e62fc Typecheck x |> f y as (f y x), not ((f y) x)
d7e34ce Remove autogeneration of @ocaml.curry
b9a0593 Port oxcaml/oxcaml#493
0a872d9 Code review fixes from oxcaml/oxcaml#491
6c168bb Remove local allocation counting
3c6e7f0 Code review fixes from oxcaml/oxcaml#478
bb97207 Rename Lambda.apply_position
a7cb650 Quieten Makefile when runtime dep files are not present
c656dc9 Merge flambda-backend changes
11b5424 Avoid printing double spaces in function argument lists
7751faa Restore locations to Typedtree.{pat,let}_bound_idents_full
e450b6c add build_ocaml_compiler.sexp
0403bb3 Revert PR 9895 to continue installing VERSION
b3447db Ensure new local attributes are namespaced properly
7f213fc Allow empty functions again
8f22ad8 Bugfix: ensure local domain state is initialised
80f54dd Bugfix for Selectgen with regions
e8133a1 Fix external-external signature inclusion
9840051 Bootstrap
d879f23 Merge remote-tracking branch 'jane/local-reviewed' into local-merge
94454f5 Use Local_store for the local allocations ref
54a164c Create fewer regions, according to typechecking (ocaml#59)
1c2479b Merge flambda-backend changes
ce34678 Fix printing of modes in return types
91f2281 Hook mode variable solving into Btype.snapshot/backtrack
54e4b09 Move Alloc_mode and Value_mode to Btype
ff4611e Merge flambda-backend changes
ce62e45 Ensure allocations are initialised, even dead ones
6b6ec5a Fix the alloc.ml test on 32-bit builds
81e9879 Merge flambda-backend changes
40a7f89 Update repo URL for ocaml-jst, and rename script.
0454ee7 Add some new locally-allocating primitives (ocaml#57)
8acdda1 Reset the local stack pointer in exception handlers (ocaml#56)
8dafa98 Improve typing for (||) and (&&) (ocaml#55)
8c64754 Fix make_check_all_arches (ocaml#54)
b50cd45 Allow arguments to primitives to be local even in tail position (ocaml#53)
cad125d Fix modes from or-patterns (ocaml#50)
4efdb72 Fix tailcalls tests with inlining (ocaml#52)
4a795cb Flambda support (ocaml#49)
74722cb Add [@ocaml.principal] and [@ocaml.noprincipal] attributes, and use in oo.mli
6d7d3b8 Ensure that functions are evaluated after their arguments (flambda-backend ocaml#353)
89bda6b Keep Sys.opaque_identity in Cmm and Mach (port upstream PR 9412)
a39126a Fix tailcalls within regions (ocaml#48)
4ac4cfd Fix stdlib manpages build
3a95f5e Merge flambda-backend changes
efe80c9 Add jane/pull-flambda-patches script
fca94c4 Register allocations for Omitted parameter closures (ocaml#47)
103b139 Remove various FIXMEs (ocaml#46)
62ba2c1 Bootstrap
a0062ad Allow local allocations for various primitives (ocaml#43)
7a2165e Allow primitives to be poly-moded (ocaml#43)
2af3f55 Fix a flaky test by refactoring TypePairs (ocaml#10638)
58dd807 Bootstrap
ee3be10 Fix modes in build_apply for partial applications
fe73656 Tweak for evaluation order of labelled partial applications (ocaml#10653)
0527570 Fix caml_modify on local allocations (ocaml#40)
e657e99 Relax modes for `as` patterns (ocaml#42)
f815bf2 Add special mode handling for tuples in matches and let bindings (ocaml#38)
39f1211 Only take the upper bounds of modes associated with allocations (ocaml#37)
aec6fde Interpret arrow types in "local positions" differently
c4f3319 Bootstrap
ff6fdad Add some missing regions
40d586d Bootstrap
66d8110 Switch to a system with 3 modes for values
f2c5a85 Bugfix for Comballoc with local allocations. (ocaml#41)
83bcd09 Fix bug with root scanning during compaction (ocaml#39)
1b5ec83 Track modes in Lambda.lfunction and onwards (ocaml#33)
f1e2e97 Port ocaml#10728
56703cd Port ocaml#10081
eb66785 Support local allocations in i386 and fix amd64 bug (ocaml#31)
c936b19 Disallow local recursive non-functions (ocaml#30)
c7a193a GC support for local allocations (ocaml#29)
8dd7270 Nonlocal fields (ocaml#28)
e19a2f0 Bootstrap
694b9ac Add syntax to the parser for local allocations (ocaml#26)
f183008 Lower initial stack size
918226f Allow local closure allocations (ocaml#27)
2552e7d Introduce mode variables (ocaml#25)
bc41c99 Minor fixes for local allocations (ocaml#24)
a2a4e60 Runtime and compiler support for more local allocations (ocaml#23)
d030554 Typechecking for local allocations (ocaml#21)
9ee2332 Bugfix missing from ocaml#20
02c4cef Retain block-structured local regions until Mach.
86dbe1c amd64: Move stack realloc calls out-of-line
324d218 More typing modes and locking of environments
a4080b8 Initial version of local allocation (unsafe)

git-subtree-dir: ocaml
git-subtree-split: 173842c
lpw25 pushed a commit to lpw25/ocaml that referenced this pull request Jun 21, 2022
stedolan pushed a commit to stedolan/ocaml that referenced this pull request Sep 21, 2022
0b0aefb Turn some partial application warnings into hints (ocaml#11338) (ocaml#30)
2caa9ee Add [@tail] and [@nontail] annotations on applications to control tailcalls (ocaml#31)
9fb218a Update `promote` target to use the `one` machinery (ocaml#28)
b5ea912 Make empty types immediate
bc08236 Add failing test of an empty type being immediate
f2d439f Propagate escaping_context to Env locks to hint about errors (ocaml#25)
35569e1 Allow warning 68 to be controlled by attributes (ocaml#16)
28a6243 Allow type_argument to weaken return modes of expected function types (ocaml#24)
cdc728f Fix 'make alldepend' in otherlibs/dynlink
7807d18 make alldepend
2d6af2f Merge flambda-backend changes

git-subtree-dir: ocaml
git-subtree-split: 0b0aefb
@samoht
Copy link
Member

samoht commented Feb 15, 2023

Seems that this can be subsumed by #9764?

@xavierleroy
Copy link
Contributor

xavierleroy commented Feb 15, 2023

Seems that this can be subsumed by #9764?

Yes, that's fair. However, #9764 is kind of stuck on how to treat structured values so as to avoid trivial collisions, so we're not done yet...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.