Beispiel #1
0
        public void Constructor_CreateBetaRuleFromNandOfLiterals_SetOfLengthOneForEachChildWithNegatedLiterals()
        {
            // Arrange
            Nand nand = (Nand)PropositionGenerator.CreateBinaryConnectiveWithRandomSymbols(Nand.SYMBOL);

            TestConstructorForNotAndEquivalent(nand);
        }
Beispiel #2
0
        public void Open()
        {
            DiskInfo disk    = SelectedDisk;
            var      storage = new CachedStorage(new DeviceStream(disk.PhysicalName, disk.Length).AsStorage(), disk.SectorSize * 100, 4, true);

            storage.SetReadOnly();
            Stream stream = storage.AsStream();

            Keyset keyset = OpenKeyset();
            var    nand   = new Nand(stream, keyset);

            Stream prodinfo    = nand.OpenProdInfo();
            var    calibration = new Calibration(prodinfo);

            keyset.EticketExtKeyRsa = Crypto.DecryptRsaKey(calibration.EticketExtKeyRsa, keyset.EticketRsaKek);
            Ticket[] tickets = GetTickets(keyset, nand);

            using (var outStream = new StreamWriter("titlekeys.txt"))
            {
                foreach (Ticket ticket in tickets)
                {
                    byte[] key = ticket.GetTitleKey(keyset);
                    outStream.WriteLine($"{ticket.RightsId.ToHexString()},{key.ToHexString()}");
                }
            }
        }
Beispiel #3
0
        public void Copy_CopyingConjunctionWithTwoRandomVariableSymbols_ExpectedDifferentReferencesForConnective()
        {
            // Arrange // Act // Assert
            Nand nand = generateNand();

            Copy_CopyingBinaryConnectiveWithTwoRandomVariableSymbols_ExpectedDifferentReferencesForConnective(nand);
        }
Beispiel #4
0
        public void Constructor_CreateAlphaRuleFromNegatedNandOfLiterals_SetOfLengthTwoReturnedWithTwoLiterals()
        {
            // Arrange
            Nand     nand        = (Nand)PropositionGenerator.CreateBinaryConnectiveWithRandomSymbols(Nand.SYMBOL);
            Negation negatedNand = new Negation();

            negatedNand.LeftSuccessor = nand;

            HashSet <Proposition> propositions = new HashSet <Proposition>()
            {
                negatedNand
            };

            SemanticTableauxElement alphaRuleElement = new SemanticTableauxElement(propositions);
            SemanticTableauxElement child            = alphaRuleElement.LeftChild;

            // Act
            HashSet <Proposition> childPropositions = child.Propositions;

            int actualNumberOfLiterals   = childPropositions.Count;
            int expectedNumberOfLiterals = 2;

            // Assert
            actualNumberOfLiterals.Should().Be(expectedNumberOfLiterals, "Because a conjunction of two literals was given");
        }
Beispiel #5
0
        public void Visit(Negation visitable)
        {
            var nandRoot = new Nand();

            InsertNodeDouble(nandRoot, visitable.LeftNode);

            BinaryTree.Root = nandRoot;
            visitable.Nand  = nandRoot;
        }
        public void Visit(Nand visitable)
        {
            var root = ConvertNandToNegation(visitable);

            root.Belongs = visitable.Belongs;
            visitable.Belongs.Components.Add(root);
            visitable.Belongs.Components.Remove(visitable);
            Calculate(root);
        }
Beispiel #7
0
        public void NandConstructorTest()
        {
            Nand a = new Nand();

            Assert.IsNotNull(a.Childs);
            Assert.AreEqual('%', a.Name);
            Assert.AreEqual(2, a.nOperand);
            Assert.AreEqual(SymbolType.operational, a.Type);
        }
Beispiel #8
0
        public void Calculate_CalculateAllPossibleTruthValues_ExpectedTrueWhenLeftAndRightAreTrue(bool leftTruthValue, bool rightTruthValue, bool expectedTruthValue)
        {
            // Arrange
            Nand   nand    = generateNand();
            string message = "because nand is only false when both left and right are true";

            // Act // Assert
            Calculate_DetermineAllPossibleValuesBetweenTwoPropositionVariables(nand, message, leftTruthValue, rightTruthValue, expectedTruthValue);
        }
