gt: 		BigInteger publicPointY = new BigInteger(1, yArray);
context: package de.persosim.simulator.crypto;
  
  import static org.junit.Assert.assertArrayEquals;
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertFalse;
  import static org.junit.Assert.assertTrue;
  
  import java.io.StringReader;
  import java.io.StringWriter;
  import java.math.BigInteger;
  import java.security.InvalidAlgorithmParameterException;
  import java.security.KeyFactory;
  import java.security.KeyPair;
  import java.security.KeyPairGenerator;
  import java.security.NoSuchAlgorithmException;
  import java.security.NoSuchProviderException;
  import java.security.SecureRandom;
  import java.security.interfaces.ECPrivateKey;
  import java.security.interfaces.ECPublicKey;
  import java.security.spec.ECPoint;
  import java.security.spec.ECPrivateKeySpec;
  import java.security.spec.ECPublicKeySpec;
  import java.security.spec.InvalidKeySpecException;
  import java.security.spec.KeySpec;
  import java.util.Arrays;
  
  import org.junit.Before;
  import org.junit.Test;
  
  import de.persosim.simulator.perso.PersonalizationFactory;
  import de.persosim.simulator.protocols.pace.GenericMappingEcdh;
  import de.persosim.simulator.test.PersoSimTestCase;
  import de.persosim.simulator.utils.HexString;
  import de.persosim.simulator.utils.Utils;
  
  public class DomainParameterSetEcdhTest extends PersoSimTestCase {
  	
  	private DomainParameterSetEcdh domParamsEcdh;
  	private KeyPair keyPairNonEc;
  	
  	@Before
  	public void setUp() throws NoSuchAlgorithmException {
  		domParamsEcdh = (DomainParameterSetEcdh) StandardizedDomainParameters.getDomainParameterSetById(13);
  		
  		KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
  	    gen.initialize(1024, new SecureRandom());
  	    keyPairNonEc = gen.generateKeyPair();
  	}
  	
  	/**
  	 * Positive test case: get byte array encoding of public key.
  	 */
  	@Test
  	public void testEncodePublicKey() throws Exception {	
  		byte[] xArray = HexString.toByteArray("4DD4D9CCB21EA76850E96699DF3EED2FA65CE0CBB3BF7604E1C458CF71B47F59");
  		byte[] yArray = HexString.toByteArray("5AF8C1A214A81761DAA6D134DE0E5EA52D54C3BE3F05944F4460F81158D89DEA");
  		
  		byte[] publicKeyEncodingPlainExpected = Utils.concatByteArrays(new byte[]{(byte) 0x04}, xArray, yArray);
  		
  		// point coordinates originate from successful PACE test run, i.e. have been verified to be on the curve
  		BigInteger publicPointX = new BigInteger(1, xArray);
  		BigInteger publicPointY = new BigInteger(1, yArray);
  		
  		ECPoint point = new ECPoint(publicPointX, publicPointY);
  		
  		KeySpec publicKeySpec = new ECPublicKeySpec(point, domParamsEcdh.getKeySpec());
  		
  		KeyFactory keyFactory = KeyFactory.getInstance("ECDH");
  		ECPublicKey ecdhPublicKeyExpected = (ECPublicKey) keyFactory.generatePublic(publicKeySpec);
  		
  		byte[] publicKeyEncodingPlain = domParamsEcdh.encodePublicKey(ecdhPublicKeyExpected);
  		
  		assertArrayEquals("reconstructed encoding", publicKeyEncodingPlainExpected, publicKeyEncodingPlain);
  	}
  	
  	/**
  	 * Negative test case: get byte array encoding of public key for non-EC key.
  	 */
  	@Test(expected = IllegalArgumentException.class)
  	public void testEncodePublicKey_NonEcKey() throws Exception {
  	    domParamsEcdh.encodePublicKey(keyPairNonEc.getPublic());
  	}
  
  	/**
  	 * Positive test case: get byte array compressed (TR-03110) encoding of public key.
  	 */
  	@Test
  	public void testComp() throws Exception {	
  		byte[] xArray = HexString.toByteArray("4DD4D9CCB21EA76850E96699DF3EED2FA65CE0CBB3BF7604E1C458CF71B47F59");
  		byte[] yArray = HexString.toByteArray("5AF8C1A214A81761DAA6D134DE0E5EA52D54C3BE3F05944F4460F81158D89DEA");
  				
  		// point coordinates originate from successful PACE test run, i.e. have been verified to be on the curve
  		BigInteger publicPointX = new BigInteger(1, xArray);
  		BigInteger publicPointY = new BigInteger(1, yArray);
  		
  		ECPoint point = new ECPoint(publicPointX, publicPointY);
  		
  		KeySpec publicKeySpec = new ECPublicKeySpec(point, domParamsEcdh.getKeySpec());
  		
  		KeyFactory keyFactory = KeyFactory.getInstance("ECDH");
  		ECPublicKey ecdhPublicKeyExpected = (ECPublicKey) keyFactory.generatePublic(publicKeySpec);
  		
  		byte[] publicKeyEncodingPlain = domParamsEcdh.comp(ecdhPublicKeyExpected);
  		
  		assertArrayEquals("reconstructed encoding", xArray, publicKeyEncodingPlain);
  	}
  	
  	/**
  	 * Positive test case: get byte array compressed (TR-03110) encoding of public ECDH key, key is padded with 0-bytes.
  	 */
  	@Test
  	public void testCompShortEcdhKey() throws Exception {	
  		byte[] xArray = HexString.toByteArray("0015A12C49DC3F2985AE44E5EF75AA0A1862527CD9D5B03D17CD1E2FC0290DB7");
  		byte[] yArray = HexString.toByteArray("0AF39509F439220E7EEA61D15668BB5D63DD256BD7F4E9E1F9753866C4A6BD59");
  				
  		// point coordinates originate from successful PACE test run, i.e. have been verified to be on the curve
  		BigInteger publicPointX = new BigInteger(1, xArray);
  		BigInteger publicPointY = new BigInteger(1, yArray);
  		
  		ECPoint point = new ECPoint(publicPointX, publicPointY);
  		
  		KeySpec publicKeySpec = new ECPublicKeySpec(point, domParamsEcdh.getKeySpec());
  		
  		KeyFactory keyFactory = KeyFactory.getInstance("ECDH");
  		ECPublicKey ecdhPublicKeyExpected = (ECPublicKey) keyFactory.generatePublic(publicKeySpec);
  		
  		byte[] publicKeyEncodingPlain = domParamsEcdh.comp(ecdhPublicKeyExpected);
  		
  		assertArrayEquals("reconstructed encoding", xArray, publicKeyEncodingPlain);
  	}
  	
  	/**
  	 * Negative test case: get byte array compressed (TR-03110) encoding of public non-EC key.
  	 */
  	@Test(expected = IllegalArgumentException.class)
  	public void testComp_nonEcKey() throws Exception {	
  		domParamsEcdh.comp(keyPairNonEc.getPublic());
  	}
  	
  	/**
  	 * Positive test case: reconstruct public key from byte array encoding (uncompressed encoding according to ANSI X9.62).
  	 */
  	@Test
  	public void testReconstructPublicKey() throws Exception {		
  		byte[] xArray = HexString.toByteArray("4DD4D9CCB21EA76850E96699DF3EED2FA65CE0CBB3BF7604E1C458CF71B47F59");
  		byte[] yArray = HexString.toByteArray("5AF8C1A214A81761DAA6D134DE0E5EA52D54C3BE3F05944F4460F81158D89DEA");
  		
  		byte[] publicKeyEncodingPlain = Utils.concatByteArrays(new byte[]{(byte) 0x04}, xArray, yArray);
  		
  		// point coordinates originate from successful PACE test run, i.e. have been verified to be on the curve
  		BigInteger publicPointX = new BigInteger(1, xArray);
  		BigInteger publicPointY = new BigInteger(1, yArray);
  		
  		ECPoint point = new ECPoint(publicPointX, publicPointY);
  		
  		KeySpec publicKeySpecExpected = new ECPublicKeySpec(point, domParamsEcdh.getKeySpec());
  		
  		KeyFactory keyFactory = KeyFactory.getInstance("ECDH");
  		ECPublicKey ecdhPublicKeyExpected = (ECPublicKey) keyFactory.generatePublic(publicKeySpecExpected);
  		
  		ECPublicKey ecdhPublicKeyReconstructed = domParamsEcdh.reconstructPublicKey(publicKeyEncodingPlain);
  		
  		assertEquals("reconstructed w", ecdhPublicKeyExpected.getW(), ecdhPublicKeyReconstructed.getW());
  		assertEquals("reconstructed curve", ecdhPublicKeyExpected.getParams().getCurve(), ecdhPublicKeyReconstructed.getParams().getCurve());
  		assertEquals("reconstructed G", ecdhPublicKeyExpected.getParams().getGenerator(), ecdhPublicKeyReconstructed.getParams().getGenerator());
  		assertEquals("reconstructed order", ecdhPublicKeyExpected.getParams().getOrder(), ecdhPublicKeyReconstructed.getParams().getOrder());
  		assertEquals("reconstructed cofactor", ecdhPublicKeyExpected.getParams().getCofactor(), ecdhPublicKeyReconstructed.getParams().getCofactor());
  		assertArrayEquals("reconstructed encoding", ecdhPublicKeyExpected.getEncoded(), ecdhPublicKeyReconstructed.getEncoded());
  	}
  	
  	/**
  	 * Negative test case: reconstruct public key from byte array encoding according to ANSI X9.62 with key material being of unexpected size.
  	 */
  	@Test(expected = IllegalArgumentException.class)
  	public void testReconstructPublicKey_illegalKeySize() throws Exception {		
  		domParamsEcdh.reconstructPublicKey(new byte[]{(byte) 0x04});
  	}
  	
  	/**
  	 * Negative test case: reconstruct public key from byte array encoding according to ANSI X9.62 with resulting point not being on the curve.
  	 */
  	@Test(expected = IllegalArgumentException.class)
  	public void testReconstructPublicKey_pointNotOnCurve() throws Exception {		
  		byte[] xArray = HexString.toByteArray("4DD4D9CCB21EA76850E96699DF3EED2FA65CE0CBB3BF7604E1C458CF71B47F5A"); // manipulated
  		byte[] yArray = HexString.toByteArray("5AF8C1A214A81761DAA6D134DE0E5EA52D54C3BE3F05944F4460F81158D89DEA");
  		
  		byte[] publicKeyEncodingPlain = Utils.concatByteArrays(new byte[]{(byte) 0x04}, xArray, yArray);
  		
  		domParamsEcdh.reconstructPublicKey(publicKeyEncodingPlain);
  	}
  	
  	/**
  	 * Positive test case: reconstruct private key from byte array encoding (plain value S).
  	 */
  	@Test
  	public void testReconstructPrivateKey() throws Exception {		
  		byte[] ecdhPrivateKeyDataPicc = HexString.toByteArray("79 84 67 4C F3 B3 A5 24 BF 92 9C E8 A6 7F CF 22 17 3D A0 BA D5 95 EE D6 DE B7 2D 22 C5 42 FA 9D");
  		BigInteger sExpected = new BigInteger(1, ecdhPrivateKeyDataPicc);
  		
  		KeySpec privateKeySpecExpected = new ECPrivateKeySpec(sExpected, domParamsEcdh.getKeySpec());
  		
  		KeyFactory keyFactory = KeyFactory.getInstance(domParamsEcdh.getKeyAgreementAlgorithm());
  		ECPrivateKey ecdhPrivateKeyExpected = (ECPrivateKey) keyFactory.generatePrivate(privateKeySpecExpected);
  		
  		ECPrivateKey ecdhPrivateKeyReceived = domParamsEcdh.reconstructPrivateKey(ecdhPrivateKeyDataPicc);
  		
  		assertEquals("reconstructed S", ecdhPrivateKeyExpected.getS(), ecdhPrivateKeyReceived.getS());
  		assertEquals("reconstructed curve", ecdhPrivateKeyExpected.getParams().getCurve(), ecdhPrivateKeyReceived.getParams().getCurve());
  		assertEquals("reconstructed G", ecdhPrivateKeyExpected.getParams().getGenerator(), ecdhPrivateKeyReceived.getParams().getGenerator());
  		assertEquals("reconstructed order", ecdhPrivateKeyExpected.getParams().getOrder(), ecdhPrivateKeyReceived.getParams().getOrder());
  		assertEquals("reconstructed cofactor", ecdhPrivateKeyExpected.getParams().getCofactor(), ecdhPrivateKeyReceived.getParams().getCofactor());
  		assertArrayEquals("reconstructed encoding", ecdhPrivateKeyExpected.getEncoded(), ecdhPrivateKeyReceived.getEncoded());
  	}
  	
  	/**
  	 * Negative test case: reconstruct private key from byte array encoding with unexpected length.
  	 */
  	@Test(expected = IllegalArgumentException.class)
  	public void testReconstructPrivateKey_illegalKeySize() throws Exception {		
  		domParamsEcdh.reconstructPrivateKey(new byte[] {(byte) 0xFF});
  	}
  	
  	/**
  	 * Positive test case: reconstruct point from X9.62 uncompressed byte array encoding.
  	 */
  	@Test
  	public void testReconstructPoint_uncompressedData() throws Exception {		
  		byte[] xArray = HexString.toByteArray("4DD4D9CCB21EA76850E96699DF3EED2FA65CE0CBB3BF7604E1C458CF71B47F59");
  		byte[] yArray = HexString.toByteArray("5AF8C1A214A81761DAA6D134DE0E5EA52D54C3BE3F05944F4460F81158D89DEA");
  		
  		byte[] keyEncodingPlain = Utils.concatByteArrays(new byte[]{(byte) 0x04}, xArray, yArray);
  		
  		// point coordinates originate from successful PACE test run, i.e. have been verified to be on the curve
  		BigInteger publicPointX = new BigInteger(1, xArray);
  