Contract

ERC20.Sol

// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity >=0.8.13 <0.9.0;
import "./EIP712WithModifier.sol";
import "./TFHE.sol";
contract EncryptedERC20 is EIP712WithModifier {
euint32 private totalSupply;
string public constant name = "Naraggara"; // City of Zama's battle
string public constant symbol = "NARA";
uint8 public constant decimals = 18;
// used for output authorization
bytes32 private DOMAIN_SEPARATOR;
// A mapping from address to an encrypted balance.
mapping(address => euint32) internal balances;
// A mapping of the form mapping(owner => mapping(spender => allowance)).
mapping(address => mapping(address => euint32)) internal allowances;
// The owner of the contract.
address internal contractOwner;
constructor() EIP712WithModifier("Authorization token", "1") {
contractOwner = msg.sender;
}
// Sets the balance of the owner to the given encrypted balance.
// Anyone can mint because it makes for a better demo
function mint(bytes calldata encryptedAmount) public {
euint32 amount = TFHE.asEuint32(encryptedAmount);
balances[msg.sender] = TFHE.add(balances[msg.sender], amount);
totalSupply = TFHE.add(totalSupply, amount);
}
// Transfers an encrypted amount from the message sender address to the `to` address.
function transfer(address to, bytes calldata encryptedAmount) public {
transfer(to, TFHE.asEuint32(encryptedAmount));
}
// Transfers an amount from the message sender address to the `to` address.
function transfer(address to, euint32 amount) internal {
_transfer(msg.sender, to, amount);
}
function getTotalSupply(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlyContractOwner
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
return TFHE.reencrypt(totalSupply, publicKey);
}
// Returns the balance of the caller under their public FHE key.
// The FHE public key is automatically determined based on the origin of the call.
function balanceOf(
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
return TFHE.reencrypt(balances[msg.sender], publicKey);
}
function balanceRaw(
bytes32 publicKey,
bytes calldata signature
)
public
view
returns (bytes memory)
{
return TFHE.reencrypt(balances[msg.sender], publicKey);
}
// Sets the `encryptedAmount` as the allowance of `spender` over the caller's tokens.
function approve(address spender, bytes calldata encryptedAmount) public {
address owner = msg.sender;
_approve(owner, spender, TFHE.asEuint32(encryptedAmount));
}
// Returns the remaining number of tokens that `spender` is allowed to spend
// on behalf of the caller. The returned ciphertext is under the caller public FHE key.
function allowance(
address spender,
bytes32 publicKey,
bytes calldata signature
)
public
view
onlySignedPublicKey(publicKey, signature)
returns (bytes memory)
{
address owner = msg.sender;
return TFHE.reencrypt(_allowance(owner, spender), publicKey);
}
// Transfers `encryptedAmount` tokens using the caller's allowance.
function transferFrom(
address from,
address to,
bytes calldata encryptedAmount
) public {
transferFrom(from, to, TFHE.asEuint32(encryptedAmount));
}
// Transfers `amount` tokens using the caller's allowance.
function transferFrom(address from, address to, euint32 amount) public {
address spender = msg.sender;
_updateAllowance(from, spender, amount);
_transfer(from, to, amount);
}
function _approve(address owner, address spender, euint32 amount) internal {
allowances[owner][spender] = amount;
}
function _allowance(
address owner,
address spender
) internal view returns (euint32) {
return allowances[owner][spender];
}
function _updateAllowance(
address owner,
address spender,
euint32 amount
) internal {
euint32 currentAllowance = _allowance(owner, spender);
TFHE.requireCt(TFHE.le(amount, currentAllowance));
_approve(owner, spender, TFHE.sub(currentAllowance, amount));
}
// Transfers an encrypted amount.
function _transfer(address from, address to, euint32 amount) internal {
// Make sure the sender has enough tokens.
TFHE.requireCt(TFHE.le(amount, balances[from]));
// Add to the balance of `to` and subract from the balance of `from`.
balances[to] = TFHE.add(balances[to], amount);
balances[from] = TFHE.sub(balances[from], amount);
}
modifier onlyContractOwner() {
require(msg.sender == contractOwner);
_;
}
}

EIP712WithModifier.Sol

// SPDX-License-Identifier: BSD-3-Clause-Clear
pragma solidity >=0.8.13 <0.9.0;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
abstract contract EIP712WithModifier is EIP712 {
constructor(
string memory name,
string memory version
) EIP712(name, version) {}
modifier onlySignedPublicKey(bytes32 publicKey, bytes memory signature) {
bytes32 digest = _hashTypedDataV4(
keccak256(
abi.encode(keccak256("Reencrypt(bytes32 publicKey)"), publicKey)
)
);
address signer = ECDSA.recover(digest, signature);
require(
signer == msg.sender,
"EIP712 signer and transaction signer do not match"
);
}
}