public static Word ToWord(this int number, int wordLength) { string s = Convert.ToString(number, 2); if (wordLength < s.Length) { return(ToWord(number)); } else { return(Word.Parse(new string('0', wordLength - s.Length) + s)); } }
public static Word operator >>(Word w1, int count) { char[] tmp = new char[w1.Size]; for (int i = 0; i < w1.Size; i++) { if (i + count > w1.Size - 1) { tmp[i] = '0'; } else { tmp[i] = w1.GetBit(i + count); } } return(Word.Parse(tmp)); }
public bool IsEqualTo(string s) { Word w = Word.Parse(s.Replace(" ", "")); if (_size != w.Size) { return(false); } for (int i = 0; i < _size; i++) { if (w.GetBit(i) != _bits[i]) { return(false); } } return(true); }
public static Word ToWord(this int number) { return(Word.Parse(Convert.ToString(number, 2))); }
public static Word SerpentSBox(int round, Word input) { if (input.Contains('?')) { return(Word.Parse("????")); } if (input.IsEqualTo("0000")) { return(Word.Parse("0000")); } int SBoxID = round % 8; switch (SBoxID) { case 0: // Input: [02] 0010 -> Output: 1??? if (input.IsEqualTo("0010")) { return(Word.Parse("1???")); } // Input: [04] 0100 -> Output: 1??? if (input.IsEqualTo("0100")) { return(Word.Parse("1???")); } // Input: [06] 0110 -> Output: 0??? if (input.IsEqualTo("0110")) { return(Word.Parse("0???")); } break; case 1: // Input: [04] 0100 -> Output: ?1?? if (input.IsEqualTo("0100")) { return(Word.Parse("?1??")); } // Input: [08] 1000 -> Output: ?1?? if (input.IsEqualTo("1000")) { return(Word.Parse("?1??")); } // Input: [0C] 1100 -> Output: ?0?? if (input.IsEqualTo("1100")) { return(Word.Parse("?0??")); } break; case 2: // Input: [02] 0010 -> Output: ???1 if (input.IsEqualTo("0010")) { return(Word.Parse("???1")); } // Input: [08] 1000 -> Output: ???1 if (input.IsEqualTo("1000")) { return(Word.Parse("???1")); } // Input: [0A] 1010 -> Output: ???0 if (input.IsEqualTo("1010")) { return(Word.Parse("???0")); } break; case 3: break; case 4: case 5: // Input: [04] 0100 -> Output: ???1 if (input.IsEqualTo("0100")) { return(Word.Parse("???1")); } // Input: [0B] 1011 -> Output: ???1 if (input.IsEqualTo("1011")) { return(Word.Parse("???1")); } // Input: [0F] 1111 -> Output: ???0 if (input.IsEqualTo("1111")) { return(Word.Parse("???0")); } break; case 6: // Input: [02] 0010 -> Output: ??1? if (input.IsEqualTo("0010")) { return(Word.Parse("??1?")); } // Input: [04] 0100 -> Output: ??1? if (input.IsEqualTo("0100")) { return(Word.Parse("??1?")); } // Input: [06] 0110 -> Output: ??0? if (input.IsEqualTo("0110")) { return(Word.Parse("??0?")); } break; case 7: break; } return(Word.Parse("????")); }
public static void OutputToInputProbability(Word output, Word input, int sbox, out int probNumerator, out int probDenominator) { if (input.Size != 4 || output.Size != 4) { throw new Exception("Input and output word sizes should be 4!"); } Word tmpIn = Word.Parse(input.ToString()); Word tmpOut = Word.Parse(output.ToString()); int[] inQ = tmpIn.QuestionMarkPositions(); int[] outQ = tmpOut.QuestionMarkPositions(); int[] inputValues = new int[0]; int[] outputValues = new int[0]; for (int i = 0; i < (1 << inQ.Length); i++) { Word w1 = i.ToWord(inQ.Length); for (int j = 0; j < inQ.Length; j++) { tmpIn.SetBit(w1.GetBit(j), inQ[j]); } int t = tmpIn.ToInt32(); Array.Resize(ref inputValues, inputValues.Length + 1); inputValues[inputValues.Length - 1] = t; } for (int i = 0; i < (1 << outQ.Length); i++) { Word w1 = i.ToWord(outQ.Length); for (int j = 0; j < outQ.Length; j++) { tmpOut.SetBit(w1.GetBit(j), outQ[j]); } int t = tmpOut.ToInt32(); Array.Resize(ref outputValues, outputValues.Length + 1); outputValues[outputValues.Length - 1] = t; } uint[,] ddt = XORTable(SBox[sbox], 4, 4); probNumerator = 0; probDenominator = 0; // Pay: Olası her bir output value için sadece olası input değerlerinin toplamı for (int i = 0; i < outputValues.Length; i++) { for (int j = 0; j < inputValues.Length; j++) { probNumerator += (int)ddt[inputValues[j], outputValues[i]]; } } // Payda: Olası her bir outputValue için tüm input değerlerinin toplamı for (int i = 0; i < outputValues.Length; i++) { for (int j = 0; j < 16; j++) { probDenominator += (int)ddt[j, outputValues[i]]; } } return; }