Beispiel #1
0
        public void SetUp()
        {
            _trueCryptoRandom = new CryptoRandom();

            // WARN: order reflects the internal implementation of the service (tests may fail after any refactoring)
            _testRandom = new TestRandom(
                NetTestVectors.NonceA,
                NetTestVectors.EphemeralKeyA.KeyBytes,
                _trueCryptoRandom.GenerateRandomBytes(100),
                NetTestVectors.NonceB,
                NetTestVectors.EphemeralKeyB.KeyBytes,
                _trueCryptoRandom.GenerateRandomBytes(100));

            _messageSerializationService = new MessageSerializationService();
            _messageSerializationService.Register(new AuthMessageSerializer());
            _messageSerializationService.Register(new AuthEip8MessageSerializer(new Eip8MessagePad(_testRandom)));
            _messageSerializationService.Register(new AckMessageSerializer());
            _messageSerializationService.Register(new AckEip8MessageSerializer(new Eip8MessagePad(_testRandom)));

            _eciesCipher = new EciesCipher(_trueCryptoRandom); // TODO: provide a separate test random with specific IV and epehemeral key for testing

            _initiatorService = new EncryptionHandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _signer, NetTestVectors.StaticKeyA, NullLogManager.Instance);
            _recipientService = new EncryptionHandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _signer, NetTestVectors.StaticKeyB, NullLogManager.Instance);

            _initiatorHandshake = new EncryptionHandshake();
            _recipientHandshake = new EncryptionHandshake();

            _auth = null;
            _ack  = null;
        }
Beispiel #2
0
        public void Can_calculate_agreement()
        {
            CryptoRandom random      = new CryptoRandom();
            PrivateKey   privateKey1 = new PrivateKey(random.GenerateRandomBytes(32));
            PrivateKey   privateKey2 = new PrivateKey(random.GenerateRandomBytes(32));

            byte[] sharedSecret1 = BouncyCrypto.Agree(privateKey1, privateKey2.PublicKey);
            byte[] sharedSecret2 = BouncyCrypto.Agree(privateKey2, privateKey1.PublicKey);

            Assert.AreEqual(sharedSecret1, sharedSecret2);
        }
        public void Chaotic_test()
        {
            const int accountsCount = 100;

            CryptoRandom random = new CryptoRandom();
            List <AddressWithStorage> addressesWithStorage = new List <AddressWithStorage>();

            for (int i = 0; i < accountsCount; i++)
            {
                AddressWithStorage addressWithStorage = new AddressWithStorage();
                addressWithStorage.StorageCells = new StorageCell[i];
                byte[] addressBytes = random.GenerateRandomBytes(20);
                addressWithStorage.Address = new Address(addressBytes);

                for (int j = 0; j < i; j++)
                {
                    byte[]      storageIndex = random.GenerateRandomBytes(32);
                    UInt256     index        = new UInt256(storageIndex);
                    StorageCell storageCell  = new StorageCell(addressWithStorage.Address, index);
                    addressWithStorage.StorageCells[j] = storageCell;
                }

                addressesWithStorage.Add(addressWithStorage);
            }

            IDb       memDb     = new MemDb();
            TrieStore trieStore = new TrieStore(memDb, LimboLogs.Instance);
            StateTree tree      = new StateTree(trieStore, LimboLogs.Instance);

            for (int i = 0; i < accountsCount; i++)
            {
                Account     account     = Build.An.Account.WithBalance((UInt256)i).TestObject;
                StorageTree storageTree = new StorageTree(trieStore, Keccak.EmptyTreeHash, LimboLogs.Instance);
                for (int j = 0; j < i; j++)
                {
                    storageTree.Set(addressesWithStorage[i].StorageCells[j].Index, new byte[1] {
                        1
                    });
                }

                storageTree.UpdateRootHash();
                storageTree.Commit(0);

                account = account.WithChangedStorageRoot(storageTree.RootHash);
                tree.Set(addressesWithStorage[i].Address, account);
            }

            tree.UpdateRootHash();
            tree.Commit(0);

            for (int i = 0; i < accountsCount; i++)
            {
                AccountProofCollector collector = new AccountProofCollector(addressesWithStorage[i].Address, addressesWithStorage[i].StorageCells.Select(sc => sc.Index).ToArray());
                tree.Accept(collector, tree.RootHash, true);

                AccountProof accountProof = collector.BuildResult();
                accountProof.Address.Should().Be(addressesWithStorage[i].Address);
                accountProof.Balance.Should().Be((UInt256)i);
                accountProof.Nonce.Should().Be(0);
                accountProof.CodeHash.Should().Be(Keccak.OfAnEmptyString);
                if (i != 0)
                {
                    accountProof.StorageRoot.Should().NotBe(Keccak.EmptyTreeHash);
                }
                accountProof.StorageProofs.Length.Should().Be(i);

                for (int j = 0; j < i; j++)
                {
                    byte[] indexBytes = new byte[32];
                    addressesWithStorage[i].StorageCells[j].Index.ToBigEndian(indexBytes.AsSpan());
                    accountProof.StorageProofs[j].Key.ToHexString().Should().Be(indexBytes.ToHexString(), $"{i} {j}");

                    TrieNode node = new TrieNode(NodeType.Unknown, accountProof.StorageProofs[j].Proof.Last());
                    node.ResolveNode(null);
                    // TestContext.Write($"|[{i},{j}]");
                    if (node.Value.Length != 1)
                    {
                        TestContext.WriteLine();
                        TestContext.WriteLine(addressesWithStorage[i].Address);
                        TestContext.WriteLine(i);
                        foreach (StorageCell storageCell in addressesWithStorage[i].StorageCells)
                        {
                            TestContext.WriteLine("storage: " + storageCell.Index);
                        }
                    }

                    node.Value.Should().BeEquivalentTo(new byte[] { 1 });
                }
            }
        }