forked from townforge/townforge
to protect against the first large monero miner to merge mine being able to easily 51% the chain
83 lines
3.2 KiB
C++
83 lines
3.2 KiB
C++
// Copyright (c) 2019, Crypto City
|
|
//
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
// permitted provided that the following conditions are met:
|
|
//
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
// conditions and the following disclaimer.
|
|
//
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
// of conditions and the following disclaimer in the documentation and/or other
|
|
// materials provided with the distribution.
|
|
//
|
|
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
|
// used to endorse or promote products derived from this software without specific
|
|
// prior written permission.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#include "gtest/gtest.h"
|
|
#include "string_tools.h"
|
|
#include "crypto/crypto.h"
|
|
#include "ringct/rctOps.h"
|
|
#include "cryptonote_basic/cryptonote_basic.h"
|
|
#include "cryptonote_basic/stake.h"
|
|
|
|
#define ASSERT_EX(x) do { bool ex = false; try { x; } catch(...) { ex = true; } ASSERT_TRUE(ex); } while(0)
|
|
|
|
TEST(stake, div256_64)
|
|
{
|
|
crypto::hash hash, exp_hash;
|
|
|
|
ASSERT_EX(cryptonote::div256_64(crypto::null_hash, 0, hash));
|
|
ASSERT_EX(cryptonote::div256_64(rct::rct2hash(rct::G), 0, hash));
|
|
|
|
cryptonote::div256_64(crypto::null_hash, 1, hash);
|
|
ASSERT_EQ(hash, crypto::null_hash);
|
|
|
|
hash = crypto::null_hash;
|
|
hash.data[0] = 128;
|
|
cryptonote::div256_64(hash, 17, hash);
|
|
ASSERT_EQ(hash.data[0], 7);
|
|
hash.data[0] = 0;
|
|
ASSERT_EQ(hash, crypto::null_hash);
|
|
|
|
hash = crypto::null_hash;
|
|
hash.data[0] = 128;
|
|
cryptonote::div256_64(hash, 128, hash);
|
|
ASSERT_EQ(hash.data[0], 1);
|
|
hash.data[0] = 0;
|
|
ASSERT_EQ(hash, crypto::null_hash);
|
|
|
|
hash = crypto::null_hash;
|
|
hash.data[0] = 128;
|
|
cryptonote::div256_64(hash, 129, hash);
|
|
ASSERT_EQ(hash, crypto::null_hash);
|
|
|
|
hash = crypto::null_hash;
|
|
hash.data[0] = 0x76;
|
|
hash.data[1] = 0x12;
|
|
hash.data[2] = 0x7e;
|
|
hash.data[3] = 0x0d;
|
|
hash.data[4] = 0xca;
|
|
cryptonote::div256_64(hash, 0xca0d7e1276, hash);
|
|
exp_hash = crypto::null_hash;
|
|
exp_hash.data[0] = 1;
|
|
ASSERT_EQ(hash, exp_hash);
|
|
|
|
ASSERT_TRUE(epee::string_tools::hex_to_pod("efcdabefcdab99887766554433221100badcfef0debc9a785634121032547698", hash));
|
|
cryptonote::div256_64(hash, 0xe598da876a073aa8, hash);
|
|
ASSERT_TRUE(epee::string_tools::hex_to_pod("0527af809a390ebb19d35977714152075a84464479b0fea90000000000000000", exp_hash));
|
|
ASSERT_EQ(hash, exp_hash);
|
|
}
|