blake3-rb 1.5.4 → 1.5.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d196553bbe63c296ef3dcd17738501590da9cf197778733c21ec5688d5acc57
4
- data.tar.gz: 8d004ab983f3702350de0d1eaca42d4e631ba7b1299618f269e60c2ab882c4e7
3
+ metadata.gz: 79bfa02f801439d4336e9cc5d1c6f66b3fea5ee900fa327c98a76006289610cb
4
+ data.tar.gz: a84aff893fc9fb311103464071c2628fe398feb370f8afb2092c8a72663fd473
5
5
  SHA512:
6
- metadata.gz: 8240caba3da4b4faba471d7b6b492ae1c2ed5531965d0ccb78927836b486a4e967122ae4ec6d546a28d3045ef073b9da3e4cfb3578a6f636a7c4d9f28e213349
7
- data.tar.gz: 606296cd47c0675001f651f00e40dba5014521991df308d7e33d8ddff29e0f01b9af7fd549e55b9b4b5858bcf6d5c090b610e764b0d79cb4558ce43f93f69e86
6
+ metadata.gz: f55aad4696d2c13f956472e3193c68501fef7f1a698d86757bb251d533c6a44d2799d24842053e49ddab0f2ca01462d34583daf8fd9a61513a0341ac0bfde95c
7
+ data.tar.gz: 06ffcdbc95270b3ae7b3b7a9ff242cd5cb9388aef94d8e7fcfc669d24e357d2913b09f1566eb24785c55e39044fa971c342eb97dfee343f16bc8161e13434e06
data/Cargo.lock CHANGED
@@ -4,9 +4,9 @@ version = 3
4
4
 
5
5
  [[package]]
6
6
  name = "aho-corasick"
7
- version = "1.1.2"
7
+ version = "1.1.3"
8
8
  source = "registry+https://github.com/rust-lang/crates.io-index"
9
- checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
9
+ checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
10
10
  dependencies = [
11
11
  "memchr",
12
12
  ]
