@@ -32,6 +32,41 @@ assert.strictEqual(
3232 }
3333}
3434
35+ {
36+ // When the inputs are floating-point numbers, timingSafeEqual neither has
37+ // equality nor SameValue semantics. It just compares the underlying bytes,
38+ // ignoring the TypedArray type completely.
39+
40+ const cmp = ( fn ) => ( a , b ) => a . every ( ( x , i ) => fn ( x , b [ i ] ) ) ;
41+ const eq = cmp ( ( a , b ) => a === b ) ;
42+ const is = cmp ( Object . is ) ;
43+
44+ function test ( a , b , { equal, sameValue, timingSafeEqual } ) {
45+ assert . strictEqual ( eq ( a , b ) , equal ) ;
46+ assert . strictEqual ( is ( a , b ) , sameValue ) ;
47+ assert . strictEqual ( crypto . timingSafeEqual ( a , b ) , timingSafeEqual ) ;
48+ }
49+
50+ test ( new Float32Array ( [ NaN ] ) , new Float32Array ( [ NaN ] ) , {
51+ equal : false ,
52+ sameValue : true ,
53+ timingSafeEqual : true
54+ } ) ;
55+
56+ test ( new Float64Array ( [ 0 ] ) , new Float64Array ( [ - 0 ] ) , {
57+ equal : true ,
58+ sameValue : false ,
59+ timingSafeEqual : false
60+ } ) ;
61+
62+ const x = new BigInt64Array ( [ 0x7ff0000000000001n , 0xfff0000000000001n ] ) ;
63+ test ( new Float64Array ( x . buffer ) , new Float64Array ( [ NaN , NaN ] ) , {
64+ equal : false ,
65+ sameValue : true ,
66+ timingSafeEqual : false
67+ } ) ;
68+ }
69+
3570assert . throws (
3671 ( ) => crypto . timingSafeEqual ( Buffer . from ( [ 1 , 2 , 3 ] ) , Buffer . from ( [ 1 , 2 ] ) ) ,
3772 {
0 commit comments