-
Notifications
You must be signed in to change notification settings - Fork 24
Tutorial: Write a test harness
Suppose we want to test a simple multiplication circuits, name it MultiTM.
Common Program View Sample Code Download Sample Code First create a class MultiTMCommon in package Program. This class should extend ProgCommon and include these two methods:
static void initCircuits() This method should initialize and build any circuits or module that would be used the in the execution process, as in the example code.
public static State execCircuit(BigInteger[] Xlbs, BigInteger[] Ylbs) This method should take inputs from the client and server side, and use the wire labels to compute the results using the circuits or modules.
If circuits are used, use startExecuting method of the Circuit class to execute the circuit. The inputs should be passed in as one State, and output should be a single State. executemethod and the inputs and outputs of this method will depend on how the module is defined. ** Some static methods of State class can be used to construct states from different kinds of inputs, here are some useful methods: flattenStateArray(State[] as) A method that flattens an array of states and return a single state.
extractState(State s, int start, int end) A method that returns a segment from the input state s, which begins from start to end.
fromWires(Wires[] ws) A method that creates a state based on the input array of wires.
fromLabels(BigInteger[] lbs) A method that returns a state based on the input array of labels.
fromLabels(BigInteger[] lbs,int begin, int end) A method that returns a state that is based on a segment of the BigInteger array lbs, which starts from begin to end.
fromConcatenation(State s1, State s2) A method that returns a state which appends s2 to s1.
Server Program View Sample Code Download Sample Code
Write a server program in package Program. This class should inherit ProgServer class. You can use the sample code and change some parts of the code to fit your needs. The parts need to be changed include:
- Data fields:
The input and output's data structure can be changed. For example, you can use an array of BigInteger instead of BigInteger for inputs, if the input is a set of elements. Also, you can add some data fields if needed, such as an integer field for length of input when the length varies depending on user inputs.
-
Constructor: The constructor can be changed in different ways, such as adding input parameters to the constructor.
-
private void generateLabelPairs()
In this method, several numbers should be changed. The number 2 should be changed to M or N depending on its place. M is the length of input from the server side, and N is the length of input from the client side. In the 2-bit multiplier, M and N are both 2.
Xlps = new BigInteger[M][2]; Ylps = new BigInteger[N][2]; for (int i=0;i<M;i++){ Xlps[i] = Wire.newLabelPair(); } for (int i=0;i<N;i++){ Ylps[i] = Wire.newLabelPair(); }
- protected void execTransfer()
In this method, the number of iterations in the for loop should be changed from 2 to M, which is the length of input from server side.
for (int i = 0; i <M; i++) {
int idx = X.testBit(i) ? 1 : 0; Utils.writeBigInteger(Xlps[i][idx], bytelength, MultiTMCommon.oos); }
- protected void execCircuit()
The size of Xlbs and Ylbs should also be the number of input wires from each side.
BigInteger[] Xlbs = new BigInteger[M];
BigInteger[] Ylbs = new BigInteger[N];
-
protected void interpretResult() This method might also need to be changed for different outputs.
-
protected void verify_result() This method is used to test whether the results are correct. It does not have to be defined but is probably necessary for checking results and debugging. It can print out the results.
Client Program View Sample Code Download Sample Code
Next, create a MultiTMClient class in package Program. This class should inherit ProgClient class. You can use the sample code, and only need to modify class names and several numbers in these methods:
- protected void init()
In this method, modifies otNumOfPairs to the length of input from the client side.
- protected void execTransfer() The size of Xlbs should be the length of input from the server side. Also, the number of iterations in the for loops should be changed accordingly. The first for loop should iterate M times, where M is the length of input from the server side. The second for loop should iterate N times, where N is the length of input from the client side.
Lastly, create two classes in package Test for both the server and client. Still, you can adapt the sample code for your needs.
Test Server View Sample Code Download Sample Code This class should contain any methods that the server side needs, such as pre-processing the inputs, and generate random test cases, etc.
Here are the list of methods that you might want to implement:
private static void process_cmdline_args(String[] args) This method can be modified to change the type of command line arguments that this class accepts.
static void generateData() This method can be modified to generate test data for your circuit.
static void readDataFile() This method can be modified to specify how the program reads input file.
The main method can be modified according to specific needs.
Test Client View Sample Code Download Sample Code This class can also be modified to add new command parameters, similar to test server.