Beispiel #9
0
        public bool AttemptDumpCert(FileInfo prodinfo = null, Nand nand = null)
        {
            try
            {
                Calibration cal0;
                byte[]      certBytes;
                using (Stream prodinfoFile = HACGUIKeyset.TempPRODINFOFileInfo.Create())
                {
                    // copy PRODINFO from local file
                    Stream prodinfoStream = null;

                    if (prodinfo != null)
                    {
                        prodinfoStream = prodinfo.OpenRead();
                    }
                    else
                    {
                        prodinfoStream = nand.OpenProdInfo();
                    }

                    prodinfoStream.CopyTo(prodinfoFile);
                    prodinfoStream.Close();

                    prodinfoFile.Seek(0, SeekOrigin.Begin);
                    cal0 = new Calibration(prodinfoFile);

                    prodinfoFile.Seek(0x0AD0, SeekOrigin.Begin); // seek to certificate length
                    byte[] buffer = new byte[0x4];
                    prodinfoFile.Read(buffer, 0, buffer.Length); // read cert length
                    uint certLength = BitConverter.ToUInt32(buffer, 0);

                    certBytes = new byte[certLength];
                    prodinfoFile.Seek(0x0AE0, SeekOrigin.Begin);      // seek to cert (should be redundant?)
                    prodinfoFile.Read(certBytes, 0, (int)certLength); // read actual cert
                }

                byte[] counter = cal0.SslExtKey.Take(0x10).ToArray();
                byte[] key     = cal0.SslExtKey.Skip(0x10).ToArray();                               // bit strange structure but it works

                new Aes128CtrTransform(HACGUIKeyset.Keyset.SslRsaKek, counter).TransformBlock(key); // decrypt private key

                X509Certificate2 certificate = new X509Certificate2();
                certificate.Import(certBytes);
                certificate.ImportPrivateKey(key);

                byte[] pfx = certificate.Export(X509ContentType.Pkcs12, "switch");
                using (Stream pfxStream = HACGUIKeyset.GetClientCertificateByName(PickConsolePage.ConsoleName).Create())
                    pfxStream.Write(pfx, 0, pfx.Length);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #10
0
        public void toNandTest()
        {
            Variable A = new Variable('A');
            Variable B = new Variable('B');

            Nand nand = new Nand(A, B);

            nand = (Nand)nand.toNand();

            Assert.AreEqual("(A % B)", nand.ToString());
        }
Beispiel #11
0
 private static void ReadSwitchFs(string nandFile)
 {
     using (var logger = new ProgressBar())
         using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
         {
             Keyset keyset = OpenKeyset();
             var    nand   = new Nand(stream, keyset);
             FatFileSystemProvider user = nand.OpenSystemPartition();
             SwitchFs sdfs = SwitchFs.OpenNandPartition(keyset, user);
         }
 }
Beispiel #12
0
 private static void ReadCalibration(string nandFile)
 {
     using (var logger = new ProgressBar())
         using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
         {
             Keyset keyset      = OpenKeyset();
             var    nand        = new Nand(stream, keyset);
             Stream prodinfo    = nand.OpenProdInfo();
             var    calibration = new Calibration(prodinfo);
         }
 }
        private Component ConvertNandToNegation(Nand visitable)
        {
            var root        = new Negation();
            var conjunction = new Conjunction();

            _binaryTree.InsertNode(conjunction, BinaryTree.CloneNode(visitable.LeftNode, _binaryTree));
            _binaryTree.InsertNode(conjunction, BinaryTree.CloneNode(visitable.RightNode, _binaryTree));
            _binaryTree.InsertNode(root, conjunction);

            return(root);
        }
Beispiel #14
0
        public AbstractValue Evaluate(Nand expr)
        {
            AbstractValue left  = Evaluate(expr.Left);
            AbstractValue right = Evaluate(expr.Right);

            if ((left != null) && (right != null) && (left is STD_LOGIC_VALUE) && (right is STD_LOGIC_VALUE))
            {
                return(STD_LOGIC_VALUE.NAND(left as STD_LOGIC_VALUE, right as STD_LOGIC_VALUE));
            }
            throw new NotImplementedException();
        }
Beispiel #15
0
        public void TestHandleNegative()
        {
            Node node = new Nand();

            node.AddDefaultInputs("IN1", true);
            node.AddDefaultInputs("IN2", true);
            node.SetDefaultInputs();

            node.Handle();

            Assert.IsFalse(node.Output);
        }
Beispiel #16
0
        public void OperateTwoTest()
        {
            Variable p = new Variable('p');
            Variable q = new Variable('q');

            Nand a = new Nand();

            a.Operate(p, q);
            Assert.AreEqual(p, a.Childs[0]);
            Assert.AreEqual(q, a.Childs[1]);

            Assert.ThrowsException <ArgumentNullException>(() => a.Operate(null, null));
        }
Beispiel #17
0
        public void Visit(Conjunction visitable)
        {
            var nandRoot = new Negation();
            var nandLeft = new Nand();

            InsertNodeSingle(nandLeft, visitable.LeftNode);
            InsertNodeSingle(nandLeft, visitable.RightNode);
            BinaryTree.InsertNode(nandRoot, nandLeft);

            Calculate(nandRoot);

            BinaryTree.Root = nandRoot.Nand;
            visitable.Nand  = nandRoot.Nand;
        }
Beispiel #18
0
        public void OperateListTest()
        {
            List <Variable> vars = new List <Variable>()
            {
                new Variable('p'),
                new Variable('q')
            };

            Nand a = new Nand();

            a.Operate(vars);
            Assert.AreEqual(vars[0], a.Childs[0]);
            Assert.AreEqual(vars[1], a.Childs[1]);
        }
Beispiel #19
0
        public void Visit(Disjunction visitable)
        {
            var nandRoot  = new Nand();
            var nandLeft  = new Nand();
            var nandRight = new Nand();

            InsertNodeDouble(nandLeft, visitable.LeftNode);
            InsertNodeDouble(nandRight, visitable.RightNode);

            BinaryTree.InsertNode(nandRoot, nandLeft);
            BinaryTree.InsertNode(nandRoot, nandRight);

            BinaryTree.Root = nandRoot;
            visitable.Nand  = nandRoot;
        }
Beispiel #20
0
        private static Ticket[] GetTickets(Keyset keyset, Nand nand, IProgressReport logger = null)
        {
            var           tickets = new List <Ticket>();
            NandPartition system  = nand.OpenSystemPartition();

            Stream saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read);

            tickets.AddRange(ReadTickets(keyset, saveE1File));

            Stream saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read);

            tickets.AddRange(ReadTickets(keyset, saveE2));

            logger?.LogMessage($"Found {tickets.Count} tickets");

            return(tickets.ToArray());
        }
