code.vegaprotocol.io/vega@v0.79.0/libs/crypto/proof_of_work_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package crypto_test
    17  
    18  import (
    19  	"encoding/hex"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/libs/crypto"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestPoW(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	_, _, err := crypto.PoW(crypto.RandomHash(), crypto.RandomHash(), 5, "nonExisting")
    32  	require.Error(t, err)
    33  
    34  	_, _, err = crypto.PoW(crypto.RandomHash(), crypto.RandomHash(), 257, "nonExisting")
    35  	require.Error(t, err)
    36  
    37  	blockHash := "2FB2146FC01F21D358323174BAA230E7DE61C0F150B7FBC415C896B0C23E50FF"
    38  	txID := "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4"
    39  
    40  	nonce, _, err := crypto.PoW(blockHash, txID, 2, crypto.Sha3)
    41  	require.NoError(t, err)
    42  	require.Equal(t, uint64(4), nonce)
    43  	success, _ := crypto.Verify(blockHash, txID, nonce, crypto.Sha3, 2)
    44  	require.True(t, success)
    45  }
    46  
    47  func TestVerify(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	success, _ := crypto.Verify("", "", 0, "non existing", 0)
    51  	require.False(t, false, success)
    52  	success, _ = crypto.Verify("", "", 0, "non existing", 1)
    53  	require.False(t, false, success)
    54  	success, _ = crypto.Verify("", "", 0, crypto.Sha3, 1)
    55  	require.False(t, false, success)
    56  	success, _ = crypto.Verify("", "", 4, crypto.Sha3, 1)
    57  	require.False(t, false, success)
    58  	success, _ = crypto.Verify("", "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", 4, crypto.Sha3, 2)
    59  	require.False(t, false, success)
    60  	success, _ = crypto.Verify("2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", 4, crypto.Sha3, 3)
    61  	require.False(t, false, success)
    62  	success, _ = crypto.Verify("2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", 4, crypto.Sha3, 2)
    63  	require.True(t, true, success)
    64  	success, _ = crypto.Verify("2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", 4, crypto.Sha3, 1)
    65  	require.True(t, true, success)
    66  	success, _ = crypto.Verify("2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4", 4, crypto.Sha3, 0)
    67  	require.True(t, true, success)
    68  }
    69  
    70  func TestCountZeros(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	tcs := []struct {
    74  		name       string
    75  		blockHash  string
    76  		txID       string
    77  		difficulty uint
    78  	}{
    79  		{
    80  			// 00001315c698aae3e559e9de507c43260e4b89e992840c281c68d54663eb02ae
    81  			name:       "with difficulty set to 19",
    82  			blockHash:  "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4",
    83  			txID:       "DFE522E234D67E6AE3F017859F898E576B3928EA57310B765398615A0D3FDE2F",
    84  			difficulty: 19,
    85  		}, {
    86  			// 00000d9ae20dd3c9ed57260ffe67832a98ccb43f797bba82f8a21be137e0ae5b
    87  			name:       "with difficulty set to 20",
    88  			blockHash:  "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4",
    89  			txID:       "5B87F9DFA41DABE84A11CA78D9FE11DA8FC2AA926004CA66454A7AF0A206480D",
    90  			difficulty: 20,
    91  		}, {
    92  			// 000003bbf0cde49e3899ad23282b18defbc12a65f07c95d768464b87024df368
    93  			name:       "with difficulty set to 21",
    94  			blockHash:  "2E7A16D9EF690F0D2BEED115FBA13BA2AAA16C8F971910AD88C72B9DB010C7D4",
    95  			txID:       "B14DD602ED48C9F7B5367105A4A97FFC9199EA0C9E1490B786534768DD1538EF",
    96  			difficulty: 21,
    97  		}, {
    98  			// 0000039c42f8c0a62ad39e1459393803104d8fdc2cd15410daaec3d8de7b85a0
    99  			name:       "with difficulty set to 22",
   100  			blockHash:  "B14DD602ED48C9F7B5367105A4A97FFC9199EA0C9E1490B786534768DD1538EF",
   101  			txID:       "94A9CB1532011081B013CCD8E6AAA832CAB1CBA603F0C5A093B14C4961E5E7F0",
   102  			difficulty: 22,
   103  		},
   104  	}
   105  
   106  	for _, tc := range tcs {
   107  		t.Run(tc.name, func(tt *testing.T) {
   108  			tt.Parallel()
   109  			_, hash, err := crypto.PoW(tc.blockHash, tc.txID, tc.difficulty, crypto.Sha3)
   110  			require.NoError(tt, err)
   111  			assert.NotEmpty(tt, hash)
   112  
   113  			zeros := crypto.CountZeros(hash)
   114  
   115  			require.Equal(tt, byte(tc.difficulty), zeros)
   116  		})
   117  	}
   118  }
   119  
   120  func TestDifficulty(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	tests := []struct {
   124  		name       string
   125  		difficulty uint
   126  		nonce      uint64
   127  		blockHash  string
   128  		tid        string
   129  		proof      []byte
   130  	}{
   131  		{
   132  			name:       "difficulty 4",
   133  			difficulty: 4,
   134  			nonce:      0,
   135  			blockHash:  "792ca202b84226c739f9923046a0f4e7b5ff9e6f1b5636d8e26a8e2c5dec70ac",
   136  			tid:        "3b8399cdffee2686d75d1a96d22cd49cd11f62c93da20e72239895bfdaf4b772",
   137  			proof:      []byte("03f9f7d9911d3ca37c3356f10cd04273e788d1f57a9bc2396e7b5aa2e8d74557"),
   138  		},
   139  		{
   140  			name:       "difficulty 8",
   141  			difficulty: 8,
   142  			nonce:      402,
   143  			blockHash:  "ffb67ea4111d466d363a5c8f355bf81e2e3504563af273f5de81a005a6247e14",
   144  			tid:        "c40de04280ce8c40ee41b5005c23a1358b4fbf31f6dcb675e8246b174458274e",
   145  			proof:      []byte("0053ea7687bd7652803af4300a7e17868267c32e4fb7f09375c46c367fd7646b"),
   146  		},
   147  		{
   148  			name:       "difficulty 12",
   149  			difficulty: 12,
   150  			nonce:      2560,
   151  			blockHash:  "d9ae00ce4c4fc96d8e72bb18f6990b833cc7724ad70322604c572f6e194d777f",
   152  			tid:        "fcbbb4cc8dcd402a07af050bb809a04bd82f9c95b6e5a56768d3724a4abb09f0",
   153  			proof:      []byte("0008bbe071959bfe7fc426c4f378fcdb9540b3f931f4a0b09469f5bf0fddcb86"),
   154  		},
   155  		{
   156  			name:       "difficulty 16",
   157  			difficulty: 16,
   158  			nonce:      23845,
   159  			blockHash:  "dc4b61de2138856406acdabcc502be708bff7c945857ea032011a8b4b0cf54f4",
   160  			tid:        "3954a15b2e1ec457ae100c56e2aa43786b4612644926403d59fd8cdcb29d825f",
   161  			proof:      []byte("00000fd8f55699845ac3192af013928916050eab088437943708b83b27490862"),
   162  		},
   163  		{
   164  			name:       "difficulty 20",
   165  			difficulty: 20,
   166  			nonce:      85863,
   167  			blockHash:  "8890702af457ddcda01fba579a126adcecae954781500acb546fef9c8087a239",
   168  			tid:        "74030ee7dc931be9d9cc5f2c9d44ac174b4144b377ef07a7bb1781856921dd43",
   169  			proof:      []byte("000007542dcb39d1471fd6c7424a547b9039382e055ceed10c839f2b76f88c0d"),
   170  		},
   171  	}
   172  	for _, tc := range tests {
   173  		t.Run(tc.name, func(tt *testing.T) {
   174  			tt.Parallel()
   175  
   176  			n, h, err := crypto.PoW(tc.blockHash, tc.tid, tc.difficulty, crypto.Sha3)
   177  			require.NoError(tt, err)
   178  			require.Equal(tt, tc.nonce, n)
   179  			require.Equal(tt, string(tc.proof), hex.EncodeToString(h))
   180  
   181  			b, d := crypto.Verify(tc.blockHash, tc.tid, tc.nonce, crypto.Sha3, tc.difficulty)
   182  			require.Equal(tt, true, b)
   183  			require.True(tt, d >= byte(tc.difficulty))
   184  		})
   185  	}
   186  }