Function subtle::slices_equal [] [src]

pub fn slices_equal(a: &[u8], b: &[u8]) -> Mask

Check equality of two slices, a and b, in constant time.

There is an assert! that the two slices are of equal length. For example, the following code is a programming error and will panic:

let a: [u8; 3] = [0, 0, 0];
let b: [u8; 4] = [0, 0, 0, 0];

assert!(slices_equal(&a, &b) == 1);

However, if the slices are equal length, but their contents do not match, 0u8 will be returned:

let a: [u8; 3] = [0, 1, 2];
let b: [u8; 3] = [1, 2, 3];

assert!(slices_equal(&a, &b) == 0);

And finally, if the contents do match, 1u8 is returned:

let a: [u8; 3] = [0, 1, 2];
let b: [u8; 3] = [0, 1, 2];

assert!(slices_equal(&a, &b) == 1);

let empty: [u8; 0] = [];

assert!(slices_equal(&empty, &empty) == 1);

This function is commonly used in various cryptographic applications, such as signature verification, among many other applications.

Return

Returns 1u8 if a == b and 0u8 otherwise.