forked from dapplink-labs/dapplink-secret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.js.d.ts
178 lines (151 loc) · 5 KB
/
secrets.js.d.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
interface SecretsConfig {
radix: number
bits: number
maxShares: number
hasCSPRNG: boolean
typeCSPRNG: string
}
interface ShareComponents {
bits: number
id: number
data: string
}
type RNGType =
| "nodeCryptoRandomBytes"
| "browserCryptoGetRandomValues"
| "testRandom"
type Shares = Array<string>
interface Share {
x: number
y: number
}
type RNGFunction = (bits: number) => string
/**
* Initialize secrets with default settings.
*/
export function init(bits: number, rngType: string): void
/**
* Get the current config.
*/
export function getConfig(): SecretsConfig
/**
* Evaluates the Lagrange interpolation polynomial at x=`at` for individual
* config.bits-length segments of each share in the `shares` Array. Each
* share is expressed in base `inputRadix`. The output is expressed in base
* `outputRadix'.
*/
export function combine(shares: Shares, at?: number): string
/**
* Given a public share, extract the bits (Integer), share ID (Integer),
* and share data (Hex) and return an Object containing those components.
*/
export function extractShareComponents(share: string): ShareComponents
/**
* Set the PRNG to use. If no RNG function is supplied, pick a default using getRNG()
* @param rng One of the acceptable RNG types.
*/
export function setRNG(rng?: RNGType): void
/**
* Converts a given UTF16 character string to the HEX representation.
* Each character of the input string is represented by
* `bytesPerChar` bytes in the output string which defaults to 2.
*/
export function str2hex(str: string, bytesPerChar?: number): string
/**
* Converts a given HEX number string to a UTF16 character string.
* Each character of the output string is represented by
* `bytesPerChar` bytes in the input string which defaults to 2.
*/
export function hex2str(str: string, bytesPerChar?: number): string
/**
* Generates a random bits-length number string using the PRNG
*/
export function random(bits: number): string
/**
* Divides a `secret` number String str expressed in radix `inputRadix` (optional, default 16)
* into `numShares` shares, each expressed in radix `outputRadix` (optional, default to `inputRadix`),
* requiring `threshold` number of shares to reconstruct the secret.
* Optionally, zero-pads the secret to a length that is a multiple of padLength before sharing.
*/
export function share(
secret: string,
numShares: number,
threshold: number,
padLength?: number
): Shares
/**
* Generate a new share with id `id` (a number between 1 and 2^bits-1)
* `id` can be a Number or a String in the default radix (16)
*/
export function newShare(id: number, shares: Shares): string
export function _reset(): void
/**
* Pads a string `str` with zeros on the left so that its length is a multiple of `bits`
*/
export function _padLeft(str: string, multipleOfBits?: number): string
export function _hex2bin(str: string): string
export function _bin2hex(str: string): string
/**
* Returns a pseudo-random number generator of the form function(bits){}
* which should output a random string of 1's and 0's of length `bits`.
* `type` (Optional) : A string representing the CSPRNG that you want to
* force to be loaded, overriding feature detection. Can be one of:
* "nodeCryptoRandomBytes"
* "browserCryptoGetRandomValues"
*/
export function _getRNG(type?: RNGType): RNGFunction
export function _isSetRNG(): boolean
/**
* Splits a number string `bits`-length segments, after first
* optionally zero-padding it to a length that is a multiple of `padLength.
* Returns array of integers (each less than 2^bits-1), with each element
* representing a `bits`-length segment of the input string from right to left,
* i.e. parts[0] represents the right-most `bits`-length segment of the input string.
*/
export function _splitNumStringToIntArray(
str: string,
padLength?: number
): Array<number>
/**
* Polynomial evaluation at `x` using Horner's Method
* NOTE: fx=fx * x + coeff[i] -> exp(log(fx) + log(x)) + coeff[i],
* so if fx===0, just set fx to coeff[i] because
* using the exp/log form will result in incorrect value
*/
export function _horner(x: number, coeffs: number): number
/**
* Evaluate the Lagrange interpolation polynomial at x = `at`
* using x and y Arrays that are of the same length, with
* corresponding elements constituting points on the polynomial.
*/
export function _lagrange(
at: number,
x: Array<number>,
y: Array<number>
): number
/**
* This is the basic polynomial generation and evaluation function
* for a `config.bits`-length secret (NOT an arbitrary length)
* Note: no error-checking at this stage! If `secret` is NOT
* a NUMBER less than 2^bits-1, the output will be incorrect!
*/
export function _getShares(
secret: number,
numShares: number,
threshold: number
): Array<Share>
export function _constructPublicShareString(
bits: string,
id: string,
data: string
): string
export function xorShare(
str: string,
xor: number,
hex: number
): string
export function inverseXor(
str: string,
xor: number,
hex: number
): string