-
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.
Add writing tokens gas cost to memory
- Loading branch information
1 parent
73f6dd5
commit 17b17df
Showing
3 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# Set to run some specific file tests (ex. fib.cairo,alloc.cairo) | ||
INTEGRATION_TESTS_FILTERS= | ||
INTEGRATION_TESTS_FILTERS=ecdsa_recover__starknet.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,72 @@ | ||
package runner | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/vm" | ||
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" | ||
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" | ||
) | ||
|
||
type TokenGasCost uint8 | ||
|
||
const ( | ||
ConstToken TokenGasCost = iota + 1 | ||
HoleToken | ||
RangeCheckToken | ||
RangeCheck96Token | ||
PedersenToken | ||
PoseidonToken | ||
BitwiseToken | ||
EcOpToken | ||
AddModToken | ||
MulModToken | ||
) | ||
|
||
func getTokenGasCost(token TokenGasCost) (uint64, error) { | ||
switch token { | ||
case ConstToken: | ||
return 1, nil | ||
case PedersenToken: | ||
return 4050, nil | ||
case PoseidonToken: | ||
return 491, nil | ||
case BitwiseToken: | ||
return 583, nil | ||
case EcOpToken: | ||
return 4085, nil | ||
case AddModToken: | ||
return 230, nil | ||
case MulModToken: | ||
return 604, nil | ||
default: | ||
return 0, fmt.Errorf("token has no cost") | ||
} | ||
} | ||
|
||
func gasInitialization(memory *memory.Memory) error { | ||
builtinsCostSegmentAddress := memory.AllocateEmptySegment() | ||
mv := mem.MemoryValueFromMemoryAddress(&builtinsCostSegmentAddress) | ||
programSegment := memory.Segments[vm.ProgramSegment] | ||
err := memory.Write(0, programSegment.Len(), &mv) | ||
if err != nil { | ||
return err | ||
} | ||
preCostTokenTypes := []TokenGasCost{PedersenToken, PoseidonToken, BitwiseToken, EcOpToken, AddModToken, MulModToken} | ||
for _, token := range preCostTokenTypes { | ||
cost, err := getTokenGasCost(token) | ||
if err != nil { | ||
return err | ||
} | ||
mv := mem.MemoryValueFromUint(cost) | ||
err = memory.WriteToAddress(&builtinsCostSegmentAddress, &mv) | ||
if err != nil { | ||
return err | ||
} | ||
builtinsCostSegmentAddress, err = builtinsCostSegmentAddress.AddOffset(1) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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