-
Notifications
You must be signed in to change notification settings - Fork 31
Add tryAdd and trySub to FHESafeMath
#206
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5efb358
Add `tryAdd` and `trySub` to `FHESafeMath`
arr00 98e5fe8
add tests
arr00 2202969
add changeset
arr00 5dcad6e
optimize
arr00 c05b8dd
fix comment
arr00 df8e85e
Update test/utils/FHESafeMath.test.ts
arr00 f091a88
up
arr00 a3e4210
Update test/utils/FHESafeMath.test.ts
arr00 2b92ca1
Update test/utils/FHESafeMath.test.ts
arr00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-confidential-contracts': minor | ||
| --- | ||
|
|
||
| `FHESafeMath`: Add `tryAdd` and `trySub` functions that return 0 upon failure. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { callAndGetResult } from '../helpers/event'; | |
| import { FhevmType } from '@fhevm/hardhat-plugin'; | ||
| import { HardhatEthersSigner } from '@nomicfoundation/hardhat-ethers/signers'; | ||
| import { expect } from 'chai'; | ||
| import { BigNumberish } from 'ethers'; | ||
| import { ethers, fhevm } from 'hardhat'; | ||
|
|
||
| const MaxUint64 = BigInt('0xffffffffffffffff'); | ||
|
|
@@ -91,4 +92,71 @@ describe('FHESafeMath', function () { | |
| }); | ||
| } | ||
| }); | ||
|
|
||
| const addArgsOptions: [BigNumberish | undefined, BigNumberish | undefined, BigNumberish | undefined, boolean][] = [ | ||
| // a + b = c & success | ||
| [undefined, undefined, undefined, true], | ||
| [undefined, 1, 1, true], | ||
| [1, undefined, 1, true], | ||
| [1, 1, 2, true], | ||
| [0, 1, 1, true], | ||
| [0, MaxUint64, MaxUint64, true], | ||
| [1, MaxUint64, 0, false], | ||
| [MaxUint64 - 1n, 2, 0, false], | ||
| [MaxUint64, MaxUint64, 0, false], | ||
| ]; | ||
| const subArgsOptions: [BigNumberish | undefined, BigNumberish | undefined, BigNumberish | undefined, boolean][] = [ | ||
| // a - b = c & success | ||
| [undefined, undefined, undefined, true], | ||
| [undefined, 0, 0, true], | ||
| [0, undefined, 0, true], | ||
| [1, 1, 0, true], | ||
arr00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [1, undefined, 1, true], | ||
| [undefined, 1, 0, false], | ||
| [0, 1, 0, false], | ||
| [MaxUint64, MaxUint64, 0, true], | ||
| ]; | ||
|
|
||
| for (const params of [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe be can merge |
||
| { | ||
| functionSignature: 'tryAdd(bytes32,bytes32)', | ||
| argsOptions: addArgsOptions, | ||
| operation: '+', | ||
| }, | ||
| { | ||
| functionSignature: 'trySub(bytes32,bytes32)', | ||
| argsOptions: subArgsOptions, | ||
| operation: '-', | ||
| }, | ||
| ]) { | ||
| describe(`try ${params.functionSignature.startsWith('tryAdd') ? 'add' : 'sub'}`, function () { | ||
| for (const args of params.argsOptions) { | ||
| it(`${args[0]} ${params.operation} ${args[1]} = ${args[2]} & ${ | ||
| args[3] ? 'success' : 'failure' | ||
| }`, async function () { | ||
| const [a, b, c, expected] = args; | ||
| const [handleA] = | ||
| a !== undefined | ||
| ? await callAndGetResult(fheSafeMath.createHandle(a), handleCreatedSignature) | ||
| : [ethers.ZeroHash]; | ||
| const [handleB] = | ||
| b !== undefined | ||
| ? await callAndGetResult(fheSafeMath.createHandle(b), handleCreatedSignature) | ||
| : [ethers.ZeroHash]; | ||
| const [success, updated] = await callAndGetResult( | ||
| (fheSafeMath as any)[params.functionSignature](handleA, handleB), | ||
| resultComputedSignature, | ||
| ); | ||
| if (c !== undefined) { | ||
| await expect( | ||
| fhevm.userDecryptEuint(FhevmType.euint64, updated, fheSafeMath.target, account), | ||
| ).to.eventually.equal(c); | ||
| } else { | ||
| expect(updated).to.eq(ethers.ZeroHash); | ||
| } | ||
| await expect(fhevm.userDecryptEbool(success, fheSafeMath.target, account)).to.eventually.equal(expected); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.