-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7b2424
commit 7a1e828
Showing
6 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
integration_tests/builtin_tests/bitwise_builtin_test.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
%builtins bitwise | ||
from starkware.cairo.common.bitwise import bitwise_and, bitwise_xor, bitwise_or, bitwise_operations | ||
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin | ||
|
||
func main{bitwise_ptr: BitwiseBuiltin*}() { | ||
let (and_a) = bitwise_and(12, 10); // Binary (1100, 1010). | ||
assert and_a = 8; // Binary 1000. | ||
let (xor_a) = bitwise_xor(12, 10); | ||
assert xor_a = 6; | ||
let (or_a) = bitwise_or(12, 10); | ||
assert or_a = 14; | ||
|
||
let (and_b, xor_b, or_b) = bitwise_operations(9, 3); | ||
assert and_b = 1; | ||
assert xor_b = 10; | ||
assert or_b = 11; | ||
return (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package builtins | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" | ||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
) | ||
|
||
const cellsPerBitwise = 5 | ||
const inputCellsPerBitwise = 2 | ||
|
||
type Bitwise struct{} | ||
|
||
func (b *Bitwise) CheckWrite(segment *memory.Segment, offset uint64, value *memory.MemoryValue) error { | ||
return nil | ||
} | ||
|
||
func (b *Bitwise) InferValue(segment *memory.Segment, offset uint64) error { | ||
bitwiseIndex := offset % cellsPerBitwise | ||
// input cell | ||
if bitwiseIndex < inputCellsPerBitwise { | ||
return errors.New("cannot infer value") | ||
} | ||
|
||
xOffset := offset - bitwiseIndex | ||
yOffset := xOffset + 1 | ||
|
||
xValue, err := segment.Read(xOffset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
yValue, err := segment.Read(yOffset) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
xFelt, err := xValue.FieldElement() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
yFelt, err := yValue.FieldElement() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
xBytes := xFelt.Bytes() | ||
yBytes := yFelt.Bytes() | ||
|
||
var bitwiseValue memory.MemoryValue | ||
var bitwiseFelt fp.Element | ||
var bitwiseBytes [32]byte | ||
for i := 0; i < 32; i++ { | ||
bitwiseBytes[i] = xBytes[i] & yBytes[i] | ||
} | ||
bitwiseFelt.SetBytes(bitwiseBytes[:]) | ||
bitwiseValue = memory.MemoryValueFromFieldElement(&bitwiseFelt) | ||
if err := segment.Write(xOffset+2, &bitwiseValue); err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < 32; i++ { | ||
bitwiseBytes[i] = xBytes[i] ^ yBytes[i] | ||
} | ||
bitwiseFelt.SetBytes(bitwiseBytes[:]) | ||
bitwiseValue = memory.MemoryValueFromFieldElement(&bitwiseFelt) | ||
if err := segment.Write(xOffset+3, &bitwiseValue); err != nil { | ||
return err | ||
} | ||
|
||
for i := 0; i < 32; i++ { | ||
bitwiseBytes[i] = xBytes[i] | yBytes[i] | ||
} | ||
bitwiseFelt.SetBytes(bitwiseBytes[:]) | ||
bitwiseValue = memory.MemoryValueFromFieldElement(&bitwiseFelt) | ||
if err := segment.Write(xOffset+4, &bitwiseValue); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *Bitwise) String() string { | ||
return "bitwise" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package builtins | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" | ||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBitwise(t *testing.T) { | ||
bitwise := &Bitwise{} | ||
segment := memory.EmptySegmentWithLength(5) | ||
segment.WithBuiltinRunner(bitwise) | ||
|
||
x, _ := new(fp.Element).SetString("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") | ||
y, _ := new(fp.Element).SetString("0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB") | ||
xValue := memory.MemoryValueFromFieldElement(x) | ||
yValue := memory.MemoryValueFromFieldElement(y) | ||
require.NoError(t, segment.Write(0, &xValue)) | ||
require.NoError(t, segment.Write(1, &yValue)) | ||
|
||
xAndY, err := segment.Read(2) | ||
require.NoError(t, err) | ||
xAndYFelt, err := xAndY.FieldElement() | ||
require.NoError(t, err) | ||
assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", xAndYFelt.Text(16)) | ||
|
||
xXorY, err := segment.Read(3) | ||
require.NoError(t, err) | ||
xXorYFelt, err := xXorY.FieldElement() | ||
require.NoError(t, err) | ||
assert.Equal(t, "11111111111111111111111111111111111111111111111111111111111111", xXorYFelt.Text(16)) | ||
|
||
xOrY, err := segment.Read(4) | ||
require.NoError(t, err) | ||
xOrYFelt, err := xOrY.FieldElement() | ||
require.NoError(t, err) | ||
assert.Equal(t, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", xOrYFelt.Text(16)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters