From c45b1766eedc24c6b88001ac9183ee41d2c27988 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 11 Apr 2019 04:15:41 -1100 Subject: [PATCH] Fix nondeterministic qsorts --- src/cc/dilithium.c | 5 +++-- src/cryptoconditions/src/asn/constr_SET_OF.c | 3 +++ src/cryptoconditions/src/threshold.c | 15 ++++++++++++--- src/komodo_gateway.h | 18 +++++++++++------- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/cc/dilithium.c b/src/cc/dilithium.c index 7ba522f0f..087dfd9f5 100644 --- a/src/cc/dilithium.c +++ b/src/cc/dilithium.c @@ -2717,8 +2717,9 @@ int64_t *tred, *tadd, *tmul, *tround, *tsample, *tpack, *tshake; static int cmp_llu(const void *a, const void*b) { if(*(int64_t *)a < *(int64_t *)b) return -1; - if(*(int64_t *)a > *(int64_t *)b) return 1; - return 0; + else if(*(int64_t *)a > *(int64_t *)b) return 1; + else if ( (uint64_t)a < (uint64_t)b ) return -1; + else return 1; } static int64_t median(int64_t *l, size_t llen) diff --git a/src/cryptoconditions/src/asn/constr_SET_OF.c b/src/cryptoconditions/src/asn/constr_SET_OF.c index 2dbc6e518..25e80cc30 100644 --- a/src/cryptoconditions/src/asn/constr_SET_OF.c +++ b/src/cryptoconditions/src/asn/constr_SET_OF.c @@ -301,6 +301,9 @@ static int _el_buf_cmp(const void *ap, const void *bp) { ret = -1; else if(a->length > b->length) ret = 1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + ret = -1; + else ret = 1; } return ret; diff --git a/src/cryptoconditions/src/threshold.c b/src/cryptoconditions/src/threshold.c index 82f0e1b0a..7fc801897 100644 --- a/src/cryptoconditions/src/threshold.c +++ b/src/cryptoconditions/src/threshold.c @@ -35,8 +35,15 @@ static uint32_t thresholdSubtypes(const CC *cond) { } -static int cmpCostDesc(const void *a, const void *b) { - return (int) ( *(unsigned long*)b - *(unsigned long*)a ); +static int cmpCostDesc(const void *a, const void *b) +{ + int retval; + retval = (int) ( *(unsigned long*)b - *(unsigned long*)a ); + if ( retval != 0 ) + return(retval); + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } @@ -79,7 +86,9 @@ static int cmpConditionBin(const void *a, const void *b) { if (ret == 0) return r0.encoded < r1.encoded ? -1 : 1; - return 0; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } diff --git a/src/komodo_gateway.h b/src/komodo_gateway.h index ee7480c48..c7a203c3e 100644 --- a/src/komodo_gateway.h +++ b/src/komodo_gateway.h @@ -2524,8 +2524,10 @@ void smooth64(int64_t dest[],int64_t src[],int32_t width,int32_t smoothiters) static int cmp_llu(const void *a, const void*b) { if(*(int64_t *)a < *(int64_t *)b) return -1; - if(*(int64_t *)a > *(int64_t *)b) return 1; - return 0; + else if(*(int64_t *)a > *(int64_t *)b) return 1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } static int64_t sort64(int64_t *l, int32_t llen) @@ -2536,13 +2538,15 @@ static int64_t sort64(int64_t *l, int32_t llen) static int revcmp_llu(const void *a, const void*b) { if(*(int64_t *)a < *(int64_t *)b) return 1; - if(*(int64_t *)a > *(int64_t *)b) return -1; - return 0; + else if(*(int64_t *)a > *(int64_t *)b) return -1; + else if ( (uint64_t)a < (uint64_t)b ) // jl777 prevent nondeterminism + return(-1); + else return(1); } static int64_t revsort64(int64_t *l, int32_t llen) { - heapsort(l,llen,sizeof(uint64_t),cmp_llu); + qsort(l,llen,sizeof(uint64_t),revcmp_llu); } int64_t komodo_priceave(int64_t *buf,int64_t *correlated,int32_t cskip) @@ -2575,7 +2579,7 @@ int64_t komodo_priceave(int64_t *buf,int64_t *correlated,int32_t cskip) decayprice = buf[0]; for (i=0; i %.4f\n",(double)halfprice/COIN,(double)price/COIN,(double)decayprice/COIN); @@ -2586,7 +2590,7 @@ int64_t komodo_priceave(int64_t *buf,int64_t *correlated,int32_t cskip) decayprice = buf[0]; for (i=0; i %.4f\n",(double)halfprice/COIN,(double)price/COIN,(double)decayprice/COIN);