github.com/cloudflare/circl@v1.5.0/xof/xof_test.go (about)

     1  package xof_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/cloudflare/circl/internal/test"
    10  	"github.com/cloudflare/circl/xof"
    11  )
    12  
    13  type vector struct {
    14  	id      xof.ID
    15  	in, out string
    16  	outLen  int
    17  }
    18  
    19  var allVectors = []vector{
    20  	{
    21  		id:     xof.SHAKE128,
    22  		in:     "",
    23  		out:    "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26",
    24  		outLen: 32,
    25  	},
    26  	{
    27  		id:     xof.SHAKE256,
    28  		in:     "",
    29  		out:    "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be",
    30  		outLen: 64,
    31  	},
    32  	{
    33  		id:     xof.SHAKE128,
    34  		in:     "The quick brown fox jumps over the lazy dog",
    35  		out:    "f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e",
    36  		outLen: 32,
    37  	},
    38  	{
    39  		id:     xof.SHAKE128,
    40  		in:     "The quick brown fox jumps over the lazy dof",
    41  		out:    "853f4538be0db9621a6cea659a06c1107b1f83f02b13d18297bd39d7411cf10c",
    42  		outLen: 32,
    43  	},
    44  	{
    45  		id:     xof.BLAKE2XB,
    46  		in:     "The quick brown fox jumps over the lazy dog",
    47  		out:    "364e84ca4c103df292306c93ebba6f6633d5e9cc8a95e040498e9a012d5ca534",
    48  		outLen: 32,
    49  	},
    50  	{
    51  		id:     xof.BLAKE2XS,
    52  		in:     "The quick brown fox jumps over the lazy dog",
    53  		out:    "0650cde4df888a06eada0f0fecb3c17594304b4a03fdd678182f27db1238b174",
    54  		outLen: 32,
    55  	},
    56  	{
    57  		id:     xof.K12D10,
    58  		in:     "The quick brown fox jumps over the lazy dog",
    59  		out:    "b4f249b4f77c58df170aa4d1723db1127d82f1d98d25ddda561ada459cd11a48",
    60  		outLen: 32,
    61  	},
    62  }
    63  
    64  func TestXof(t *testing.T) {
    65  	for i, v := range allVectors {
    66  		X := v.id.New()
    67  		_, err := X.Write([]byte(v.in))
    68  		test.CheckNoErr(t, err, "error on xof.Write")
    69  
    70  		got := make([]byte, v.outLen)
    71  		want, _ := hex.DecodeString(v.out)
    72  
    73  		for _, x := range []io.Reader{X, X.Clone()} {
    74  			n, err := x.Read(got)
    75  			test.CheckNoErr(t, err, "error on xof.Read")
    76  			if n != v.outLen || !bytes.Equal(got, want) {
    77  				test.ReportError(t, got, want, i, v.id)
    78  			}
    79  		}
    80  	}
    81  
    82  	err := test.CheckPanic(func() {
    83  		var nonID xof.ID
    84  		nonID.New()
    85  	})
    86  	test.CheckNoErr(t, err, "must panic")
    87  }