Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement CheckDilutedCheckUsage #659

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ func (runner *ZeroRunner) checkUsedCells() error {
}
}
}

err := runner.checkDilutedCheckUsage()
if err != nil {
return err
}

return runner.checkRangeCheckUsage()
}

Expand Down Expand Up @@ -359,6 +365,17 @@ func (runner *ZeroRunner) getPermRangeCheckLimits() (uint16, uint16) {
return rcMin, rcMax
}

func (runner *ZeroRunner) checkDilutedCheckUsage() error {
if runner.layout.DilutedPoolInstanceDef.IsNone() {
return nil
}

// dilutedPoolInstance := runner.layout.DilutedPoolInstanceDef.Unwrap()
// usedUnitsByBuiltins := 0

return nil
}

// FinalizeSegments calculates the final size of the builtins segments,
// using number of allocated instances and memory cells per builtin instance.
// Additionally it sets the final size of the program segment to the program size.
Expand Down
24 changes: 24 additions & 0 deletions pkg/vm/builtins/bitwise.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,27 @@ func (b *Bitwise) String() string {
func (b *Bitwise) GetAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64) (uint64, error) {
return getBuiltinAllocatedSize(segmentUsedSize, vmCurrentStep, b.ratio, inputCellsPerBitwise, instancesPerComponentBitwise, cellsPerBitwise)
}

func (b *Bitwise) GetUsedDilutedCheckUnits(dilutedSpacing uint32, dilutedNBits uint32) uint64 {
totalNBits := uint32(251)
partition := make([]uint32, 0, totalNBits)

for i := uint32(0); i < totalNBits; i += dilutedSpacing * dilutedNBits {
for j := uint32(0); j < dilutedSpacing; j++ {
if i+j < totalNBits {
partition = append(partition, i+j)
}
}
}

partitionLength := uint64(len(partition))
numTrimmed := uint64(0)

for _, elem := range partition {
if elem+dilutedSpacing*(dilutedNBits-1)+1 > totalNBits {
numTrimmed++
}
}

return 4*partitionLength + numTrimmed
}
40 changes: 40 additions & 0 deletions pkg/vm/builtins/diluted_pool_instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package builtins

type DilutedPoolInstance struct {
UnitsPerStep uint32
Spacing uint32
NBits uint32
}

// DilutedPoolInstanceOption represents an option to a `DilutedPoolInstance`
type DilutedPoolInstanceOption struct {
value *DilutedPoolInstance
}

// `SomeDilutedPoolInstance` creates a DilutedPoolInstanceOption with a value
func SomeDilutedPoolInstance(value DilutedPoolInstance) DilutedPoolInstanceOption {
return DilutedPoolInstanceOption{value: &value}
}

// `NoneDilutedPoolInstance` creates a DilutedPoolInstanceOption without a value
func NoneDilutedPoolInstance() DilutedPoolInstanceOption {
return DilutedPoolInstanceOption{value: nil}
}

// `IsNone` checks if the DilutedPoolInstanceOption has no value
func (o DilutedPoolInstanceOption) IsNone() bool {
return o.value == nil
}

// `IsSome` checks if the DilutedPoolInstanceOption has a value
func (o DilutedPoolInstanceOption) IsSome() bool {
return o.value != nil
}

// `Unwrap` returns the value if it exists, panics otherwise
func (o DilutedPoolInstanceOption) Unwrap() DilutedPoolInstance {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh feels like rust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or Cairo :)

if o.IsNone() {
panic("Tried to unwrap None DilutedPoolInstanceOption")
}
return *o.value
}
8 changes: 8 additions & 0 deletions pkg/vm/builtins/keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ func (k *Keccak) String() string {
func (k *Keccak) GetAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64) (uint64, error) {
return getBuiltinAllocatedSize(segmentUsedSize, vmCurrentStep, k.ratio, inputCellsPerKeccak, instancesPerComponentKeccak, cellsPerKeccak)
}

