From ce474970161993b8d37f4ecd91c083a39da05848 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 2 Oct 2023 18:57:27 -0300 Subject: [PATCH 01/17] Begin implemneting blake2s --- pkg/hints/hint_utils/blake2s_hash.go | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/hints/hint_utils/blake2s_hash.go diff --git a/pkg/hints/hint_utils/blake2s_hash.go b/pkg/hints/hint_utils/blake2s_hash.go new file mode 100644 index 00000000..93a131a5 --- /dev/null +++ b/pkg/hints/hint_utils/blake2s_hash.go @@ -0,0 +1,44 @@ +package hint_utils + +func IV() [8]uint32 { + return [8]uint32{ + 0x6A09E667, + 0xBB67AE85, + 0x3C6EF372, + 0xA54FF53A, + 0x510E527F, + 0x9B05688C, + 0x1F83D9AB, + 0x5BE0CD19} +} + +func SIGMA() [10][16]uint32 { + return [10][16]uint32{ + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}, + {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4}, + {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8}, + {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13}, + {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9}, + {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11}, + {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10}, + {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5}, + {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}, + } +} + +func rightRot(value uint32, n uint32) uint32 { + return (value >> n) | ((value & ((1 << n) - 1)) << (32 - n)) +} + +func mix(a uint32, b uint32, c uint32, d uint32, m0 uint32, m1 uint32) (uint32, uint32, uint32) { + a = a + b + m0 + d = rightRot(d^a, 16) + c = c + d + b = rightRot(b^c, 12) + a = a + b + m1 + d = rightRot(d^a, 8) + c = c + d + b = rightRot(b^c, 7) + return a, b, c +} From bc5f5d4b618753cfb08440b789c8c9bea7457607 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 11:07:45 -0300 Subject: [PATCH 02/17] Finish blake2s impl --- pkg/hints/hint_utils/blake2s_hash.go | 93 +++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/pkg/hints/hint_utils/blake2s_hash.go b/pkg/hints/hint_utils/blake2s_hash.go index 93a131a5..fd92b2ad 100644 --- a/pkg/hints/hint_utils/blake2s_hash.go +++ b/pkg/hints/hint_utils/blake2s_hash.go @@ -31,7 +31,7 @@ func rightRot(value uint32, n uint32) uint32 { return (value >> n) | ((value & ((1 << n) - 1)) << (32 - n)) } -func mix(a uint32, b uint32, c uint32, d uint32, m0 uint32, m1 uint32) (uint32, uint32, uint32) { +func mix(a uint32, b uint32, c uint32, d uint32, m0 uint32, m1 uint32) (uint32, uint32, uint32, uint32) { a = a + b + m0 d = rightRot(d^a, 16) c = c + d @@ -40,5 +40,94 @@ func mix(a uint32, b uint32, c uint32, d uint32, m0 uint32, m1 uint32) (uint32, d = rightRot(d^a, 8) c = c + d b = rightRot(b^c, 7) - return a, b, c + return a, b, c, d +} + +func blakeRound(state []uint32, message [16]uint32, sigma [16]uint32) []uint32 { + state[0], state[4], state[8], state[12] = mix( + state[0], + state[4], + state[8], + state[12], + message[sigma[0]], + message[sigma[1]], + ) + state[1], state[5], state[9], state[13] = mix( + state[1], + state[5], + state[9], + state[13], + message[sigma[2]], + message[sigma[3]], + ) + state[2], state[6], state[10], state[14] = mix( + state[2], + state[6], + state[10], + state[14], + message[sigma[4]], + message[sigma[5]], + ) + state[3], state[7], state[11], state[15] = mix( + state[3], + state[7], + state[11], + state[15], + message[sigma[6]], + message[sigma[7]], + ) + state[0], state[5], state[10], state[15] = mix( + state[0], + state[5], + state[10], + state[15], + message[sigma[8]], + message[sigma[9]], + ) + state[1], state[6], state[11], state[12] = mix( + state[1], + state[6], + state[11], + state[12], + message[sigma[10]], + message[sigma[11]], + ) + state[2], state[7], state[8], state[13] = mix( + state[2], + state[7], + state[8], + state[13], + message[sigma[12]], + message[sigma[13]], + ) + state[3], state[4], state[9], state[14] = mix( + state[3], + state[4], + state[9], + state[14], + message[sigma[14]], + message[sigma[15]], + ) + return state +} + +func Blake2sCompress(h [8]uint32, message [16]uint32, t0 uint32, t1 uint32, f0 uint32, f1 uint32) []uint32 { + iv := IV() + + state := make([]uint32, 0, 16) + + state = append(state, h[:]...) + state = append(state, iv[:4]...) + state = append(state, iv[4]^t0, iv[5]^t1, iv[6]^f0, iv[7]^f1) + + for _, sigmaList := range SIGMA() { + state = blakeRound(state, message, sigmaList) + } + + newState := make([]uint32, 0, 8) + for i := 0; i < 8; i++ { + newState = append(newState, h[i]^state[i]^state[8+i]) + } + + return newState } From 756aec9d47b3555fed042fef262e520fea7109a2 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 11:18:44 -0300 Subject: [PATCH 03/17] Add unit test --- pkg/hints/hint_utils/bake2s_hash_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 pkg/hints/hint_utils/bake2s_hash_test.go diff --git a/pkg/hints/hint_utils/bake2s_hash_test.go b/pkg/hints/hint_utils/bake2s_hash_test.go new file mode 100644 index 00000000..6c778c9c --- /dev/null +++ b/pkg/hints/hint_utils/bake2s_hash_test.go @@ -0,0 +1,18 @@ +package hint_utils_test + +import ( + "reflect" + "testing" + + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" +) + +func TestBlake2sCompressA(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + expectedState := []uint32{412110711, 3234706100, 3894970767, 982912411, 937789635, 742982576, 3942558313, 1407547065} + newState := Blake2sCompress(h, message, 2, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} From 4e89d38d3a77b7c332f853bd850bf7211db403a6 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 11:25:08 -0300 Subject: [PATCH 04/17] Add more unit tests --- pkg/hints/hint_utils/bake2s_hash_test.go | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/pkg/hints/hint_utils/bake2s_hash_test.go b/pkg/hints/hint_utils/bake2s_hash_test.go index 6c778c9c..0d5a57a0 100644 --- a/pkg/hints/hint_utils/bake2s_hash_test.go +++ b/pkg/hints/hint_utils/bake2s_hash_test.go @@ -16,3 +16,64 @@ func TestBlake2sCompressA(t *testing.T) { t.Error("Wrong state returned by Blake2sCompress") } } + +func TestBlake2sCompressB(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{456710651, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + expectedState := []uint32{1061041453, 3663967611, 2158760218, 836165556, 3696892209, 3887053585, 2675134684, 2201582556} + newState := Blake2sCompress(h, message, 2, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} + +func TestBlake2sCompressC(t *testing.T) { + //Hashing "Hello World" + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{1819043144, 1870078063, 6581362, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + expectedState := []uint32{939893662, 3935214984, 1704819782, 3912812968, 4211807320, 3760278243, 674188535, 2642110762} + newState := Blake2sCompress(h, message, 9, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} + +func TestBlake2sCompressD(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{1819043144, 1870078063, 6581362, 274628678, 715791845, 175498643, 871587583, 0, 0, 0, 0, 0, 0, 0, 0, 0} + expectedState := []uint32{3980510537, 3982966407, 1593299263, 2666882356, 3288094120, 2682988286, 1666615862, 378086837} + newState := Blake2sCompress(h, message, 28, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} + +func TestBlake2sCompressE(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{1819043144, 1870078063, 6581362, 274628678, 715791845, 175498643, 871587583, 635963558, 557369694, 1576875962, 215769785, 0, 0, 0, 0, 0} + expectedState := []uint32{3251785223, 1946079609, 2665255093, 3508191500, 3630835628, 3067307230, 3623370123, 656151356} + newState := Blake2sCompress(h, message, 44, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} + +func TestBlake2sCompressF(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{1819043144, 1870078063, 6581362, 274628678, 715791845, 175498643, 871587583, 635963558, 557369694, 1576875962, 215769785, 152379578, 585849303, 764739320, 437383930, 74833930} + expectedState := []uint32{2593218707, 3238077801, 914875393, 3462286058, 4028447058, 3174734057, 2001070146, 3741410512} + newState := Blake2sCompress(h, message, 64, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} + +func TestBlake2sCompressG(t *testing.T) { + h := [8]uint32{1795745351, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225} + message := [16]uint32{11563522, 43535528, 653255322, 274628678, 73471943, 17549868, 87158958, 635963558, 343656565, 1576875962, 215769785, 152379578, 585849303, 76473202, 437253230, 74833930} + expectedState := []uint32{3496615692, 3252241979, 3771521549, 2125493093, 3240605752, 2885407061, 3962009872, 3845288240} + newState := Blake2sCompress(h, message, 64, 0, 4294967295, 0) + if !reflect.DeepEqual(expectedState, newState) { + t.Error("Wrong state returned by Blake2sCompress") + } +} From 3073bda7f60d7dd7ca766fb8c1c6ebad13227737 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 11:33:07 -0300 Subject: [PATCH 05/17] Add integration test --- cairo_programs/blake2s_hello_world_hash.cairo | 20 +++++++++++++++++++ pkg/vm/cairo_run/cairo_run_test.go | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 cairo_programs/blake2s_hello_world_hash.cairo diff --git a/cairo_programs/blake2s_hello_world_hash.cairo b/cairo_programs/blake2s_hello_world_hash.cairo new file mode 100644 index 00000000..9e832e7d --- /dev/null +++ b/cairo_programs/blake2s_hello_world_hash.cairo @@ -0,0 +1,20 @@ +%builtins range_check bitwise + +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.cairo_blake2s.blake2s import blake2s +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin + +// Computes the hash of "Hello World" +func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { + alloc_locals; + let inputs: felt* = alloc(); + assert inputs[0] = 'Hell'; + assert inputs[1] = 'o Wo'; + assert inputs[2] = 'rld'; + let (local blake2s_ptr_start) = alloc(); + let blake2s_ptr = blake2s_ptr_start; + let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9); + assert output.low = 219917655069954262743903159041439073909; + assert output.high = 296157033687865319468534978667166017272; + return (); +} \ No newline at end of file diff --git a/pkg/vm/cairo_run/cairo_run_test.go b/pkg/vm/cairo_run/cairo_run_test.go index 845a3076..8dea5736 100644 --- a/pkg/vm/cairo_run/cairo_run_test.go +++ b/pkg/vm/cairo_run/cairo_run_test.go @@ -348,3 +348,7 @@ func TestCairoKeccak(t *testing.T) { func TestKeccakAddUint256(t *testing.T) { testProgram("keccak_add_uint256", t) } + +func TestBlake2sHelloWorldHash(t *testing.T) { + testProgram("blake2s_hello_world_hash", t) +} From d9564ff8e321098daef95a9b4a33f9f2d53bb4b5 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:03:05 -0300 Subject: [PATCH 06/17] Implement BLAKE2S_COMPUTE --- pkg/hints/blake2s_hints.go | 84 ++++++++++++++++++++++ pkg/hints/hint_codes/blake2s_hint_codes.go | 4 ++ pkg/hints/hint_processor.go | 2 + pkg/lambdaworks/lambdaworks.go | 10 +++ 4 files changed, 100 insertions(+) create mode 100644 pkg/hints/blake2s_hints.go create mode 100644 pkg/hints/hint_codes/blake2s_hint_codes.go diff --git a/pkg/hints/blake2s_hints.go b/pkg/hints/blake2s_hints.go new file mode 100644 index 00000000..d66c4794 --- /dev/null +++ b/pkg/hints/blake2s_hints.go @@ -0,0 +1,84 @@ +package hints + +import ( + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" + . "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" +) + +func Uint32SliceToMRSlice(u32Slice []uint32) []MaybeRelocatable { + mRSlice := make([]MaybeRelocatable, 0, len(u32Slice)) + for _, u32 := range u32Slice { + mRSlice = append(mRSlice, *NewMaybeRelocatableFelt(FeltFromUint(uint(u32)))) + } + return mRSlice + +} + +func feltSliceToUint32Slice(feltSlice []Felt) ([]uint32, error) { + uint32Slice := make([]uint32, 0, len(feltSlice)) + for _, felt := range feltSlice { + val, err := felt.ToU32() + if err != nil { + return nil, err + } + uint32Slice = append(uint32Slice, val) + } + return uint32Slice, nil +} + +// Returns the range from (baseAddr - baseAddrOffset) to (baseAddr - baseAddrOffset + Size) +func getUint32MemoryRange(baseAddr Relocatable, baseAddrOffset uint, size uint, segments *MemorySegmentManager) ([]uint32, error) { + baseAddr, err := baseAddr.SubUint(baseAddrOffset) + if err != nil { + return nil, err + } + feltRange, err := segments.GetFeltRange(baseAddr, size) + if err != nil { + return nil, err + } + return feltSliceToUint32Slice(feltRange) +} + +// Returns the u32 value at memory[baseAddr - baseAddrOffset] +func getU32FromMemory(baseAddr Relocatable, baseAddrOffset uint, memory *Memory) (uint32, error) { + baseAddr, err := baseAddr.SubUint(baseAddrOffset) + if err != nil { + return 0, err + } + felt, err := memory.GetFelt(baseAddr) + if err != nil { + return 0, err + } + return felt.ToU32() +} + +func blake2sCompute(ids IdsManager, vm *VirtualMachine) error { + output, err := ids.GetRelocatable("output", vm) + if err != nil { + return err + } + h, err := getUint32MemoryRange(output, 26, 8, &vm.Segments) + if err != nil { + return err + } + message, err := getUint32MemoryRange(output, 18, 16, &vm.Segments) + if err != nil { + return err + } + t, err := getU32FromMemory(output, 2, &vm.Segments.Memory) + if err != nil { + return err + } + f, err := getU32FromMemory(output, 1, &vm.Segments.Memory) + if err != nil { + return err + } + + newState := Blake2sCompress([8]uint32(h), [16]uint32(message), t, 0, f, 0) + data := Uint32SliceToMRSlice(newState) + + _, err = vm.Segments.LoadData(output, &data) + return err +} diff --git a/pkg/hints/hint_codes/blake2s_hint_codes.go b/pkg/hints/hint_codes/blake2s_hint_codes.go new file mode 100644 index 00000000..fb26e1f5 --- /dev/null +++ b/pkg/hints/hint_codes/blake2s_hint_codes.go @@ -0,0 +1,4 @@ +package hint_codes + +const BLAKE2S_COMPUTE = `from starkware.cairo.common.cairo_blake2s.blake2s_utils import compute_blake2s_func +compute_blake2s_func(segments=segments, output_ptr=ids.output)` diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index ccac7bd2..3cd88a9d 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -198,6 +198,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return fastEcAddAssignNewX(data.Ids, vm, execScopes, "pt0", "pt1", SECP_P()) case FAST_EC_ADD_ASSIGN_NEW_Y: return fastEcAddAssignNewY(execScopes) + case BLAKE2S_COMPUTE: + return blake2sCompute(data.Ids, vm) default: return errors.Errorf("Unknown Hint: %s", data.Code) } diff --git a/pkg/lambdaworks/lambdaworks.go b/pkg/lambdaworks/lambdaworks.go index 3cd29d21..1362c1b9 100644 --- a/pkg/lambdaworks/lambdaworks.go +++ b/pkg/lambdaworks/lambdaworks.go @@ -8,6 +8,7 @@ package lambdaworks import "C" import ( + "math" "math/big" "reflect" "strings" @@ -101,6 +102,15 @@ func (felt Felt) ToUint() (uint, error) { return uint(felt_u64), nil } +// turns a felt to uint32 +func (felt Felt) ToU32() (uint32, error) { + feltU64, err := felt.ToU64() + if err != nil || feltU64 > math.MaxUint32 { + return 0, ConversionError(felt, "uint32") + } + return uint32(feltU64), nil +} + func (felt Felt) ToLeBytes() *[32]byte { var result_c [32]C.uint8_t var value C.felt_t = felt.toC() From 5039f585649599e45f0074dc0302a14c684d4b34 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:11:12 -0300 Subject: [PATCH 07/17] Add unit tests --- pkg/hints/blake2s_hints_test.go | 154 ++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 pkg/hints/blake2s_hints_test.go diff --git a/pkg/hints/blake2s_hints_test.go b/pkg/hints/blake2s_hints_test.go new file mode 100644 index 00000000..42b88d26 --- /dev/null +++ b/pkg/hints/blake2s_hints_test.go @@ -0,0 +1,154 @@ +package hints_test + +import ( + "testing" + + . "github.com/lambdaclass/cairo-vm.go/pkg/hints" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_codes" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" + . "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" +) + +func TestBlake2sComputeOutputOffsetZero(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + output := vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "output": {NewMaybeRelocatableRelocatable(output)}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_COMPUTE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err == nil { + t.Errorf("BLAKE2S_COMPUTE hint test should have failed") + } +} + +func TestBlake2sComputeOutputSegmentEmpty(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + output := vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "output": {NewMaybeRelocatableRelocatable(output.AddUint(26))}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_COMPUTE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err == nil { + t.Errorf("BLAKE2S_COMPUTE hint test should have failed") + } +} + +func TestBlake2sComputeBigOutput(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + output := vm.Segments.AddSegment() + data := []MaybeRelocatable{ + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + *NewMaybeRelocatableFelt(FeltFromDecString("7842562439562793675803603603688959")), + } + output, _ = vm.Segments.LoadData(output, &data) + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "output": {NewMaybeRelocatableRelocatable(output)}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_COMPUTE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err == nil { + t.Errorf("BLAKE2S_COMPUTE hint test should have failed") + } +} + +func TestBlake2sComputeOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + output := vm.Segments.AddSegment() + data := []MaybeRelocatable{ + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + *NewMaybeRelocatableFelt(FeltFromDecString("17")), + } + output, _ = vm.Segments.LoadData(output, &data) + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "output": {NewMaybeRelocatableRelocatable(output)}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_COMPUTE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err != nil { + t.Errorf("BLAKE2S_COMPUTE hint test failed with error %s", err) + } +} From e32a854feedd8a0f8ca9c2fc1553a1944b4ec02f Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:15:01 -0300 Subject: [PATCH 08/17] Add unit tests --- pkg/lambdaworks/lambdaworks.go | 2 +- pkg/lambdaworks/lambdaworks_test.go | 34 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/lambdaworks/lambdaworks.go b/pkg/lambdaworks/lambdaworks.go index 1362c1b9..c98606fc 100644 --- a/pkg/lambdaworks/lambdaworks.go +++ b/pkg/lambdaworks/lambdaworks.go @@ -106,7 +106,7 @@ func (felt Felt) ToUint() (uint, error) { func (felt Felt) ToU32() (uint32, error) { feltU64, err := felt.ToU64() if err != nil || feltU64 > math.MaxUint32 { - return 0, ConversionError(felt, "uint32") + return 0, ConversionError(felt, "u32") } return uint32(feltU64), nil } diff --git a/pkg/lambdaworks/lambdaworks_test.go b/pkg/lambdaworks/lambdaworks_test.go index bf7c404d..fdd3cd89 100644 --- a/pkg/lambdaworks/lambdaworks_test.go +++ b/pkg/lambdaworks/lambdaworks_test.go @@ -427,6 +427,40 @@ func TestToU64Fail(t *testing.T) { } } +func TestToU321(t *testing.T) { + felt := lambdaworks.FeltOne() + result, err := felt.ToU32() + + var expected uint32 = 1 + + if expected != result { + t.Errorf("Error in conversion expected: %v, got %v with err: %v", expected, result, err) + } + +} + +func TestToU3210230(t *testing.T) { + felt := lambdaworks.FeltFromUint64(10230) + result, err := felt.ToU32() + + var expected uint32 = 10230 + + if expected != result { + t.Errorf("Error in conversion expected: %v, got %v with err: %v", expected, result, err) + } +} + +func TestTo32Fail(t *testing.T) { + felt := lambdaworks.FeltFromUint64(4294967297) + + _, err := felt.ToU32() + expected_err := lambdaworks.ConversionError(felt, "u32") + + if err.Error() != expected_err.Error() { + t.Errorf("Conversion test should fail with error: %v", expected_err) + } +} + func TestToUint1(t *testing.T) { felt := lambdaworks.FeltOne() result, err := felt.ToUint() From 628dbcd623f7f3e9c52486abc821f100dfeb98bd Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:15:53 -0300 Subject: [PATCH 09/17] Add newline --- cairo_programs/blake2s_hello_world_hash.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo_programs/blake2s_hello_world_hash.cairo b/cairo_programs/blake2s_hello_world_hash.cairo index 9e832e7d..6d591cb9 100644 --- a/cairo_programs/blake2s_hello_world_hash.cairo +++ b/cairo_programs/blake2s_hello_world_hash.cairo @@ -17,4 +17,4 @@ func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { assert output.low = 219917655069954262743903159041439073909; assert output.high = 296157033687865319468534978667166017272; return (); -} \ No newline at end of file +} From e3ae43d81b432ad735f9d4bf4d9267edab238021 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:31:51 -0300 Subject: [PATCH 10/17] Add integration test --- cairo_programs/blake2s_felts.cairo | 35 ++++++++++++++++++++++++++++++ pkg/vm/cairo_run/cairo_run_test.go | 4 ++++ 2 files changed, 39 insertions(+) create mode 100644 cairo_programs/blake2s_felts.cairo diff --git a/cairo_programs/blake2s_felts.cairo b/cairo_programs/blake2s_felts.cairo new file mode 100644 index 00000000..fd913b2a --- /dev/null +++ b/cairo_programs/blake2s_felts.cairo @@ -0,0 +1,35 @@ +%builtins range_check bitwise + +from starkware.cairo.common.bool import TRUE +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.cairo_blake2s.blake2s import blake2s_felts +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin + +func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { + alloc_locals; + let inputs: felt* = alloc(); + assert inputs[0] = 3456722; + assert inputs[1] = 435425528; + assert inputs[2] = 3232553; + assert inputs[3] = 2576195; + assert inputs[4] = 73471943; + assert inputs[5] = 17549868; + assert inputs[6] = 87158958; + assert inputs[7] = 6353668; + assert inputs[8] = 343656565; + assert inputs[9] = 1255962; + assert inputs[10] = 25439785; + assert inputs[11] = 1154578; + assert inputs[12] = 585849303; + assert inputs[13] = 763502; + assert inputs[14] = 43753647; + assert inputs[15] = 74256930; + let (local blake2s_ptr_start) = alloc(); + let blake2s_ptr = blake2s_ptr_start; + let (result) = blake2s_felts{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}( + 16, inputs, TRUE + ); + assert result.low = 23022179997536219430502258022509199703; + assert result.high = 136831746058902715979837770794974289597; + return (); +} diff --git a/pkg/vm/cairo_run/cairo_run_test.go b/pkg/vm/cairo_run/cairo_run_test.go index 8dea5736..eaf02fc4 100644 --- a/pkg/vm/cairo_run/cairo_run_test.go +++ b/pkg/vm/cairo_run/cairo_run_test.go @@ -352,3 +352,7 @@ func TestKeccakAddUint256(t *testing.T) { func TestBlake2sHelloWorldHash(t *testing.T) { testProgram("blake2s_hello_world_hash", t) } + +func TestBlake2sFelts(t *testing.T) { + testProgram("blake2s_felts", t) +} From a87d704b125cbabd13187e186a79e4c62628ffe9 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:46:14 -0300 Subject: [PATCH 11/17] Implement BLAKE2S_ADD_UINT256_BIGEND hint --- pkg/hints/blake2s_hints.go | 38 ++++++++++++++++++++++ pkg/hints/hint_codes/blake2s_hint_codes.go | 5 +++ pkg/hints/hint_processor.go | 2 ++ 3 files changed, 45 insertions(+) diff --git a/pkg/hints/blake2s_hints.go b/pkg/hints/blake2s_hints.go index d66c4794..144ae54f 100644 --- a/pkg/hints/blake2s_hints.go +++ b/pkg/hints/blake2s_hints.go @@ -1,6 +1,8 @@ package hints import ( + "math" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" . "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" . "github.com/lambdaclass/cairo-vm.go/pkg/vm" @@ -82,3 +84,39 @@ func blake2sCompute(ids IdsManager, vm *VirtualMachine) error { _, err = vm.Segments.LoadData(output, &data) return err } + +func blake2sAddUint256Bigend(ids IdsManager, vm *VirtualMachine) error { + // Fetch ids variables + dataPtr, err := ids.GetRelocatable("data", vm) + if err != nil { + return err + } + low, err := ids.GetFelt("low", vm) + if err != nil { + return err + } + high, err := ids.GetFelt("high", vm) + if err != nil { + return err + } + // Hint logic + const MASK = math.MaxUint32 + const B = 32 + mask := FeltFromUint(MASK) + // First batch + data := make([]MaybeRelocatable, 0, 4) + for i := uint(0); i < 4; i++ { + data = append(data, *NewMaybeRelocatableFelt(high.Shr(B * (3 - i)).And(mask))) + } + dataPtr, err = vm.Segments.LoadData(dataPtr, &data) + if err != nil { + return err + } + // Second batch + data = make([]MaybeRelocatable, 0, 4) + for i := uint(0); i < 4; i++ { + data = append(data, *NewMaybeRelocatableFelt(low.Shr(B * (3 - i)).And(mask))) + } + _, err = vm.Segments.LoadData(dataPtr, &data) + return err +} diff --git a/pkg/hints/hint_codes/blake2s_hint_codes.go b/pkg/hints/hint_codes/blake2s_hint_codes.go index fb26e1f5..b6ea16f8 100644 --- a/pkg/hints/hint_codes/blake2s_hint_codes.go +++ b/pkg/hints/hint_codes/blake2s_hint_codes.go @@ -2,3 +2,8 @@ package hint_codes const BLAKE2S_COMPUTE = `from starkware.cairo.common.cairo_blake2s.blake2s_utils import compute_blake2s_func compute_blake2s_func(segments=segments, output_ptr=ids.output)` + +const BLAKE2S_ADD_UINT256_BIGEND = `B = 32 +MASK = 2 ** 32 - 1 +segments.write_arg(ids.data, [(ids.high >> (B * (3 - i))) & MASK for i in range(4)]) +segments.write_arg(ids.data + 4, [(ids.low >> (B * (3 - i))) & MASK for i in range(4)])` diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 3cd88a9d..136e8c9c 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -200,6 +200,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return fastEcAddAssignNewY(execScopes) case BLAKE2S_COMPUTE: return blake2sCompute(data.Ids, vm) + case BLAKE2S_ADD_UINT256_BIGEND: + return blake2sAddUint256Bigend(data.Ids, vm) default: return errors.Errorf("Unknown Hint: %s", data.Code) } From d7004d936c268169fc69a752ed5a777423b549d4 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 12:58:43 -0300 Subject: [PATCH 12/17] Add unit test --- pkg/hints/blake2s_hints_test.go | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pkg/hints/blake2s_hints_test.go b/pkg/hints/blake2s_hints_test.go index 42b88d26..ca761455 100644 --- a/pkg/hints/blake2s_hints_test.go +++ b/pkg/hints/blake2s_hints_test.go @@ -1,6 +1,7 @@ package hints_test import ( + "reflect" "testing" . "github.com/lambdaclass/cairo-vm.go/pkg/hints" @@ -152,3 +153,41 @@ func TestBlake2sComputeOk(t *testing.T) { t.Errorf("BLAKE2S_COMPUTE hint test failed with error %s", err) } } + +func TestBlake2sAddUint256BigEndOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + data := vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "data": {NewMaybeRelocatableRelocatable(data)}, + "high": {NewMaybeRelocatableFelt(FeltFromUint(25))}, + "low": {NewMaybeRelocatableFelt(FeltFromUint(20))}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_ADD_UINT256_BIGEND, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err != nil { + t.Errorf("BLAKE2S_ADD_UINT256_BIGEND hint test failed with error %s", err) + } + // Check the data segment + dataSegment, err := vm.Segments.GetFeltRange(data, 8) + expectedDataSegment := []Felt{ + FeltZero(), + FeltZero(), + FeltZero(), + FeltFromUint(25), + FeltZero(), + FeltZero(), + FeltZero(), + FeltFromUint(20), + } + if err != nil || !reflect.DeepEqual(dataSegment, expectedDataSegment) { + t.Errorf("Wrong/No data loaded.\n Expected %v, got %v", expectedDataSegment, dataSegment) + } +} From c55aefdb4b07572916955857ff1c54844c9cb981 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 13:09:16 -0300 Subject: [PATCH 13/17] Add integration test --- cairo_programs/finalize_blake2s.cairo | 18 ++++++++++++++++++ pkg/vm/cairo_run/cairo_run_test.go | 4 ++++ 2 files changed, 22 insertions(+) create mode 100644 cairo_programs/finalize_blake2s.cairo diff --git a/cairo_programs/finalize_blake2s.cairo b/cairo_programs/finalize_blake2s.cairo new file mode 100644 index 00000000..9c7dd7f7 --- /dev/null +++ b/cairo_programs/finalize_blake2s.cairo @@ -0,0 +1,18 @@ +%builtins range_check bitwise + +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.cairo_blake2s.blake2s import blake2s, finalize_blake2s +from starkware.cairo.common.cairo_builtins import BitwiseBuiltin + +func main{range_check_ptr, bitwise_ptr: BitwiseBuiltin*}() { + alloc_locals; + let inputs: felt* = alloc(); + assert inputs[0] = 'Hell'; + assert inputs[1] = 'o Wo'; + assert inputs[2] = 'rld'; + let (local blake2s_ptr_start) = alloc(); + let blake2s_ptr = blake2s_ptr_start; + let (output) = blake2s{range_check_ptr=range_check_ptr, blake2s_ptr=blake2s_ptr}(inputs, 9); + finalize_blake2s(blake2s_ptr_start, blake2s_ptr); + return (); +} diff --git a/pkg/vm/cairo_run/cairo_run_test.go b/pkg/vm/cairo_run/cairo_run_test.go index eaf02fc4..7927f305 100644 --- a/pkg/vm/cairo_run/cairo_run_test.go +++ b/pkg/vm/cairo_run/cairo_run_test.go @@ -356,3 +356,7 @@ func TestBlake2sHelloWorldHash(t *testing.T) { func TestBlake2sFelts(t *testing.T) { testProgram("blake2s_felts", t) } + +func TestFinalizeBlake2s(t *testing.T) { + testProgram("finalize_blake2s", t) +} From 6b90c983c5b44eaed5e5c7f95ac1867688c486a4 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 13:31:39 -0300 Subject: [PATCH 14/17] Implement finalize_blake2s hint --- pkg/hints/blake2s_hints.go | 22 ++++++++++++++++++++++ pkg/hints/hint_codes/blake2s_hint_codes.go | 21 +++++++++++++++++++++ pkg/hints/hint_processor.go | 2 ++ 3 files changed, 45 insertions(+) diff --git a/pkg/hints/blake2s_hints.go b/pkg/hints/blake2s_hints.go index 144ae54f..8a678812 100644 --- a/pkg/hints/blake2s_hints.go +++ b/pkg/hints/blake2s_hints.go @@ -120,3 +120,25 @@ func blake2sAddUint256Bigend(ids IdsManager, vm *VirtualMachine) error { _, err = vm.Segments.LoadData(dataPtr, &data) return err } + +func blake2sFinalize(ids IdsManager, vm *VirtualMachine) error { + const N_PACKED_INSTANCES = 7 + blake2sPtrEnd, err := ids.GetRelocatable("blake2s_ptr_end", vm) + if err != nil { + return err + } + var message [16]uint32 + modifiedIv := IV() + output := Blake2sCompress(modifiedIv, message, 0, 0, 0xffffffff, 0) + padding := modifiedIv[:] + padding = append(padding, message[:]...) + padding = append(padding, 0, 0xffffffff) + padding = append(padding, output[:]...) + fullPadding := padding + for i := 2; i < N_PACKED_INSTANCES; i++ { + fullPadding = append(fullPadding, padding...) + } + data := Uint32SliceToMRSlice(fullPadding) + _, err = vm.Segments.LoadData(blake2sPtrEnd, &data) + return err +} diff --git a/pkg/hints/hint_codes/blake2s_hint_codes.go b/pkg/hints/hint_codes/blake2s_hint_codes.go index b6ea16f8..c75a9990 100644 --- a/pkg/hints/hint_codes/blake2s_hint_codes.go +++ b/pkg/hints/hint_codes/blake2s_hint_codes.go @@ -7,3 +7,24 @@ const BLAKE2S_ADD_UINT256_BIGEND = `B = 32 MASK = 2 ** 32 - 1 segments.write_arg(ids.data, [(ids.high >> (B * (3 - i))) & MASK for i in range(4)]) segments.write_arg(ids.data + 4, [(ids.low >> (B * (3 - i))) & MASK for i in range(4)])` + +const BLAKE2S_FINALIZE = `# Add dummy pairs of input and output. +from starkware.cairo.common.cairo_blake2s.blake2s_utils import IV, blake2s_compress + +_n_packed_instances = int(ids.N_PACKED_INSTANCES) +assert 0 <= _n_packed_instances < 20 +_blake2s_input_chunk_size_felts = int(ids.INPUT_BLOCK_FELTS) +assert 0 <= _blake2s_input_chunk_size_felts < 100 + +message = [0] * _blake2s_input_chunk_size_felts +modified_iv = [IV[0] ^ 0x01010020] + IV[1:] +output = blake2s_compress( + message=message, + h=modified_iv, + t0=0, + t1=0, + f0=0xffffffff, + f1=0, +) +padding = (modified_iv + message + [0, 0xffffffff] + output) * (_n_packed_instances - 1) +segments.write_arg(ids.blake2s_ptr_end, padding)` diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 136e8c9c..a43edae5 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -202,6 +202,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return blake2sCompute(data.Ids, vm) case BLAKE2S_ADD_UINT256_BIGEND: return blake2sAddUint256Bigend(data.Ids, vm) + case BLAKE2S_FINALIZE: + return blake2sFinalize(data.Ids, vm) default: return errors.Errorf("Unknown Hint: %s", data.Code) } From 2e6a7e0c2024294299b95c2840b85ece1c8334c6 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 15:04:35 -0300 Subject: [PATCH 15/17] Fix removed line --- pkg/hints/blake2s_hints.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/hints/blake2s_hints.go b/pkg/hints/blake2s_hints.go index 8a678812..c3382da3 100644 --- a/pkg/hints/blake2s_hints.go +++ b/pkg/hints/blake2s_hints.go @@ -129,6 +129,7 @@ func blake2sFinalize(ids IdsManager, vm *VirtualMachine) error { } var message [16]uint32 modifiedIv := IV() + modifiedIv[0] = modifiedIv[0] ^ 0x01010020 output := Blake2sCompress(modifiedIv, message, 0, 0, 0xffffffff, 0) padding := modifiedIv[:] padding = append(padding, message[:]...) From 3d44715a7b247f954120ec8af5adf948a4574ef3 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 15:05:23 -0300 Subject: [PATCH 16/17] Add unit test --- pkg/hints/blake2s_hints_test.go | 52 +++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pkg/hints/blake2s_hints_test.go b/pkg/hints/blake2s_hints_test.go index ca761455..a7d1cbde 100644 --- a/pkg/hints/blake2s_hints_test.go +++ b/pkg/hints/blake2s_hints_test.go @@ -191,3 +191,55 @@ func TestBlake2sAddUint256BigEndOk(t *testing.T) { t.Errorf("Wrong/No data loaded.\n Expected %v, got %v", expectedDataSegment, dataSegment) } } + +func TestBlake2sFinaizeOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + data := vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "blake2s_ptr_end": {NewMaybeRelocatableRelocatable(data)}, + }, + vm, + ) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: BLAKE2S_FINALIZE, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil) + if err != nil { + t.Errorf("BLAKE2S_FINALIZE hint test failed with error %s", err) + } + // Check the data segment + dataSegment, err := vm.Segments.GetFeltRange(data, 204) + + expectedDataSegment := []Felt{ + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + + FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), + FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), + } + if err != nil || !reflect.DeepEqual(dataSegment, expectedDataSegment) { + t.Errorf("Wrong/No data loaded.\n Expected: %v.\n Got: %v", expectedDataSegment, dataSegment) + } +} From 101dc93e0b915fb0acc9a8b6fd61e7e74dbe03a9 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 3 Oct 2023 15:09:19 -0300 Subject: [PATCH 17/17] Fix test values --- pkg/hints/blake2s_hints_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/hints/blake2s_hints_test.go b/pkg/hints/blake2s_hints_test.go index a7d1cbde..d633820e 100644 --- a/pkg/hints/blake2s_hints_test.go +++ b/pkg/hints/blake2s_hints_test.go @@ -215,27 +215,27 @@ func TestBlake2sFinaizeOk(t *testing.T) { dataSegment, err := vm.Segments.GetFeltRange(data, 204) expectedDataSegment := []Felt{ - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), - FeltFromUint(179574535), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), + FeltFromUint(1795745351), FeltFromUint(3144134277), FeltFromUint(1013904242), FeltFromUint(2773480762), FeltFromUint(1359893119), FeltFromUint(2600822924), FeltFromUint(528734635), FeltFromUint(1541459225), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltZero(), FeltFromUint(4294967295), FeltFromUint(813310313), FeltFromUint(2491453561), FeltFromUint(3491828193), FeltFromUint(2085238082), FeltFromUint(1219908895), FeltFromUint(514171180), FeltFromUint(4245497115), FeltFromUint(4193177630), }