@@ -45,15 +45,15 @@ dependencies = [
45
45
 
46
46
  [[package]]
47
47
  name = "bitflags"
48
- version = "2.4.2"
48
+ version = "2.5.0"
49
49
  source = "registry+https://github.com/rust-lang/crates.io-index"
50
- checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
50
+ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
51
51
 
52
52
  [[package]]
53
53
  name = "blake3"
54
- version = "1.5.0"
54
+ version = "1.5.1"
55
55
  source = "registry+https://github.com/rust-lang/crates.io-index"
56
- checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87"
56
+ checksum = "30cca6d3674597c30ddf2c587bf8d9d65c9a84d2326d941cc79c9842dfe0ef52"
57
57
  dependencies = [
58
58
  "arrayref",
59
59
  "arrayvec",
@@ -67,17 +67,18 @@ name = "blake3_ext"
67
67
  version = "0.1.0"
68
68
  dependencies = [
69
69
  "blake3",
70
+ "hex",
71
+ "rand",
70
72
  "rb-sys",
73
+ "rb-sys-env",
74
+ "rb-sys-test-helpers",
71
75
  ]
72
76
 
73
77
  [[package]]
74
78
  name = "cc"
75
- version = "1.0.83"
79
+ version = "1.0.97"
76
80
  source = "registry+https://github.com/rust-lang/crates.io-index"
77
- checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
78
- dependencies = [
79
- "libc",
80
- ]
81
+ checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4"
81
82
 
82
83
  [[package]]
83
84
  name = "cexpr"
@@ -113,9 +114,20 @@ checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
113
114
 
114
115
  [[package]]
115
116
  name = "either"
116
- version = "1.10.0"
117
+ version = "1.11.0"
117
118
  source = "registry+https://github.com/rust-lang/crates.io-index"
118
- checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a"
119
+ checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2"
120
+
121
+ [[package]]
122
+ name = "getrandom"
123
+ version = "0.2.15"
124
+ source = "registry+https://github.com/rust-lang/crates.io-index"
125
+ checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
126
+ dependencies = [
127
+ "cfg-if",
128
+ "libc",
129
+ "wasi",
130
+ ]
119
131
 
120
132
  [[package]]
121
133
  name = "glob"
@@ -123,6 +135,12 @@ version = "0.3.1"
123
135
  source = "registry+https://github.com/rust-lang/crates.io-index"
124
136
  checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
125
137
 
138
+ [[package]]
139
+ name = "hex"
140
+ version = "0.4.3"
141
+ source = "registry+https://github.com/rust-lang/crates.io-index"
142
+ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
143
+
126
144
  [[package]]
127
145
  name = "itertools"
128
146
  version = "0.12.1"
@@ -146,25 +164,25 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
146
164
 
147
165
  [[package]]
148
166
  name = "libc"
149
- version = "0.2.153"
167
+ version = "0.2.154"
150
168
  source = "registry+https://github.com/rust-lang/crates.io-index"
151
- checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
169
+ checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
152
170
 
153
171
  [[package]]
154
172
  name = "libloading"
155
- version = "0.8.1"
173
+ version = "0.8.3"
156
174
  source = "registry+https://github.com/rust-lang/crates.io-index"
157
- checksum = "c571b676ddfc9a8c12f1f3d3085a7b163966a8fd8098a90640953ce5f6170161"
175
+ checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
158
176
  dependencies = [
159
177
  "cfg-if",
160
- "windows-sys",
178
+ "windows-targets",
161
179
  ]
162
180
 
163
181
  [[package]]
164
182
  name = "memchr"
165
- version = "2.7.1"
183
+ version = "2.7.2"
166
184
  source = "registry+https://github.com/rust-lang/crates.io-index"
167
- checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
185
+ checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
168
186
 
169
187
  [[package]]
170
188
  name = "minimal-lexical"
@@ -182,38 +200,74 @@ dependencies = [
182
200
  "minimal-lexical",
183
201
  ]
184
202
 
203
+ [[package]]
204
+ name = "ppv-lite86"
205
+ version = "0.2.17"
206
+ source = "registry+https://github.com/rust-lang/crates.io-index"
207
+ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
208
+
185
209
  [[package]]
186
210
  name = "proc-macro2"
187
- version = "1.0.78"
211
+ version = "1.0.82"
188
212
  source = "registry+https://github.com/rust-lang/crates.io-index"
189
- checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
213
+ checksum = "8ad3d49ab951a01fbaafe34f2ec74122942fe18a3f9814c3268f1bb72042131b"
190
214
  dependencies = [
191
215
  "unicode-ident",
192
216
  ]
193
217
 
194
218
  [[package]]
195
219
  name = "quote"
196
- version = "1.0.35"
220
+ version = "1.0.36"
197
221
  source = "registry+https://github.com/rust-lang/crates.io-index"
198
- checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
222
+ checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
199
223
  dependencies = [
200
224
  "proc-macro2",
201
225
  ]
202
226
 
227
+ [[package]]
228
+ name = "rand"
229
+ version = "0.8.5"
230
+ source = "registry+https://github.com/rust-lang/crates.io-index"
231
+ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
232
+ dependencies = [
233
+ "libc",
234
+ "rand_chacha",
235
+ "rand_core",
236
+ ]
237
+
238
+ [[package]]
239
+ name = "rand_chacha"
240
+ version = "0.3.1"
241
+ source = "registry+https://github.com/rust-lang/crates.io-index"
242
+ checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
243
+ dependencies = [
244
+ "ppv-lite86",
245
+ "rand_core",
246
+ ]
247
+
248
+ [[package]]
249
+ name = "rand_core"
250
+ version = "0.6.4"
251
+ source = "registry+https://github.com/rust-lang/crates.io-index"
252
+ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
253
+ dependencies = [
254
+ "getrandom",
255
+ ]
256
+
203
257
  [[package]]
204
258
  name = "rb-sys"
205
- version = "0.9.88"
259
+ version = "0.9.97"
206
260
  source = "registry+https://github.com/rust-lang/crates.io-index"
207
- checksum = "d11b0965e6df9ef800ac71efc46070960a119f92af1b64d6acd4c357dc139350"
261
+ checksum = "47d30bcad206b51f2f66121190ca678dce1fdf3a2eae0ac5d838d1818b19bdf5"
208
262
  dependencies = [
209
263
  "rb-sys-build",
210
264
  ]
211
265
 
212
266
  [[package]]
213
267
  name = "rb-sys-build"
214
- version = "0.9.88"
268
+ version = "0.9.97"
215
269
  source = "registry+https://github.com/rust-lang/crates.io-index"
216
- checksum = "5c0fce6e53552df8fcd3e4269d7944b0afa38db70d252bbf12caad454caf6d83"
270
+ checksum = "3cbd92f281615f3c2dcb9dcb0f0576624752afbf9a7f99173b37c4b55b62dd8a"
217
271
  dependencies = [
218
272
  "bindgen",
219
273
  "lazy_static",
@@ -224,11 +278,38 @@ dependencies = [
224
278
  "syn",
225
279
  ]
226
280
 
281
+ [[package]]
282
+ name = "rb-sys-env"
283
+ version = "0.1.2"
284
+ source = "registry+https://github.com/rust-lang/crates.io-index"
285
+ checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
286
+
287
+ [[package]]
288
+ name = "rb-sys-test-helpers"
289
+ version = "0.2.0"
290
+ source = "registry+https://github.com/rust-lang/crates.io-index"
291
+ checksum = "bbd592029353df7955a76ca0b1bf0d9ccc57feab96615f80d1fe756462864949"
292
+ dependencies = [
293
+ "rb-sys",
294
+ "rb-sys-env",
295
+ "rb-sys-test-helpers-macros",
296
+ ]
297
+
298
+ [[package]]
299
+ name = "rb-sys-test-helpers-macros"
300
+ version = "0.2.0"
301
+ source = "registry+https://github.com/rust-lang/crates.io-index"
302
+ checksum = "c21f156459adb755d58f73dbd783dc1de8b403635e637f7d1daec1c7a920c1f5"
303
+ dependencies = [
304
+ "quote",
305
+ "syn",
306
+ ]
307
+
227
308
  [[package]]
228
309
  name = "regex"
229
- version = "1.10.3"
310
+ version = "1.10.4"
230
311
  source = "registry+https://github.com/rust-lang/crates.io-index"
231
- checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
312
+ checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
232
313
  dependencies = [
233
314
  "aho-corasick",
234
315
  "memchr",
@@ -238,9 +319,9 @@ dependencies = [
238
319
 
239
320
  [[package]]
240
321
  name = "regex-automata"
241
- version = "0.4.5"
322
+ version = "0.4.6"
242
323
  source = "registry+https://github.com/rust-lang/crates.io-index"
243
- checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
324
+ checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
244
325
  dependencies = [
245
326
  "aho-corasick",
246
327
  "memchr",
@@ -249,9 +330,9 @@ dependencies = [
249
330
 
250
331
  [[package]]
251
332
  name = "regex-syntax"
252
- version = "0.8.2"
333
+ version = "0.8.3"
253
334
  source = "registry+https://github.com/rust-lang/crates.io-index"
254
- checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
335
+ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
255
336
 
256
337
  [[package]]
257
338
  name = "rustc-hash"
@@ -273,9 +354,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
273
354
 
274
355
  [[package]]
275
356
  name = "syn"
276
- version = "2.0.48"
357
+ version = "2.0.64"
277
358
  source = "registry+https://github.com/rust-lang/crates.io-index"
278
- checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
359
+ checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f"
279
360
  dependencies = [
280
361
  "proc-macro2",
281
362
  "quote",
@@ -289,23 +370,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
289
370
  checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
290
371
 
291
372
  [[package]]
292
- name = "windows-sys"
293
- version = "0.48.0"
373
+ name = "wasi"
374
+ version = "0.11.0+wasi-snapshot-preview1"
294
375
  source = "registry+https://github.com/rust-lang/crates.io-index"
295
- checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
296
- dependencies = [
297
- "windows-targets",
298
- ]
376
+ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
299
377
 
300
378
  [[package]]
301
379
  name = "windows-targets"
302
- version = "0.48.5"
380
+ version = "0.52.5"
303
381
  source = "registry+https://github.com/rust-lang/crates.io-index"
304
- checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
382
+ checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
305
383
  dependencies = [
306
384
  "windows_aarch64_gnullvm",
307
385
  "windows_aarch64_msvc",
308
386
  "windows_i686_gnu",
387
+ "windows_i686_gnullvm",
309
388
  "windows_i686_msvc",
310
389
  "windows_x86_64_gnu",
311
390
  "windows_x86_64_gnullvm",
@@ -314,42 +393,48 @@ dependencies = [
314
393
 
315
394
  [[package]]
316
395
  name = "windows_aarch64_gnullvm"
317
- version = "0.48.5"
396
+ version = "0.52.5"
318
397
  source = "registry+https://github.com/rust-lang/crates.io-index"
319
- checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
398
+ checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
320
399
 
321
400
  [[package]]
322
401
  name = "windows_aarch64_msvc"
323
- version = "0.48.5"
402
+ version = "0.52.5"
324
403
  source = "registry+https://github.com/rust-lang/crates.io-index"
325
- checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
404
+ checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
326
405
 
327
406
  [[package]]
328
407
  name = "windows_i686_gnu"
329
- version = "0.48.5"
408
+ version = "0.52.5"
409
+ source = "registry+https://github.com/rust-lang/crates.io-index"
410
+ checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
411
+
412
+ [[package]]
413
+ name = "windows_i686_gnullvm"
414
+ version = "0.52.5"
330
415
  source = "registry+https://github.com/rust-lang/crates.io-index"
331
- checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
416
+ checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
332
417
 
333
418
  [[package]]
334
419
  name = "windows_i686_msvc"
335
- version = "0.48.5"
420
+ version = "0.52.5"
336
421
  source = "registry+https://github.com/rust-lang/crates.io-index"
337
- checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
422
+ checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
338
423
 
339
424
  [[package]]
340
425
  name = "windows_x86_64_gnu"
341
- version = "0.48.5"
426
+ version = "0.52.5"
342
427
  source = "registry+https://github.com/rust-lang/crates.io-index"
343
- checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
428
+ checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
344
429
 
345
430
  [[package]]
346
431
  name = "windows_x86_64_gnullvm"
347
- version = "0.48.5"
432
+ version = "0.52.5"
348
433
  source = "registry+https://github.com/rust-lang/crates.io-index"
349
- checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
434
+ checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
350
435
 
351
436
  [[package]]
352
437
  name = "windows_x86_64_msvc"
353
- version = "0.48.5"
438
+ version = "0.52.5"
354
439
  source = "registry+https://github.com/rust-lang/crates.io-index"
355
- checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
440
+ checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
data/Cargo.toml CHANGED
@@ -10,3 +10,4 @@ resolver = "2"
10
10
  lto = true
11
11
  opt-level = 3
12
12
  codegen-units = 1
13
+ debug = true
@@ -10,5 +10,13 @@ publish = false
10
10
  crate-type = ["cdylib"]
11
11
 
12
12
  [dependencies]
13
- rb-sys = { version = "0.9.88", features = ["stable-api-compiled-fallback"] }
14
- blake3 = { version = "1.5.0" }
13
+ rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] }
14
+ blake3 = { version = "1.5" }
15
+
16
+ [dev-dependencies]
17
+ hex = "0.4.3"
18
+ rand = "0.8.5"
19
+ rb-sys-test-helpers = "0.2.0"
20
+
21
+ [build-dependencies]
22
+ rb-sys-env = "0.1.2"
@@ -0,0 +1,4 @@
1
+ fn main() -> Result<(), Box<dyn std::error::Error>> {
2
+ rb_sys_env::activate()?;
3
+ Ok(())
4
+ }
@@ -5,57 +5,61 @@ use rb_sys::{
5
5
  rb_cObject, rb_const_get, rb_define_class_under, rb_intern, rb_ivar_set, rb_require, size_t,
6
6
  };
7
7
  use std::ffi::{c_int, c_uchar, c_void};
8
+ use std::mem::MaybeUninit;
8
9
  use std::os::raw::c_char;
9
10
 
10
11
  use bindings::{rb_digest_make_metadata, RbDigestMetadataT, RUBY_DIGEST_API_VERSION};
11
12
 
12
- const BLOCK_LEN: usize = 64;
13
- const DIGEST_LEN: usize = 32;
14
-
15
13
  #[repr(C)]
16
- #[derive(Debug)]
17
- struct Blake3Ctx {
18
- inner: Option<Hasher>,
14
+ #[derive(Debug, Default)]
15
+ struct Blake3 {
16
+ hasher: Hasher,
19
17
  }
20
18
 
21
- static DIGEST_METADATA: RbDigestMetadataT = RbDigestMetadataT {
22
- api_version: RUBY_DIGEST_API_VERSION,
23
- digest_len: DIGEST_LEN as _,
24
- block_len: BLOCK_LEN as _,
25
- ctx_size: std::mem::size_of::<Blake3Ctx>() as _,
26
- init_func: blake3_init,
27
- update_func: blake3_update,
28
- finish_func: blake3_finish,
29
- };
19
+ impl Blake3 {
20
+ const BLOCK_LEN: usize = 64;
21
+ const DIGEST_LEN: usize = 32;
30
22
 
31
- // Initialize the context, which has already been allocated by Ruby.
32
- extern "C" fn blake3_init(ctx: *mut c_void) -> c_int {
33
- let ctx = ctx as *mut Blake3Ctx;
34
- let ctx = unsafe { &mut *ctx };
35
- ctx.inner = Some(Hasher::new());
36
- 1
37
- }
23
+ fn digest_metadata() -> &'static RbDigestMetadataT {
24
+ static DIGEST_METADATA: RbDigestMetadataT = RbDigestMetadataT {
25
+ api_version: RUBY_DIGEST_API_VERSION,
26
+ digest_len: Blake3::DIGEST_LEN as _,
27
+ block_len: Blake3::BLOCK_LEN as _,
28
+ ctx_size: std::mem::size_of::<Blake3>() as _,
29
+ init_func: Blake3::init_in_place,
30
+ update_func: Blake3::update,
31
+ finish_func: Blake3::finish,
32
+ };
33
+ &DIGEST_METADATA
34
+ }
38
35
 
39
- // Update the context with the given data.
40
- extern "C" fn blake3_update(ctx: *mut c_void, data: *mut c_uchar, len: size_t) {
41
- let ctx = ctx as *mut Blake3Ctx;
42
- let ctx = unsafe { &mut *ctx };
43
- if let Some(inner) = ctx.inner.as_mut() {
36
+ // Initialize the context, which has already been allocated by Ruby.
37
+ extern "C" fn init_in_place(ctx: *mut c_void) -> c_int {
38
+ let ctx = ctx as *mut MaybeUninit<Blake3>;
39
+ let ctx = unsafe { &mut *ctx };
40
+ ctx.write(Blake3::default());
41
+ true as _
42
+ }
43
+
44
+ // Update the context with the given data.
45
+ extern "C" fn update(ctx: *mut c_void, data: *mut c_uchar, len: size_t) {
46
+ let ctx = ctx as *mut MaybeUninit<Blake3>;
47
+ let ctx = unsafe { &mut *ctx };
48
+ let ctx = unsafe { ctx.assume_init_mut() };
44
49
  let slice = unsafe { std::slice::from_raw_parts(data, len as _) };
45
- inner.update(slice);
50
+
51
+ ctx.hasher.update(slice);
46
52
  }
47
- }
48
53
 
49
- // Finalize the context and write the digest to the given pointer.
50
- extern "C" fn blake3_finish(ctx: *mut c_void, digest: *mut c_uchar) -> c_int {
51
- let ctx = ctx as *mut Blake3Ctx;
52
- let ctx = unsafe { &mut *ctx };
53
- if let Some(inner) = ctx.inner.as_mut() {
54
- let slice = unsafe { std::slice::from_raw_parts_mut(digest, DIGEST_LEN) };
55
- inner.finalize_xof().fill(slice);
56
- 1
57
- } else {
58
- 0
54
+ // Finalize the context and write the digest to the given pointer. The
55
+ // memory for the digest is managed by Ruby, so we don't need to free it.
56
+ extern "C" fn finish(ctx: *mut c_void, digest: *mut c_uchar) -> c_int {
57
+ let ctx = ctx as *mut MaybeUninit<Blake3>;
58
+ let ctx = unsafe { &mut *ctx };
59
+ let ctx = unsafe { ctx.assume_init_mut() };
60
+ let outbuf = unsafe { std::slice::from_raw_parts_mut(digest, Self::DIGEST_LEN) };
61
+ ctx.hasher.finalize_xof().fill(outbuf);
62
+ true as _
59
63
  }
60
64
  }
61
65
 
@@ -71,7 +75,101 @@ pub unsafe extern "C" fn Init_blake3_ext() {
71
75
  "Blake3\0".as_ptr() as *const c_char,
72
76
  digest_base,
73
77
  );
74
- let meta = rb_digest_make_metadata(&DIGEST_METADATA);
78
+ let meta = rb_digest_make_metadata(Blake3::digest_metadata());
75
79
  let metadata_id = rb_intern("metadata\0".as_ptr() as *const c_char);
76
80
  rb_ivar_set(klass, metadata_id, meta);
77
81
  }
82
+
83
+ #[cfg(test)]
84
+ mod tests {
85
+ use rb_sys::{
86
+ rb_cObject, rb_const_get, rb_enc_set_index, rb_funcallv_public, rb_intern, rb_str_new,
87
+ rb_utf8_encindex, RSTRING_LEN, RSTRING_PTR,
88
+ };
89
+ use rb_sys_test_helpers::{protect, ruby_test};
90
+
91
+ use crate::Init_blake3_ext;
92
+
93
+ #[ruby_test]
94
+ fn fuzz_parity_binary() {
95
+ setup();
96
+
97
+ for _ in 0..1024 {
98
+ let input_data = gen_random_bytes(4096);
99
+ let expected = compute_rust_digest(input_data.as_slice());
100
+ let actual = compute_ruby_digest(input_data.as_slice(), rb_utf8_encindex);
101
+
102
+ assert_eq!(expected, actual);
103
+ }
104
+ }
105
+
106
+ #[ruby_test]
107
+ fn fuzz_parity_utf8() {
108
+ setup();
109
+
110
+ for _ in 0..1024 {
111
+ let input_data = gen_random_string(512);
112
+ let expected = compute_rust_digest(&input_data);
113
+ let actual = compute_ruby_digest(&input_data, rb_utf8_encindex);
114
+
115
+ assert_eq!(expected, actual);
116
+ }
117
+ }
118
+
119
+ fn setup() {
120
+ static INIT: std::sync::Once = std::sync::Once::new();
121
+ INIT.call_once(|| {
122
+ protect(|| unsafe { Init_blake3_ext() })
123
+ .expect("Failed to initialize Blake3 extension");
124
+ });
125
+ }
126
+
127
+ fn gen_random_bytes(max_len: usize) -> Vec<u8> {
128
+ let size = rand::random::<usize>() % max_len;
129
+ (0..size).map(|_| rand::random::<u8>()).collect()
130
+ }
131
+
132
+ fn gen_random_string(max_len: usize) -> String {
133
+ let size = rand::random::<usize>() % max_len;
134
+ (0..size)
135
+ .map(|_| rand::random::<char>())
136
+ .collect::<String>()
137
+ }
138
+
139
+ fn compute_rust_digest<T: AsRef<[u8]>>(input: T) -> String {
140
+ let mut hasher = blake3::Hasher::new();
141
+ hasher.update(input.as_ref());
142
+ let mut result = [0u8; 32];
143
+ hasher.finalize_xof().fill(&mut result);
144
+ hex::encode(result)
145
+ }
146
+
147
+ fn compute_ruby_digest<T: AsRef<[u8]>>(
148
+ input: T,
149
+ encoding: unsafe extern "C" fn() -> i32,
150
+ ) -> String {
151
+ let input = input.as_ref();
152
+ let ruby_digest = protect(|| unsafe {
153
+ Init_blake3_ext();
154
+ let klass = rb_const_get(rb_cObject, rb_intern("Digest\0".as_ptr() as _));
155
+ let klass = rb_const_get(klass, rb_intern("Blake3\0".as_ptr() as _));
156
+ let string = rb_str_new(input.as_ptr() as _, input.len() as _);
157
+ rb_enc_set_index(string, encoding());
158
+ let mut args = [string];
159
+ rb_funcallv_public(
160
+ klass,
161
+ rb_intern("hexdigest\0".as_ptr() as _),
162
+ args.len() as _,
163
+ args.as_mut_ptr(),
164
+ )
165
+ })
166
+ .unwrap();
167
+
168
+ let rstring_ptr = unsafe { RSTRING_PTR(ruby_digest) };
169
+ let rstring_len = unsafe { RSTRING_LEN(ruby_digest) };
170
+
171
+ let bytes =
172
+ unsafe { std::slice::from_raw_parts(rstring_ptr as *const u8, rstring_len as _) };
173
+ std::str::from_utf8(bytes).unwrap().to_string()
174
+ }
175
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blake3-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.4
4
+ version: 1.5.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-14 00:00:00.000000000 Z
11
+ date: 2024-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb_sys
@@ -36,6 +36,7 @@ files:
36
36
  - LICENSE.txt
37
37
  - README.md
38
38
  - ext/digest/blake3_ext/Cargo.toml
39
+ - ext/digest/blake3_ext/build.rs
39
40
  - ext/digest/blake3_ext/extconf.rb
40
41
  - ext/digest/blake3_ext/src/bindings.rs
41
42
  - ext/digest/blake3_ext/src/lib.rs