sub
Subtract two integers serialized as arrays of the same size, in constant time.
The result is stored into result, and true is returned if an underflow occurred.
Function parameters
Parameters
Compares two arrays in constant time (for a given length) and returns whether they are equal.
Functions
- eql
- Compares two arrays in constant time (for a given length) and returns whether they are equal.
- compare
- Compare two integers serialized as arrays of the same size, in constant time.
- add
- Add two integers serialized as arrays of the same size, in constant time.
- sub
- Subtract two integers serialized as arrays of the same size, in constant time.
- classify
- Mark a value as sensitive or secret, helping to detect potential side-channel vulnerabilities.
- declassify
- Mark a value as non-sensitive or public, indicating it's safe from side-channel attacks.
Source
Implementation
pub fn sub(comptime T: type, a: []const T, b: []const T, result: []T, endian: Endian) bool {
const len = a.len;
assert(len == b.len and len == result.len);
var borrow: u1 = 0;
if (endian == .little) {
var i: usize = 0;
while (i < len) : (i += 1) {
const ov1 = @subWithOverflow(a[i], b[i]);
const ov2 = @subWithOverflow(ov1[0], borrow);
result[i] = ov2[0];
borrow = ov1[1] | ov2[1];
}
} else {
var i: usize = len;
while (i != 0) {
i -= 1;
const ov1 = @subWithOverflow(a[i], b[i]);
const ov2 = @subWithOverflow(ov1[0], borrow);
result[i] = ov2[0];
borrow = ov1[1] | ov2[1];
}
}
return @as(bool, @bitCast(borrow));
}