Skip to content

Commit

Permalink
test(shiftLeft): add tests for shift left
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik committed Mar 24, 2024
1 parent 85cfc67 commit f3a6385
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions applet/src/test/java/tests/BigNatInternal/ShiftLeftTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package tests.BigNatInternal;

import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import opencrypto.jcmathlib.BigNat;
import opencrypto.jcmathlib.ResourceManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ShiftLeftTest {
@Test
public void shiftLeft_0() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x05, 0x08};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.ctShiftLeft((short) 0);

byte[] expectedResult = {0x05, 0x08};
byte[] actualResult = new byte[2];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_1() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x05, 0x08};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.ctShiftLeft((short) 1);

byte[] expectedResult = {0x0A, 0x10};
byte[] actualResult = new byte[2];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_5() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x05, 0x08};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.shiftLeft((short) 5);

byte[] expectedResult = {(byte) 0xA1, (byte) 0x00};
byte[] actualResult = new byte[2];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_7() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x05, 0x08};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.shiftLeft((short) 7);

byte[] expectedResult = {(byte) 0x02, (byte) 0x84, (byte) 0x00};
byte[] actualResult = new byte[3];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_carry1() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x00, 0x01};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.ctShiftLeft((short) 1, (short) 1);

byte[] expectedResult = {0x00, 0x03};
byte[] actualResult = new byte[2];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_carry128() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x00, 0x01};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
bn1.ctShiftLeft((short) 1, (short) 4);

byte[] expectedResult = {0x00, 0x06};
byte[] actualResult = new byte[2];
bn1.copyToByteArray(actualResult, (short) 0);
Assertions.assertArrayEquals(expectedResult, actualResult);
}

@Test
public void shiftLeft_invalidShift8() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x00, 0x02};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
Assertions.assertThrows(ISOException.class, () -> bn1.ctShiftRight((short) 8));
}

@Test
public void shiftLeft_invalidShiftNegative() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn1 = new BigNat((short) 10, memoryType, rm);

byte[] data1 = {0x00, 0x02};
bn1.fromByteArray(data1, (short) 0, (short) data1.length);
Assertions.assertThrows(ISOException.class, () -> bn1.ctShiftRight((short) -1));
}
}

0 comments on commit f3a6385

Please sign in to comment.