func (k *Keccak) GetUsedDilutedCheckUnits(dilutedNBits uint32) uint64 {
totalCells := uint64(262144)
if dilutedNBits == 0 {
return 0
}
return totalCells / uint64(dilutedNBits)
}
20 changes: 11 additions & 9 deletions pkg/vm/builtins/layouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ type Layout struct {
RcUnits uint64
// List of builtins to be included in given layout
Builtins []LayoutBuiltin
// Option of a diluted pool instance
DilutedPoolInstanceDef DilutedPoolInstanceOption
}

func getPlainLayout() Layout {
return Layout{Name: "plain", RcUnits: 16, Builtins: []LayoutBuiltin{}}
return Layout{Name: "plain", RcUnits: 16, Builtins: []LayoutBuiltin{}, DilutedPoolInstanceDef: NoneDilutedPoolInstance()}
}

func getSmallLayout() Layout {
Expand All @@ -34,7 +36,7 @@ func getSmallLayout() Layout {
{Runner: &Pedersen{ratio: 8}, Builtin: starknet.Pedersen},
{Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck},
{Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA},
}}
}, DilutedPoolInstanceDef: NoneDilutedPoolInstance()}
}

func getDexLayout() Layout {
Expand All @@ -43,7 +45,7 @@ func getDexLayout() Layout {
{Runner: &Pedersen{ratio: 8}, Builtin: starknet.Pedersen},
{Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck},
{Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA},
}}
}, DilutedPoolInstanceDef: NoneDilutedPoolInstance()}
}

func getRecursiveLayout() Layout {
Expand All @@ -52,7 +54,7 @@ func getRecursiveLayout() Layout {
{Runner: &Pedersen{ratio: 128}, Builtin: starknet.Pedersen},
{Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck},
{Runner: &Bitwise{ratio: 8}, Builtin: starknet.Bitwise},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 16, Spacing: 4, NBits: 16})}
}

func getStarknetLayout() Layout {
Expand All @@ -64,7 +66,7 @@ func getStarknetLayout() Layout {
{Runner: &Bitwise{ratio: 64}, Builtin: starknet.Bitwise},
{Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP},
{Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 2, Spacing: 4, NBits: 16})}
}

func getStarknetWithKeccakLayout() Layout {
Expand All @@ -77,7 +79,7 @@ func getStarknetWithKeccakLayout() Layout {
{Runner: &EcOp{ratio: 1024, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP},
{Runner: &Keccak{ratio: 2048, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Keccak},
{Runner: &Poseidon{ratio: 32, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 16, Spacing: 4, NBits: 16})}
}

func getRecursiveLargeOutputLayout() Layout {
Expand All @@ -87,7 +89,7 @@ func getRecursiveLargeOutputLayout() Layout {
{Runner: &RangeCheck{ratio: 8, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck},
{Runner: &Bitwise{ratio: 8}, Builtin: starknet.Bitwise},
{Runner: &Poseidon{ratio: 8, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 16, Spacing: 4, NBits: 16})}
}

func getRecursiveWithPoseidonLayout() Layout {
Expand All @@ -97,7 +99,7 @@ func getRecursiveWithPoseidonLayout() Layout {
{Runner: &RangeCheck{ratio: 16, RangeCheckNParts: 8}, Builtin: starknet.RangeCheck},
{Runner: &Bitwise{ratio: 16}, Builtin: starknet.Bitwise},
{Runner: &Poseidon{ratio: 64, cache: make(map[uint64]fp.Element)}, Builtin: starknet.Poseidon},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 8, Spacing: 4, NBits: 16})}
}

func getAllSolidityLayout() Layout {
Expand All @@ -108,7 +110,7 @@ func getAllSolidityLayout() Layout {
{Runner: &ECDSA{ratio: 512}, Builtin: starknet.ECDSA},
{Runner: &Bitwise{ratio: 256}, Builtin: starknet.Bitwise},
{Runner: &EcOp{ratio: 256, cache: make(map[uint64]fp.Element)}, Builtin: starknet.ECOP},
}}
}, DilutedPoolInstanceDef: SomeDilutedPoolInstance(DilutedPoolInstance{UnitsPerStep: 16, Spacing: 4, NBits: 16})}
}

func GetLayout(layout string) (Layout, error) {
Expand Down
Loading