github.com/cloudflare/circl@v1.5.0/zk/dl/dl_test.go (about)

     1  package dl_test
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  
     7  	"github.com/cloudflare/circl/group"
     8  	"github.com/cloudflare/circl/zk/dl"
     9  )
    10  
    11  const testzkDLCount = 1 << 8
    12  
    13  func testzkDL(t *testing.T, myGroup group.Group) {
    14  	kA := myGroup.RandomNonZeroScalar(rand.Reader)
    15  	DB := myGroup.RandomElement(rand.Reader)
    16  
    17  	R := myGroup.NewElement()
    18  	R.Mul(DB, kA)
    19  
    20  	dst := "zeroknowledge"
    21  	rnd := rand.Reader
    22  	proof := dl.Prove(myGroup, DB, R, kA, []byte("Prover"), []byte(dst), rnd)
    23  
    24  	verify := dl.Verify(myGroup, DB, R, proof, []byte("Prover"), []byte(dst))
    25  	if verify == false {
    26  		t.Error("zk/dl verification failed")
    27  	}
    28  }
    29  
    30  func testzkDLNegative(t *testing.T, myGroup group.Group) {
    31  	kA := myGroup.RandomNonZeroScalar(rand.Reader)
    32  	DB := myGroup.RandomElement(rand.Reader)
    33  
    34  	R := myGroup.RandomElement(rand.Reader)
    35  
    36  	dst := "zeroknowledge"
    37  	rnd := rand.Reader
    38  	proof := dl.Prove(myGroup, DB, R, kA, []byte("Prover"), []byte(dst), rnd)
    39  
    40  	verify := dl.Verify(myGroup, DB, R, proof, []byte("Prover"), []byte(dst))
    41  	if verify == true {
    42  		t.Error("zk/dl verification should fail")
    43  	}
    44  }
    45  
    46  func TestZKDL(t *testing.T) {
    47  	t.Run("zkDL", func(t *testing.T) {
    48  		for i := 0; i < testzkDLCount; i++ {
    49  			currGroup := group.P256
    50  			testzkDL(t, currGroup)
    51  		}
    52  	})
    53  
    54  	t.Run("zkDLNegative", func(t *testing.T) {
    55  		for i := 0; i < testzkDLCount; i++ {
    56  			currGroup := group.P256
    57  			testzkDLNegative(t, currGroup)
    58  		}
    59  	})
    60  }