Home > front end >  How does the modulus shorten a number to only 16 digits in this example?
How does the modulus shorten a number to only 16 digits in this example?

Time:02-26

I am learning Solidity by following various tutorials online. One of these tutorials is Cryptozombies. In this tutorial we create a zombie that has "dna" (a number). These numbers must be only 16 digits long to work with the code. We do this by defining

uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;

At some point in the lesson, we define a function that generates "random" dna by passing a string to the keccak256 hash function.

function _generateRandomDna(string memory _str) private view returns (uint) {
    uint rand = uint(keccak256(abi.encodePacked(_str)));
    return rand % dnaModulus;
}

I am trying to figure out how the output of keccak256 % 10^16 is always a 16 digit integer. I guess part of the problem is that I don't exactly understand what keccak256 outputs. What I (think) I know is that it outputs a 256 bit number. I bit is either 0 or 1, so there are 2^256 possible outputs from this function? If you need more information please let me know. I think included everything relevant from the code.

CodePudding user response:

Whatever keccak256(abi.encodePacked(_str)) returns, it converted into uint because of the casting uint(...).

uint rand = uint(keccak256(abi.encodePacked(_str)));

When it's an uint then the simple math, because it's modulus.

xxxxx % 100 always < 100

CodePudding user response:

I see now that this code is mean to produce a number that has no more than 16 digits, not exactly 16 digits.

Take x % 10^16

If x < 10^16, then then the remainder is x, and x is by definition less than 10^16, which is the smallest possible number with 17 digits I.e. every number less than 10^16 has 16 or fewer digits.

If x = 10^16, then remainder is 0 which has fewer than 16 digits.

If x > 10^16, then either 10^16 goes into x fully or not. If it does fully, remainder is zero which has less than 16 digits, if it goes in partially, then remainder is only a part of 10^16 which will always have 16 or fewer digits.

  • Related