Skip to content

Instantly share code, notes, and snippets.

View k06a's full-sized avatar
🚀
DeFi dreamer

Anton Bukov k06a

🚀
DeFi dreamer
View GitHub Profile
@k06a
k06a / isPowOfTen.yul
Last active September 29, 2022 14:49
isPowOfTen.yul
// https://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup
function isPowerOfTen(uint256 x) public pure returns (bool result) {
assembly {
let dict := 0x00011c021d0e18031e16140f191104081f1b0d17151310071a0c12060b050a09
let zeroes := 0
for { let offset := 0 } lt(offset, 256) { offset := add(offset, 32) } {
let v := and(shr(offset, x), 0xffffffff)
switch v
const { BN } = require('bn.js');
const { keccak256 } = require('ethereumjs-util');
const { toBN } = require('web3-utils');
// function hash (selector, salt, m) {
// return new BN(selector, 'hex').xor(toBN(salt)).toNumber() % 25469 % m;
// }
function hash (selector, salt, m) {
const saltStr = toBN(salt).toString('hex', 8);
@k06a
k06a / sodis.js
Created June 22, 2022 20:45
Constant-time Solidity dispatcher MVP
const { BN } = require('bn.js');
const { keccak256 } = require('ethereumjs-util');
const { toBN } = require('web3-utils');
const numberOfSelectors = 1000;
const tableSizeKoef = 1.5;
const maxBucketSize = 2;
function hash (selector, salt, m) {
return new BN(selector, 'hex').xor(toBN(salt)).toNumber() % 65537 % m;
@k06a
k06a / ERC20WithFactory.sol
Created February 16, 2022 11:28
ERC20 extension with create2-based factory for arbitrary calls
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Create2.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "github.com/1inch/solidity-utils/contracts/libraries/AddressArray.sol";
contract Caller {
@k06a
k06a / selector-table.js
Last active February 9, 2022 22:14
SelectorTable
const { toBN } = require('web3-utils');
function findBucket(iterations) {
const selectors = [
0x12345678,
0x11223344,
0xaabbccdd,
0xabcdefff,
0xabbcccdd,
0xbbbbbbbb,
@k06a
k06a / .zprofile
Created January 23, 2022 18:13
.env
eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH=$PATH:$HOME/.cargo/bin
alias stree='/Applications/SourceTree.app/Contents/Resources/stree'
alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
alias gc='git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d'
@k06a
k06a / lohi.h
Created January 21, 2022 12:23
Efficient BigInt - Lohi (uint128_t, uint256_t, uint512_t)
#ifndef Lohi_h
#define Lohi_h
namespace lohi {
template<typename T>
struct Lohi;
template<typename T>
T zero(thread const T &) {
return 0;
@k06a
k06a / ModInv.sol
Last active February 7, 2022 15:07
Modular inverse with binary shifts and uint256
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract ModInv {
uint256 public constant n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F;
function inverseDiv(uint256 a) public pure returns(uint256) {
return inverseDiv(a, n);
}
pragma solidity ^0.5.0;
contract CompressedCaller {
function compressedCall(
address target,
uint256 totalLength,
bytes memory zipped
)
@k06a
k06a / ShardedToken.sol
Last active May 17, 2023 13:15
ShardedToken
contract ShardedToken {
using SafeMath for uint256;
address owner = msg.sender;
uint256 private _totalSupply;
mapping(address => ShardedToken.Extension) private extensions;
function register() public {
extensions[msg.sender] = new Token.Extension();
}