forked from sujeet/Yess-Key
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
114 lines (85 loc) · 3.18 KB
/
test.js
File metadata and controls
114 lines (85 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/********* External Imports and Convenience Functions ********/
"use strict"; // Makes it easier to catch errors.
var sjcl = require("./lib/sjcl");
var hash = sjcl.hash.sha256.hash; // Hashes a string or bitArray to a bitArray.
var is_equal = sjcl.bitArray.equal; // Compares two bitArrays.
var hex = sjcl.codec.hex.fromBits; // Converts a bitArray to a hex string.
var pow2 = Math.pow.bind(this, 2); // Calculates 2 to a given power.
var log2 = function(x) {return Math.log(x) / Math.log(2);} // Calculates log base 2.
var skey = require("./skey");
/******** Run the hash chain. ********/
function verifyBitArray(arr) {
if (!(arr instanceof Array)) {
console.trace("Expected a bitArray; got something else: " + arr);
throw "Halting tests."
}
}
function test_chain(constructor, seed, num_iterations) {
var initial_ = seed;
for (var i = 0; i < num_iterations + 1; i++) {
initial_ = hash(initial_);
}
var chain = constructor();
var initial = chain.initialize(num_iterations, seed);
verifyBitArray(initial);
if (initial instanceof Array) {
console.log("[SUCCESS] Initialized successfully.");
} else {
console.log("[FAILED] Initial value should be a bitArray.");
}
// Test initial value;
if (is_equal(initial, initial_)) {
console.log("[SUCCESS] Initial value is correct.");
} else {
console.log("[FAILED] Initial should be " + hex(initial_) + ", but it is " + hex(initial) + ".");
}
// Test the entire chain.
var vk = initial;
for (var i = 0; i < num_iterations; i++) {
var current = chain.advance();
verifyBitArray(current);
if (! is_equal(hash(current), vk)) {
console.log("[FAILED] Value #" + i + " is incorrect. Halting tests.");
return;
}
var vk = current;
}
// console.log (chain.hash_count / num_iterations);
console.log("[SUCCESS] All values in the chain were correct.")
// Check that the chain ends.
var after_last = chain.advance();
if (after_last === null) {
console.log("[SUCCESS] Chain ends correctly.");
} else {
console.log("[FAILED] After the hash chain ended, advance() did not output null.");
}
// Test saving.
var chain = constructor();
var initial = chain.initialize(num_iterations, seed);
for (var i = 1; i < num_iterations/3; i++) {
chain.advance();
}
var previous = chain.advance();
var saved = chain.save();
if (typeof saved == "string") {
console.log("[SUCCESS] Saved successfully.");
} else {
console.log("[FAILED] Saving state did not produce a string.");
}
// Test loading.
var new_chain = constructor();
new_chain.load(saved);
new_chain.advance();
var next = new_chain.advance();
verifyBitArray(next);
if (is_equal(previous, hash(hash(next)))) {
console.log("[SUCCESS] Loaded from saved state successfully.");
} else {
console.log("[FAILED] Loading from saved state failed.");
}
}
var num_iterations = pow2(4);
console.log("-------- Testing Naive Algorithm --------");
test_chain(skey.naive_chain, "The rain in spain stays mainly in the plain.", num_iterations);
console.log("-------- Testing Pebble Algorithm --------");
test_chain(skey.pebble_chain, "The rain in spain stays mainly in the plain.", num_iterations);