Skip to content

Conversation

@notJoon
Copy link
Member

@notJoon notJoon commented Nov 28, 2025

Summary

This PR optimizes the int256 package by removing unused functions and improving initialization performance.

Changes

  • Remove 25+ unused arithmetic and utility functions that are not used in this codebase
  • Optimize MaxInt256 and MinInt256 initialization using direct word arrays instead of decimal parsing
  • Avoid unnecessary SetInt64 calls when creating new Int256 arrays
  • Update gas measurement tests to reflect the optimizations

Removed Functions

The following functions were removed as they are not used in any contract:

  • AddUint256, SubUint256, MulUint256, DivUint256
  • AddDelta, AddDeltaOverflow
  • Quo, Rem, Mod
  • And (bitwise operation)
  • Neq, Lte, Gte (comparison operations)
  • SetUint64, SetUint256
  • NegOverflow, AbsGt, AbsLt, AbsOverflow
  • And several others...

@notJoon notJoon marked this pull request as ready for review November 28, 2025 06:10
notJoon and others added 3 commits November 28, 2025 15:15
…ion and related tests

- Remove panic on zero amount to allow valid handling
- Update test case to reflect new behavior, ensuring zero amount does not panic and validates the result correctly
Copy link
Member

@jinoosss jinoosss left a comment

Choose a reason for hiding this comment

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

There is a structural issue with Int256 package operations.
When overflow occurs, the value is set to the minimum or maximum value of uint256, not the minimum or maximum value of int256.

As this is a structural issue, thorough discussion will likely be necessary.

func (z *Int) Add(x, y *Int) *Int {
	z = z.initiateAbs()

	if x.neg == y.neg {
		// If both numbers have the same sign, add their absolute values
		z.abs.Add(x.abs, y.abs)
		z.neg = x.neg
	} else {
		// If signs are different, subtract the smaller absolute value from the larger
		if x.abs.Cmp(y.abs) >= 0 {
			z.abs.Sub(x.abs, y.abs)
			z.neg = x.neg
		} else {
			z.abs.Sub(y.abs, x.abs)
			z.neg = y.neg
		}
	}

	// Ensure zero is always positive
	if z.abs.IsZero() {
		z.neg = false
	}

	return z
}

@sonarqubecloud
Copy link

@notJoon
Copy link
Member Author

notJoon commented Nov 28, 2025

replaced by #1021

@notJoon notJoon closed this Nov 28, 2025
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.

3 participants