byte* TrapdoorPermutationAbs::hardCoreFunction(TPElement * tpEl) { if (!isKeySet()) throw IllegalStateException("keys aren't set"); /* * We use this implementation both in RSA permutation and in Rabin permutation. * Thus, We implement it in TrapdoorPermutationAbs and let derived classes override it if needed. */ // gets the element value as byte array biginteger elementValue = tpEl->getElement(); byte* bytesValue=NULL; int bytesSize = allocateAndEncodeBigInteger(elementValue, bytesValue); // the number of bytes to get the log (N) least significant bits double logBits = NumberOfBits(modulus) / 2.0; //log N bits int logBytes = (int)ceil(logBits / 8); //log N bites in bytes // if the element length is less than log(N), the return byte[] should be all the element bytes int size = min(logBytes, bytesSize); byte* leastSignificantBytes = new byte[size]; // copies the bytes to the output array for (int i = 0; i < size; i++) leastSignificantBytes[i] = bytesValue[bytesSize - size + i]; return leastSignificantBytes; }
RSAElement::RSAElement(biginteger modN){ /* * samples a number between 1 to n-1 */ mt19937 generator = get_seeded_random(); biginteger randNumber; int numbit = NumberOfBits(modN); biginteger expo = mp::pow(biginteger(2), numbit-1); boost::random::uniform_int_distribution<biginteger> ui(0, expo); do { randNumber = ui(generator); // samples a random BigInteger with modN.bitLength()+1 bits } while (randNumber > (modN - 2)); // drops the element if it's bigger than mod(N)-2 // gets a random biginteger between 1 to modN-1 randNumber += 1; // sets it to be the element element = randNumber; }
RSAElement::RSAElement(biginteger modN){ /* * samples a number between 1 to n-1 */ mt19937 generator = get_seeded_random(); biginteger randNumber; int numbit = NumberOfBits(modN); biginteger expo = mp::pow(biginteger(2), numbit-1); do { // samples a random BigInteger with modN.bitLength()+1 bits randNumber = getRandomInRange(0, expo, generator); } while (randNumber > (modN - 2)); // drops the element if it's bigger than mod(N)-2 // gets a random biginteger between 1 to modN-1 randNumber += 1; // sets it to be the element element = randNumber; }
/** * Checks the validity of the given soundness parameter.<p> * t must be less than a third of the length of the public key n. * @return true if the soundness parameter is valid; false, otherwise. */ bool checkSoundnessParam(const biginteger & modulus) { //If soundness parameter is not less than a third of the publicKey n, return false. int third = NumberOfBits(modulus) / 3; return (t < third); }