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

Fixes #56 : new add functions and test for the changes #60

Closed
wants to merge 1 commit into from

Conversation

jkktom
Copy link
Contributor

@jkktom jkktom commented Sep 22, 2023

Fixes #56
I made changes to the memory_value.go and vm_test.go
Please review and let me know the followup actions.

@@ -330,6 +342,76 @@ func TestComputeAddRes(t *testing.T) {
assert.Equal(t, expected, res)
}

func TestComputeAddRes_Jake(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please follow the current style of testing, where each case gets its own function. If we want to change the style, we should do it then for the whole project to be consistent across packages

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. I will change it as the current style.

Comment on lines +240 to +244
} else if rhs.IsFelt() {
// lhs : felt, rhs : felt
mv.felt = mv.felt.Add(lhs.felt, rhs.felt)
} else {
return nil, errors.New("invalid rhs type")
Copy link
Contributor

Choose a reason for hiding this comment

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

It is safe to go for just an else here, no need to check if it is a felt. Memory values are either one or the other. There are never the two at the same time, or none.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it.

Comment on lines +230 to 235
} else if rhs.IsFelt() {
// lhs : Address, rhs : felt
mv.address, err = mv.address.Add(lhs.address, rhs.felt)
} else {
return nil, errors.New("invalid rhs type")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

A simple else is enough here, no need to check if it is a field element after checking if it is an address

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay.

Comment on lines +210 to +218
func (address *MemoryAddress) Add(lhs *MemoryAddress, rhs *f.Element) (*MemoryAddress, error) {
if !rhs.IsUint64() {
return nil, fmt.Errorf("field element does not fit in uint64: %s", rhs.String())
}

address.SegmentIndex = lhs.SegmentIndex
address.Offset = lhs.Offset + rhs.Uint64()
return address, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you change the position of this function definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it back to where it was. I was carefully checking if there's any missing cases.

Comment on lines +186 to +207
// // Adds two memory values is the second one is a Felt
// func (mv *MemoryValue) Add(lhs, rhs *MemoryValue) (*MemoryValue, error) {
// var err error
// if lhs.IsAddress() {
// if !rhs.IsFelt() {
// return nil, errors.New("rhs is not a felt")
// }
// // lhs : Address, rhs : Address
// mv.address, err = mv.address.Add(lhs.address, rhs.felt)
// } else {
// if rhs.IsAddress() {
// mv.address, err = mv.address.Add(rhs.address, lhs.felt)
// } else {
// mv.felt = mv.felt.Add(lhs.felt, rhs.felt)
// }
// }

// if err != nil {
// return nil, err
// }
// return mv, nil
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we leaving this comment here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will erase. I tried to compare it side by side before / after the change

Comment on lines +361 to +364
name: "Type B : MemoryAddressPlusMemoryAddress_SameSegment",
op0: mem.MemoryValueFromMemoryAddress(mem.NewMemoryAddress(2, 10)),
op1: mem.MemoryValueFromMemoryAddress(mem.NewMemoryAddress(2, 15)),
expected: mem.MemoryValueFromMemoryAddress(mem.NewMemoryAddress(2, 25)),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should crash, since address + address is not allowed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Will modify it.

Comment on lines +379 to +382
name: "Type E : IntegerPlusMemoryAddress",
op0: mem.MemoryValueFromInt(15),
op1: mem.MemoryValueFromMemoryAddress(mem.NewMemoryAddress(2, 10)),
shouldFail: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

This one should succeed since sum operations are commutative, addr + felt = felt + addr

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay will look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test correct calculation of the res auxiliar value
2 participants