github.com/consensys/gnark-crypto@v0.14.0/fiat-shamir/transcript_test.go (about)

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fiatshamir
    16  
    17  import (
    18  	"bytes"
    19  	"crypto/sha256"
    20  	"testing"
    21  )
    22  
    23  func initTranscript() *Transcript {
    24  
    25  	fs := NewTranscript(sha256.New(), "alpha", "beta", "gamma")
    26  
    27  	values := [][]byte{[]byte("v1"), []byte("v2"), []byte("v3"), []byte("v4"), []byte("v5"), []byte("v6")}
    28  	if err := fs.Bind("alpha", values[0]); err != nil {
    29  		panic(err)
    30  	}
    31  	if err := fs.Bind("alpha", values[1]); err != nil {
    32  		panic(err)
    33  	}
    34  	if err := fs.Bind("beta", values[2]); err != nil {
    35  		panic(err)
    36  	}
    37  	if err := fs.Bind("beta", values[3]); err != nil {
    38  		panic(err)
    39  	}
    40  	if err := fs.Bind("gamma", values[4]); err != nil {
    41  		panic(err)
    42  	}
    43  	if err := fs.Bind("gamma", values[5]); err != nil {
    44  		panic(err)
    45  	}
    46  
    47  	return fs
    48  }
    49  
    50  func TestTranscript(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	fs := initTranscript()
    54  
    55  	// test when everything is fine
    56  	alpha, err := fs.ComputeChallenge("alpha")
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	beta, err := fs.ComputeChallenge("beta")
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	gamma, err := fs.ComputeChallenge("gamma")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	if len(alpha) == 0 || len(beta) == 0 || len(gamma) == 0 {
    70  		t.Fatal("one of the challenge result is empty")
    71  	}
    72  
    73  	// re compute the challenges to verify they are the same
    74  	alphaBis, err := fs.ComputeChallenge("alpha")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	betaBis, err := fs.ComputeChallenge("beta")
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	gammaBis, err := fs.ComputeChallenge("gamma")
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  
    87  	if !bytes.Equal(alpha, alphaBis) {
    88  		t.Fatal("computing the same challenge twice should return the same value")
    89  	}
    90  	if !bytes.Equal(beta, betaBis) {
    91  		t.Fatal("computing the same challenge twice should return the same value")
    92  	}
    93  	if !bytes.Equal(gamma, gammaBis) {
    94  		t.Fatal("computing the same challenge twice should return the same value")
    95  	}
    96  
    97  }
    98  
    99  func TestNonExistingChallenge(t *testing.T) {
   100  	t.Parallel()
   101  
   102  	fs := initTranscript()
   103  
   104  	// query inexisting challenges
   105  	_, err := fs.ComputeChallenge("delta")
   106  	if err == nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  }
   111  
   112  func TestWrongOrder(t *testing.T) {
   113  	t.Parallel()
   114  
   115  	fs := initTranscript()
   116  
   117  	// query inexisting challenges
   118  	_, err := fs.ComputeChallenge("beta")
   119  	if err == nil {
   120  		t.Fatal(err)
   121  	}
   122  
   123  }
   124  
   125  func TestBindToComputedChallenge(t *testing.T) {
   126  	t.Parallel()
   127  
   128  	fs := initTranscript()
   129  
   130  	_, err := fs.ComputeChallenge("alpha")
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	// bind value to an already computed challenge
   136  	err = fs.Bind("alpha", []byte("test"))
   137  	if err == nil {
   138  		t.Fatal(err)
   139  	}
   140  
   141  }