[Webfunds-commits] java/webfunds/token/algorithm AbstractPublicTokenParameters.java ChaumConstants.java ChaumPrivateTokenParameters.java ChaumPublicTokenParameters.java
Edwin Woudt
edwin@cypherpunks.ai
Wed, 3 Jan 2001 22:08:15 -0400 (AST)
edwin 01/01/03 22:08:15
Modified: webfunds/token/algorithm AbstractPublicTokenParameters.java
ChaumConstants.java
ChaumPrivateTokenParameters.java
ChaumPublicTokenParameters.java
Log:
Finish the last part of the Chaum blinding classes: the parameter
generation and encoding/decoding.
Everything is still pretty much untested: that's the next step.
(Also fix a small thing in my previous commit).
Revision Changes Path
1.4 +3 -3 java/webfunds/token/algorithm/AbstractPublicTokenParameters.java
Index: AbstractPublicTokenParameters.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/token/algorithm/AbstractPublicTokenParameters.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbstractPublicTokenParameters.java 2001/01/04 01:16:10 1.3
+++ AbstractPublicTokenParameters.java 2001/01/04 02:08:14 1.4
@@ -1,4 +1,4 @@
-/* $Id: AbstractPublicTokenParameters.java,v 1.3 2001/01/04 01:16:10 edwin Exp $
+/* $Id: AbstractPublicTokenParameters.java,v 1.4 2001/01/04 02:08:14 edwin Exp $
*
* Copyright (c) Systemics Ltd 2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
@@ -21,7 +21,7 @@
/**
* Class description
*
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
* @author Edwin Woudt <edwin@webfunds.org>
*/
public abstract class AbstractPublicTokenParameters
@@ -86,7 +86,7 @@
int publen = EncodeDecodeUtil.decodeInt(data, start, len);
start += 4; len -= 4;
- decodePublicImpl(data, start, publen);
+ decodeImpl(data, start, publen);
start += publen; len -= publen;
}
1.3 +3 -2 java/webfunds/token/algorithm/ChaumConstants.java
Index: ChaumConstants.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/token/algorithm/ChaumConstants.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ChaumConstants.java 2001/01/03 02:19:19 1.2
+++ ChaumConstants.java 2001/01/04 02:08:15 1.3
@@ -1,4 +1,4 @@
-/* $Id: ChaumConstants.java,v 1.2 2001/01/03 02:19:19 edwin Exp $
+/* $Id: ChaumConstants.java,v 1.3 2001/01/04 02:08:15 edwin Exp $
*
* Copyright (c) Systemics Ltd 2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
@@ -8,7 +8,7 @@
/**
* Constants for the Chaum blinding algorithm
*
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
* @author Edwin Woudt <edwin@webfunds.org>
*/
public final class ChaumConstants {
@@ -19,6 +19,7 @@
public static final int RANDOM_SIZE = 160/8;
public static final int HASH_SIZE = 160/8;
public static final String HASH_ALG = "SHA-1";
+ public static final int PRIME_CERTAINTY = 80;
public static final byte MAJOR_TOKEN_VERSION = 1;
public static final byte MINOR_TOKEN_VERSION = 0;
1.3 +124 -12 java/webfunds/token/algorithm/ChaumPrivateTokenParameters.java
Index: ChaumPrivateTokenParameters.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/token/algorithm/ChaumPrivateTokenParameters.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ChaumPrivateTokenParameters.java 2001/01/04 01:16:10 1.2
+++ ChaumPrivateTokenParameters.java 2001/01/04 02:08:15 1.3
@@ -1,4 +1,4 @@
-/* $Id: ChaumPrivateTokenParameters.java,v 1.2 2001/01/04 01:16:10 edwin Exp $
+/* $Id: ChaumPrivateTokenParameters.java,v 1.3 2001/01/04 02:08:15 edwin Exp $
*
* Copyright (c) Systemics Ltd 2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
@@ -15,30 +15,99 @@
/**
* Class description
*
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
* @author Edwin Woudt <edwin@webfunds.org>
+ * @author Jeroen van Gelderen <gelderen@cryptix.org>
*/
public final class ChaumPrivateTokenParameters
extends AbstractPrivateTokenParameters {
+// Instance variables
+//.............................................................................
+
+ BigInteger n;
+ BigInteger[] e, d;
+ long[] denominations;
+
+
// Abstract init methods
//.............................................................................
public void generateImpl(SecureRandom sr, long[] denominations) {
- throw new RuntimeException("NYI");
+
+ this.denominations = denominations;
+
+ // Part of the following code has been copied from the RSA key
+ // generation code from the Cryptix JCE.
+
+ int keysize = ChaumConstants.KEY_SIZE * 8;
+ int pLen = keysize / 2;
+ int qLen = keysize - pLen;
+ BigInteger p, q, pMinus1, qMinus1, phi;
+
+ do
+ {
+ p = new BigInteger(pLen, ChaumConstants.PRIME_CERTAINTY, sr);
+ q = new BigInteger(qLen, ChaumConstants.PRIME_CERTAINTY, sr);
+ n = p.multiply(q);
+ }
+ while( (p.compareTo(q) == 0) || (n.bitLength() != keysize) );
+
+ pMinus1 = p.subtract(BigInteger.valueOf(1));
+ qMinus1 = q.subtract(BigInteger.valueOf(1));
+ phi = pMinus1.multiply(qMinus1);
+
+ d = new BigInteger[denominations.length];
+ e = new BigInteger[denominations.length];
+
+ long currentE = 65537; // 4th fermat number, starting point
+
+ for (int i=0; i<denominations.length; i++) {
+ while (true) {
+ try {
+ e[i] = BigInteger.valueOf(currentE);
+ currentE += 2;
+ d[i] = e[i].modInverse(phi);
+ break; // in case no exception is thrown, we found one
+ } catch (ArithmeticException ae) {
+ // try again
+ }
+ }
+ }
+
}
- protected void decodePrivateImpl(byte[] data, int start, int len)
+ protected void decodePublicImpl(byte[] data, int start, int len)
throws DataFormatException, UnsupportedVersionException
{
- throw new RuntimeException("NYI");
+
+ n = EncodeDecodeUtil.decodeMPI(data, start, len);
+ int delta = EncodeDecodeUtil.getMPILength(data, start, len);
+ start += delta; start -= len;
+
+ this.denominations = getDenominations();
+ e = new BigInteger[denominations.length];
+
+ for (int i=0; i<denominations.length; i++) {
+ e[i] = EncodeDecodeUtil.decodeMPI(data, start, len);
+ delta = EncodeDecodeUtil.getMPILength(data, start, len);
+ start += delta; start -= len;
+ }
}
- protected void decodePublicImpl(byte[] data, int start, int len)
+ protected void decodePrivateImpl(byte[] data, int start, int len)
throws DataFormatException, UnsupportedVersionException
{
- throw new RuntimeException("NYI");
+
+ this.denominations = getDenominations();
+ d = new BigInteger[denominations.length];
+
+ for (int i=0; i<denominations.length; i++) {
+ d[i] = EncodeDecodeUtil.decodeMPI(data, start, len);
+ int delta = EncodeDecodeUtil.getMPILength(data, start, len);
+ start += delta; start -= len;
+ }
}
@@ -46,11 +115,42 @@
//.............................................................................
protected byte[] encodePublicDataImpl() {
- throw new RuntimeException("NYI");
+
+ int len = EncodeDecodeUtil.getMPILength(n);
+ for (int i=0; i<denominations.length; i++) {
+ len += EncodeDecodeUtil.getMPILength(e[i]);
+ }
+
+ byte[] output = new byte[len];
+ int start = 0;
+
+ int delta = EncodeDecodeUtil.encodeMPI(n, output, start);
+ start += delta; len -= delta;
+
+ for (int i=0; i<denominations.length; i++) {
+ delta = EncodeDecodeUtil.encodeMPI(e[i], output, start);
+ start += delta; len -= delta;
+ }
+
+ return output;
}
protected byte[] encodePrivateDataImpl() {
- throw new RuntimeException("NYI");
+
+ int len = 0;
+ for (int i=0; i<denominations.length; i++) {
+ len += EncodeDecodeUtil.getMPILength(e[i]);
+ }
+
+ byte[] output = new byte[len];
+ int start = 0;
+
+ for (int i=0; i<denominations.length; i++) {
+ int delta = EncodeDecodeUtil.encodeMPI(d[i], output, start);
+ start += delta; len -= delta;
+ }
+
+ return output;
}
@@ -58,15 +158,27 @@
//.............................................................................
public BigInteger getN() {
- throw new RuntimeException("NYI");
+ return n;
}
public BigInteger getE(long value) {
- throw new RuntimeException("NYI");
+
+ for (int i=0; i<denominations.length; i++) {
+ if (value == denominations[i]) {
+ return e[i];
+ }
+ }
+ throw new IllegalArgumentException("Value not found");
}
public BigInteger getD(long value) {
- throw new RuntimeException("NYI");
+
+ for (int i=0; i<denominations.length; i++) {
+ if (value == denominations[i]) {
+ return d[i];
+ }
+ }
+ throw new IllegalArgumentException("Value not found");
}
}
1.2 +31 -5 java/webfunds/token/algorithm/ChaumPublicTokenParameters.java
Index: ChaumPublicTokenParameters.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/token/algorithm/ChaumPublicTokenParameters.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ChaumPublicTokenParameters.java 2000/12/31 03:54:21 1.1
+++ ChaumPublicTokenParameters.java 2001/01/04 02:08:15 1.2
@@ -1,4 +1,4 @@
-/* $Id: ChaumPublicTokenParameters.java,v 1.1 2000/12/31 03:54:21 edwin Exp $
+/* $Id: ChaumPublicTokenParameters.java,v 1.2 2001/01/04 02:08:15 edwin Exp $
*
* Copyright (c) Systemics Ltd 2000 on behalf of
* the WebFunds Development Team. All Rights Reserved.
@@ -15,20 +15,40 @@
/**
* Class description
*
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
* @author Edwin Woudt <edwin@webfunds.org>
*/
public final class ChaumPublicTokenParameters
extends AbstractPublicTokenParameters {
+// Instance variables
+//.............................................................................
+
+ BigInteger n;
+ BigInteger[] e;
+ long[] denominations;
+
+
// Init methods
//.............................................................................
protected void decodeImpl(byte[] data, int start, int len)
throws DataFormatException, UnsupportedVersionException
{
- throw new RuntimeException("NYI");
+
+ n = EncodeDecodeUtil.decodeMPI(data, start, len);
+ int delta = EncodeDecodeUtil.getMPILength(data, start, len);
+ start += delta; start -= len;
+
+ this.denominations = getDenominations();
+ e = new BigInteger[denominations.length];
+
+ for (int i=0; i<denominations.length; i++) {
+ e[i] = EncodeDecodeUtil.decodeMPI(data, start, len);
+ delta = EncodeDecodeUtil.getMPILength(data, start, len);
+ start += delta; start -= len;
+ }
}
@@ -36,11 +56,17 @@
//.............................................................................
public BigInteger getN() {
- throw new RuntimeException("NYI");
+ return n;
}
public BigInteger getE(long value) {
- throw new RuntimeException("NYI");
- }
+ for (int i=0; i<denominations.length; i++) {
+ if (value == denominations[i]) {
+ return e[i];
+ }
+ }
+ throw new IllegalArgumentException("Value not found");
+ }
+
}