github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/ctcrypto/shuffle/cgoBridge_test.go (about)

     1  package shuffle
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	crypto2 "github.com/ethereum/go-ethereum/ctcrypto/crypto"
     8  )
     9  
    10  func GenCommitment(amount, r *crypto2.Key) (commitment crypto2.Key) {
    11  	crypto2.AddKeys2(&commitment, r, amount, &crypto2.H)
    12  	return
    13  }
    14  
    15  func TestShuffle_ver(t *testing.T) {
    16  	m := 64
    17  	n := 64
    18  	inputs := make([]crypto2.Key, m*n)
    19  	for i := range inputs {
    20  		inputs[i] = crypto2.SkGen()
    21  		inputs[i] = GenCommitment(&inputs[i], &inputs[i])
    22  	}
    23  	outputs, _, proof := Shuffle_gen(m, n, inputs)
    24  	for i := range outputs {
    25  		if !outputs[i].Public_Key_Valid() {
    26  			t.Fatalf("output invalid")
    27  		}
    28  	}
    29  	if !Shuffle_ver(m, n, inputs, outputs, proof) {
    30  		t.Fatalf("Shuffle_ver failed")
    31  	}
    32  }
    33  
    34  func TestShuffle_with_regulation(t *testing.T) {
    35  	m := 64
    36  	n := 64
    37  	inputs := make([]crypto2.Key, m*n)
    38  	for i := range inputs {
    39  		inputs[i] = crypto2.SkGen()
    40  		inputs[i] = GenCommitment(&inputs[i], &inputs[i])
    41  	}
    42  	permutation := Gen_permutation(m * n)
    43  	R := Gen_R(m * n)
    44  	outputs, proof := Shuffle_gen_with_regulation(m, n, inputs, permutation, R)
    45  	for i := range inputs {
    46  		if !outputs[i].Public_Key_Valid() {
    47  			t.Fatalf("output invalid")
    48  		}
    49  		var expectOutput crypto2.Key
    50  		gr2 := crypto2.ScalarmultBase(R[permutation[i]])
    51  		input := inputs[permutation[i]]
    52  		crypto2.AddKeys(&expectOutput, &input, &gr2)
    53  		if !bytes.Equal(expectOutput[:], outputs[i][:]) {
    54  			t.Fatalf("output is wrong")
    55  		}
    56  	}
    57  	if !Shuffle_ver(m, n, inputs, outputs, proof) {
    58  		t.Fatalf("Shuffle_ver failed")
    59  	}
    60  }