-
Notifications
You must be signed in to change notification settings - Fork 24
/
combine.js
161 lines (134 loc) · 3.72 KB
/
combine.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { BIN_ENCODING, BIT_SIZE, MAX_SHARES } from './constants.js'
import { Buffer, isBufferLike } from './buffer.js'
import { logs, exps } from './table.js'
import codec from './codec.js'
export const MAX_BITS = BIT_SIZE - 1
/**
* @typedef {{ id?: number, bits?: number, data?: string }} ShareData
* @typedef {import('./buffer.js').BufferLike} BufferLike
*/
export class Share {
id = 0
bits = 0
data = ''
/**
* Creates a `Share` object from a variety of input
* @param {ShareData|number|string} [id]
* @param {?number} [bits = 0]
* @param {?string} [data = null]
*/
static from (id = 0, bits = 0, data = null) {
if (id !== null && typeof id === 'object') {
const share = /** @type {ShareData} */ (id)
// @ts-ignore
return new this(share.id, share.bits || bits, share.data || data)
}
return new this(id || 0, bits || 0, data)
}
/**
* `Share` class constructor.
* @param {string|number} [id]
* @param {string|number} [bits]
* @param {?string} [data]
*/
constructor (id = 0, bits = 0, data = null) {
this.id = typeof id === 'number' ? id : parseInt(id, 16)
this.bits = typeof bits === 'number' ? bits : parseInt(bits, 36)
this.data = typeof data === 'string' ? data : ''
}
}
/**
* Parsed `input` string or buffer into a `Share` object.
* @param {string|BufferLike} input
* @return {Share}
*/
export function parse (input) {
const share = new Share()
const string = isBufferLike(input)
? Buffer.from(input).toString('hex')
: input
const normalized = string[0] === '0'
? string.slice(1)
: string
// bit count is in base36
share.bits = parseInt(normalized.slice(0, 1), 36)
const idLength = MAX_BITS.toString(16).length
const regex = `^([a-kA-K3-9]{1})([a-fA-F0-9]{${idLength}})([a-fA-F0-9]+)$`
const matches = new RegExp(regex).exec(normalized)
if (matches && matches.length) {
share.id = parseInt(matches[2], 16)
share.data = matches[3]
}
return share
}
/**
* Computes the lagrange interoplation aof `x` for node points `p`
* @param {number} x
* @param {number[][]} p
* @return {number}
*/
export function lagrange (x, p) {
const n = MAX_SHARES
let product = 0
let sum = 0
for (let i = 0; i < p[0].length; ++i) {
if (p[1][i]) {
product = logs[p[1][i]]
for (let j = 0; j < p[0].length; ++j) {
// m != j
if (i !== j) {
if (x === p[0][j]) {
product = -1
break
}
const a = logs[x ^ p[0][j]] - logs[p[0][i] ^ p[0][j]]
product = (product + a + n) % n
}
}
sum = -1 === sum ? sum : sum ^ exps[product]
}
}
return sum
}
/**
* Reconstruct a secret from a distinct set of shares.
* @public
* @param {string[]|BufferLike[]} shares
* @return {Buffer}
*/
export function combine (shares) {
const chunks = []
const x = []
const y = []
const t = shares.length
for (let i = 0; i < t; ++i) {
const share = parse(shares[i])
// @ts-ignore
if (x.indexOf(share.id) === -1) {
// @ts-ignore
x.push(share.id)
const bin = codec.bin(share.data, 16)
const parts = codec.split(bin, 0, 2)
for (let j = 0; j < parts.length; ++j) {
if (!y[j]) {
// @ts-ignore
y[j] = []
}
// @ts-ignore
y[j][x.length - 1] = parts[j]
}
}
}
for (let i = 0; i < y.length; ++i) {
const p = lagrange(0, [x, y[i]])
const padded = codec.pad(p.toString(2))
// @ts-ignore
chunks.unshift(padded)
}
const string = chunks.join('')
const bin = string.slice(1 + string.indexOf('1')) // >= 0
const hex = codec.hex(bin, BIN_ENCODING)
const value = codec.decode(hex)
return Buffer.from(value)
}
export default combine