Beispiel #21
0
        private static void DumpTickets(string nandFile)
        {
            using (var logger = new ProgressBar())
                using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
                {
                    Keyset   keyset  = OpenKeyset();
                    var      nand    = new Nand(stream, keyset);
                    Ticket[] tickets = GetTickets(keyset, nand, logger);

                    Directory.CreateDirectory("tickets");
                    foreach (Ticket ticket in tickets)
                    {
                        string filename = Path.Combine("tickets", $"{ticket.RightsId.ToHexString()}.tik");
                        File.WriteAllBytes(filename, ticket.File);
                    }
                }
        }
Beispiel #22
0
        private static Ticket[] GetTickets(Keyset keyset, Nand nand, IProgressReport logger = null)
        {
            var tickets = new List <Ticket>();
            FatFileSystemProvider system = nand.OpenSystemPartition();

            IFile saveE1File = system.OpenFile("/save/80000000000000E1", OpenMode.Read);

            tickets.AddRange(ReadTickets(keyset, saveE1File.AsStream()));

            IFile saveE2 = system.OpenFile("/save/80000000000000E2", OpenMode.Read);

            tickets.AddRange(ReadTickets(keyset, saveE2.AsStream()));

            logger?.LogMessage($"Found {tickets.Count} tickets");

            return(tickets.ToArray());
        }
