github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/cmd/dero-miner/difficulty.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package main
    18  
    19  // ripoff from blockchain folder
    20  import "math/big"
    21  import "github.com/deroproject/derosuite/crypto"
    22  
    23  var (
    24  	// bigZero is 0 represented as a big.Int.  It is defined here to avoid
    25  	// the overhead of creating it multiple times.
    26  	bigZero = big.NewInt(0)
    27  
    28  	// bigOne is 1 represented as a big.Int.  It is defined here to avoid
    29  	// the overhead of creating it multiple times.
    30  	bigOne = big.NewInt(1)
    31  
    32  	// oneLsh256 is 1 shifted left 256 bits.  It is defined here to avoid
    33  	// the overhead of creating it multiple times.
    34  	oneLsh256 = new(big.Int).Lsh(bigOne, 256)
    35  
    36  	// enabling this will simulation mode with hard coded difficulty set to 1
    37  	// the variable is knowingly not exported, so no one can tinker with it
    38  	//simulation = false // simulation mode is disabled
    39  )
    40  
    41  // HashToBig converts a PoW has into a big.Int that can be used to
    42  // perform math comparisons.
    43  func HashToBig(buf crypto.Hash) *big.Int {
    44  	// A Hash is in little-endian, but the big package wants the bytes in
    45  	// big-endian, so reverse them.
    46  	blen := len(buf) // its hardcoded 32 bytes, so why do len but lets do it
    47  	for i := 0; i < blen/2; i++ {
    48  		buf[i], buf[blen-1-i] = buf[blen-1-i], buf[i]
    49  	}
    50  
    51  	return new(big.Int).SetBytes(buf[:])
    52  }
    53  
    54  // this function calculates the difficulty in big num form
    55  func ConvertDifficultyToBig(difficultyi uint64) *big.Int {
    56  	if difficultyi == 0 {
    57  		panic("difficulty can never be zero")
    58  	}
    59  	// (1 << 256) / (difficultyNum )
    60  	difficulty := new(big.Int).SetUint64(difficultyi)
    61  	denominator := new(big.Int).Add(difficulty, bigZero) // above 2 lines can be merged
    62  	return new(big.Int).Div(oneLsh256, denominator)
    63  }
    64  
    65  func ConvertIntegerDifficultyToBig(difficultyi *big.Int) *big.Int {
    66  
    67  	if difficultyi.Cmp(bigZero) == 0 { // if work_pow is less than difficulty
    68  		panic("difficulty can never be zero")
    69  	}
    70  
    71  	return new(big.Int).Div(oneLsh256, difficultyi)
    72  }
    73  
    74  // this function check whether the pow hash meets difficulty criteria
    75  // however, it take diff in  bigint format
    76  func CheckPowHashBig(pow_hash crypto.Hash, big_difficulty_integer *big.Int) bool {
    77  	big_pow_hash := HashToBig(pow_hash)
    78  
    79  	big_difficulty := ConvertIntegerDifficultyToBig(big_difficulty_integer)
    80  	if big_pow_hash.Cmp(big_difficulty) <= 0 { // if work_pow is less than difficulty
    81  		return true
    82  	}
    83  	return false
    84  }