github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/jjm/verify_test.go (about)

     1  package jjm
     2  
     3  import (
     4  	"crypto/elliptic"
     5  	"crypto/rand"
     6  	"github.com/ronperry/cryptoedge/eccutil"
     7  	"math/big"
     8  	"testing"
     9  )
    10  
    11  func TestVerify(t *testing.T) {
    12  	msg := []byte("Message to be blind-signed")
    13  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    14  	privkey, pubkey, err := c.GenerateKey()
    15  	if err != nil {
    16  		t.Fatalf("Error creating keys: %s", err)
    17  	}
    18  	signer := NewSigner(privkey, pubkey, c)
    19  	publicSigParams, privateSigParams, err := signer.NewSignRequest()
    20  	if err != nil {
    21  		t.Errorf("Error occured throughout parameter creation: %s", err)
    22  	}
    23  	bc := NewBlindingClient(c, pubkey)
    24  
    25  	privateBlindingParams, err := bc.CalculateBlindingParams(publicSigParams)
    26  	if err != nil {
    27  		t.Errorf("CalculateBlindingParams failed: %s", err)
    28  	}
    29  
    30  	blindmessage, err := bc.Blind(msg, publicSigParams, privateBlindingParams)
    31  	if err != nil {
    32  		t.Errorf("Blind failed: %s", err)
    33  	}
    34  
    35  	blindsig, err := signer.Sign(blindmessage, privateSigParams)
    36  	if err != nil {
    37  		t.Errorf("Sign failed: %s", err)
    38  	}
    39  	signature, err := bc.Unblind(blindsig, privateBlindingParams)
    40  	if err != nil {
    41  		t.Errorf("Unblind failed: %s", err)
    42  	}
    43  
    44  	ok := bc.Verify(msg, signature)
    45  	if !ok {
    46  		t.Errorf("Verify failed")
    47  	}
    48  	badsig1 := new(SignatureInt)
    49  	badsig1.PointR = signature.PointR
    50  	badsig1.ScalarR = signature.ScalarR
    51  	badsig1.ScalarS = new(big.Int)
    52  	badsig1.ScalarS = badsig1.ScalarS.Sub(signature.ScalarS, big.NewInt(1))
    53  	ok = bc.Verify(msg, badsig1)
    54  	if ok {
    55  		t.Errorf("Verify must fail")
    56  	}
    57  	badsig2 := new(SignatureInt)
    58  	badsig2.PointR = signature.PointR
    59  	badsig2.ScalarS = signature.ScalarS
    60  	badsig2.ScalarR = new(big.Int)
    61  	badsig2.ScalarR = badsig2.ScalarR.Sub(signature.ScalarR, big.NewInt(1))
    62  	ok = bc.Verify(msg, badsig1)
    63  	if ok {
    64  		t.Errorf("Verify must fail")
    65  	}
    66  	badsig3 := new(SignatureInt)
    67  	badsig3.PointR = new(eccutil.Point)
    68  	badsig3.PointR.Y = signature.PointR.Y
    69  	badsig3.PointR.X = new(big.Int)
    70  	badsig3.PointR.X = badsig3.PointR.X.Sub(signature.PointR.X, big.NewInt(1))
    71  	badsig3.ScalarS = signature.ScalarS
    72  	badsig3.ScalarR = signature.ScalarR
    73  	ok = bc.Verify(msg, badsig3)
    74  	if ok {
    75  		t.Errorf("Verify must fail")
    76  	}
    77  
    78  	msg2 := []byte("Another message that may not verify")
    79  	ok = bc.Verify(msg2, signature)
    80  	if ok {
    81  		t.Errorf("Verify must fail")
    82  	}
    83  
    84  	_, pubkey2, err := c.GenerateKey()
    85  	if err != nil {
    86  		t.Fatalf("Error creating keys: %s", err)
    87  	}
    88  	bc2 := NewBlindingClient(c, pubkey2)
    89  	ok = bc2.Verify(msg, signature)
    90  	if ok {
    91  		t.Errorf("Verify must fail")
    92  	}
    93  }