~duangle/scopes

[PATCH] fixed bug in >64-bit hash, added tests

Details
Message ID
<c138a1cfe2954377eaa6.1590007351@pop-os.localdomain>
DKIM signature
missing
Download raw message
# HG changeset patch
# User Westerbly Snaydley <westerbly@gmail.com>
# Date 1589957291 18000
#      Wed May 20 01:48:11 2020 -0500
# Node ID c138a1cfe2954377eaa6b08763176c4c246792f3
# Parent  832bacacb1887c4af85f2c8f37140b5f0ca8d643
fixed bug in >64-bit hash, added tests

diff -r 832bacacb188 -r c138a1cfe295 lib/scopes/core.sc
--- a/lib/scopes/core.sc	Tue May 19 02:00:23 2020 +0200
+++ b/lib/scopes/core.sc	Wed May 20 01:48:11 2020 -0500
@@ -3720,7 +3720,7 @@
                 `(bitcast (sc_hash conv_u64 [('sizeof T)]) hash)
             else
                 let chunk-count = ((bits + (64 - 1)) // 64)
-                let first-chunk = `(sc_hash (itrunc (value >> 64) u64) [('sizeof T)])
+                let first-chunk = `(sc_hash (itrunc value u64) [('sizeof T)])
                 let hash-chain =
                     loop (chunk-index computed = 1 first-chunk)
                         if (chunk-index == chunk-count)
@@ -7916,4 +7916,4 @@
 if true
     run-main;
 
-return;
\ No newline at end of file
+return;
diff -r 832bacacb188 -r c138a1cfe295 testing/test_hash.sc
--- a/testing/test_hash.sc	Tue May 19 02:00:23 2020 +0200
+++ b/testing/test_hash.sc	Wed May 20 01:48:11 2020 -0500
@@ -9,3 +9,18 @@
 
 test ((hash i32) == (hash i32))
 
+do
+    let u128 = (integer 128)
+    let u256 = (integer 256)
+    # a few prime numbers just below 2^64
+    let a b c d =
+        0xFFFFFFFFFFFFFFC5:u64
+        0xFFFFFFFFFFFFFF7D:u64
+        0xFFFFFFFFFFFFFF6B:u64
+        0xFFFFFFFFFFFFFE87:u64
+
+    let bigA = (((u128) | a) | (((u128) | b) << 64)) # ba
+    test ((hash bigA) == (hash a b))
+    let bigB = (((u128) | c) | (((u128) | d) << 64)) # dc
+    let bigC = (((u256) | bigA) | (((u256) | bigB) << 128)) # dcba
+    test ((hash bigC) == (hash a b c d))
Reply to thread Export thread (mbox)