Skip to content

Commit

Permalink
internal/bigmod: disable race detector on tight loops #285
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Dec 5, 2024
1 parent 14087e2 commit fcd1aa2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/bigmod/nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ func (x *Nat) SetUint(y uint, m *Modulus) *Nat {
// Equal returns 1 if x == y, and 0 otherwise.
//
// Both operands must have the same announced length.
//
//go:norace
func (x *Nat) Equal(y *Nat) choice {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -261,6 +263,8 @@ func (x *Nat) Equal(y *Nat) choice {
}

// IsZero returns 1 if x == 0, and 0 otherwise.
//
//go:norace
func (x *Nat) IsZero() choice {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -274,6 +278,8 @@ func (x *Nat) IsZero() choice {
}

// IsOne returns 1 if x == 1, and 0 otherwise.
//
//go:norace
func (x *Nat) IsOne() choice {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -294,13 +300,17 @@ func (x *Nat) IsOne() choice {
//
// The length of x must be the same as the modulus. x must already be reduced
// modulo m.
//
//go:norace
func (x *Nat) IsMinusOne(m *Modulus) choice {
minusOne := m.Nat()
minusOne.SubOne(m)
return x.Equal(minusOne)
}

// IsOdd returns 1 if x is odd, and 0 otherwise.
//
//go:norace
func (x *Nat) IsOdd() choice {
if len(x.limbs) == 0 {
return no
Expand All @@ -326,6 +336,8 @@ func (x *Nat) TrailingZeroBitsVarTime() uint {
// CmpGeq returns 1 if x >= y, and 0 otherwise.
//
// Both operands must have the same announced length.
//
//go:norace
func (x *Nat) CmpGeq(y *Nat) choice {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -344,6 +356,8 @@ func (x *Nat) CmpGeq(y *Nat) choice {
// assign sets x <- y if on == 1, and does nothing otherwise.
//
// Both operands must have the same announced length.
//
//go:norace
func (x *Nat) assign(on choice, y *Nat) *Nat {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -360,6 +374,8 @@ func (x *Nat) assign(on choice, y *Nat) *Nat {
// add computes x += y and returns the carry.
//
// Both operands must have the same announced length.
//
//go:norace
func (x *Nat) add(y *Nat) (c uint) {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -375,6 +391,8 @@ func (x *Nat) add(y *Nat) (c uint) {
// sub computes x -= y. It returns the borrow of the subtraction.
//
// Both operands must have the same announced length.
//
//go:norace
func (x *Nat) sub(y *Nat) (c uint) {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand All @@ -390,6 +408,8 @@ func (x *Nat) sub(y *Nat) (c uint) {
// ShiftRightVarTime sets x = x >> n.
//
// The announced length of x is unchanged.
//
//go:norace
func (x *Nat) ShiftRightVarTime(n uint) *Nat {
// Eliminate bounds checks in the loop.
size := len(x.limbs)
Expand Down Expand Up @@ -596,6 +616,8 @@ func (x *Nat) shiftIn(y uint, m *Modulus) *Nat {
// shiftIn calculates x = x << _W + y mod m.
//
// This assumes that x is already reduced mod m, and that y < 2^_W.
//
//go:norace
func (x *Nat) shiftInNat(y uint, m *Nat) *Nat {
d := NewNat().reset(len(m.limbs))

Expand Down Expand Up @@ -902,6 +924,8 @@ func (x *Nat) montgomeryMul(a *Nat, b *Nat, m *Modulus) *Nat {
// addMulVVW multiplies the multi-word value x by the single-word value y,
// adding the result to the multi-word value z and returning the final carry.
// It can be thought of as one row of a pen-and-paper column multiplication.
//
//go:norace
func addMulVVW(z, x []uint, y uint) (carry uint) {
_ = x[len(z)-1] // bounds check elimination hint
for i := range z {
Expand Down Expand Up @@ -1170,6 +1194,7 @@ func (x *Nat) InverseVarTime(a *Nat, m *Modulus) (*Nat, bool) {
}
}

//go:norace
func rshift1(a *Nat, carry uint) {
size := len(a.limbs)
aLimbs := a.limbs[:size]
Expand Down

0 comments on commit fcd1aa2

Please sign in to comment.