public static ulong?ToUlong(BitVecExpr value, uint nBits, Solver solver, Context ctx) { if (value.IsBVNumeral) { return(((BitVecNum)value).UInt64); } Tv[] results = new Tv[nBits]; using (BitVecNum ONE = ctx.MkBV(1, 1)) { for (uint bit = 0; bit < nBits; ++bit) { using (BoolExpr b = ToolsZ3.GetBit(value, bit, ONE, ctx)) { switch (ToolsZ3.GetTv(b, solver, ctx)) { case Tv.ONE: results[bit] = Tv.ONE; break; case Tv.ZERO: results[bit] = Tv.ZERO; break; default: return(null); } } } } return(ToolsZ3.ToUlong(results)); }
public static BoolExpr Create_CF_Sub(BitVecExpr a, BitVecExpr b, uint nBits, Context ctx) { BitVecExpr ax = ctx.MkZeroExt(1, a); BitVecExpr bx = ctx.MkZeroExt(1, b); BitVecNum ONE = ctx.MkBV(1, 1); return(ToolsZ3.GetBit(ctx.MkBVSub(ax, bx), nBits, ONE, ctx)); //return ctx.MkNot(ctx.MkBVSubNoUnderflow(a, b, false)); }
public static BoolExpr Create_CF_Add(BitVecExpr a, BitVecExpr b, uint nBits, Context ctx) { BitVecExpr ax = ctx.MkZeroExt(1, a); BitVecExpr bx = ctx.MkZeroExt(1, b); BitVecExpr sum = ctx.MkBVAdd(ax, bx); return(ToolsZ3.GetBit(sum, nBits, ctx.MkBV(1, 1), ctx)); //return ctx.MkNot(ctx.MkBVAddNoOverflow(a, b, false)); }
public static BoolExpr Create_SF(BitVecExpr value, uint nBits, Context ctx) { Debug.Assert(value != null, "BitVecExpr value cannot be null"); Debug.Assert(ctx != null, "Context cannot be null"); Debug.Assert(nBits <= value.SortSize); Debug.Assert(nBits >= 1); uint bitPos = nBits - 1; return(ToolsZ3.GetBit(value, bitPos, ctx.MkBV(1, 1), ctx)); }
public static (BitVecExpr result, BoolExpr cf) ShiftOperations( Mnemonic op, BitVecExpr value, BitVecExpr nShifts, BoolExpr carryIn, string prevKey, Context ctx) { Contract.Requires(value != null); Contract.Requires(nShifts != null); Contract.Requires(ctx != null); Contract.Requires(nShifts.SortSize == 8); //Console.WriteLine("ShiftOperations:nShifts=" + nShifts); uint nBits = value.SortSize; BitVecExpr carryBV = ctx.MkITE(carryIn, ctx.MkBV(1, 1), ctx.MkBV(0, 1)) as BitVecExpr; BitVecExpr nShifts65 = ctx.MkZeroExt(nBits + 1 - 8, nShifts); BitVecExpr value_out; BoolExpr bitValue; switch (op) { case Mnemonic.RCR: { BitVecExpr valueWithCarry = ctx.MkConcat(carryBV, value); BitVecExpr rotatedValue = ctx.MkBVRotateRight(valueWithCarry, nShifts65); value_out = ctx.MkExtract(nBits - 1, 0, rotatedValue); bitValue = ToolsZ3.GetBit(rotatedValue, nBits, ctx.MkBV(1, 1), ctx); } break; case Mnemonic.RCL: { BitVecExpr valueWithCary = ctx.MkConcat(value, carryBV); BitVecExpr rotatedValue = ctx.MkBVRotateLeft(valueWithCary, nShifts65); value_out = ctx.MkExtract(nBits, 1, rotatedValue); bitValue = ToolsZ3.GetBit(rotatedValue, 0, ctx.MkBV(1, 1), ctx); } break; default: throw new Exception(); } BoolExpr cf_current = Tools.Create_Key(Flags.CF, prevKey, ctx); BoolExpr cf = ctx.MkITE(ctx.MkEq(nShifts, ctx.MkBV(0, 8)), cf_current, bitValue) as BoolExpr; //Console.WriteLine("ShiftOperations:cf=" + cf); return(result : value_out, cf : cf); }
public static Tv[] GetTvArray(BitVecExpr value, int nBits, Solver solver, Context ctx) { Tv[] results = new Tv[nBits]; if (value == null) { Console.WriteLine("WARNING: ToolsZ3:GetTv5Array: value is null, assuming UNKNOWN"); return(results); } using (BitVecNum bv1_1bit = ctx.MkBV(1, 1)) { for (uint bit = 0; bit < nBits; ++bit) { using (BoolExpr b = ToolsZ3.GetBit(value, bit, bv1_1bit, ctx)) { results[bit] = ToolsZ3.GetTv(b, solver, ctx); } } } return(results); }
/// <summary>Returns true if the provided valueExpr and undef yield the same tv5 array as the provided valueTv </summary> public static bool Equals(BitVecExpr valueExpr, BitVecExpr undef, Tv[] valueTv, int nBits, Solver solver, Solver solver_U, Context ctx) { using (BitVecNum bv1_1bit = ctx.MkBV(1, 1)) { for (uint bit = 0; bit < nBits; ++bit) { using (BoolExpr b = ToolsZ3.GetBit(valueExpr, bit, bv1_1bit, ctx)) using (BoolExpr b_undef = ToolsZ3.GetBit(undef, bit, bv1_1bit, ctx)) { // this can be done faster Tv tv = ToolsZ3.GetTv(b, b_undef, solver, solver_U, ctx); if (tv != valueTv[bit]) { return(false); } } } } return(true); }
public static (BitVecExpr result, BoolExpr cf) ShiftOperations( Mnemonic op, BitVecExpr value, BitVecExpr nShifts, Context ctx, Random rand) { Debug.Assert(nShifts.SortSize == 8); BitVecExpr value_out; BitVecNum one = ctx.MkBV(1, 8); BitVecExpr nBitsBV = ctx.MkBV(value.SortSize, 8); BitVecExpr bitPos; BitVecExpr nShifts64 = ctx.MkZeroExt(value.SortSize - 8, nShifts); switch (op) { case Mnemonic.SHR: { bitPos = ctx.MkBVSub(nShifts, one); value_out = ctx.MkBVLSHR(value, nShifts64); } break; case Mnemonic.SAR: { bitPos = ctx.MkBVSub(nShifts, one); value_out = ctx.MkBVASHR(value, nShifts64); } break; case Mnemonic.ROR: { bitPos = ctx.MkBVSub(nShifts, one); value_out = ctx.MkBVRotateRight(value, nShifts64); } break; case Mnemonic.SHL: // SHL and SAL are equal in functionality case Mnemonic.SAL: { bitPos = ctx.MkBVSub(nBitsBV, nShifts); //Console.WriteLine("BitOperations:SHL: bitPos=" + bitPos.Simplify()); value_out = ctx.MkBVSHL(value, nShifts64); } break; case Mnemonic.ROL: { bitPos = ctx.MkBVSub(nBitsBV, nShifts); value_out = ctx.MkBVRotateLeft(value, nShifts64); } break; default: throw new Exception(); } bitPos = ctx.MkZeroExt(value.SortSize - 8, bitPos); BoolExpr bitValue = ToolsZ3.GetBit(value, bitPos, ctx); BoolExpr CF_undef = Tools.Create_Flag_Key_Fresh(Flags.CF, rand, ctx); BoolExpr cf = ctx.MkITE(ctx.MkEq(nShifts, ctx.MkBV(0, 8)), CF_undef, bitValue) as BoolExpr; return(result : value_out, cf : cf); }