-
Notifications
You must be signed in to change notification settings - Fork 43
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
Conversation
@@ -330,6 +342,76 @@ func TestComputeAddRes(t *testing.T) { | |||
assert.Equal(t, expected, res) | |||
} | |||
|
|||
func TestComputeAddRes_Jake(t *testing.T) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
} 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.
} 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") | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay.
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 | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
// // 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 | ||
// } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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)), |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. Will modify it.
name: "Type E : IntegerPlusMemoryAddress", | ||
op0: mem.MemoryValueFromInt(15), | ||
op1: mem.MemoryValueFromMemoryAddress(mem.NewMemoryAddress(2, 10)), | ||
shouldFail: true, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Fixes #56
I made changes to the memory_value.go and vm_test.go
Please review and let me know the followup actions.