aboutsummaryrefslogtreecommitdiff
path: root/tests/spec
diff options
context:
space:
mode:
authorIan Coleman <ian@iancoleman.io>2020-10-01 23:44:38 +0000
committerIan Coleman <ian@iancoleman.io>2020-10-01 23:44:38 +0000
commitbf96267f89d18f278e78cf02c97ab1e7513fb871 (patch)
treef832e39596ae0f356a4ec1fe0f4f1e9e837b5a27 /tests/spec
parent920f7aa0785f3d2fb7b08667ea371f349eb4bced (diff)
downloadBIP39-bf96267f89d18f278e78cf02c97ab1e7513fb871.tar.gz
BIP39-bf96267f89d18f278e78cf02c97ab1e7513fb871.tar.zst
BIP39-bf96267f89d18f278e78cf02c97ab1e7513fb871.zip
Remove bias from entropy in base 6 and base 10
Diffstat (limited to 'tests/spec')
-rw-r--r--tests/spec/tests.js96
1 files changed, 51 insertions, 45 deletions
diff --git a/tests/spec/tests.js b/tests/spec/tests.js
index 14cdb8e..92df64a 100644
--- a/tests/spec/tests.js
+++ b/tests/spec/tests.js
@@ -3120,7 +3120,7 @@ it("Shows the number of bits of entropy for 4 bits of binary", function(done) {
3120 testEntropyBits(done, "0000", "4"); 3120 testEntropyBits(done, "0000", "4");
3121}); 3121});
3122it("Shows the number of bits of entropy for 1 character of base 6 (dice)", function(done) { 3122it("Shows the number of bits of entropy for 1 character of base 6 (dice)", function(done) {
3123 // 6 in card is 0 in base 6, 0 in base 6 is 2.6 bits (rounded down to 2 bits) 3123 // 6 in card is 0 in base 6, 0 is mapped to 00 by entropy.js
3124 testEntropyBits(done, "6", "2"); 3124 testEntropyBits(done, "6", "2");
3125}); 3125});
3126it("Shows the number of bits of entropy for 1 character of base 10 with 3 bits", function(done) { 3126it("Shows the number of bits of entropy for 1 character of base 10 with 3 bits", function(done) {
@@ -3128,13 +3128,15 @@ it("Shows the number of bits of entropy for 1 character of base 10 with 3 bits",
3128 testEntropyBits(done, "7", "3"); 3128 testEntropyBits(done, "7", "3");
3129}); 3129});
3130it("Shows the number of bits of entropy for 1 character of base 10 with 4 bis", function(done) { 3130it("Shows the number of bits of entropy for 1 character of base 10 with 4 bis", function(done) {
3131 testEntropyBits(done, "8", "4"); 3131 // 8 in base 10 is mapped to 0 by entropy.js
3132 testEntropyBits(done, "8", "1");
3132}); 3133});
3133it("Shows the number of bits of entropy for 1 character of hex", function(done) { 3134it("Shows the number of bits of entropy for 1 character of hex", function(done) {
3134 testEntropyBits(done, "F", "4"); 3135 testEntropyBits(done, "F", "4");
3135}); 3136});
3136it("Shows the number of bits of entropy for 2 characters of base 10", function(done) { 3137it("Shows the number of bits of entropy for 2 characters of base 10", function(done) {
3137 testEntropyBits(done, "29", "6"); 3138 // 2 as base 10 is binary 010, 9 is mapped to binary 1 by entropy.js
3139 testEntropyBits(done, "29", "4");
3138}); 3140});
3139it("Shows the number of bits of entropy for 2 characters of hex", function(done) { 3141it("Shows the number of bits of entropy for 2 characters of hex", function(done) {
3140 testEntropyBits(done, "0A", "8"); 3142 testEntropyBits(done, "0A", "8");
@@ -3159,17 +3161,17 @@ it("Shows the number of bits of entropy for 4 characters of hex with leading zer
3159 testEntropyBits(done, "000A", "16"); 3161 testEntropyBits(done, "000A", "16");
3160}); 3162});
3161it("Shows the number of bits of entropy for 4 characters of base 6", function(done) { 3163it("Shows the number of bits of entropy for 4 characters of base 6", function(done) {
3162 testEntropyBits(done, "5555", "11"); 3164 // 5 in base 6 is mapped to binary 1
3165 testEntropyBits(done, "5555", "4");
3163}); 3166});
3164it("Shows the number of bits of entropy for 4 characters of base 6 dice", function(done) { 3167it("Shows the number of bits of entropy for 4 characters of base 6 dice", function(done) {
3165 // uses dice, so entropy is actually 0000 in base 6, which is 4 lots of 3168 // uses dice, so entropy is actually 0000 in base 6, which is 4 lots of
3166 // 2.58 bits, which is 10.32 bits (rounded down to 10 bits) 3169 // binary 00
3167 testEntropyBits(done, "6666", "10"); 3170 testEntropyBits(done, "6666", "8");
3168}); 3171});
3169it("Shows the number of bits of entropy for 4 charactes of base 10", function(done) { 3172it("Shows the number of bits of entropy for 4 charactes of base 10", function(done) {
3170 // Uses base 10, which is 4 lots of 3.32 bits, which is 13.3 bits (rounded 3173 // 2 in base 10 is binary 010 and 7 is binary 111 so is 4 events of 3 bits
3171 // down to 13) 3174 testEntropyBits(done, "2227", "12");
3172 testEntropyBits(done, "2227", "13");
3173}); 3175});
3174it("Shows the number of bits of entropy for 4 characters of hex with 2 leading zeros", function(done) { 3176it("Shows the number of bits of entropy for 4 characters of hex with 2 leading zeros", function(done) {
3175 testEntropyBits(done, "222F", "16"); 3177 testEntropyBits(done, "222F", "16");
@@ -3178,13 +3180,16 @@ it("Shows the number of bits of entropy for 4 characters of hex starting with F"
3178 testEntropyBits(done, "FFFF", "16"); 3180 testEntropyBits(done, "FFFF", "16");
3179}); 3181});
3180it("Shows the number of bits of entropy for 10 characters of base 10", function(done) { 3182it("Shows the number of bits of entropy for 10 characters of base 10", function(done) {
3181 // 10 events at 3.32 bits per event 3183 // 10 events with 3 bits for each event
3182 testEntropyBits(done, "0000101017", "33"); 3184 testEntropyBits(done, "0000101017", "30");
3185});
3186it("Shows the number of bits of entropy for 10 characters of base 10 account for bias", function(done) {
3187 // 9 events with 3 bits per event and 1 event with 1 bit per event
3188 testEntropyBits(done, "0000101018", "28");
3183}); 3189});
3184it("Shows the number of bits of entropy for a full deck of cards", function(done) { 3190it("Shows the number of bits of entropy for a full deck of cards", function(done) {
3185 // cards are not replaced, so a full deck is not 52^52 entropy which is 296 3191 // removing bias is 32*5 + 16*4 + 4*2
3186 // bits, it's 52!, which is 225 bits 3192 testEntropyBits(done, "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "232");
3187 testEntropyBits(done, "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", "225");
3188}); 3193});
3189 3194
3190it("Shows details about the entered entropy", function(done) { 3195it("Shows details about the entered entropy", function(done) {
@@ -3310,7 +3315,7 @@ it("Shows details about the entered entropy", function(done) {
3310 entropy: "7d", 3315 entropy: "7d",
3311 type: "card", 3316 type: "card",
3312 events: "1", 3317 events: "1",
3313 bits: "4", 3318 bits: "5",
3314 words: 0, 3319 words: 0,
3315 strength: "less than a second", 3320 strength: "less than a second",
3316 } 3321 }
@@ -3322,7 +3327,7 @@ it("Shows details about the entered entropy", function(done) {
3322 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", 3327 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
3323 type: "card (full deck)", 3328 type: "card (full deck)",
3324 events: "52", 3329 events: "52",
3325 bits: "225", 3330 bits: "232",
3326 words: 21, 3331 words: 21,
3327 strength: "centuries", 3332 strength: "centuries",
3328 } 3333 }
@@ -3334,7 +3339,7 @@ it("Shows details about the entered entropy", function(done) {
3334 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks3d", 3339 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks3d",
3335 type: "card (full deck, 1 duplicate: 3d)", 3340 type: "card (full deck, 1 duplicate: 3d)",
3336 events: "53", 3341 events: "53",
3337 bits: "254", 3342 bits: "237",
3338 words: 21, 3343 words: 21,
3339 strength: "centuries", 3344 strength: "centuries",
3340 } 3345 }
@@ -3346,7 +3351,7 @@ it("Shows details about the entered entropy", function(done) {
3346 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d", 3351 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d",
3347 type: "card (2 duplicates: 3d 4d, 1 missing: KS)", 3352 type: "card (2 duplicates: 3d 4d, 1 missing: KS)",
3348 events: "53", 3353 events: "53",
3349 bits: "254", 3354 bits: "240",
3350 words: 21, 3355 words: 21,
3351 strength: "centuries", 3356 strength: "centuries",
3352 } 3357 }
@@ -3358,8 +3363,8 @@ it("Shows details about the entered entropy", function(done) {
3358 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d5d6d", 3363 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqs3d4d5d6d",
3359 type: "card (4 duplicates: 3d 4d 5d..., 1 missing: KS)", 3364 type: "card (4 duplicates: 3d 4d 5d..., 1 missing: KS)",
3360 events: "55", 3365 events: "55",
3361 bits: "264", 3366 bits: "250",
3362 words: 24, 3367 words: 21,
3363 strength: "centuries", 3368 strength: "centuries",
3364 } 3369 }
3365 ); 3370 );
@@ -3367,13 +3372,12 @@ it("Shows details about the entered entropy", function(done) {
3367it("Shows details about the entered entropy", function(done) { 3372it("Shows details about the entered entropy", function(done) {
3368 testEntropyFeedback(done, 3373 testEntropyFeedback(done,
3369 // Next test was throwing uncaught error in zxcvbn 3374 // Next test was throwing uncaught error in zxcvbn
3370 // Also tests 451 bits, ie Math.log2(52!)*2 = 225.58 * 2
3371 { 3375 {
3372 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsksac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", 3376 entropy: "ac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsksac2c3c4c5c6c7c8c9ctcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
3373 type: "card (full deck, 52 duplicates: ac 2c 3c...)", 3377 type: "card (full deck, 52 duplicates: ac 2c 3c...)",
3374 events: "104", 3378 events: "104",
3375 bits: "499", 3379 bits: "464",
3376 words: 45, 3380 words: 42,
3377 strength: "centuries", 3381 strength: "centuries",
3378 } 3382 }
3379 ); 3383 );
@@ -3385,7 +3389,7 @@ it("Shows details about the entered entropy", function(done) {
3385 entropy: "asAS", 3389 entropy: "asAS",
3386 type: "card (1 duplicate: AS)", 3390 type: "card (1 duplicate: AS)",
3387 events: "2", 3391 events: "2",
3388 bits: "9", 3392 bits: "8",
3389 words: 0, 3393 words: 0,
3390 strength: "less than a second", 3394 strength: "less than a second",
3391 } 3395 }
@@ -3397,7 +3401,7 @@ it("Shows details about the entered entropy", function(done) {
3397 entropy: "ASas", 3401 entropy: "ASas",
3398 type: "card (1 duplicate: as)", 3402 type: "card (1 duplicate: as)",
3399 events: "2", 3403 events: "2",
3400 bits: "9", 3404 bits: "8",
3401 words: 0, 3405 words: 0,
3402 strength: "less than a second", 3406 strength: "less than a second",
3403 } 3407 }
@@ -3410,8 +3414,8 @@ it("Shows details about the entered entropy", function(done) {
3410 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", 3414 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d5d6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
3411 type: "card (1 missing: 9C)", 3415 type: "card (1 missing: 9C)",
3412 events: "51", 3416 events: "51",
3413 bits: "221", 3417 bits: "227",
3414 words: 18, 3418 words: 21,
3415 strength: "centuries", 3419 strength: "centuries",
3416 } 3420 }
3417 ); 3421 );
@@ -3422,7 +3426,7 @@ it("Shows details about the entered entropy", function(done) {
3422 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", 3426 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d7d8d9dtdjdqdkdah2h3h4h5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
3423 type: "card (2 missing: 9C 5D)", 3427 type: "card (2 missing: 9C 5D)",
3424 events: "50", 3428 events: "50",
3425 bits: "216", 3429 bits: "222",
3426 words: 18, 3430 words: 18,
3427 strength: "centuries", 3431 strength: "centuries",
3428 } 3432 }
@@ -3434,7 +3438,7 @@ it("Shows details about the entered entropy", function(done) {
3434 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d7d8d9dtdjd kdah2h3h 5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks", 3438 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d7d8d9dtdjd kdah2h3h 5h6h7h8h9hthjhqhkhas2s3s4s5s6s7s8s9stsjsqsks",
3435 type: "card (4 missing: 9C 5D QD...)", 3439 type: "card (4 missing: 9C 5D QD...)",
3436 events: "48", 3440 events: "48",
3437 bits: "208", 3441 bits: "212",
3438 words: 18, 3442 words: 18,
3439 strength: "centuries", 3443 strength: "centuries",
3440 } 3444 }
@@ -3447,20 +3451,21 @@ it("Shows details about the entered entropy", function(done) {
3447 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d 8d9d jd kdah2h3h 5h6h7h8h9hthjhqhkh 2s3s4s5s6s7s8s9stsjsqsks", 3451 entropy: "ac2c3c4c5c6c7c8c tcjcqckcad2d3d4d 6d 8d9d jd kdah2h3h 5h6h7h8h9hthjhqhkh 2s3s4s5s6s7s8s9stsjsqsks",
3448 type: "card", 3452 type: "card",
3449 events: "45", 3453 events: "45",
3450 bits: "195", 3454 bits: "198",
3451 words: 18, 3455 words: 18,
3452 strength: "centuries", 3456 strength: "centuries",
3453 } 3457 }
3454 ); 3458 );
3455}); 3459});
3456it("Shows details about the entered entropy", function(done) { 3460it("Shows details about the entered entropy", function(done) {
3461 // multiple decks does not affect the bits per event
3462 // since the bits are hardcoded in entropy.js
3457 testEntropyFeedback(done, 3463 testEntropyFeedback(done,
3458 // Multiple decks of cards increases bits per event
3459 { 3464 {
3460 entropy: "3d", 3465 entropy: "3d",
3461 events: "1", 3466 events: "1",
3462 bits: "4", 3467 bits: "5",
3463 bitsPerEvent: "4.34", 3468 bitsPerEvent: "4.46",
3464 } 3469 }
3465 ); 3470 );
3466}); 3471});
@@ -3469,8 +3474,8 @@ it("Shows details about the entered entropy", function(done) {
3469 { 3474 {
3470 entropy: "3d3d", 3475 entropy: "3d3d",
3471 events: "2", 3476 events: "2",
3472 bits: "9", 3477 bits: "10",
3473 bitsPerEvent: "4.80", 3478 bitsPerEvent: "4.46",
3474 } 3479 }
3475 ); 3480 );
3476}); 3481});
@@ -3480,7 +3485,7 @@ it("Shows details about the entered entropy", function(done) {
3480 entropy: "3d3d3d", 3485 entropy: "3d3d3d",
3481 events: "3", 3486 events: "3",
3482 bits: "15", 3487 bits: "15",
3483 bitsPerEvent: "5.01", 3488 bitsPerEvent: "4.46",
3484 } 3489 }
3485 ); 3490 );
3486}); 3491});
@@ -3490,7 +3495,7 @@ it("Shows details about the entered entropy", function(done) {
3490 entropy: "3d3d3d3d", 3495 entropy: "3d3d3d3d",
3491 events: "4", 3496 events: "4",
3492 bits: "20", 3497 bits: "20",
3493 bitsPerEvent: "5.14", 3498 bitsPerEvent: "4.46",
3494 } 3499 }
3495 ); 3500 );
3496}); 3501});
@@ -3499,8 +3504,8 @@ it("Shows details about the entered entropy", function(done) {
3499 { 3504 {
3500 entropy: "3d3d3d3d3d", 3505 entropy: "3d3d3d3d3d",
3501 events: "5", 3506 events: "5",
3502 bits: "26", 3507 bits: "25",
3503 bitsPerEvent: "5.22", 3508 bitsPerEvent: "4.46",
3504 } 3509 }
3505 ); 3510 );
3506}); 3511});
@@ -3509,8 +3514,8 @@ it("Shows details about the entered entropy", function(done) {
3509 { 3514 {
3510 entropy: "3d3d3d3d3d3d", 3515 entropy: "3d3d3d3d3d3d",
3511 events: "6", 3516 events: "6",
3512 bits: "31", 3517 bits: "30",
3513 bitsPerEvent: "5.28", 3518 bitsPerEvent: "4.46",
3514 } 3519 }
3515 ); 3520 );
3516}); 3521});
@@ -3519,8 +3524,8 @@ it("Shows details about the entered entropy", function(done) {
3519 { 3524 {
3520 entropy: "3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d", 3525 entropy: "3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d3d",
3521 events: "33", 3526 events: "33",
3522 bits: "184", 3527 bits: "165",
3523 bitsPerEvent: "5.59", 3528 bitsPerEvent: "4.46",
3524 strength: 'less than a second - Repeats like "abcabcabc" are only slightly harder to guess than "abc"', 3529 strength: 'less than a second - Repeats like "abcabcabc" are only slightly harder to guess than "abc"',
3525 } 3530 }
3526 ); 3531 );
@@ -3571,10 +3576,11 @@ it('Converts very long entropy to very long mnemonics', function(done) {
3571// https://bip32jp.github.io/english/index.html 3576// https://bip32jp.github.io/english/index.html
3572// NOTES: 3577// NOTES:
3573// Is incompatible with: 3578// Is incompatible with:
3579// base 6
3574// base 20 3580// base 20
3575it('Is compatible with bip32jp.github.io', function(done) { 3581it('Is compatible with bip32jp.github.io', function(done) {
3576 var entropy = "543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543210543"; 3582 var entropy = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
3577 var expectedPhrase = "train then jungle barely whip fiber purpose puppy eagle cloud clump hospital robot brave balcony utility detect estate old green desk skill multiply virus"; 3583 var expectedPhrase = "primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary fetch primary foster";
3578 driver.findElement(By.css('.use-entropy')) 3584 driver.findElement(By.css('.use-entropy'))
3579 .click(); 3585 .click();
3580 driver.findElement(By.css('.entropy')) 3586 driver.findElement(By.css('.entropy'))