Skip to content

Commit

Permalink
Improve random generators
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Jul 6, 2024
1 parent 7ef9c31 commit 733e53d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 46 deletions.
20 changes: 10 additions & 10 deletions lib/src/algorithms/random_generators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import 'dart:math';
import 'dart:typed_data';

import 'package:hashlib/src/algorithms/keccak.dart';
import 'package:hashlib/src/algorithms/keccak/keccak.dart';
import 'package:hashlib/src/algorithms/md4.dart';
import 'package:hashlib/src/algorithms/sha2.dart';
import 'package:hashlib/src/algorithms/sha2/sha2.dart';
import 'package:hashlib/src/algorithms/sm3.dart';
import 'package:hashlib/src/algorithms/xxh64.dart';
import 'package:hashlib/src/algorithms/xxh64/xxh64.dart';
import 'package:hashlib/src/core/hash_base.dart';

const int _mask32 = 0xFFFFFFFF;
Expand Down Expand Up @@ -65,20 +65,20 @@ abstract class RandomGenerators {
static void $seedList(TypedData data, int seed) {
var list = Uint32List.view(data.buffer);
var inp = [
0x90BEFFFA ^ seed & _mask32,
seed & _mask32,
seed >>> 32,
list.length,
0xD5A79147,
0x14292967 + list.length,
0xD192E819 | ~seed,
0x59F111F1 ^ -seed.bitLength,
0xD6990624 ^ ~seed >>> 32,
0x106AA070 + seed,
0x71374491 - seed,
0x06CA6351 ^ seed,
0x650A7354 ^ ~seed,
0xF40E3585 - ~seed,
0x650A7354,
0xF40E3585,
0x766A0ABB ^ -seed,
0x81C2C92E ^ ~list.length,
0x92722C85 | ~seed,
0x81C2C92E,
0x92722C85,
0x748F82EE ^ -list.length,
0x78A5636F | -seed,
];
Expand Down
83 changes: 47 additions & 36 deletions test/random_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,41 @@ void main() {
test("system random", () {
runFunctionalText(HashlibRandom(RandomGenerator.system));
});
test("keccak random", () {
runFunctionalText(HashlibRandom(RandomGenerator.keccak));
group("keccak random", () {
test('functions', () {
runFunctionalText(HashlibRandom(RandomGenerator.keccak));
});
test('with seed', () {
var rand = HashlibRandom(RandomGenerator.keccak, seed: 100);
expect(rand.nextInt(), 3662713900);
});
});
test("sha256 random", () {
runFunctionalText(HashlibRandom(RandomGenerator.sha256));
group("sha256 random", () {
test("functions", () {
runFunctionalText(HashlibRandom(RandomGenerator.sha256));
});
test('with seed', () {
var rand = HashlibRandom(RandomGenerator.sha256, seed: 100);
expect(rand.nextInt(), 3449288731);
});
});
test("sm3 random", () {
runFunctionalText(HashlibRandom(RandomGenerator.sm3));
group("sm3 random", () {
test("functions", () {
runFunctionalText(HashlibRandom(RandomGenerator.sm3));
});
test('with seed', () {
var rand = HashlibRandom(RandomGenerator.sm3, seed: 100);
expect(rand.nextInt(), 894660838);
});
});
test("md5 random", () {
runFunctionalText(HashlibRandom(RandomGenerator.md5));
group("md5 random", () {
test("functions", () {
runFunctionalText(HashlibRandom(RandomGenerator.md5));
});
test('with seed', () {
var rand = HashlibRandom(RandomGenerator.md5, seed: 100);
expect(rand.nextInt(), 2852136378);
});
});
test("xxh64 random", () {
runFunctionalText(HashlibRandom(RandomGenerator.xxh64));
Expand Down Expand Up @@ -84,13 +108,27 @@ void main() {
});

test('next between', () {
var rand = HashlibRandom(RandomGenerator.keccak, seed: 100);
var rand = HashlibRandom.secure();
expect(rand.nextBetween(0, 0), 0);
expect(rand.nextBetween(1, 1), 1);
expect(rand.nextBetween(5, 10), lessThanOrEqualTo(10));
expect(rand.nextBetween(10, 5), greaterThanOrEqualTo(5));
expect(rand.nextBetween(-5, -2), lessThan(0));
expect(rand.nextBetween(-5, -15), lessThan(0));
for (int i = 0; i < 100; ++i) {
expect(rand.nextBetween(0, 1), lessThanOrEqualTo(1));
expect(rand.nextBetween(0, 3), lessThanOrEqualTo(3));
expect(rand.nextBetween(0, 10), lessThanOrEqualTo(10));
expect(rand.nextBetween(0, 50), lessThanOrEqualTo(50));
expect(rand.nextBetween(0, 500), lessThanOrEqualTo(500));
expect(rand.nextBetween(0, 85701), lessThanOrEqualTo(85701));
expect(rand.nextBetween(1, _maxInt), greaterThanOrEqualTo(1));
expect(rand.nextBetween(3, _maxInt), greaterThanOrEqualTo(3));
expect(rand.nextBetween(10, _maxInt), greaterThanOrEqualTo(10));
expect(rand.nextBetween(50, _maxInt), greaterThanOrEqualTo(50));
expect(rand.nextBetween(500, _maxInt), greaterThanOrEqualTo(500));
expect(rand.nextBetween(85701, _maxInt), greaterThanOrEqualTo(85701));
}
});

test('random string throws StateError on empty whitelist', () {
Expand All @@ -115,31 +153,4 @@ void main() {
),
throwsStateError);
});

group('keccak random', () {
test('with seed', () {
var rand = HashlibRandom(RandomGenerator.keccak, seed: 10);
expect(rand.nextInt(), 863158043);
}, tags: 'vm-only');
test('default seed', () {
var rand = HashlibRandom(RandomGenerator.keccak);
for (int i = 0; i < 100; ++i) {
expect(rand.nextBetween(0, 1), lessThanOrEqualTo(1));
expect(rand.nextBetween(0, 3), lessThanOrEqualTo(3));
expect(rand.nextBetween(0, 10), lessThanOrEqualTo(10));
expect(rand.nextBetween(0, 50), lessThanOrEqualTo(50));
expect(rand.nextBetween(0, 500), lessThanOrEqualTo(500));
expect(rand.nextBetween(0, 85701), lessThanOrEqualTo(85701));
expect(rand.nextBetween(1, _maxInt), greaterThanOrEqualTo(1));
expect(rand.nextBetween(3, _maxInt), greaterThanOrEqualTo(3));
expect(rand.nextBetween(10, _maxInt), greaterThanOrEqualTo(10));
expect(rand.nextBetween(50, _maxInt), greaterThanOrEqualTo(50));
expect(rand.nextBetween(500, _maxInt), greaterThanOrEqualTo(500));
expect(rand.nextBetween(85701, _maxInt), greaterThanOrEqualTo(85701));
}
});
test('random bytes length = 100', () {
expect(randomBytes(100, generator: RandomGenerator.keccak).length, 100);
});
});
}

0 comments on commit 733e53d

Please sign in to comment.