solidity
kompreni 🚂 pfp
2 replies
1 recast
29 reactions

kazani.base.eth 🦂 pfp
0 reply
0 recast
0 reaction

arseniy pfp
Perplexity on encoding deck of cards in uint256: To encode a **full permutation** (the exact order) of a 52-card deck in Solidity using a `uint256`, you can use the **factorial number system** (also called Lehmer code). This system allows you to map any permutation of 52 elements to a unique integer between 0 and $$52! - 1$$, and vice versa[8][14]. ### Key Points - **Bits required:** $$ \log_2(52!) \approx 225.58 $$ bits, so a `uint256` (256 bits) is sufficient. - **Encoding:** Convert the permutation to its Lehmer code, then to a single integer. - **Decoding:** Convert the integer back to a Lehmer code, then reconstruct the permutation[14]. ### How It Works 1. **Lehmer code:** For each position in the permutation, count how many remaining elements are less than the current element. This produces a sequence of 52 numbers, each less than or equal to its positional factorial base. 2. **Integer representation:** Each digit of the Lehmer code is multiplied by its factorial base and summed, resulting in a unique integer for each permutation[14]. ### Solidity Implementation Outline While the search results do not provide Solidity code for permutation encoding/decoding, the factorial number system is well-documented and can be implemented as follows: #### Encoding a permutation to an integer (Lehmer code): ```solidity function permutationToIndex(uint8[] memory perm) public pure returns (uint256 index) { uint8[52] memory temp; for (uint8 i = 0; i < 52; i++) temp[i] = i; for (uint8 i = 0; i < 52; i++) { uint8 v = perm[i]; uint8 less = 0; for (uint8 j = 0; j < 52 - i; j++) { if (temp[j] == v) { index = index * (52 - i) + less; // Remove used element for (uint8 k = j; k < 51 - i; k++) temp[k] = temp[k+1]; break; } less++; } } } ``` #### Decoding an integer to a permutation: ```solidity function indexToPermutation(uint256 index) public pure returns (uint8[] memory perm) { perm = new uint8[](52); uint8[52] memory temp; for (uint8 i = 0; i < 52; i++) temp[i] = i; for (uint8 i = 0; i < 52; i++) { uint256 f = factorial(52 - 1 - i); uint8 pos = uint8(index / f); perm[i] = temp[pos]; // Remove used element for (uint8 k = pos; k < 51 - i; k++) temp[k] = temp[k+1]; index %= f; } } ``` (The `factorial` function must be implemented efficiently; precompute values if needed.) ### Summary Table | Deck Representation | Bits Needed | Fits in uint256? | Encoding Method | |---------------------------|-------------|------------------|------------------------| | Full permutation (order) | ~226 | Yes | Factorial number system| **References:** - Factorial number system and permutation encoding[8][14]. This is the **most efficient way** to encode a full permutation of 52 cards in Solidity. Sources [1] Mathematical Operations in Solidity - GeeksforGeeks https://www.geeksforgeeks.org/solidity/mathematical-operations-in-solidity/ [2] Factorial contract errors in version 0.5.1 - Ethereum Stack Exchange https://ethereum.stackexchange.com/questions/72169/factorial-contract-errors-in-version-0-5-1 [3] EthIR/examples/factorial.sol at master - GitHub https://github.com/costa-group/EthIR/blob/master/examples/factorial.sol [4] Calculate factorial | DApp World https://dapp-world.com/problem/math-calculate-factorial-tvfh/problem [5] bc factorial - Posts - OneCompiler https://onecompiler.com/posts/3zz8q623f/bc-factorial [6] Contracts — Solidity 0.8.31 documentation https://docs.soliditylang.org/en/latest/contracts.html [7] Solidity Contract for demoing how to use libraries. The ... - GitHub Gist https://gist.github.com/ashwinYardi/029cfddd5b7eceb677d4b750cf064e68 [8] Factorial number system and enumerating permutations - Reddit https://www.reddit.com/r/math/comments/kz5sc/factorial_number_system_and_enumerating/ [9] Solidity by Example — Solidity 0.8.31 documentation https://docs.soliditylang.org/en/latest/solidity-by-example.html [10] Solidity and Rust Syntax, Data Types, and Basic Constructs https://developers.stellar.org/docs/learn/migrate/evm/solidity-and-rust-basics [11] Implementation of a nonlinear transformation of a substitution in the form of elements of a numbering system of a number of factorial sets https://bit.mephi.ru/index.php/bit/article/view/1365 [12] Design and implementation of NFT-based system https://drpress.org/ojs/index.php/HSET/article/view/4929 [13] IID-Based QPP-RNG: A Random Number Generator Utilizing Random Permutation Sorting Driven by System Jitter https://arxiv.org/abs/2502.18609 [14] Factorial Numbers and Their Practical Applications https://www.mdpi.com/2076-3417/14/19/8588 [15] A Universal Classification System of Skin Substitutes Inspired by Factorial Design. https://www.liebertpub.com/doi/10.1089/ten.teb.2017.0477 [16] Engagement is a necessary condition to test audit and feedback design features: results of a pragmatic, factorial, cluster-randomized trial with an embedded process evaluation https://implementationscience.biomedcentral.com/articles/10.1186/s13012-023-01271-6 [17] Improving medication adherence among persons with cardiovascular disease through m-health and community health worker-led interventions in Kerala; protocol for a type II effectiveness-implementation research-(SHRADDHA-ENDIRA) https://trialsjournal.biomedcentral.com/articles/10.1186/s13063-024-08244-0 [18] Implementation Intention and Reminder Effects on Behavior Change in a Mobile Health System: A Predictive Cognitive Model http://www.jmir.org/2017/11/e397/ [19] IMPACT OF DEMOGRAPHIC FACTORS GENDER AND NUMBER OF YEARS OF ERP POST IMPLEMENTATION OPERATIONS ON USER PRODUCTIVITY: AN EMPIRICAL STUDY IN HIGHER EDUCATION SETTINGS, IN INDIAN CONTEXT https://www.granthaalayahpublication.org/journals/index.php/granthaalayah/article/view/10_IJRG19_A11_2887 [20] PERFORMANCE COMPARISON OF THE LANGUAGE PROGRAMMING LUA AND C++ ON IMPLEMENTATION PROGRAM FIBONACCI AND FACTORIAL. https://www.semanticscholar.org/paper/e9e80120862824bc1d2de862f3ce35bfbed5cb77
0 reply
0 recast
2 reactions

