shithub: opus

Download patch

ref: 5c3cc55614e2f7eb2f911269d6cde38dcf3633f1
parent: 3a4754853683555759f684e20374e9e51058be9c
author: Timothy B. Terriberry <territim@amazon.com>
date: Wed Nov 10 13:01:42 EST 2021

Minor fixes to kiss99

--- a/dnn/kiss99.c
+++ b/dnn/kiss99.c
@@ -45,6 +45,15 @@
   if(i-3<_ndata)_this->z^=_data[i-3];
   if(i-2<_ndata)_this->w^=_data[i-2];
   if(i-1<_ndata)_this->jsr^=_data[i-1];
+  /*Fix any potential short cycles that show up.
+    These are not too likely, given the way we initialize the state, but they
+     are technically possible, so let us go ahead and eliminate that
+     possibility.
+    See Gregory G. Rose: "KISS: A Bit Too Simple", Cryptographic Communications
+     No. 10, pp. 123---137, 2018.*/
+  if(_this->z==0||_this->z==0x9068FFFF)_this->z++;
+  if(_this->w==0||_this->w==0x464FFFFF)_this->w++;
+  if(_this->jsr==0)_this->jsr++;
 }
 
 uint32_t kiss99_rand(kiss99_ctx *_this){
@@ -56,8 +65,12 @@
   znew=36969*(_this->z&0xFFFF)+(_this->z>>16);
   wnew=18000*(_this->w&0xFFFF)+(_this->w>>16);
   mwc=(znew<<16)+wnew;
-  shr3=_this->jsr^(_this->jsr<<17);
-  shr3^=shr3>>13;
+  /*We swap the 13 and 17 from the original 1999 algorithm to produce a single
+     cycle of maximal length, matching KISS11.
+    We are not actually using KISS11 because of the impractically large (16 MB)
+     internal state of the full algorithm.*/
+  shr3=_this->jsr^(_this->jsr<<13);
+  shr3^=shr3>>17;
   shr3^=shr3<<5;
   cong=69069*_this->jcong+1234567;
   _this->z=znew;
--- a/dnn/kiss99.h
+++ b/dnn/kiss99.h
@@ -27,6 +27,10 @@
 # define _kiss99_H (1)
 # include <stdint.h>
 
+/*KISS PRNG from George Marsaglia (1999 version).
+  See https://en.wikipedia.org/wiki/KISS_(algorithm) for details.
+  This is suitable for simulations, but not for use in crytographic contexts.*/
+
 typedef struct kiss99_ctx kiss99_ctx;
 
 struct kiss99_ctx{
--