Beispiel #23
0
        public void NandConstructorTest1()
        {
            Variable p = new Variable('p');
            Variable q = new Variable('q');

            Nand a = new Nand(p, q);

            Assert.IsNotNull(a.Childs);
            Assert.AreEqual('%', a.Name);
            Assert.AreEqual(2, a.nOperand);
            Assert.AreEqual(SymbolType.operational, a.Type);

            Assert.AreEqual(p, a.Childs[0]);
            Assert.AreEqual(q, a.Childs[1]);

            //
            Assert.ThrowsException <ArgumentNullException>(() => new Nand(null, null));
        }
Beispiel #24
0
        public void GetTruthValueArrayTest()
        {
            Variable p = new Variable('p');
            Variable q = new Variable('q');
            Nand     a = new Nand(p, q);

            bool[] dict = new bool[130];

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    dict['p'] = i == 1;
                    dict['q'] = j == 1;

                    Assert.AreEqual((i & j) == 0, a.GetTruthValue(dict));
                }
            }
        }
Beispiel #25
0
        public void ToStringTest()
        {
            Variable p = new Variable('p');
            Variable q = new Variable('q');
            Nand     a = new Nand(p, q);

            Assert.AreEqual("(p % q)", a.ToString());

            Not Left  = new Not(p);
            Or  Right = new Or(p, q);

            a.Operate(Left, Right);

            Assert.AreEqual("(~p % (p | q))", a.ToString());

            Left = new Not(new Nand(p, q));
            a.Operate(Left, Right);

            Assert.AreEqual("(~(p % q) % (p | q))", a.ToString());
        }
Beispiel #26
0
        private static void GetTitleKeys(string nandFile)
        {
            using (var logger = new ProgressBar())
                using (var stream = new FileStream(nandFile, FileMode.Open, FileAccess.Read))
                {
                    Keyset keyset      = OpenKeyset();
                    var    nand        = new Nand(stream, keyset);
                    Stream prodinfo    = nand.OpenProdInfo();
                    var    calibration = new Calibration(prodinfo);

                    keyset.EticketExtKeyRsa = Crypto.DecryptRsaKey(calibration.EticketExtKeyRsa, keyset.EticketRsaKek);
                    Ticket[] tickets = GetTickets(keyset, nand, logger);

                    foreach (Ticket ticket in tickets)
                    {
                        byte[] key = ticket.GetTitleKey(keyset);
                        logger.LogMessage($"{ticket.RightsId.ToHexString()},{key.ToHexString()}");
                    }
                }
        }
Beispiel #27
0
 public static bool InsertNAND(IStorage input, bool raw)
 {
     if (Validator(input))
     {
         if (NAND != null && !raw)
         {
             if (!RequestSwitchSource())
             {
                 return(false);
             }
         }
         NAND       = new Nand(input.AsStream(FileAccess.Read), HACGUIKeyset.Keyset);
         NANDSource = input;
         OnNANDPluggedIn();
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #28
0
        public void GetTruthValueDictTest()
        {
            Variable p = new Variable('p');
            Variable q = new Variable('q');
            Nand     a = new Nand(p, q);

            Dictionary <char, bool> dict = new Dictionary <char, bool>();

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    dict['p'] = i == 1;
                    dict['q'] = j == 1;

                    Assert.AreEqual((i & j) == 0, a.GetTruthValue(dict));
                }
            }

            dict.Remove('p');
            Assert.ThrowsException <KeyNotFoundException>(
                () => a.GetTruthValue(dict));
        }
Beispiel #29
0
 public void Visit(Nand visited)
 {
     ResetNode(visited);
 }
Beispiel #30
0
 public void Visit(Nand visited)
 {
     PrintStandardNode(visited);
 }