removed unused tempering in random number generator, added comments and license text

This commit is contained in:
csoler 2018-05-30 21:19:53 +02:00
parent 6cf2090149
commit 858dcfc14c
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
2 changed files with 51 additions and 29 deletions

View File

@ -1,3 +1,24 @@
/*******************************************************************************
* libretroshare/src/util: rsrandom.cc *
* *
* libretroshare: retroshare core library *
* *
* Copyright (C) 2010 Cyril Soler <csoler@users.sourceforge.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
@ -35,6 +56,10 @@ bool RSRandom::seed(uint32_t s)
for (j=1; j<N; j++) for (j=1; j<N; j++)
MT[j] = (1812433253UL * (MT[j-1] ^ (MT[j-1] >> 30)) + j) & 0xffffffffUL ; MT[j] = (1812433253UL * (MT[j-1] ^ (MT[j-1] >> 30)) + j) & 0xffffffffUL ;
// This *does not* replace the internal seed state of RAND_bytes(), but only *adds* entropy to the random pool
// So calling this method with the same value twice does not guarranty that the output of the random bytes
// will be the same.
RAND_seed((unsigned char *)&MT[0],N*sizeof(uint32_t)) ; RAND_seed((unsigned char *)&MT[0],N*sizeof(uint32_t)) ;
locked_next_state() ; locked_next_state() ;
@ -66,11 +91,13 @@ uint32_t RSRandom::random_u32()
y = MT[index] ; y = MT[index] ;
} }
#ifdef UNNECESSARY_CODE
// Tempering // Tempering
y ^= (y >> 11); y ^= (y >> 11);
y ^= (y << 7 ) & 0x9d2c5680UL; y ^= (y << 7 ) & 0x9d2c5680UL;
y ^= (y << 15) & 0xefc60000UL; y ^= (y << 15) & 0xefc60000UL;
y ^= (y >> 18); y ^= (y >> 18);
#endif
return y; return y;
} }

View File

@ -1,36 +1,31 @@
/**************************************************************** /*******************************************************************************
* RetroShare is distributed under the following license: * libretroshare/src/util: rsrandom.h *
* * *
* Copyright (C) 2010 Cyril Soler <csoler@users.sourceforge.net> * libretroshare: retroshare core library *
* * *
* This program is free software; you can redistribute it and/or * Copyright (C) 2010 Cyril Soler <csoler@users.sourceforge.net> *
* modify it under the terms of the GNU General Public License * *
* as published by the Free Software Foundation; either version 2 * This program is free software: you can redistribute it and/or modify *
* of the License, or (at your option) any later version. * it under the terms of the GNU Lesser General Public License as *
* * published by the Free Software Foundation, either version 3 of the *
* This program is distributed in the hope that it will be useful, * License, or (at your option) any later version. *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * This program is distributed in the hope that it will be useful, *
* GNU General Public License for more details. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* You should have received a copy of the GNU General Public License * GNU Lesser General Public License for more details. *
* along with this program; if not, write to the Free Software * *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, * You should have received a copy of the GNU Lesser General Public License *
* Boston, MA 02110-1301, USA. * along with this program. If not, see <https://www.gnu.org/licenses/>. *
****************************************************************/ * *
*******************************************************************************/
#pragma once #pragma once
// RSRandom contains a random number generator that is // RSRandom contains a random number generator that is
// - thread safe // - thread safe
// - system independant // - system independant
// - fast // - fast
// - NOT CRYPTOGRAPHICALLY SAFE // - CRYPTOGRAPHICALLY SAFE, because it is based on openssl random number generator
// - DO NOT USE FOR ANYTHING REQUIRING STRONG RANDOMNESS
//
// The implementation is adapted from the Mersenne Twister page of Wikipedia.
//
// http://en.wikipedia.org/wiki/Mersenne_twister
#include <vector> #include <vector>
#include <util/rsthreads.h> #include <util/rsthreads.h>