Hoot 🎩 pfp
0 reply
0 recast
4 reactions

Apoorv Lathey pfp
1 reply
0 recast
3 reactions

nicholas 🧨 pfp
1 reply
0 recast
1 reaction

kazani.base.eth 🦂 pfp
OpenZeppelin Solidity Contracts v5.3 is here 🧑‍💻 This release introduces new token standards and governance improvements, along with new smart account and crosschain capabilities through the Community Contracts Library. Here's what's new 👇 📝 Core Contracts Highlights ERC-6909 – New multi-token standard with: • Minimal implementation • Metadata support • Supply tracking • Content URI extension Governor Improvements: • New Proposal Guardian role • Added Sequential Proposal IDs • New Super Quorum mechanism ❤️‍🩹 5.3 also packs quality-of-life upgrades such as:  • trySafeTransfer() for ERC20s (no more surprise reverts) • clear() for EnumerableSet and EnumerableMap • MerkleTree.update() for dynamic leaf management • 512-bit and saturating math operations (add512, mul512, etc)
1 reply
0 recast
5 reactions

James McComish pfp
0 reply
0 recast
0 reaction

⟠ ichristwin〔▸‿◂〕 pfp
2 replies
0 recast
2 reactions

Devin Conley pfp
0 reply
0 recast
4 reactions

kazani.base.eth 🦂 pfp
0 reply
0 recast
0 reaction

Patricio Worthalter pfp
0 reply
3 recasts
15 reactions

kompreni 🚂 pfp
0 reply
0 recast
4 reactions

Jem pfp
3 replies
0 recast
2 reactions

what pfp
1 reply
0 recast
0 reaction