Skip to content

Commit 9957fd0

Browse files
improving math/bits speed
1 parent c395ba4 commit 9957fd0

5 files changed

Lines changed: 470 additions & 3 deletions

File tree

compiler/expressions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,12 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
667667
return fc.formatExpr("debugger")
668668
case "InternalObject":
669669
return fc.translateExpr(e.Args[0])
670+
case "MakeUint64":
671+
return fc.formatExpr("new $Uint64(%e, %e)", e.Args[0], e.Args[1])
672+
case "Uint64High":
673+
return fc.formatExpr("%e.$high", e.Args[0])
674+
case "Uint64Low":
675+
return fc.formatExpr("%e.$low", e.Args[0])
670676
}
671677
}
672678
return fc.translateCall(e, sig, fc.translateExpr(f))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//go:build js
2+
3+
package bigmod
4+
5+
import "math/bits"
6+
7+
// GOPHERJS: The original has specialized cases for 1024, 1536, and 2048 bit
8+
// sizes that use addMulVVW1024/1536/2048 which take *uint pointers.
9+
// Those functions then use unsafe.Slice to convert back to slices.
10+
// In GopherJS, creating pointers via &slice[i] generates an $indexPtr.
11+
// We avoid this by always using the no-asm slice-based implementation
12+
// which calls addMulVVW with slices directly.
13+
//
14+
//gopherjs:replace
15+
func (x *Nat) montgomeryMul(a *Nat, b *Nat, m *Modulus) *Nat {
16+
n := len(m.nat.limbs)
17+
mLimbs := m.nat.limbs[:n]
18+
aLimbs := a.limbs[:n]
19+
bLimbs := b.limbs[:n]
20+
21+
T := make([]uint, n*2)
22+
23+
var c uint
24+
for i := 0; i < n; i++ {
25+
_ = T[n+i] // bounds check elimination hint
26+
d := bLimbs[i]
27+
c1 := addMulVVW(T[i:n+i], aLimbs, d)
28+
Y := T[i] * m.m0inv
29+
c2 := addMulVVW(T[i:n+i], mLimbs, Y)
30+
T[n+i], c = bits.Add(c1, c2, c)
31+
}
32+
33+
copy(x.reset(n).limbs, T[n:])
34+
x.maybeSubtractModulus(choice(c), m)
35+
36+
return x
37+
}
38+
39+
//gopherjs:purge
40+
func addMulVVW1024(z, x *uint, y uint) (c uint)
41+
42+
//gopherjs:purge
43+
func addMulVVW1536(z, x *uint, y uint) (c uint)
44+
45+
//gopherjs:purge
46+
func addMulVVW2048(z, x *uint, y uint) (c uint)

0 commit comments

Comments
 (0)