Skip to content

Commit

Permalink
feat: Increase maximum bit length for BigNum conversion to uint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
10d9e committed May 9, 2024
1 parent 3a5cb36 commit b0c47b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contracts/BigNum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ library BigNum {

// convert BigNum to uint
function toUint(BigNumber memory a) internal pure returns(uint) {
require(a.bitlen <= 256);
require(a.bitlen <= 512);
uint result;
assembly {
result := mload(add(a, 0x20))
Expand Down
10 changes: 2 additions & 8 deletions contracts/examples/DiscreteERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ contract DiscreteERC20 {
string memory _name,
string memory _symbol,
uint8 _decimals,
Ciphertext memory _initialSupply,
address _paillier,
PublicKey memory _publicKey
) {
Expand All @@ -77,7 +78,7 @@ contract DiscreteERC20 {
decimals = _decimals;
paillier = Paillier(_paillier);
publicKey = _publicKey;
totalSupply = _zero();
totalSupply = _initialSupply;
}

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

/// @dev Internal function to generate an encrypted zero value using randomness
/// @return A Ciphertext structure representing an encrypted value of zero
function _zero() public view returns (Ciphertext memory) {
bytes memory rnd = abi.encodePacked(block.timestamp, blockhash(block.number - 1));
return Ciphertext(paillier.encryptZero(rnd, publicKey).val);
}

/// @dev Internal function to add two encrypted values
/// @param a The first encrypted value
/// @param b The second encrypted value
Expand Down
9 changes: 7 additions & 2 deletions test/examples/DiscreteERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ describe('DiscreteERC20', function () {
const [admin] = await ethers.getSigners();

const Paillier = await ethers.deployContract('Paillier');
let add: string = await Paillier.getAddress();
let addr: string = await Paillier.getAddress();

const { publicKey, privateKey } = await paillierBigint.generateRandomKeys(256);
// Public key
const pubKey: PublicKey = {
n: ethers.toBeHex(publicKey.n),
g: ethers.toBeHex(publicKey.g),
};
const DiscreteERC20 = await ethers.deployContract('DiscreteERC20', ['DiscreteERC20', 'D20', 18, add, pubKey]);
// encrypt starting balance
const starting_balance: Ciphertext = {
value: ethers.toBeHex(publicKey.encrypt(BigInt(0))),
};

const DiscreteERC20 = await ethers.deployContract('DiscreteERC20', ['DiscreteERC20', 'D20', 18, starting_balance, addr, pubKey]);
return { DiscreteERC20, publicKey, privateKey };
}

Expand Down
9 changes: 9 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as bcu from 'bigint-crypto-utils';

export function getRandom(n: bigint): bigint {
let r: bigint = BigInt(0);
do {
r = bcu.randBetween(n);
} while (bcu.gcd(r, n) !== 1n);
return r;
}

0 comments on commit b0c47b0

Please sign in to comment.