diff --git a/src/msm/msm.zig b/src/msm/msm.zig index 1b9d687..e684438 100644 --- a/src/msm/msm.zig +++ b/src/msm/msm.zig @@ -92,8 +92,10 @@ pub fn PrecompMSM( for (0..scalars.len) |s_i| { var k: u16 = 0; while (k < Fr.BitSize) : (k += t) { - const bit = scalars[s_i] >> (@as(u8, @intCast(k + t - t_i - 1))) & 1; - curr_window_scalar |= @as(usize, @intCast(bit << (b - curr_window_b_idx - 1))); + if (k + t - t_i - 1 < Fr.BitSize) { + const bit = scalars[s_i] >> (@as(u8, @intCast(k + t - t_i - 1))) & 1; + curr_window_scalar |= @as(usize, @intCast(bit << (b - curr_window_b_idx - 1))); + } curr_window_b_idx += 1; if (curr_window_b_idx == b) { @@ -107,6 +109,9 @@ pub fn PrecompMSM( } } } + if (curr_window_scalar > 0) { + accum = bandersnatch.ExtendedPoint.mixedAdd(accum, self.table[curr_window_idx * window_size .. (curr_window_idx + 1) * window_size][curr_window_scalar]); + } } return Element{ .point = accum };