You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello.
I was using the Rsh method and came across an unexpected behavior. I think this code might reproduce the problem.
I am comparing it with uint256.
Result uint256: 18446050711097703530
Result int256: 18446050711097703530
I think it has to do with the order some things are done here:
func (z *Int) rsh(x *Int, n uint) *Int {
if n >= 255 {
return z.Clear()
}
switch {
case n >= 192:
n -= 192
z[3], z[2], z[1], z[0] = 0, 0, 0, x[3]>>n
case n >= 128:
n -= 128
z[3], z[2] = 0, 0// If z and x are the same reference, then this will change x[3] and x[2] as well
z[1] = x[3] >> n // causing this to be z[1] = 0 >> n.
z[0] = (x[3] << (64 - n)) | (x[2] >> n) // and this to be (0 << (64 - n)) | (0 >> n).
case n >= 64:
// I think this case have the same problem as the previous one.
n -= 64
z[3] = 0
z[2] = x[3] >> n
z[1] = (x[3] << (64 - n)) | (x[2] >> n)
z[0] = (x[2] << (64 - n)) | (x[1] >> n)
default:
z[3] = x[3] >> n
z[2] = (x[3] << (64 - n)) | (x[2] >> n)
z[1] = (x[2] << (64 - n)) | (x[1] >> n)
z[0] = (x[1] << (64 - n)) | (x[0] >> n)
}
return `z`
}
It worked when I changed to:
func (z *Int) rsh(x *Int, n uint) *Int {
if n >= 255 {
return z.Clear()
}
switch {
case n >= 192:
n -= 192
z[3], z[2], z[1], z[0] = 0, 0, 0, x[3]>>n
case n >= 128:
n -= 128
z[0] = (x[3] << (64 - n)) | (x[2] >> n)
z[1] = x[3] >> n
z[3], z[2] = 0, 0
case n >= 64:
n -= 64
z[0] = (x[2] << (64 - n)) | (x[1] >> n)
z[1] = (x[3] << (64 - n)) | (x[2] >> n)
z[2] = x[3] >> n
z[3] = 0
default:
z[0] = (x[1] << (64 - n)) | (x[0] >> n)
z[1] = (x[2] << (64 - n)) | (x[1] >> n)
z[2] = (x[3] << (64 - n)) | (x[2] >> n)
z[3] = x[3] >> n
}
return z
}
I hope this can help somehow.
The text was updated successfully, but these errors were encountered:
Hello.
I was using the Rsh method and came across an unexpected behavior. I think this code might reproduce the problem.
I am comparing it with uint256.
The code produce this result:
A Rsh operation where z and x are same have that error, but if we use a new receiver as below it does not happen.
The result is the same as uint256.
I think it has to do with the order some things are done here:
It worked when I changed to:
I hope this can help somehow.
The text was updated successfully, but these errors were encountered: