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

Add bits package with add function #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hextrust-0
Copy link
Contributor

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