File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -1293,6 +1293,21 @@ sealed abstract class ByteVector
1293
1293
case _ => false
1294
1294
}
1295
1295
1296
+ /** Like [[equals ]] but compares all bytes instead of returning after first non-matching byte.
1297
+ * @group collection
1298
+ */
1299
+ def equalsConstantTime (other : ByteVector ): Boolean =
1300
+ if (this .size != other.size) false
1301
+ else {
1302
+ var result, idx = 0
1303
+ val s = this .size
1304
+ while (idx < s) {
1305
+ result = result | (this (idx) ^ other(idx))
1306
+ idx += 1
1307
+ }
1308
+ result == 0
1309
+ }
1310
+
1296
1311
/** Display the size and bytes of this `ByteVector`. For bit vectors beyond a certain size, only a
1297
1312
* hash of the contents are shown.
1298
1313
* @group collection
Original file line number Diff line number Diff line change @@ -59,6 +59,13 @@ class ByteVectorTest extends BitsSuite {
59
59
forAll((b : ByteVector , b2 : ByteVector ) => assert((b == b2) == (b === b2)))
60
60
}
61
61
62
+ property(" equalsConstantTime" ) {
63
+ forAll(bytesWithIndex) { case (b, m) =>
64
+ assert((b.take(m) ++ b.drop(m)).equalsConstantTime(b))
65
+ } &&
66
+ forAll((b : ByteVector , b2 : ByteVector ) => assert((b === b2) == (b.equalsConstantTime(b2))))
67
+ }
68
+
62
69
test(" compact is a no-op for already compact byte vectors" ) {
63
70
val b = ByteVector (0x80 )
64
71
assert((b.compact eq b.compact) == true )
You can’t perform that action at this time.
0 commit comments