Skip to content

Commit 7149ce7

Browse files
committed
Attempt to imitate mpz_rrandomb behavior in RandomBigInt
1 parent 298a5ea commit 7149ce7

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

Classes/FCryptoTestMutator.uc

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ var(FCryptoTests) int CurrentTestDelegateIndex;
113113

114114
var private bool bRandPrimesRequested;
115115

116+
var const array<byte> RepeatedBinaryRandoms;
117+
116118
/*
117119
* AES known-answer tests. Order: key, plaintext, ciphertext.
118120
*/
@@ -618,6 +620,7 @@ private final simulated function RandomBigInt(
618620
local array<int> BigIntN;
619621
local int BICLen;
620622
local int BINLen;
623+
local int RandomIndex;
621624
// local string BigIntNString;
622625

623626
class'FCryptoBigInt'.static.Decode(BigIntN, N, N.Length);
@@ -654,11 +657,27 @@ private final simulated function RandomBigInt(
654657
return;
655658
}
656659

660+
RandomIndex = 0;
657661
Dst.Length = 0;
658662
for (I = 0; I <= Rounds; ++I)
659663
{
660664
Ctl = 0;
661-
Dst[I] = Rand(256);
665+
666+
// NOTE: this is attempting to somewhat imitate GMP mpz_rrandomb behavior
667+
// to generate an integer with long strings of zeros and ones in the
668+
// binary representation.
669+
// TODO: better algorithm.
670+
if (FRand() >= 0.33)
671+
{
672+
// Pick one of known numbers with a binary representation that has a lot
673+
// of consecutive ones or zeros.
674+
Dst[I] = RepeatedBinaryRandoms[RandomIndex];
675+
RandomIndex = (RandomIndex + 1) % RepeatedBinaryRandoms.Length;
676+
}
677+
else
678+
{
679+
Dst[I] = Rand(256);
680+
}
662681

663682
// Convert Dst to a BigInt for checking whether it's
664683
// less or greater than BigIntN.
@@ -1375,6 +1394,27 @@ DefaultProperties
13751394
Begin Object Class=FCryptoUtils Name=Utils
13761395
End Object
13771396

1397+
RepeatedBinaryRandoms={(
1398+
255, // 11111111
1399+
255, // 11111111
1400+
255, // 11111111
1401+
245, // 11111110
1402+
0, // 00000000
1403+
64, // 10000000
1404+
0, // 00000000
1405+
0, // 00000000
1406+
0, // 00000000
1407+
0, // 00000000
1408+
65, // 10000010
1409+
0, // 00000000
1410+
66, // 10000100
1411+
0, // 00000000
1412+
255, // 11111111
1413+
255, // 11111111
1414+
255, // 11111111
1415+
255, // 11111111
1416+
)}
1417+
13781418
Bytes_0(0)=0
13791419

13801420
// GMP mpz big endian export format.

0 commit comments

Comments
 (0)