Skip to content

Commit

Permalink
uint256: optimize MulMod, MulModWithReciprocal (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronChen0 committed May 27, 2024
1 parent 70cbe2b commit b85bc2a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ func Reciprocal(m *Int) (mu [5]uint64) {

// reduce4 computes the least non-negative residue of x modulo m
//
// requires a four-word modulus (m[3] > 1) and its inverse (mu)
func reduce4(x [8]uint64, m *Int, mu [5]uint64) (z Int) {
// requires a four-word modulus (m[3] != 0) and its inverse (mu)
func (z *Int) reduce4(x *[8]uint64, m *Int, mu *[5]uint64) *Int {

// NB: Most variable names in the comments match the pseudocode for
// Barrett reduction in the Handbook of Applied Cryptography.
Expand Down
6 changes: 2 additions & 4 deletions uint256.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,7 @@ func (z *Int) MulModWithReciprocal(x, y, m *Int, mu *[5]uint64) *Int {
umul(x, y, &p)

if m[3] != 0 {
r := reduce4(p, m, *mu)
return z.Set(&r)
return z.reduce4(&p, m, mu)
}

var (
Expand Down Expand Up @@ -713,8 +712,7 @@ func (z *Int) MulMod(x, y, m *Int) *Int {

if m[3] != 0 {
mu := Reciprocal(m)
r := reduce4(p, m, mu)
return z.Set(&r)
return z.reduce4(&p, m, &mu)
}

var (
Expand Down

0 comments on commit b85bc2a

Please sign in to comment.