From 76cd9f404857b916435df223ebe8f930ea70026e Mon Sep 17 00:00:00 2001 From: quetzalcoatl Date: Wed, 24 Oct 2018 09:20:53 +0200 Subject: [PATCH 1/7] added bunch of tests that show that and64/or64/shift64 are unsafe for large numbers that may be treated as signed --- curation_calculator.html | 171 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/curation_calculator.html b/curation_calculator.html index 63af62f..bfcf4e1 100644 --- a/curation_calculator.html +++ b/curation_calculator.html @@ -507,5 +507,176 @@

return hi*Math.pow(2,32) + lo; } + + From 9d31391342b93e0f1e5a9de995db49288a73619d Mon Sep 17 00:00:00 2001 From: quetzalcoatl Date: Wed, 24 Oct 2018 09:23:23 +0200 Subject: [PATCH 2/7] forced and64/or64 result to be unsigned --- curation_calculator.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/curation_calculator.html b/curation_calculator.html index bfcf4e1..b622ed4 100644 --- a/curation_calculator.html +++ b/curation_calculator.html @@ -479,16 +479,16 @@

function and64(x,y){ x = hi_lo(x); y = hi_lo(y); - hi = x.hi & y.hi; - lo = x.lo & y.lo; + hi = (x.hi & y.hi) >>> 0; + lo = (x.lo & y.lo) >>> 0; return hi*Math.pow(2,32) + lo; } function or64(x,y){ x = hi_lo(x); y = hi_lo(y); - hi = x.hi | y.hi; - lo = x.lo | y.lo; + hi = (x.hi | y.hi) >>> 0; + lo = (x.lo | y.lo) >>> 0; return hi*Math.pow(2,32) + lo; } From d7334df225021e1f6afda585a0ee8f1a853ce847 Mon Sep 17 00:00:00 2001 From: quetzalcoatl Date: Wed, 24 Oct 2018 09:25:41 +0200 Subject: [PATCH 3/7] corrected and simplified both shift64, added truncation to shiftleft64 to ensure it returns integer not longer than 64bit --- curation_calculator.html | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/curation_calculator.html b/curation_calculator.html index b622ed4..3cd97ef 100644 --- a/curation_calculator.html +++ b/curation_calculator.html @@ -493,19 +493,12 @@

} function shiftleft64(x,y){ - x = hi_lo(x); - hi = x.hi * Math.pow(2,y); - lo = x.lo * Math.pow(2,y); - return hi*Math.pow(2,32) + lo; + return (x * Math.pow(2,y)) % Math.pow(2,64); // mod64 as a safeguard against 'val' exceeding 64bit integer range } function shiftright64(x,y){ - x = hi_lo(x); - hi = Math.floor(x.hi / Math.pow(2,y)); - aux = x.hi % Math.pow(2,y); - lo = Math.floor(x.lo / Math.pow(2,y)) + aux * Math.pow(2,32-y); - return hi*Math.pow(2,32) + lo; - } + return Math.floor(x / Math.pow(2,y)); + }