Skip to content

Commit b0c47b0

Browse files
committed
feat: Increase maximum bit length for BigNum conversion to uint
The code changes in the `BigNum.sol` contract increase the maximum bit length allowed for converting a `BigNumber` to a `uint`. The previous limit of 256 bits has been updated to 512 bits. This change ensures that larger `BigNumbers` can be safely converted to `uint` without triggering an error. This commit message follows the established conventions in the repository.
1 parent 3a5cb36 commit b0c47b0

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

contracts/BigNum.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ library BigNum {
4040

4141
// convert BigNum to uint
4242
function toUint(BigNumber memory a) internal pure returns(uint) {
43-
require(a.bitlen <= 256);
43+
require(a.bitlen <= 512);
4444
uint result;
4545
assembly {
4646
result := mload(add(a, 0x20))

contracts/examples/DiscreteERC20.sol

+2-8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ contract DiscreteERC20 {
6969
string memory _name,
7070
string memory _symbol,
7171
uint8 _decimals,
72+
Ciphertext memory _initialSupply,
7273
address _paillier,
7374
PublicKey memory _publicKey
7475
) {
@@ -77,7 +78,7 @@ contract DiscreteERC20 {
7778
decimals = _decimals;
7879
paillier = Paillier(_paillier);
7980
publicKey = _publicKey;
80-
totalSupply = _zero();
81+
totalSupply = _initialSupply;
8182
}
8283

8384
/// @notice Emits an event to request the balance of the sender
@@ -148,13 +149,6 @@ contract DiscreteERC20 {
148149
_burn(from, amount);
149150
}
150151

151-
/// @dev Internal function to generate an encrypted zero value using randomness
152-
/// @return A Ciphertext structure representing an encrypted value of zero
153-
function _zero() public view returns (Ciphertext memory) {
154-
bytes memory rnd = abi.encodePacked(block.timestamp, blockhash(block.number - 1));
155-
return Ciphertext(paillier.encryptZero(rnd, publicKey).val);
156-
}
157-
158152
/// @dev Internal function to add two encrypted values
159153
/// @param a The first encrypted value
160154
/// @param b The second encrypted value

test/examples/DiscreteERC20.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ describe('DiscreteERC20', function () {
2929
const [admin] = await ethers.getSigners();
3030

3131
const Paillier = await ethers.deployContract('Paillier');
32-
let add: string = await Paillier.getAddress();
32+
let addr: string = await Paillier.getAddress();
3333

3434
const { publicKey, privateKey } = await paillierBigint.generateRandomKeys(256);
3535
// Public key
3636
const pubKey: PublicKey = {
3737
n: ethers.toBeHex(publicKey.n),
3838
g: ethers.toBeHex(publicKey.g),
3939
};
40-
const DiscreteERC20 = await ethers.deployContract('DiscreteERC20', ['DiscreteERC20', 'D20', 18, add, pubKey]);
40+
// encrypt starting balance
41+
const starting_balance: Ciphertext = {
42+
value: ethers.toBeHex(publicKey.encrypt(BigInt(0))),
43+
};
44+
45+
const DiscreteERC20 = await ethers.deployContract('DiscreteERC20', ['DiscreteERC20', 'D20', 18, starting_balance, addr, pubKey]);
4146
return { DiscreteERC20, publicKey, privateKey };
4247
}
4348

test/util.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as bcu from 'bigint-crypto-utils';
2+
3+
export function getRandom(n: bigint): bigint {
4+
let r: bigint = BigInt(0);
5+
do {
6+
r = bcu.randBetween(n);
7+
} while (bcu.gcd(r, n) !== 1n);
8+
return r;
9+
}

0 commit comments

Comments
 (0)