Skip to content

Commit 3f94b99

Browse files
committed
Refactor
Entropy.bits -> entropyBits
1 parent 4207e84 commit 3f94b99

File tree

10 files changed

+89
-82
lines changed

10 files changed

+89
-82
lines changed

examples/gen5.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { Random, Entropy, charset16 } = require('./entropy-string')
2+
const { entropyBits } = require('./entropy')
23

34
const random = new Random(charset16)
4-
const bits = Entropy.bits(10000, 1000000)
5+
const bits = entropyBits(10000, 1000000)
56
const strings = []
67
for (let i = 0; i < 5; i += 1) {
78
const string = random.string(bits)

examples/more_1.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Ten thousand potential strings with 1 in a million risk of repeat
22

33
const { Random, Entropy } = require('./entropy-string')
4+
const { entropyBits } = require('./entropy')
45

56
const random = new Random()
6-
const bits = Entropy.bits(10000, 1000000)
7+
const bits = entropyBits(10000, 1000000)
78
const string = random.string(bits)
89

910
console.log(`\n Ten thousand potential strings with 1 in a million risk of repeat: ${string}\n`)

examples/more_2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Base 16 (hex) and base 4
22

33
const {
4-
Random, Entropy, charset16, charset4
4+
Random, charset16, charset4
55
} = require('./entropy-string')
66

7+
const { entropyBits } = require('./entropy')
8+
79
console.log('\n 30 potential strings with 1 in a million risk of repeat: \n')
810

911
const random = new Random(charset16)
10-
const bits = Entropy.bits(30, 100000)
12+
const bits = entropyBits(30, 100000)
1113
let string = random.string(bits)
1214
console.log(` Base 16: ${string}\n`)
1315

examples/more_3.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Ten billion potential strings with 1 in a trillion risk of repeat
22

3-
const { Random, Entropy } = require('./entropy-string')
3+
const { Random } = require('./entropy-string')
4+
const { entropyBits } = require('./entropy')
45

56
const random = new Random()
6-
const bits = Entropy.bits(1e10, 1e12)
7+
const bits = entropyBits(1e10, 1e12)
78
const string = random.string(bits)
89

910
console.log(`\n Ten billion potential strings with 1 in a trillion risk of repeat: ${string}\n`)

examples/tldr2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Base32 string with a 1 in a billion chance of repeat in a million strings
22

3-
const { Random, Entropy } = require('./entropy-string')
3+
const { Random } = require('./entropy-string')
4+
const { entropyBits } = require('./entropy')
45

56
const random = new Random()
6-
const bits = Entropy.bits(1e6, 1e9)
7+
const bits = entropyBits(1e6, 1e9)
78
const string = random.string(bits)
89

910
console.log(`\n Base32 string with a 1 in a billion chance of repeat in a million strings: ${string}\n`)

examples/tldr_1.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Potential of _1 million_ random strings with _1 in a billion_ chance of repeat
22

3-
const { Random, Entropy } = require('./entropy-string')
3+
const { Random } = require('./entropy-string')
4+
const { entropyBits } = require('./entropy')
45

56
const random = new Random()
6-
const bits = Entropy.bits(1e6, 1e9)
7+
const bits = entropyBits(1e6, 1e9)
78

89
const string = random.string(bits)
910

examples/tldr_2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Generate a potential of _1 million_ random strings with _1 in a billion_ chance of repeat using
22
// hexadecimal strings.
33

4-
const { Random, Entropy, charset16 } = require('./entropy-string')
4+
const { Random, charset16 } = require('./entropy-string')
5+
const { entropyBits } = require('./entropy')
56

67
const random = new Random(charset16)
7-
const bits = Entropy.bits(1e6, 1e9)
8+
const bits = entropyBits(1e6, 1e9)
89

910
const string = random.string(bits)
1011

examples/tldr_3.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Custom uppercase hexadecimal characters
22

3-
const { Random, Entropy } = require('./entropy-string')
3+
const { Random } = require('./entropy-string')
4+
const { entropyBits } = require('./entropy')
45

56
const random = new Random('0123456789ABCDEF')
6-
const bits = Entropy.bits(1e6, 1e9)
7+
const bits = entropyBits(1e6, 1e9)
78

89
const string = random.string(bits)
910

lib/entropy.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const bits = (total, risk) => {
1+
export const entropyBits = (total, risk) => {
22
if (total === 0) { return 0 }
33

44
const { log2 } = Math
@@ -11,7 +11,3 @@ const bits = (total, risk) => {
1111
}
1212
return (N + log2(risk)) - 1
1313
}
14-
15-
export default {
16-
bits
17-
}

test/entropy.js

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
1-
const Entropy = require('../lib/entropy').default
1+
const { entropyBits } = require('../lib/entropy')
22
const test = require('ava')
33

4-
const { round } = Math
4+
const rbits = (t, r) => {
5+
return Math.round(entropyBits(t,r))
6+
}
57

68
test('zero entropy', (t) => {
7-
t.is(Entropy.bits(0, 10), 0)
9+
t.is(entropyBits(0, 10), 0)
810
})
911

1012
test('Bits using total, risk', (t) => {
11-
t.is(round(Entropy.bits(10, 1000)), 15)
12-
t.is(round(Entropy.bits(10, 10000)), 19)
13-
t.is(round(Entropy.bits(10, 100000)), 22)
13+
t.is(rbits(10, 1000), 15)
14+
t.is(rbits(10, 10000), 19)
15+
t.is(rbits(10, 100000), 22)
1416

15-
t.is(round(Entropy.bits(100, 1000)), 22)
16-
t.is(round(Entropy.bits(100, 10000)), 26)
17-
t.is(round(Entropy.bits(100, 100000)), 29)
17+
t.is(rbits(100, 1000), 22)
18+
t.is(rbits(100, 10000), 26)
19+
t.is(rbits(100, 100000), 29)
1820

19-
t.is(round(Entropy.bits(1000, 1000)), 29)
20-
t.is(round(Entropy.bits(1000, 10000)), 32)
21-
t.is(round(Entropy.bits(1000, 100000)), 36)
21+
t.is(rbits(1000, 1000), 29)
22+
t.is(rbits(1000, 10000), 32)
23+
t.is(rbits(1000, 100000), 36)
2224

23-
t.is(round(Entropy.bits(10000, 1000)), 36)
24-
t.is(round(Entropy.bits(10000, 10000)), 39)
25-
t.is(round(Entropy.bits(10000, 100000)), 42)
25+
t.is(rbits(10000, 1000), 36)
26+
t.is(rbits(10000, 10000), 39)
27+
t.is(rbits(10000, 100000), 42)
2628

27-
t.is(round(Entropy.bits(100000, 1000)), 42)
28-
t.is(round(Entropy.bits(100000, 10000)), 46)
29-
t.is(round(Entropy.bits(100000, 100000)), 49)
29+
t.is(rbits(100000, 1000), 42)
30+
t.is(rbits(100000, 10000), 46)
31+
t.is(rbits(100000, 100000), 49)
3032
})
3133

3234
// preshing.com tests come from table at http://preshing.com/20110504/hash-collision-probabilities/
3335
test('preshing.com, 32-bit column', (t) => {
34-
t.is(round(Entropy.bits(30084, 10)), 32)
35-
t.is(round(Entropy.bits(9292, 100)), 32)
36-
t.is(round(Entropy.bits(2932, 1e3)), 32)
37-
t.is(round(Entropy.bits(927, 1e4)), 32)
38-
t.is(round(Entropy.bits(294, 1e5)), 32)
39-
t.is(round(Entropy.bits(93, 1e6)), 32)
40-
t.is(round(Entropy.bits(30, 1e7)), 32)
41-
t.is(round(Entropy.bits(10, 1e8)), 32)
36+
t.is(rbits(30084, 10), 32)
37+
t.is(rbits(9292, 100), 32)
38+
t.is(rbits(2932, 1e3), 32)
39+
t.is(rbits(927, 1e4), 32)
40+
t.is(rbits(294, 1e5), 32)
41+
t.is(rbits(93, 1e6), 32)
42+
t.is(rbits(30, 1e7), 32)
43+
t.is(rbits(10, 1e8), 32)
4244
})
4345

4446
test('preshing.com, 64-bit column', (t) => {
45-
t.is(round(Entropy.bits(1.97e9, 10)), 64)
46-
t.is(round(Entropy.bits(6.09e8, 100)), 64)
47-
t.is(round(Entropy.bits(1.92e8, 1e3)), 64)
48-
t.is(round(Entropy.bits(6.07e7, 1e4)), 64)
49-
t.is(round(Entropy.bits(1.92e7, 1e5)), 64)
50-
t.is(round(Entropy.bits(6.07e6, 1e6)), 64)
51-
t.is(round(Entropy.bits(1.92e6, 1e7)), 64)
52-
t.is(round(Entropy.bits(607401, 1e8)), 64)
53-
t.is(round(Entropy.bits(192077, 1e9)), 64)
54-
t.is(round(Entropy.bits(60704, 1e10)), 64)
55-
t.is(round(Entropy.bits(19208, 1e11)), 64)
56-
t.is(round(Entropy.bits(6074, 1e12)), 64)
57-
t.is(round(Entropy.bits(1921, 1e13)), 64)
58-
t.is(round(Entropy.bits(608, 1e14)), 64)
59-
t.is(round(Entropy.bits(193, 1e15)), 64)
60-
t.is(round(Entropy.bits(61, 1e16)), 64)
61-
t.is(round(Entropy.bits(20, 1e17)), 64)
62-
t.is(round(Entropy.bits(7, 1e18)), 64)
47+
t.is(rbits(1.97e9, 10), 64)
48+
t.is(rbits(6.09e8, 100), 64)
49+
t.is(rbits(1.92e8, 1e3), 64)
50+
t.is(rbits(6.07e7, 1e4), 64)
51+
t.is(rbits(1.92e7, 1e5), 64)
52+
t.is(rbits(6.07e6, 1e6), 64)
53+
t.is(rbits(1.92e6, 1e7), 64)
54+
t.is(rbits(607401, 1e8), 64)
55+
t.is(rbits(192077, 1e9), 64)
56+
t.is(rbits(60704, 1e10), 64)
57+
t.is(rbits(19208, 1e11), 64)
58+
t.is(rbits(6074, 1e12), 64)
59+
t.is(rbits(1921, 1e13), 64)
60+
t.is(rbits(608, 1e14), 64)
61+
t.is(rbits(193, 1e15), 64)
62+
t.is(rbits(61, 1e16), 64)
63+
t.is(rbits(20, 1e17), 64)
64+
t.is(rbits(7, 1e18), 64)
6365
})
6466

6567
test('preshing.com, 160-bit column', (t) => {
66-
t.is(round(Entropy.bits(1.42e24, 2)), 160)
67-
t.is(round(Entropy.bits(5.55e23, 10)), 160)
68-
t.is(round(Entropy.bits(1.71e23, 100)), 160)
69-
t.is(round(Entropy.bits(5.41e22, 1000)), 160)
70-
t.is(round(Entropy.bits(1.71e22, 1.0e04)), 160)
71-
t.is(round(Entropy.bits(5.41e21, 1.0e05)), 160)
72-
t.is(round(Entropy.bits(1.71e21, 1.0e06)), 160)
73-
t.is(round(Entropy.bits(5.41e20, 1.0e07)), 160)
74-
t.is(round(Entropy.bits(1.71e20, 1.0e08)), 160)
75-
t.is(round(Entropy.bits(5.41e19, 1.0e09)), 160)
76-
t.is(round(Entropy.bits(1.71e19, 1.0e10)), 160)
77-
t.is(round(Entropy.bits(5.41e18, 1.0e11)), 160)
78-
t.is(round(Entropy.bits(1.71e18, 1.0e12)), 160)
79-
t.is(round(Entropy.bits(5.41e17, 1.0e13)), 160)
80-
t.is(round(Entropy.bits(1.71e17, 1.0e14)), 160)
81-
t.is(round(Entropy.bits(5.41e16, 1.0e15)), 160)
82-
t.is(round(Entropy.bits(1.71e16, 1.0e16)), 160)
83-
t.is(round(Entropy.bits(5.41e15, 1.0e17)), 160)
84-
t.is(round(Entropy.bits(1.71e15, 1.0e18)), 160)
68+
t.is(rbits(1.42e24, 2), 160)
69+
t.is(rbits(5.55e23, 10), 160)
70+
t.is(rbits(1.71e23, 100), 160)
71+
t.is(rbits(5.41e22, 1000), 160)
72+
t.is(rbits(1.71e22, 1.0e04), 160)
73+
t.is(rbits(5.41e21, 1.0e05), 160)
74+
t.is(rbits(1.71e21, 1.0e06), 160)
75+
t.is(rbits(5.41e20, 1.0e07), 160)
76+
t.is(rbits(1.71e20, 1.0e08), 160)
77+
t.is(rbits(5.41e19, 1.0e09), 160)
78+
t.is(rbits(1.71e19, 1.0e10), 160)
79+
t.is(rbits(5.41e18, 1.0e11), 160)
80+
t.is(rbits(1.71e18, 1.0e12), 160)
81+
t.is(rbits(5.41e17, 1.0e13), 160)
82+
t.is(rbits(1.71e17, 1.0e14), 160)
83+
t.is(rbits(5.41e16, 1.0e15), 160)
84+
t.is(rbits(1.71e16, 1.0e16), 160)
85+
t.is(rbits(5.41e15, 1.0e17), 160)
86+
t.is(rbits(1.71e15, 1.0e18), 160)
8587
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy