Skip to content

Commit

Permalink
test widemul128
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Sep 16, 2023
1 parent d7ece89 commit df113ac
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/hintrunner/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -96,3 +97,62 @@ func TestTestLessThanTrue(t *testing.T) {
readFrom(vm, VM.ExecutionSegment, 1),
)
}

func TestWideMul128(t *testing.T) {
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0

var dstLow ApCellRef = 1
var dstHigh ApCellRef = 2

lhs := Immediate(*big.NewInt(1).Lsh(big.NewInt(1), 127))
rhs := Immediate(*big.NewInt(1<<8 + 1))

hint := WideMul128{
low: dstLow,
high: dstHigh,
lhs: lhs,
rhs: rhs,
}

err := hint.Execute(vm)
require.Nil(t, err)

low := &f.Element{}
low.SetBigInt(big.NewInt(1).Lsh(big.NewInt(1), 127))
require.Nil(t, err)

require.Equal(
t,
memory.MemoryValueFromFieldElement(low),
readFrom(vm, VM.ExecutionSegment, 1),
)
require.Equal(
t,
memory.MemoryValueFromInt(1<<7),
readFrom(vm, VM.ExecutionSegment, 2),
)
}

func TestWideMul128IncorrectRange(t *testing.T) {
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0

var dstLow ApCellRef = 1
var dstHigh ApCellRef = 2

lhs := Immediate(*big.NewInt(1).Lsh(big.NewInt(1), 128))
rhs := Immediate(*big.NewInt(1))

hint := WideMul128{
low: dstLow,
high: dstHigh,
lhs: lhs,
rhs: rhs,
}

err := hint.Execute(vm)
require.ErrorContains(t, err, "should be u128")
}

0 comments on commit df113ac

Please sign in to comment.