[Webfunds-commits] java/webfunds/token TokenTest.java

Edwin Woudt edwin@cypherpunks.ai
Fri, 5 Jan 2001 03:40:16 -0400 (AST)


edwin       01/01/05 03:40:16

  Added:       webfunds/token TokenTest.java
  Log:
  Simple tests.

Revision  Changes    Path
1.1                  java/webfunds/token/TokenTest.java

Index: TokenTest.java
===================================================================
/* $Id: TokenTest.java,v 1.1 2001/01/05 07:40:15 edwin Exp $
 *
 * Copyright (c) Systemics Ltd 2000 on behalf of
 * the WebFunds Development Team.  All Rights Reserved.
 */
package webfunds.token;

import java.security.*;
import java.util.*;
import webfunds.token.algorithm.*;

public final class TokenTest {

// Constants
//.............................................................................

    private static final long[] denominations = {1, 2, 4, 6, 8, 16, 32, 64, 128,
        256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 
        524288, 1048576 };
    private static final byte[] currencyID = "Grolsch beugels".getBytes();
    private static final byte[] series     = {1, 2, 3, 4};
    private static final Date expiry       = new Date(02, 01, 01, 00, 00, 00);
    private static final SecureRandom sr   = new SecureRandom();


// Step 1: generating parameters
//.............................................................................

    private static byte[] pubparams, privparams;
    private static PublicKey signkey;
    
    public static void generate() throws DataFormatException, Exception {
        // executed by Ivan
        
        // Generate a DSA sign key. Normally the application would use an
        // already established key for signing.
        System.out.println(". random");
        sr.nextInt();
        KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
        System.out.println(". DSA key");
        KeyPair pair = gen.generateKeyPair();
        signkey = pair.getPublic();
        
        // The real work
        System.out.println(". Chaum params");
        ChaumPrivateTokenParameters params = new ChaumPrivateTokenParameters();
        params.generate(sr, denominations, currencyID, series, expiry);
        params.sign(pair.getPrivate(), "DSA", null, sr);
        
        // Send this to the client
        pubparams = params.encodePublicData();
        // Keep this on the server
        privparams = params.encodePrivateData();
    }


// Step 2: generating prototokens on the client
//.............................................................................

    private static byte[] blindedprotos, locallysaved;

    public static void createprotos() throws DataFormatException, Exception {
        // executed by Alice
        
        // Decode public parameters received from the server
        ChaumPublicTokenParameters params = new ChaumPublicTokenParameters();
        params.decode(pubparams, 0, pubparams.length);
        // Verify the signature
        if (! params.verify(signkey, "DSA", null))
            throw new RuntimeException("Incorrect param signature");
        
        // Create a new blinded protocoin
        ChaumProtoToken token = new ChaumProtoToken();
        token.generate(sr, 4, params);
        
        // Send this to the server for signing
        blindedprotos = token.encodeBlindedProtoToken();
        // Save this locally
        locallysaved  = token.encodeLocallySavedData();
    }

    
// Step 3: server signs blinded protocoins
//.............................................................................

    private static byte[] signedblinded;

    public static void signblinded() throws DataFormatException, Exception {
        // executed by Ivan
    
        // Decode saved private parameters
        ChaumPrivateTokenParameters params = new ChaumPrivateTokenParameters();
        params.decode(privparams, 0, privparams.length);
        // Verify the signature (may not be needed on the server)
        if (! params.verify(signkey, "DSA", null))
            throw new RuntimeException("Incorrect param signature");
        
        
        // decode token received from client
        ChaumBlindedToken token = new ChaumBlindedToken();
        token.decode(blindedprotos, 0, blindedprotos.length);
        // check if a client challenge is needed
        // note: DO CHECK THIS, even if you don;t support it
        if (token.needsClientChallenge()) 
            throw new RuntimeException("Client challenge not supported");
        
        
        // sign token
        token.sign(sr, params);
        
        
        // send this back to the client
        signedblinded = token.encode();
    }
    

// Step 4: client unblinds signed coins
//.............................................................................

    private static byte[] coin;

    public static void unblindsigned() throws DataFormatException, Exception {
        // executed by Alice

        // Decode public parameters received from the server
        ChaumPublicTokenParameters params = new ChaumPublicTokenParameters();
        params.decode(pubparams, 0, pubparams.length);
        // Verify the signature
        if (! params.verify(signkey, "DSA", null))
            throw new RuntimeException("Incorrect param signature");
        
        
        // Decode and unblind
        ChaumToken token = new ChaumToken();
        token.unblind(signedblinded, 0, signedblinded.length,
                      locallysaved,  0, locallysaved.length,
                      sr, params);
        // Check the signature
        if (! token.verifyPublic(params))
            throw new RuntimeException("Incorrect token signature");
        
        // Save this for later use
        coin = token.encode();
    }
    

// Step 5: client verifies paid coin
//.............................................................................

    public static void transfercoinclient() throws DataFormatException, Exception {
        // executed by Bob

        // Decode public parameters received from the server
        ChaumPublicTokenParameters params = new ChaumPublicTokenParameters();
        params.decode(pubparams, 0, pubparams.length);
        // Verify the signature
        if (! params.verify(signkey, "DSA", null))
            throw new RuntimeException("Incorrect param signature");

        // Decode coin
        ChaumToken token = new ChaumToken();
        token.decode(coin, 0, coin.length);
        // Verify coin
        if (! token.verifyPublic(params))
            throw new RuntimeException("Incorrect token signature");

        // Now send the coin to the server for verification
    }
    
    
// Step 6: server verifies paid coin
//.............................................................................

    public static void transfercoinsserver() throws DataFormatException, Exception {
        // executed by Ivan

        // Decode saved private parameters
        ChaumPrivateTokenParameters params = new ChaumPrivateTokenParameters();
        params.decode(privparams, 0, privparams.length);
        // Verify the signature (may not be needed on the server)
        if (! params.verify(signkey, "DSA", null))
            throw new RuntimeException("Incorrect param signature");

        // Decode coin
        ChaumToken token = new ChaumToken();
        token.decode(coin, 0, coin.length);
        // Verify coin with private parameters 
        if (! token.verifyPrivate(params))
            throw new RuntimeException("Incorrect token signature");

        // Check the following with the double spent database
        byte[] unique = token.getUniqueID();
    }


// Main method
//.............................................................................

    public static void main(String[] args) throws Exception {
        System.out.println("generate");
        generate();
        System.out.println("createprotos");
        createprotos();
        System.out.println("signblinded");
        signblinded();
        System.out.println("unblindsigned");
        unblindsigned();
        System.out.println("transfercoinclient");
        transfercoinclient();
        System.out.println("transfercoinsserver");
        transfercoinsserver();
    }
    
}