Function add [src]

Add two integers serialized as arrays of the same size, in constant time. The result is stored into result, and true is returned if an overflow occurred.

Prototype

pub fn add(comptime T: type, a: []const T, b: []const T, result: []T, endian: Endian) bool

Parameters

T: typea: []const Tb: []const Tresult: []Tendian: Endian

Source

pub fn add(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 carry: u1 = 0; if (endian == .little) { var i: usize = 0; while (i < len) : (i += 1) { const ov1 = @addWithOverflow(a[i], b[i]); const ov2 = @addWithOverflow(ov1[0], carry); result[i] = ov2[0]; carry = ov1[1] | ov2[1]; } } else { var i: usize = len; while (i != 0) { i -= 1; const ov1 = @addWithOverflow(a[i], b[i]); const ov2 = @addWithOverflow(ov1[0], carry); result[i] = ov2[0]; carry = ov1[1] | ov2[1]; } } return @as(bool, @bitCast(carry)); }