-
Notifications
You must be signed in to change notification settings - Fork 0
/
luby.js
122 lines (92 loc) · 2.65 KB
/
luby.js
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
115
116
117
118
119
120
121
122
'use strict'
let _ = require('lodash');
let c = 0;
function basicSoliton(n) {
let r = Math.random();
for (let i=0; i < distribution.length; i++) {
if (r <= distribution[i]) return i+1;
}
}
function randomD(n) {
return basicSoliton(n);
/*
c++;
if (c % 5 === 0) {
return 1;
}
return _.random(2, n);
*/
}
let msg = process.argv[2];
let msgLength = msg.length;
let data = msg.split('').map((c,i) => { return {c:c, idx:i}});
let distribution = [];
distribution.push(1 / msgLength);
let start = distribution[0]
for (let i=2; i <= msgLength; i++) {
let v = start + ( 1 / (i*(i-1)) );
distribution.push( v );
start = v;
}
console.log(distribution);
let decodeMsg = new Array(msg.length);
let reserve = [];
function encode(data) {
let sample = _.sampleSize(data, randomD(data.length));
// console.log('encode', sample.length);
let packet = {
data: 0,
size: data.length
};
sample.forEach( s => packet.data ^= s.c.charCodeAt());
packet.indicies = sample.map( s => s.idx);
return packet;
}
function decode(packet) {
// 0) Strip the packet of any already decoded symbols
console.log('raw', packet);
for (let i=0; i < decodeMsg.length; i++) {
if (! decodeMsg[i] || packet.indicies.indexOf(i) === -1) continue;
let c = decodeMsg[i].charCodeAt();
packet.data ^= c;
_.remove(packet.indicies, (d) => d === i);
}
// console.log('stripped', packet);
// 1) Either resolve (length = 1) or put it into reserve for later
if (packet.indicies.length === 1) {
let idx = packet.indicies[0];
decodeMsg[idx] = String.fromCharCode(packet.data);
// console.log('length 1 packet', decodeMsg[idx]);
// console.log('going to resolve');
resolve(packet.data, idx, 1);
} else if (packet.indicies.length > 1) {
reserve.push(packet);
}
// console.log('');
}
function resolve(data, idx, iter) {
reserve.forEach( packet => {
if (packet.indicies.indexOf(idx) !== -1) {
packet.data ^= data;
_.remove(packet.indicies, (d)=> d === idx);
}
});
let rlist = _.remove(reserve, p => p.indicies.length === 1);
rlist.forEach(p => {
let pidx = p.indicies[0];
decodeMsg[pidx] = String.fromCharCode(p.data);
// console.log('\t'.repeat(iter) + 'resolved ', decodeMsg[pidx]);
resolve(p.data, pidx, ++iter);
});
}
let counter = 0;
while (decodeMsg.join('').localeCompare(msg) !== 0 && c < 100) {
let packet = encode(data);
// console.log('receviing', packet, decodeMsg.join(''));
if (reserve.filter( p => p.data === packet.data).length > 0) {
} else {
counter ++;
decode(packet);
}
}
console.log('Took ' + counter + ' packets. Original message lenth ' + msgLength);