Skip to content

Commit 98f6487

Browse files
author
txaty
committed
Fix Hurwitz integer prod issue.
Signed-off-by: txaty <[email protected]>
1 parent 1d1e4d6 commit 98f6487

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

hurwitz_integer.go

+20-24
Original file line numberDiff line numberDiff line change
@@ -301,48 +301,44 @@ func (h *HurwitzInt) Prod(a, b *HurwitzInt) *HurwitzInt {
301301
if h.dblR == nil {
302302
h.dblR = new(big.Int)
303303
}
304-
r := h.dblR
305304
if h.dblI == nil {
306305
h.dblI = new(big.Int)
307306
}
308-
i := h.dblI
309307
if h.dblJ == nil {
310308
h.dblJ = new(big.Int)
311309
}
312-
j := h.dblJ
313310
if h.dblK == nil {
314311
h.dblK = new(big.Int)
315312
}
316-
k := h.dblK
317313
opt := iPool.Get().(*big.Int)
318314
defer iPool.Put(opt)
319315
// 1 part
320-
r.Mul(a.dblR, b.dblR)
321-
r.Sub(r, opt.Mul(a.dblI, b.dblI))
322-
r.Sub(r, opt.Mul(a.dblJ, b.dblJ))
323-
r.Sub(r, opt.Mul(a.dblK, b.dblK))
324-
r.Rsh(r, 1)
316+
h.dblR.Mul(a.dblR, b.dblR)
317+
h.dblR.Sub(h.dblR, opt.Mul(a.dblI, b.dblI))
318+
h.dblR.Sub(h.dblR, opt.Mul(a.dblJ, b.dblJ))
319+
h.dblR.Sub(h.dblR, opt.Mul(a.dblK, b.dblK))
320+
h.dblR.Rsh(h.dblR, 1)
325321

326322
// i part
327-
i.Mul(a.dblR, b.dblI)
328-
i.Add(i, opt.Mul(a.dblI, b.dblR))
329-
i.Add(i, opt.Mul(a.dblJ, b.dblK))
330-
i.Sub(i, opt.Mul(a.dblK, b.dblJ))
331-
i.Rsh(i, 1)
323+
h.dblI.Mul(a.dblR, b.dblI)
324+
h.dblI.Add(h.dblI, opt.Mul(a.dblI, b.dblR))
325+
h.dblI.Add(h.dblI, opt.Mul(a.dblJ, b.dblK))
326+
h.dblI.Sub(h.dblI, opt.Mul(a.dblK, b.dblJ))
327+
h.dblI.Rsh(h.dblI, 1)
332328

333329
// j part
334-
j.Mul(a.dblR, b.dblJ)
335-
j.Sub(j, opt.Mul(a.dblI, b.dblK))
336-
j.Add(j, opt.Mul(a.dblJ, b.dblR))
337-
j.Add(j, opt.Mul(a.dblK, b.dblI))
338-
j.Rsh(j, 1)
330+
h.dblJ.Mul(a.dblR, b.dblJ)
331+
h.dblJ.Sub(h.dblJ, opt.Mul(a.dblI, b.dblK))
332+
h.dblJ.Add(h.dblJ, opt.Mul(a.dblJ, b.dblR))
333+
h.dblJ.Add(h.dblJ, opt.Mul(a.dblK, b.dblI))
334+
h.dblJ.Rsh(h.dblJ, 1)
339335

340336
// k part
341-
k.Mul(a.dblR, b.dblK)
342-
k.Add(k, opt.Mul(a.dblI, b.dblJ))
343-
k.Sub(k, opt.Mul(a.dblJ, b.dblI))
344-
k.Add(k, opt.Mul(a.dblK, b.dblR))
345-
k.Rsh(k, 1)
337+
h.dblK.Mul(a.dblR, b.dblK)
338+
h.dblK.Add(h.dblK, opt.Mul(a.dblI, b.dblJ))
339+
h.dblK.Sub(h.dblK, opt.Mul(a.dblJ, b.dblI))
340+
h.dblK.Add(h.dblK, opt.Mul(a.dblK, b.dblR))
341+
h.dblK.Rsh(h.dblK, 1)
346342

347343
return h
348344
}

hurwitz_integer_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestHurwitzInt_Prod(t *testing.T) {
2424
want *HurwitzInt
2525
}{
2626
{
27-
name: "test_(1+i+j+k)+(1+i+j+k)",
27+
name: "test_(1+i+j+k) * (1+i+j+k)",
2828
fields: fields{
2929
dblR: nil,
3030
dblI: nil,
@@ -37,6 +37,20 @@ func TestHurwitzInt_Prod(t *testing.T) {
3737
},
3838
want: NewHurwitzInt(big.NewInt(-2), big.NewInt(2), big.NewInt(2), big.NewInt(2), false),
3939
},
40+
{
41+
name: "test_2i * 1",
42+
fields: fields{
43+
dblR: nil,
44+
dblI: nil,
45+
dblJ: nil,
46+
dblK: nil,
47+
},
48+
args: args{
49+
a: NewHurwitzInt(big.NewInt(0), big.NewInt(2), big.NewInt(0), big.NewInt(0), false),
50+
b: NewHurwitzInt(big.NewInt(1), big.NewInt(0), big.NewInt(0), big.NewInt(0), false),
51+
},
52+
want: NewHurwitzInt(big.NewInt(0), big.NewInt(2), big.NewInt(0), big.NewInt(0), false),
53+
},
4054
}
4155
for _, tt := range tests {
4256
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)