Skip to content

Conversation

@mshsdev
Copy link
Contributor

@mshsdev mshsdev commented May 9, 2024

This PR adds the bits package with an Add function.
The function can check if the sum of two integers overflows or not.
An example of this function's use is in a modular addition function, as shown below:

func modAdd(x uint256, y uint256, mod uint256) uint256 {
	zero := uint256(0)
	sum, overflow := bits.Add(x, y, zero)
	if overflow != 0 || sum > mod {
		sum -= mod
	}

	return sum
}

Note that above example modAdd only works when mod is greater than 2^128 .

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.

1 participant