1 /*Daala video codec
2 Copyright (c) 2012 Daala project contributors. All rights reserved.
3 Author: Timothy B. Terriberry
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 - Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "kiss99.h"
31
kiss99_srand(kiss99_ctx * _this,const unsigned char * _data,int _ndata)32 void kiss99_srand(kiss99_ctx *_this,const unsigned char *_data,int _ndata){
33 int i;
34 _this->z=362436069;
35 _this->w=521288629;
36 _this->jsr=123456789;
37 _this->jcong=380116160;
38 for(i=3;i<_ndata;i+=4){
39 _this->z^=_data[i-3];
40 _this->w^=_data[i-2];
41 _this->jsr^=_data[i-1];
42 _this->jcong^=_data[i];
43 kiss99_rand(_this);
44 }
45 if(i-3<_ndata)_this->z^=_data[i-3];
46 if(i-2<_ndata)_this->w^=_data[i-2];
47 if(i-1<_ndata)_this->jsr^=_data[i-1];
48 /*Fix any potential short cycles that show up.
49 These are not too likely, given the way we initialize the state, but they
50 are technically possible, so let us go ahead and eliminate that
51 possibility.
52 See Gregory G. Rose: "KISS: A Bit Too Simple", Cryptographic Communications
53 No. 10, pp. 123---137, 2018.*/
54 if(_this->z==0||_this->z==0x9068FFFF)_this->z++;
55 if(_this->w==0||_this->w==0x464FFFFF)_this->w++;
56 if(_this->jsr==0)_this->jsr++;
57 }
58
kiss99_rand(kiss99_ctx * _this)59 uint32_t kiss99_rand(kiss99_ctx *_this){
60 uint32_t znew;
61 uint32_t wnew;
62 uint32_t mwc;
63 uint32_t shr3;
64 uint32_t cong;
65 znew=36969*(_this->z&0xFFFF)+(_this->z>>16);
66 wnew=18000*(_this->w&0xFFFF)+(_this->w>>16);
67 mwc=(znew<<16)+wnew;
68 /*We swap the 13 and 17 from the original 1999 algorithm to produce a single
69 cycle of maximal length, matching KISS11.
70 We are not actually using KISS11 because of the impractically large (16 MB)
71 internal state of the full algorithm.*/
72 shr3=_this->jsr^(_this->jsr<<13);
73 shr3^=shr3>>17;
74 shr3^=shr3<<5;
75 cong=69069*_this->jcong+1234567;
76 _this->z=znew;
77 _this->w=wnew;
78 _this->jsr=shr3;
79 _this->jcong=cong;
80 return (mwc^cong)+shr3;
81 }
82