cairo-vm-go icon indicating copy to clipboard operation
cairo-vm-go copied to clipboard

Implement `blockPermutation` hint

Open TAdev0 opened this issue 1 year ago • 7 comments

@MaksymMalicki @cicr99 @rodrigo-pino this is a first draft.

I changed all the code to use KeccakF1600 as specified by Rodrigo. This function doesn't return anything but alters the value of the variable the pointer passed as argument points to. This result is then written to memory starting at keccakPtr address.

There is a check in the python hint to revert if keccakStateSize >= 100 , but keccakStateSize is defined as a constant value (25). I'm wondering if this check could be removed?

Closes #282

TAdev0 avatar Apr 26 '24 12:04 TAdev0

@MaksymMalicki we loop over inputValuesInRange to fill the keccakInput []uint64 array.

And inputValuesInRange is calculated as follows:

			inputValuesInRange, err := hinter.GetConsecutiveValues(vm, readAddr, int16(keccakStateSize))

or in the python hint :

memory.get_range(ids.keccak_ptr - _keccak_state_size_felts, _keccak_state_size_felts)

It seems obvious that keccakInput will never contain more than keccakStateSize elements, do you confirm?

TAdev0 avatar Apr 29 '24 14:04 TAdev0

@cicr99 @MaksymMalicki @har777 ok so i'm having real difficulty to test in an isolated way this hint. I always get errors like

Error:          Expected nil, but got: &fmt.wrapError{msg:"segment 1, offset 7: no builtin: reading unknown value"` 

when trying to run the test without any check. I have no idea how to create a setup to run this hint separately at the moment.

TAdev0 avatar May 01 '24 16:05 TAdev0

Hi @TAdev0 I would suggest your test to look like this:

		"BlockPermutation": {
			{
				operanders: []*hintOperander{
					{Name: "KECCAK_STATE_SIZE_FELTS", Kind: apRelative, Value: feltUint64(25)},
					{Name: "keccak_ptr", Kind: fpRelative, Value: addr(31)},
					{Name: "data.0", Kind: apRelative, Value: feltUint64(1)},
					{Name: "data.1", Kind: apRelative, Value: feltUint64(2)},
					{Name: "data.2", Kind: apRelative, Value: feltUint64(3)},
					{Name: "data.3", Kind: apRelative, Value: feltUint64(4)},
					{Name: "data.4", Kind: apRelative, Value: feltUint64(5)},
					{Name: "data.5", Kind: apRelative, Value: feltUint64(6)},
					{Name: "data.6", Kind: apRelative, Value: feltUint64(7)},
					{Name: "data.7", Kind: apRelative, Value: feltUint64(8)},
					{Name: "data.8", Kind: apRelative, Value: feltUint64(9)},
					{Name: "data.9", Kind: apRelative, Value: feltUint64(10)},
					{Name: "data.10", Kind: apRelative, Value: feltUint64(11)},
					{Name: "data.11", Kind: apRelative, Value: feltUint64(12)},
					{Name: "data.12", Kind: apRelative, Value: feltUint64(13)},
					{Name: "data.13", Kind: apRelative, Value: feltUint64(14)},
					{Name: "data.14", Kind: apRelative, Value: feltUint64(15)},
					{Name: "data.15", Kind: apRelative, Value: feltUint64(16)},
					{Name: "data.16", Kind: apRelative, Value: feltUint64(17)},
					{Name: "data.17", Kind: apRelative, Value: feltUint64(18)},
					{Name: "data.18", Kind: apRelative, Value: feltUint64(19)},
					{Name: "data.19", Kind: apRelative, Value: feltUint64(20)},
					{Name: "data.20", Kind: apRelative, Value: feltUint64(21)},
					{Name: "data.21", Kind: apRelative, Value: feltUint64(22)},
					{Name: "data.22", Kind: apRelative, Value: feltUint64(23)},
					{Name: "data.23", Kind: apRelative, Value: feltUint64(24)},
					{Name: "data.24", Kind: apRelative, Value: feltUint64(25)},
				},
				makeHinter: func(ctx *hintTestContext) hinter.Hinter {
					return newBlockPermutationHint(ctx.operanders["KECCAK_STATE_SIZE_FELTS"], ctx.operanders["keccak_ptr"])
				},
				check: func(t *testing.T, ctx *hintTestContext) {
					t.Fatal("TODO: implement")
				},
			},
		},

The problem you have is when you pass a pointer to the function and try to read consecutive, uninitialised memory blocks. When you assign some values to them, your code works fine. Let me know if it works

MaksymMalicki avatar May 08 '24 12:05 MaksymMalicki

@MaksymMalicki thank you so much for the help !

TAdev0 avatar May 08 '24 12:05 TAdev0

@rodrigo-pino @har777 ready for review!

TAdev0 avatar May 16 '24 14:05 TAdev0

lgtm! 🚀 You just need to update this branch and it's good to go

MaksymMalicki avatar May 20 '24 10:05 MaksymMalicki

@cicr99 @rodrigo-pino waiting for your review to merge

TAdev0 avatar May 20 '24 10:05 TAdev0