-
Notifications
You must be signed in to change notification settings - Fork 1
/
bip39.red
284 lines (264 loc) · 5.4 KB
/
bip39.red
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
Red [
Title: "bip39"
Author: "bitbegin"
File: %bip39.red
Tabs: 4
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
]
#include %pbkdf2.red
urandom: routine [
len [integer!]
/local
data [byte-ptr!]
][
data: allocate len
crypto/urandom data len
stack/set-last as red-value! binary/load data len
free data
]
word-list: context [
BIP39_WORDLIST_ENGLISH: load %bip39-english.txt
word-nums: length? BIP39_WORDLIST_ENGLISH
get-index: func [
word [word!]
return: [integer! none!]
/local f
][
if f: find BIP39_WORDLIST_ENGLISH word [return (index? f) - 1]
none
]
get-word: func [
index [integer!]
return: [word! none!]
][
if any [
index < 0
index >= word-nums
][none]
pick BIP39_WORDLIST_ENGLISH index + 1
]
]
derive-seed: func [
entropy [string!]
password [string!]
return: [binary!]
/local salt
][
salt: copy "mnemonic"
append salt password
pbkdf2/derive entropy salt 2048 64 'SHA512
]
MnemonicType: context [
word-nums: 5
config: [
;type word ebits cbits tbits
Type12Words 12 128 4 132
Type15Words 15 160 5 165
Type18Words 18 192 6 198
Type21Words 21 224 7 231
Type24Words 24 256 8 264
]
for_word_count: func [
size [integer!]
return: [word! none!]
/local i
][
i: 2
loop word-nums [
if config/(i) = size [return config/(i - 1)]
i: i + 5
]
none
]
for_key_size: func [
size [integer!]
return: [word! none!]
/local i
][
i: 3
loop word-nums [
if config/(i) = size [return config/(i - 2)]
i: i + 5
]
none
]
for_total_size: func [
size [integer!]
return: [word! none!]
/local i
][
i: 5
loop word-nums [
if config/(i) = size [return config/(i - 4)]
i: i + 5
]
none
]
total_bits: func [
type [word!]
return: [integer! none!]
/local i
][
i: 1
loop word-nums [
if config/(i) = type [return config/(i + 4)]
i: i + 5
]
none
]
entropy_bits: func [
type [word!]
return: [integer! none!]
/local i
][
i: 1
loop word-nums [
if config/(i) = type [return config/(i + 2)]
i: i + 5
]
none
]
checksum_bits: func [
type [word!]
return: [integer! none!]
/local i
][
i: 1
loop word-nums [
if config/(i) = type [return config/(i + 3)]
i: i + 5
]
none
]
word_count: func [
type [word!]
return: [integer! none!]
/local i
][
i: 1
loop word-nums [
if config/(i) = type [return config/(i + 1)]
i: i + 5
]
none
]
]
string-to-entropy: func [
str [string!]
return: [string!]
][
enbase/base debase/base str 16 2
]
binary-to-entropy: func [
bin [binary!]
return: [string!]
][
enbase/base bin 2
]
entropy-to-string: func [
entropy [string!]
return: [string!]
][
enbase/base debase/base entropy 2 16
]
entropy-to-binary: func [
entropy [string!]
return: [binary!]
][
debase/base entropy 2
]
Mnemonic: context [
entropy-valid?: func [
entropy [string!]
return: [logic!]
/local type ebits cbits raw ehash rhash
][
type: MnemonicType/for_total_size length? entropy
ebits: MnemonicType/entropy_bits type
cbits: MnemonicType/checksum_bits type
raw: entropy-to-binary copy/part entropy ebits
rhash: copy/part skip entropy ebits cbits
ehash: copy/part binary-to-entropy checksum raw 'SHA256 cbits
rhash = ehash
]
get-binary: func [
entropy [string!]
return: [binary!]
/local type ebits
][
if not entropy-valid? entropy [do make error! "invalid entropy!"]
type: MnemonicType/for_total_size length? entropy
ebits: MnemonicType/entropy_bits type
entropy-to-binary copy/part entropy ebits
]
words-to-entropy: func [
blk [block!]
return: [string!]
/local num type ebits cbits entropy elen epos w vl
][
num: length? blk
if none = type: MnemonicType/for_word_count num [
do make error! "invalid type!"
]
ebits: MnemonicType/entropy_bits type
cbits: MnemonicType/checksum_bits type
elen: ebits / 8
entropy: make string! ebits + cbits
foreach w blk [
;bit-access/write-bits entropy epos 11 (word-list/get-index to word! w) - 1
vl: skip binary-to-entropy to binary! word-list/get-index w 21
append entropy vl
]
if not entropy-valid? entropy [do make error! "invalid entropy!"]
entropy
]
from-words: func [
blk [block!]
password [string!]
return: [block!] ;-- [words entropy seed]
/local seed entropy
][
entropy: words-to-entropy blk
seed: derive-seed form blk password
reduce [blk entropy seed]
]
from-entropy: func [
entropy [string!]
password [string!]
return: [block!] ;-- [words entropy seed]
/local type cbits nwords bin ehash blk vl
][
if none = type: MnemonicType/for_key_size length? entropy [
do make error! "invalid type!"
]
cbits: MnemonicType/checksum_bits type
nwords: MnemonicType/word_count type
bin: entropy-to-binary entropy
ehash: binary-to-entropy checksum bin 'SHA256
append/part entropy ehash cbits
blk: make block! nwords
loop nwords [
insert/dup vl: copy/part entropy 11 "0" 5
vl: to integer! entropy-to-binary vl
append blk word-list/get-word vl
entropy: skip entropy 11
]
from-words blk password
]
from-binary: func [
bin [binary!]
password [string!]
return: [block!] ;-- [words entropy seed]
][
from-entropy binary-to-entropy bin password
]
new: func [
type [word!]
password [string!]
return: [block!] ;-- [words entropy seed]
/local elen
][
elen: (MnemonicType/entropy_bits type) / 8
from-binary urandom elen password
]
]