Commit d420293
committed
bitref: bit-level reference types
Adds a crate providing a `&BitSlice`/`&mut BitSlice` type which is
constructable from `&[u8]` but provides slicing at the granularity of
individual bits. The name of the crate is a play on `bitvec`, which
provides a similar type. However, the implementation in this crate is
significantly simpler with a much smaller code surface and minimal use
of `unsafe` code.
The implementation is a generalization of RustCrypto/formats#2300 which
sought to implement a similar data structure as a reference type for
representing ASN.1 BIT STRINGs. However, using this approach was
deferred because the implementation relies on a conversion which is
sound under Tree Borrows (as verified by Miri) but unsound under Stacked
Borrows as it loses provenance. See rust-lang/unsafe-code-guidelines#134
There are several places such a data structure is potentially useful for
RustCrypto projects. Beyond the previously mentioned ASN.1 BIT STRING
use case, being able to iterate over bits is useful in many numerical
algorithms with applications in cryptography, notably in `crypto-bigint`
and for elliptic curves.
Elliptic curve scalar multiplication is generally implemented as a loop
over the bits of a scalar. Having an iterator type for this purpose
avoids problems relating to the endianness of how scalars are serialized
when implementing generic scalar multiplication algorithms, e.g. wNAF
(see RustCrypto/group#12).
Given the current open soundness story, I'm not rushing to use this in
`crypto-bigint` until that changes. Where we could use it today though
is as an optional dependency to `der`, where it can act as an ASN.1
BIT STRING type, but implement `ToOwned` producing a
`der::asn1::BitString` (which, to make `ToOwned` work, needs to impl
`Borrow<BitSlice>`).
This would make it optionally possible to use `Cow` for copy-on-write
BIT STRINGs today with `BitSlice` as the borrowed form, but leaving the
preferred default data structure for that purpose as
`der::asn1::BitStringRef`, which is a lifetime-parameterized struct
that avoids the open soundness questions around `BitSlice`.
From there we can see what develops around the soundness story and SB/TB
discrepancy, and beyond that new Rust features like custom DSTs which
may make expressing structures like this less of a hack.1 parent 66cb272 commit d420293
13 files changed
Lines changed: 1813 additions & 0 deletions
File tree
- .github/workflows
- bitref
- src
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments