Skip to content

Commit 22c9de6

Browse files
committed
feat: Migrate PrivateToken and PrivateAuction to 256-bit operations
- Update contracts to use ctUint256/gtUint256/itUint256 types - Migrate tests to encryptValue256/decryptValue256 methods
1 parent 0c5072a commit 22c9de6

File tree

4 files changed

+89
-82
lines changed

4 files changed

+89
-82
lines changed

contracts/PrivateAuction.sol

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ contract PrivateAuction {
1111
address public beneficiary;
1212

1313
// Current highest bid.
14-
ctUint64 internal highestBid;
14+
ctUint256 internal highestBid;
1515

1616
// Mapping from bidder to their bid value.
17-
mapping(address => ctUint64) internal bids;
17+
mapping(address => ctUint256) internal bids;
1818

1919
// Number of bid
2020
uint public bidCounter;
@@ -63,13 +63,13 @@ contract PrivateAuction {
6363
}
6464

6565
function bid(
66-
itUint64 calldata itBid
66+
itUint256 calldata itBid
6767
) public onlyBeforeEnd {
68-
ctUint64 existingBid = bids[msg.sender];
68+
ctUint256 memory existingBid = bids[msg.sender];
6969

70-
gtUint64 gtBid = MpcCore.validateCiphertext(itBid);
70+
gtUint256 gtBid = MpcCore.validateCiphertext(itBid);
7171

72-
if (ctUint64.unwrap(existingBid) == 0) {
72+
if (ctUint128.unwrap(existingBid.ciphertextHigh) == 0 && ctUint128.unwrap(existingBid.ciphertextLow) == 0) {
7373
bidCounter++;
7474
bids[msg.sender] = MpcCore.offBoard(gtBid);
7575
tokenContract.transferFrom(
@@ -86,7 +86,7 @@ contract PrivateAuction {
8686
)
8787
) {
8888
bids[msg.sender] = MpcCore.offBoard(gtBid);
89-
gtUint64 toTransfer = MpcCore.sub(
89+
gtUint256 toTransfer = MpcCore.sub(
9090
gtBid,
9191
MpcCore.onBoard(existingBid)
9292
);
@@ -96,9 +96,9 @@ contract PrivateAuction {
9696
toTransfer
9797
);
9898
}
99-
ctUint64 currentBid = bids[msg.sender];
99+
ctUint256 memory currentBid = bids[msg.sender];
100100
if (
101-
ctUint64.unwrap(highestBid) == 0 ||
101+
(ctUint128.unwrap(highestBid.ciphertextHigh) == 0 && ctUint128.unwrap(highestBid.ciphertextLow) == 0) ||
102102
MpcCore.decrypt(
103103
MpcCore.ge(
104104
MpcCore.onBoard(existingBid),
@@ -110,8 +110,8 @@ contract PrivateAuction {
110110
}
111111
}
112112

113-
function getBid() public returns (ctUint64) {
114-
gtUint64 bidGt = MpcCore.onBoard(bids[msg.sender]);
113+
function getBid() public returns (ctUint256 memory) {
114+
gtUint256 bidGt = MpcCore.onBoard(bids[msg.sender]);
115115
return MpcCore.offBoardToUser(bidGt, msg.sender);
116116
}
117117

@@ -122,9 +122,10 @@ contract PrivateAuction {
122122

123123
function doIHaveHighestBid() public {
124124
gtBool isHighest = MpcCore.setPublic(false);
125+
ctUint256 memory userBid = bids[msg.sender];
125126
if (
126-
ctUint64.unwrap(highestBid) != 0 &&
127-
ctUint64.unwrap(bids[msg.sender]) != 0
127+
!(ctUint128.unwrap(highestBid.ciphertextHigh) == 0 && ctUint128.unwrap(highestBid.ciphertextLow) == 0) &&
128+
!(ctUint128.unwrap(userBid.ciphertextHigh) == 0 && ctUint128.unwrap(userBid.ciphertextLow) == 0)
128129
) {
129130
isHighest = MpcCore.ge(
130131
MpcCore.onBoard(bids[msg.sender]),
@@ -147,7 +148,7 @@ contract PrivateAuction {
147148
if (MpcCore.decrypt(canClaim)) {
148149
objectClaimed = MpcCore.offBoard(MpcCore.setPublic(true));
149150
bids[msg.sender] = MpcCore.offBoardToUser(
150-
MpcCore.setPublic64(0),
151+
MpcCore.setPublic256(0),
151152
msg.sender
152153
);
153154
emit Winner(msg.sender);
@@ -166,7 +167,7 @@ contract PrivateAuction {
166167

167168
// Withdraw a bid from the auction to the caller once the auction has stopped.
168169
function withdraw() public onlyAfterEnd {
169-
gtUint64 bidValue = MpcCore.onBoard(bids[msg.sender]);
170+
gtUint256 bidValue = MpcCore.onBoard(bids[msg.sender]);
170171
gtBool isHighestBid = MpcCore.ge(bidValue, MpcCore.onBoard(highestBid));
171172
gtBool canWithdraw = MpcCore.not(
172173
MpcCore.and(
@@ -176,7 +177,7 @@ contract PrivateAuction {
176177
);
177178
if (MpcCore.decrypt(canWithdraw)) {
178179
bids[msg.sender] = MpcCore.offBoardToUser(
179-
MpcCore.setPublic64(0),
180+
MpcCore.setPublic256(0),
180181
msg.sender
181182
);
182183
tokenContract.transfer(msg.sender, bidValue);

contracts/PrivateToken.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import {PrivateERC20} from "@coti-io/coti-contracts/contracts/token/PrivateERC20
66
import "@coti-io/coti-contracts/contracts/utils/mpc/MpcCore.sol";
77

88
contract PrivateToken is PrivateERC20 {
9-
uint64 private _totalSupply;
9+
uint256 private _totalSupply;
1010

1111
constructor(string memory name_, string memory symbol_) PrivateERC20(name_, symbol_) {}
1212

1313
function totalSupply() public view override returns (uint256) {
1414
return _totalSupply;
1515
}
1616

17-
function mint(address account, uint64 amount) external {
18-
gtBool success = _mint(account, MpcCore.setPublic64(amount));
17+
function mint(address account, uint256 amount) external {
18+
gtBool success = _mint(account, MpcCore.setPublic256(amount));
1919

2020
if (MpcCore.decrypt(success)) {
2121
_totalSupply += amount;
2222
}
2323
}
2424

25-
function burn(address account, uint64 amount) external {
26-
gtBool success = _burn(account, MpcCore.setPublic64(amount));
25+
function burn(address account, uint256 amount) external {
26+
gtBool success = _burn(account, MpcCore.setPublic256(amount));
2727

2828
if (MpcCore.decrypt(success)) {
2929
_totalSupply -= amount;

test/PrivateAuction.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import hre from "hardhat"
22
import { expect } from "chai"
3-
import { itUint, Wallet } from "@coti-io/coti-ethers"
3+
import { itUint256, Wallet } from "@coti-io/coti-ethers"
44
import { setupAccounts } from "./utils/accounts"
55

66
const gasLimit = 12000000
@@ -19,7 +19,7 @@ async function deploy() {
1919
await (
2020
await token
2121
.connect(owner)
22-
.mint(owner.address, initialSupply)
22+
.mint(owner.address, initialSupply, { gasLimit })
2323
).wait()
2424

2525
const factory = await hre.ethers.getContractFactory("PrivateAuction")
@@ -45,7 +45,7 @@ async function expectBalance(
4545
user: Wallet
4646
) {
4747
const ctBalance = await token["balanceOf(address)"](user.address)
48-
let balance = await user.decryptValue(ctBalance)
48+
let balance = await user.decryptValue256(ctBalance)
4949
expect(balance).to.equal(amount)
5050
}
5151

@@ -55,7 +55,7 @@ async function expectBid(
5555
user: Wallet
5656
) {
5757
const ctBalance = await contract.connect(user).getBid.staticCall()
58-
let bid = await user.decryptValue(ctBalance)
58+
let bid = await user.decryptValue256(ctBalance)
5959
expect(bid).to.equal(amount)
6060
}
6161

@@ -93,24 +93,24 @@ describe("Private Auction", function () {
9393
it(`Bid ${bidAmount}`, async function () {
9494
const { token, tokenAddress, contract, contractAddress, owner } = deployment
9595

96-
const initialBalance = Number(await owner.decryptValue(await token["balanceOf(address)"](owner.address)))
96+
const initialBalance = Number(await owner.decryptValue256(await token["balanceOf(address)"](owner.address)))
9797

98-
let itBidAmount = await owner.encryptValue(
98+
let itBidAmount = await owner.encryptValue256(
9999
bidAmount,
100100
tokenAddress,
101-
token["approve(address,(uint256,bytes))"].fragment.selector
102-
) as itUint
101+
token.interface.getFunction("approve(address,((uint256,uint256),bytes))").selector
102+
) as itUint256
103103

104104
await (
105105
await token
106106
.connect(owner)
107-
["approve(address,(uint256,bytes))"]
107+
.getFunction("approve(address,((uint256,uint256),bytes))")
108108
(contractAddress, itBidAmount, { gasLimit })
109109
).wait()
110110

111111
const func = contract.connect(owner).bid
112112
const selector = func.fragment.selector
113-
itBidAmount = await owner.encryptValue(BigInt(bidAmount), contractAddress, selector) as itUint
113+
itBidAmount = await owner.encryptValue256(BigInt(bidAmount), contractAddress, selector) as itUint256
114114
await (await func(itBidAmount, { gasLimit })).wait()
115115

116116
await expectBalance(token, initialBalance - bidAmount, owner)
@@ -121,24 +121,24 @@ describe("Private Auction", function () {
121121
it(`Increase Bid ${bidAmount * 2}`, async function () {
122122
const { token, tokenAddress, contract, contractAddress, owner } = deployment
123123

124-
const initialBalance = Number(await owner.decryptValue(await token["balanceOf(address)"](owner.address)))
124+
const initialBalance = Number(await owner.decryptValue256(await token["balanceOf(address)"](owner.address)))
125125

126-
let itBidAmount = await owner.encryptValue(
126+
let itBidAmount = await owner.encryptValue256(
127127
bidAmount * 2,
128128
tokenAddress,
129-
token["approve(address,(uint256,bytes))"].fragment.selector
130-
) as itUint
129+
token.interface.getFunction("approve(address,((uint256,uint256),bytes))").selector
130+
) as itUint256
131131

132132
await (
133133
await token
134134
.connect(owner)
135-
["approve(address,(uint256,bytes))"]
135+
.getFunction("approve(address,((uint256,uint256),bytes))")
136136
(contractAddress, itBidAmount, { gasLimit })
137137
).wait()
138138

139139
const func = contract.connect(owner).bid
140140
const selector = func.fragment.selector
141-
itBidAmount = await owner.encryptValue(BigInt(bidAmount * 2), contractAddress, selector) as itUint
141+
itBidAmount = await owner.encryptValue256(BigInt(bidAmount * 2), contractAddress, selector) as itUint256
142142
await (await func(itBidAmount, { gasLimit })).wait()
143143

144144
await expectBalance(token, initialBalance - bidAmount, owner)

0 commit comments

Comments
 (0)