From a843e4ce4c5d1babf612e73d265855d8f70234f0 Mon Sep 17 00:00:00 2001 From: erhant Date: Mon, 2 Oct 2023 10:37:13 +0300 Subject: [PATCH] test with actual large polynomial --- src/Huffd1.huff | 15 +++- src/main.huff | 219 ---------------------------------------------- test/Huffd1.t.sol | 2 +- 3 files changed, 12 insertions(+), 224 deletions(-) delete mode 100644 src/main.huff diff --git a/src/Huffd1.huff b/src/Huffd1.huff index 37e1cd5..1c5f40d 100644 --- a/src/Huffd1.huff +++ b/src/Huffd1.huff @@ -11,12 +11,19 @@ /////////////////////////////////////////////////////////////////////////////// //// TABLE //// /////////////////////////////////////////////////////////////////////////////// +// #define table Basis { +// 0x01050700020c000607 +// } +// #define constant TOTAL_SUPPLY = 0x03 +// #define constant ORDER = 0x0d +// #define constant COEFF_SIZE = 0x01 #define table Basis { - 0x01050700020c000607 + 0x0000000000000000000000000000000000000001aed4ed4ed4ed4ed4ed4ed4ed4ed4ed4ed4ed4eb2716596596596596596596596596596596596595467bb5f442d26609eed7d10b499827bb5f442d251a1ffffffffffffffffffffffffffffffffffffe306795ceb240795ceb240795ceb240795ceb240780eaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa83d3a3e4e8f93a3e4e8f93a3e4e8f93a3e4e8f92fddefbefbefbefbefbefbefbefbefbefbefbefbc7a5bc18334a5116d889fa66c2ddf4fbc18334a4f300000000000000000000000000000000000000000000000000000000000000000000000000000009257c57c57c57c57c57c57c57c57c57c57c57c565220ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea15c7a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f7b79a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f90b3e93e93e93e93e93e93e93e93e93e93e93e93c837e93e93e93e93e93e93e93e93e93e93e93e93df5ef56f56f56f56f56f56f56f56f56f56f56f56e42c6326326326326326326326326326326326325b0000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffbfb507507507507507507507507507507507507510615f15f15f15f15f15f15f15f15f15f15f15f128e582d82d82d82d82d82d82d82d82d82d82d82d6a0ec16c16c16c16c16c16c16c16c16c16c16c16ba92d82d82d82d82d82d82d82d82d82d82d82d82be416c16c16c16c16c16c16c16c16c16c16c16c160d29da9da9da9da9da9da9da9da9da9da9da9da774e736736736736736736736736736736736736650000000000000000000000000000000000000000000000000000000000000000000000000000001c749f49f49f49f49f49f49f49f49f49f49f49f444e3518a6dfc3518a6dfc3518a6dfc3518a6dfc36b4eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeebf7cfa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4ed3444444444444444444444444444444444444439016c16c16c16c16c16c16c16c16c16c16c16c16c082d82d82d82d82d82d82d82d82d82d82d82d82c9e480f2b9d6480f2b9d6480f2b9d6480f2b9d62b00000000000000000000000000000000000000007fffffffffffffffffffffffffffffffffffffc95ccccccccccccccccccccccccccccccccccccd0de3333333333333333333333333333333333332b8f53e93e93e93e93e93e93e93e93e93e93e93e93cce4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa1d27a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa03be93e93e93e93e93e93e93e93e93e93e93e93de864fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa379293e93e93e93e93e93e93e93e93e93e93e93e79000000000000000000000000000000000000000033333333333333333333333333333333333333433fffffffffffffffffffffffffffffffffffffb2d00000000000000000000000000000000000001e2c71c71c71c71c71c71c71c71c71c71c71c71c452e7d27d27d27d27d27d27d27d27d27d27d27d280ac71c71c71c71c71c71c71c71c71c71c71c71c5060e38e38e38e38e38e38e38e38e38e38e38e38d2e71c71c71c71c71c71c71c71c71c71c71c71c6f26d6c16c16c16c16c16c16c16c16c16c16c16c1580000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffc31b05b05b05b05b05b05b05b05b05b05b05b05b2626480f2b9d6480f2b9d6480f2b9d6480f2b9d61a5a22222222222222222222222222222222222227b305b05b05b05b05b05b05b05b05b05b05b05ade6dddddddddddddddddddddddddddddddddddddcbc4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4d61cfa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f561b7f0d4629b7f0d4629b7f0d4629b7f0d4629a60000000000000000000000000000000000000000b6db6db6db6db6db6db6db6db6db6db6db6db6bf541d41d41d41d41d41d41d41d41d41d41d41d4002ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea740b60b60b60b60b60b60b60b60b60b60b60b60a2013e93e93e93e93e93e93e93e93e93e93e93e941460b60b60b60b60b60b60b60b60b60b60b60b5fee7b87b87b87b87b87b87b87b87b87b87b87b878ea521521521521521521521521521521521521503b18c98c98c98c98c98c98c98c98c98c98c98c96c00000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffff95f50750750750750750750750750750750750742cc57c57c57c57c57c57c57c57c57c57c57c57c2fe4d82d82d82d82d82d82d82d82d82d82d82d82b0c65b05b05b05b05b05b05b05b05b05b05b05b0365182d82d82d82d82d82d82d82d82d82d82d82d7479b05b05b05b05b05b05b05b05b05b05b05b059a6a54854854854854854854854854854854854841d39cd9cd9cd9cd9cd9cd9cd9cd9cd9cd9cd9cd760000000000000000000000000000000000000000c71c71c71c71c71c71c71c71c71c71c71c71c6f8d4374374374374374374374374374374374374105d116d889fa66c2ddf4fbc18334a5116d889fa56c0888888888888888888888888888888888888657cb9d6480f2b9d6480f2b9d6480f2b9d6480f2a39cccccccccccccccccccccccccccccccccccccb084d4629b7f0d4629b7f0d4629b7f0d4629b7f0bc4e736736736736736736736736736736736736655a43e7ccb5aee9277605993d220b043e7ccb5ade } -#define constant TOTAL_SUPPLY = 0x03 -#define constant ORDER = 0x0d -#define constant COEFF_SIZE = 0x01 +#define constant TOTAL_SUPPLY = 0xa // 10 tokens +#define constant COEFF_SIZE = 0x14 // 20 byte +#define constant ORDER = 0xffffffffffffffffffffffffffffffffffffffd1 + /////////////////////////////////////////////////////////////////////////////// //// INTERFACES //// diff --git a/src/main.huff b/src/main.huff deleted file mode 100644 index d7fe7cd..0000000 --- a/src/main.huff +++ /dev/null @@ -1,219 +0,0 @@ -/// @title Huffd1 -/// @notice SPDX-License-Identifier: MIT -/// @author erhant - -/////////////////////////////////////////////////////////////////////////////// -//// INCLUDES //// -/////////////////////////////////////////////////////////////////////////////// -#include "./util/Owned.huff" -#include "./util/Polynomial.huff" - -/////////////////////////////////////////////////////////////////////////////// -//// TABLE //// -/////////////////////////////////////////////////////////////////////////////// -#define table Basis { - 0x0000000000000000000000000000000000000001aed4ed4ed4ed4ed4ed4ed4ed4ed4ed4ed4ed4eb2716596596596596596596596596596596596595467bb5f442d26609eed7d10b499827bb5f442d251a1ffffffffffffffffffffffffffffffffffffe306795ceb240795ceb240795ceb240795ceb240780eaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa83d3a3e4e8f93a3e4e8f93a3e4e8f93a3e4e8f92fddefbefbefbefbefbefbefbefbefbefbefbefbc7a5bc18334a5116d889fa66c2ddf4fbc18334a4f300000000000000000000000000000000000000000000000000000000000000000000000000000009257c57c57c57c57c57c57c57c57c57c57c57c565220ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea15c7a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f7b79a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f90b3e93e93e93e93e93e93e93e93e93e93e93e93c837e93e93e93e93e93e93e93e93e93e93e93e93df5ef56f56f56f56f56f56f56f56f56f56f56f56e42c6326326326326326326326326326326326325b0000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffbfb507507507507507507507507507507507507510615f15f15f15f15f15f15f15f15f15f15f15f128e582d82d82d82d82d82d82d82d82d82d82d82d6a0ec16c16c16c16c16c16c16c16c16c16c16c16ba92d82d82d82d82d82d82d82d82d82d82d82d82be416c16c16c16c16c16c16c16c16c16c16c16c160d29da9da9da9da9da9da9da9da9da9da9da9da774e736736736736736736736736736736736736650000000000000000000000000000000000000000000000000000000000000000000000000000001c749f49f49f49f49f49f49f49f49f49f49f49f444e3518a6dfc3518a6dfc3518a6dfc3518a6dfc36b4eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeebf7cfa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4ed3444444444444444444444444444444444444439016c16c16c16c16c16c16c16c16c16c16c16c16c082d82d82d82d82d82d82d82d82d82d82d82d82c9e480f2b9d6480f2b9d6480f2b9d6480f2b9d62b00000000000000000000000000000000000000007fffffffffffffffffffffffffffffffffffffc95ccccccccccccccccccccccccccccccccccccd0de3333333333333333333333333333333333332b8f53e93e93e93e93e93e93e93e93e93e93e93e93cce4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa1d27a4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa03be93e93e93e93e93e93e93e93e93e93e93e93de864fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa379293e93e93e93e93e93e93e93e93e93e93e93e79000000000000000000000000000000000000000033333333333333333333333333333333333333433fffffffffffffffffffffffffffffffffffffb2d00000000000000000000000000000000000001e2c71c71c71c71c71c71c71c71c71c71c71c71c452e7d27d27d27d27d27d27d27d27d27d27d27d280ac71c71c71c71c71c71c71c71c71c71c71c71c5060e38e38e38e38e38e38e38e38e38e38e38e38d2e71c71c71c71c71c71c71c71c71c71c71c71c6f26d6c16c16c16c16c16c16c16c16c16c16c16c1580000000000000000000000000000000000000000ffffffffffffffffffffffffffffffffffffffc31b05b05b05b05b05b05b05b05b05b05b05b05b2626480f2b9d6480f2b9d6480f2b9d6480f2b9d61a5a22222222222222222222222222222222222227b305b05b05b05b05b05b05b05b05b05b05b05ade6dddddddddddddddddddddddddddddddddddddcbc4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4d61cfa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4fa4f561b7f0d4629b7f0d4629b7f0d4629b7f0d4629a60000000000000000000000000000000000000000b6db6db6db6db6db6db6db6db6db6db6db6db6bf541d41d41d41d41d41d41d41d41d41d41d41d4002ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea0ea740b60b60b60b60b60b60b60b60b60b60b60b60a2013e93e93e93e93e93e93e93e93e93e93e93e941460b60b60b60b60b60b60b60b60b60b60b60b5fee7b87b87b87b87b87b87b87b87b87b87b87b878ea521521521521521521521521521521521521503b18c98c98c98c98c98c98c98c98c98c98c98c96c00000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffff95f50750750750750750750750750750750750742cc57c57c57c57c57c57c57c57c57c57c57c57c2fe4d82d82d82d82d82d82d82d82d82d82d82d82b0c65b05b05b05b05b05b05b05b05b05b05b05b0365182d82d82d82d82d82d82d82d82d82d82d82d7479b05b05b05b05b05b05b05b05b05b05b05b059a6a54854854854854854854854854854854854841d39cd9cd9cd9cd9cd9cd9cd9cd9cd9cd9cd9cd760000000000000000000000000000000000000000c71c71c71c71c71c71c71c71c71c71c71c71c6f8d4374374374374374374374374374374374374105d116d889fa66c2ddf4fbc18334a5116d889fa56c0888888888888888888888888888888888888657cb9d6480f2b9d6480f2b9d6480f2b9d6480f2a39cccccccccccccccccccccccccccccccccccccb084d4629b7f0d4629b7f0d4629b7f0d4629b7f0bc4e736736736736736736736736736736736736655a43e7ccb5aee9277605993d220b043e7ccb5ade -} -#define constant TOTAL_SUPPLY = 0xa // 10 tokens -#define constant COEFF_SIZE = 0x14 // 20 byte -#define constant ORDER = 0xffffffffffffffffffffffffffffffffffffffd1 - -/////////////////////////////////////////////////////////////////////////////// -//// INTERFACES //// -/////////////////////////////////////////////////////////////////////////////// -#define function name() nonpayable returns (string) -#define function symbol() nonpayable returns (string) -#define function ownerOf(uint256) view returns (address) -#define function balanceOf(address) view returns (uint256) -#define function transfer(address, uint256) nonpayable returns () - -/////////////////////////////////////////////////////////////////////////////// -//// STORAGE //// -/////////////////////////////////////////////////////////////////////////////// -#define constant BAL_SLOT = 0xbabe69babe69babe69babe69babe69 // balances -// #define constant APV_SLOT = 0xface42face42face42face42face42 // approvals - -/////////////////////////////////////////////////////////////////////////////// -//// MACROS //// -/////////////////////////////////////////////////////////////////////////////// -#define macro NAME() = takes (0) returns (0) { - 0x20 0x00 mstore // offset - 0x06 0x20 mstore // length (12 hex chars, 6 bytes) - // encoded string "Huffd1" - __RIGHTPAD(0x487566666431) 0x40 mstore - 0x60 0x00 return -} - -#define macro SYMBOL() = takes (0) returns (0) { - 0x20 0x00 mstore // offset - 0x04 0x20 mstore // length (8 hex chars, 4 bytes) - // encoded string "FFD1" - __RIGHTPAD(0x46464431) 0x40 mstore - 0x60 0x00 return -} - -#define macro OWNER_OF() = takes (1) returns (1) { - // input: [t] (tokenId) - [BAL_SLOT] // [t, s] (storageSlot) - 0x00 // [t, s, m] (memoryOffset) - [TOTAL_SUPPLY] // [t, s, m, l] (length) - POLY_SLOAD() // [t, m, l] - dup3 // [t, m, l, t] (token id) - POLY_EVAL(ORDER) // [t, m, l, a] (address) - swap3 // [a, m, l, t] - pop pop pop // [a] - // output: [a] (owner) -} - -#define macro TRANSFER() = takes (3) returns (0) { - // input: // [t, b, a] (token, to, from) - - // assert that token is owner by caller - dup3 OWNER_OF() // [t, b, a, o] (owner of t) - dup2 eq // [t, b, a, a == o] - success jumpi - 0x00 0x00 revert - success: - - // find scale amount - [ORDER] sub // [t, b, -a] (-a = p - a) in field - add // [t, v] (v := b - a) - - // load balance polynomial - [BAL_SLOT] // [t, v, s] (storageSlot) - 0x00 // [t, v, s, mP] (memoryOffset) - [TOTAL_SUPPLY] // [t, v, s, mP, l] (length) - POLY_SLOAD() // [t, v, mP, l] - - // load basis polynomial - __tablestart(Basis) // [t, v, mP, l, c] - dup5 // [t, v, mP, l, c, t] - POLY_OFFSET(COEFF_SIZE, TOTAL_SUPPLY) - // [t, v, mP, l, c, o] - add // [t, v, mP, l, c] (codeOffset) - [COEFF_SIZE] // [t, v, mP, l, c, s] (coefficient size) - 0xFFFF // [t, v, mP, l, c, s, mQ] (memOffset) - dup4 // [t, v, mP, l, c, s, mQ, l] (length) - POLY_CODECOPY() // [t, v, mP, l, mQ, l] - pop swap1 // [t, v, mP, mQ, l] - - // scale basis polynomial with (b - a) - dup4 // [t, v, mP, mQ, l, v] - POLY_SCALE(ORDER) // [t, v, mP, mQ, l] - - // add scaled basis polynomial to balance polynomial - POLY_ADD(ORDER) // [t, v, mP, l] - - // store the new polynomial - [BAL_SLOT] swap1 // [t, v, mP, sQ, l] - POLY_SSTORE() // [t, v, sQ, l] - pop pop pop pop // [] -} - -#define macro BALANCE_OF() = takes (1) returns (1) { - // input: // [a] (address) - - // load balance polynomial once at the start - [BAL_SLOT] // [a, $] (storageSlot) - 0x00 // [a, $, m] (memoryOffset) - [TOTAL_SUPPLY] // [a, $, m, l] (length) - POLY_SLOAD() // [a, m, l] - - 0x00 // [a, m, l, s] (total balance initially 0) - swap1 // [a, m, s, l] - 0x00 // [a, m, s, l, i] (index starts with 0) - - // loop_begin: - // [a, m, s, l, i] - - // loop condition - dup2 dup2 // [a, m, s, l, i, l, i] - lt iszero // [a, m, s, l, i, i >= l] - loop_end jumpi - - // check ownership for token i - dup4 // [a, m, s, l, i, m] - dup3 // [a, m, s, l, i, m, l] - dup3 // [a, m, s, l, i, m, l, i] (token id = index) - POLY_EVAL(ORDER)// [a, m, s, l, i, m, l, o] (owner) - - // check if owner is the given address - dup8 // [a, m, s, l, i, m, l, o, a] - eq // [a, m, s, l, i, m, l, eq] (eq := o == a) - - // add to total balance - dup6 // [a, m, s, l, i, m, l, eq, s] - add // [a, m, s, l, i, m, l, s'] (s' := s + eq) - swap5 // [a, m, s',l, i, m, l, s] - pop pop // [a, m, s, l, i] - - // increment index - 0x01 add // [a, m, s',l, i'] (i' := i + 1) - - // [a, m, s, l, i] - // loop_begin jump - loop_end: - - pop // [a, m, s, l] - pop // [a, m, s] - swap1 // [s, a] - pop // [s] - // output: // [s] (total balance) -} - -/////////////////////////////////////////////////////////////////////////////// -//// MAIN //// -/////////////////////////////////////////////////////////////////////////////// -#define macro HUFFD1_CONSTRUCTOR() = takes (0) returns (0) { - // the constant polynomial equal to OWNER will result - // in all tokens being owned by that OWNER - [OWNER] sload // [owner] - [BAL_SLOT] sstore // [] -} - -#define macro HUFFD1_MAIN() = takes (1) returns (1) { - dup1 __FUNC_SIG(name) eq name jumpi - dup1 __FUNC_SIG(symbol) eq symbol jumpi - dup1 __FUNC_SIG(ownerOf) eq ownerOf jumpi - dup1 __FUNC_SIG(transfer) eq transfer jumpi - dup1 __FUNC_SIG(balanceOf) eq balanceOf jumpi - no_match jump - - name: - NAME() - symbol: - SYMBOL() - ownerOf: - 0x04 calldataload // [t] (tokenId) - OWNER_OF() // [a] (address) - 0x00 mstore // [] - 0x20 0x00 return // [] - transfer: - 0x24 calldataload // [t] (tokenId) - 0x04 calldataload // [t, b] (to address) - caller // [t, b, a] (from address) - TRANSFER() // [] - 0x00 0x00 return // [] - balanceOf: - 0x04 calldataload // [a] (address) - BALANCE_OF() // [s] (balance) - 0x00 mstore // [] - 0x20 0x00 return // [] - - no_match: -} - -/////////////////////////////////////////////////////////////////////////////// -//// WRAPPERS //// -/////////////////////////////////////////////////////////////////////////////// -#define macro CONSTRUCTOR() = takes (0) returns (0) { - OWNED_CONSTRUCTOR() - HUFFD1_CONSTRUCTOR() -} - -#define macro MAIN() = takes (0) returns (0) { - 0x00 calldataload 0xE0 shr - OWNED_MAIN() - HUFFD1_MAIN() - 0x00 0x00 revert -} diff --git a/test/Huffd1.t.sol b/test/Huffd1.t.sol index 2f76370..501365c 100644 --- a/test/Huffd1.t.sol +++ b/test/Huffd1.t.sol @@ -8,7 +8,7 @@ import {console} from "forge-std/console.sol"; contract Huffd1Test is Test { Huffd1 huffd1; address constant OWNER = address(0x1); // small address to work for all orders - uint256 constant TOTAL_SUPPLY = 0x03; + uint256 constant TOTAL_SUPPLY = 10; // 3 /// @dev Set-up to run before each test. function setUp() public {