Skip to content

Commit

Permalink
Added rudimentary MACS support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mburrough committed Jul 13, 2017
1 parent 5968a49 commit dddaaee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
42 changes: 34 additions & 8 deletions keyProb/KeyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class KeyTest
private int Buffer;
private int PinCount;
private int Depths;
private int MACS;
bool[,] testMap;

/**
Expand All @@ -21,12 +22,13 @@ class KeyTest
* depths - number of possible cut depths per pin
* buffer - number of pins disallowed around master. 0 means a pin wafer of size 1 is allowed. Must be >= 0.
**/
public KeyTest(int pinCount, int depths, int buffer, Random random)
public KeyTest(int pinCount, int depths, int buffer, int macs, Random random)
{
rnd = random;
PinCount = pinCount;
Depths = depths;
Buffer = buffer;
MACS = macs;
TestedKeys = new List<int[]>();
GenerateMaster();
BuildTestMap();
Expand All @@ -41,12 +43,13 @@ public KeyTest(int pinCount, int depths, int buffer, Random random)
* buffer - number of pins disallowed around master. 0 means a pin wafer of size 1 is allowed. Must be >= 0.
* masterKey - if you want to test against a specific master key, instead of a randomly generated one, provide one here.
**/
public KeyTest(int pinCount, int depths, int buffer, int[] masterKey, Random random)
public KeyTest(int pinCount, int depths, int buffer, int macs, int[] masterKey, Random random)
{
rnd = random;
PinCount = pinCount;
Depths = depths;
Buffer = buffer;
MACS = macs;
TestedKeys = new List<int[]>();
master = masterKey;
BuildTestMap();
Expand All @@ -59,7 +62,9 @@ private void GenerateMaster()
master = new int[PinCount];
for (int i = 0; i < PinCount; i++)
{
master[i] = rnd.Next(Depths);
do {
master[i] = rnd.Next(Depths);
} while (!passesMACS(i, master));
Console.Write(master[i] + " ");
}
Console.WriteLine();
Expand Down Expand Up @@ -162,7 +167,7 @@ private int[] GenerateValidKey()
do
{
key[i] = rnd.Next(Depths);
} while (!isValid(i, key[i]));
} while (!isValid(i, key));
}
} while (!unique(key));

Expand Down Expand Up @@ -203,22 +208,43 @@ private bool unique(int[] key)
return true;
}

private bool passesMACS(int pos, int[] key)
{
if (pos > 0)
{
if (key[pos - 1] > (key[pos] + MACS))
{
//Console.WriteLine("MAC Fail: {0} > {1} + {2}", key[pos - 1], key[pos], MACS);
return false;
}
else if (key[pos - 1] < (key[pos] - MACS))
{
//Console.WriteLine("MAC Fail: {0} < {1} - {2}", key[pos - 1], key[pos], MACS);
return false;
}
}

return true;
}

//Check if a given pin value is too close to the master value
private bool isValid(int pos, int val)
private bool isValid(int pos, int[] key)
{
//if (master[pos] == val)
//return false;

//BUFFER
for (int i = 0; i <= Buffer; i++)
{
if (master[pos] == (val + i))
if (master[pos] == (key[pos] + i))
return false;

if (master[pos] == (val - i))
if (master[pos] == (key[pos] - i))
return false;
}

return true;
//MACS
return passesMACS(pos, key);
}
}
}
11 changes: 6 additions & 5 deletions keyProb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ class Program
{
static void Main(string[] args)
{
int tests = 10000; //How many tests to run
int pinPositions = 7; //How many key stacks in this lock
int tests = 1000; //How many tests to run
int pinPositions = 7; //How many pin stacks in this lock
int depths = 6; //How many possible cut depths per pin.
int buffer = 0; //How many cuts on either side of master are illegal. (E.g. disallow pin 'wafers' of this size).
int macs = depths-2; //Adjacent cuts cannot be more than this far off from each other's depths. Set equal to depths to allow any adjacent cut.

//Create a specific master key set to all 0's.
int[] m = new int[pinPositions];
for (int i = 0; i < m.Length; i++)
m[i] = 0;


Random r = new Random(); //Pass in random so we don't keep regenerate it each test run, which can lead to issues with the random values.
Random r = new Random(); //Pass in random so we don't keep regenerating it each test run, which can lead to issues with the random values.
double min = Double.MaxValue;
double max = 0;
double total=0;
double result = 0;
Stopwatch stopwatch = Stopwatch.StartNew();
for (int tries = 0; tries < tests; tries++)
{
KeyTest k = new KeyTest(pinPositions, depths, buffer, r);
//KeyTest k = new KeyTest(pinPositions, depths, buffer, m, r); // Optionally, provide a fixed master to test
KeyTest k = new KeyTest(pinPositions, depths, buffer, macs, r);
//KeyTest k = new KeyTest(pinPositions, depths, buffer, macs, m, r); // Optionally, provide a fixed master to test
result = k.Simulation();
total += result;
if(min > result)
Expand Down

0 comments on commit dddaaee

Please sign in to comment.