Skip to content

Commit

Permalink
test(getFirstBitPosition): Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
xhanulik committed Apr 27, 2024
1 parent 4e6aba2 commit 448f03e
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tests.BigNatInternal;

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 getFirstBitPositionTest {
@Test
public void noZero() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn = new BigNat((short) 10, memoryType, rm);

byte[] data = {(byte) 0xff};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 0);
Assertions.assertEquals((short) 8, position); /* no zero bit */
}

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

byte[] data = {(byte) 0x00};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 1);
Assertions.assertEquals((short) 8, position); /* no zero bit */
}

@Test
public void oneByteZero() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn = new BigNat((short) 3, memoryType, rm);

byte[] data = {(byte) 0x1f};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 0);
Assertions.assertEquals((short) 5, position);
}

@Test
public void oneByteOne() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn = new BigNat((short) 3, memoryType, rm);

byte[] data = {(byte) 0x20};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 1);
Assertions.assertEquals((short) 5, position);
}

@Test
public void twoBytesZero() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn = new BigNat((short) 3, memoryType, rm);

byte[] data = {(byte) 0x3F, (byte) 0xff};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 0);
Assertions.assertEquals((short) 14, position);
}

@Test
public void twoBytesOne() {
ResourceManager rm = new ResourceManager((short) 256);
byte memoryType = JCSystem.MEMORY_TYPE_TRANSIENT_RESET;
BigNat bn = new BigNat((short) 3, memoryType, rm);

byte[] data = {(byte) 0xC0, (byte) 0x00};
bn.fromByteArray(data, (short) 0, (short) data.length);

short position = bn.getFirstBitPosition((byte) 1);
Assertions.assertEquals((short) 14, position);
}
}

0 comments on commit 448f03e

Please sign in to comment.