github.com/cloudflare/circl@v1.5.0/sign/dilithium/mode3/internal/sample_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  
     7  	common "github.com/cloudflare/circl/sign/internal/dilithium"
     8  )
     9  
    10  func TestVectorDeriveUniform(t *testing.T) {
    11  	var p, p2 common.Poly
    12  	var seed [32]byte
    13  	p2 = common.Poly{
    14  		2901364, 562527, 5258502, 3885002, 4190126, 4460268, 6884052,
    15  		3514511, 5383040, 213206, 2155865, 5179607, 3551954, 2312357,
    16  		6066350, 8126097, 1179080, 4787182, 6552182, 6713644,
    17  		1561067, 7626063, 7859743, 5052321, 7032876, 7815031, 157938,
    18  		1865184, 490802, 5717642, 3451902, 7000218, 3743250, 1677431,
    19  		1875427, 5596150, 671623, 3819041, 6247594, 1014875, 4933545,
    20  		7122446, 6682963, 3388398, 3335295, 943002, 1145083, 3113071,
    21  		105967, 1916675, 7474561, 1107006, 700548, 2147909, 1603855,
    22  		5049181, 437882, 6118899, 5656914, 6731065, 3066622, 865453,
    23  		5427634, 981549, 4650873, 861291, 4003872, 5104220, 6171453,
    24  		3723302, 7426315, 6137283, 4874820, 6052561, 53441, 5032874,
    25  		5614778, 2248550, 1756499, 8280764, 8263880, 7600081,
    26  		5118374, 795344, 7543392, 6869925, 1841187, 4181568, 584562,
    27  		7483939, 4938664, 6863397, 5126354, 5218129, 6236086,
    28  		4149293, 379169, 4368487, 7490569, 3409215, 1580463, 3081737,
    29  		1278732, 7109719, 7371700, 2097931, 399836, 1700274, 7188595,
    30  		6830029, 1548850, 6593138, 6849097, 1518037, 2859442,
    31  		7772265, 7325153, 3281191, 7856131, 4995056, 4684325,
    32  		1351194, 8223904, 6817307, 2484146, 131782, 397032, 7436778,
    33  		7973479, 3171829, 5624626, 3540123, 7150120, 8313283,
    34  		3604714, 1043574, 117692, 7797783, 7909392, 903315, 7335342,
    35  		7501562, 5826142, 2709813, 8245473, 2369045, 2782257,
    36  		5762833, 6474114, 6862031, 424522, 594248, 2626630, 7659983,
    37  		5642869, 4075194, 1592129, 245547, 5271031, 3205046, 982375,
    38  		267873, 1286496, 7230481, 3208972, 7485411, 676111, 4944500,
    39  		2959742, 5934456, 1414847, 6067948, 1709895, 4648315, 126008,
    40  		8258986, 2183134, 2302072, 4674924, 4306056, 7465311,
    41  		6500270, 4247428, 4016815, 4973426, 294287, 2456847, 3289700,
    42  		2732169, 1159447, 5569724, 140001, 3237977, 8007761, 5874533,
    43  		255652, 3119586, 2102434, 6248250, 8152822, 8006066, 7708625,
    44  		6997719, 6260212, 6186962, 6636650, 7836834, 7998017,
    45  		2061516, 1197591, 1706544, 733027, 2392907, 2700000, 8254598,
    46  		4488002, 160495, 2985325, 2036837, 2703633, 6406550, 3579947,
    47  		6195178, 5552390, 6804584, 6305468, 5731980, 6095195,
    48  		3323409, 1322661, 6690942, 3374630, 5615167, 479044, 3136054,
    49  		4380418, 2833144, 7829577, 1770522, 6056687, 240415, 14780,
    50  		3740517, 5224226, 3547288, 2083124, 4699398, 3654239,
    51  		5624978, 585593, 3655369, 2281739, 3338565, 1908093, 7784706,
    52  		4352830,
    53  	}
    54  	for i := 0; i < 32; i++ {
    55  		seed[i] = byte(i)
    56  	}
    57  	PolyDeriveUniform(&p, &seed, 30000)
    58  	if p != p2 {
    59  		t.Fatalf("%v != %v", p, p2)
    60  	}
    61  }
    62  
    63  func TestDeriveUniform(t *testing.T) {
    64  	var p common.Poly
    65  	var seed [32]byte
    66  	for i := 0; i < 100; i++ {
    67  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
    68  		PolyDeriveUniform(&p, &seed, uint16(i))
    69  		if !PolyNormalized(&p) {
    70  			t.Fatal()
    71  		}
    72  	}
    73  }
    74  
    75  func TestDeriveUniformLeqEta(t *testing.T) {
    76  	var p common.Poly
    77  	var seed [64]byte
    78  	for i := 0; i < 100; i++ {
    79  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
    80  		PolyDeriveUniformLeqEta(&p, &seed, uint16(i))
    81  		for j := 0; j < common.N; j++ {
    82  			if p[j] < common.Q-Eta || p[j] > common.Q+Eta {
    83  				t.Fatal()
    84  			}
    85  		}
    86  	}
    87  }
    88  
    89  func TestDeriveUniformLeGamma1(t *testing.T) {
    90  	var p common.Poly
    91  	var seed [64]byte
    92  	for i := 0; i < 100; i++ {
    93  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
    94  		PolyDeriveUniformLeGamma1(&p, &seed, uint16(i))
    95  		for j := 0; j < common.N; j++ {
    96  			if (p[j] > Gamma1 && p[j] <= common.Q-Gamma1) || p[j] >= common.Q {
    97  				t.Fatal()
    98  			}
    99  		}
   100  	}
   101  }
   102  
   103  func TestDeriveUniformBall(t *testing.T) {
   104  	var p common.Poly
   105  	var seed [CTildeSize]byte
   106  	for i := 0; i < 100; i++ {
   107  		binary.LittleEndian.PutUint64(seed[:], uint64(i))
   108  		PolyDeriveUniformBall(&p, seed[:])
   109  		nonzero := 0
   110  		for j := 0; j < common.N; j++ {
   111  			if p[j] != 0 {
   112  				if p[j] != 1 && p[j] != common.Q-1 {
   113  					t.Fatal()
   114  				}
   115  				nonzero++
   116  			}
   117  		}
   118  		if nonzero != Tau {
   119  			t.Fatal()
   120  		}
   121  	}
   122  }
   123  
   124  func TestDeriveUniformX4(t *testing.T) {
   125  	if !DeriveX4Available {
   126  		t.SkipNow()
   127  	}
   128  	var ps [4]common.Poly
   129  	var p common.Poly
   130  	var seed [32]byte
   131  	nonces := [4]uint16{12345, 54321, 13532, 37377}
   132  
   133  	for i := 0; i < len(seed); i++ {
   134  		seed[i] = byte(i)
   135  	}
   136  
   137  	PolyDeriveUniformX4([4]*common.Poly{&ps[0], &ps[1], &ps[2], &ps[3]}, &seed,
   138  		nonces)
   139  	for i := 0; i < 4; i++ {
   140  		PolyDeriveUniform(&p, &seed, nonces[i])
   141  		if ps[i] != p {
   142  			t.Fatal()
   143  		}
   144  	}
   145  }
   146  
   147  func TestDeriveUniformBallX4(t *testing.T) {
   148  	if !DeriveX4Available {
   149  		t.SkipNow()
   150  	}
   151  	var ps [4]common.Poly
   152  	var p common.Poly
   153  	var seed [CTildeSize]byte
   154  	PolyDeriveUniformBallX4(
   155  		[4]*common.Poly{&ps[0], &ps[1], &ps[2], &ps[3]},
   156  		seed[:],
   157  	)
   158  	for j := 0; j < 4; j++ {
   159  		PolyDeriveUniformBall(&p, seed[:])
   160  		if ps[j] != p {
   161  			t.Fatalf("%d\n%v\n%v", j, ps[j], p)
   162  		}
   163  	}
   164  }
   165  
   166  func BenchmarkPolyDeriveUniformBall(b *testing.B) {
   167  	var seed [32]byte
   168  	var p common.Poly
   169  	var w1 VecK
   170  	for i := 0; i < b.N; i++ {
   171  		w1[0][0] = uint32(i)
   172  		PolyDeriveUniformBall(&p, seed[:])
   173  	}
   174  }
   175  
   176  func BenchmarkPolyDeriveUniformBallX4(b *testing.B) {
   177  	var seed [32]byte
   178  	var p common.Poly
   179  	var w1 VecK
   180  	for i := 0; i < b.N; i++ {
   181  		w1[0][0] = uint32(i)
   182  		PolyDeriveUniformBallX4(
   183  			[4]*common.Poly{&p, &p, &p, &p},
   184  			seed[:],
   185  		)
   186  	}
   187  }
   188  
   189  func BenchmarkPolyDeriveUniform(b *testing.B) {
   190  	var seed [32]byte
   191  	var p common.Poly
   192  	for i := 0; i < b.N; i++ {
   193  		PolyDeriveUniform(&p, &seed, uint16(i))
   194  	}
   195  }
   196  
   197  func BenchmarkPolyDeriveUniformX4(b *testing.B) {
   198  	if !DeriveX4Available {
   199  		b.SkipNow()
   200  	}
   201  	var seed [32]byte
   202  	var p [4]common.Poly
   203  	for i := 0; i < b.N; i++ {
   204  		nonce := uint16(4 * i)
   205  		PolyDeriveUniformX4([4]*common.Poly{&p[0], &p[1], &p[2], &p[3]},
   206  			&seed, [4]uint16{nonce, nonce + 1, nonce + 2, nonce + 3})
   207  	}
   208  }
   209  
   210  func BenchmarkPolyDeriveUniformLeGamma1(b *testing.B) {
   211  	var seed [64]byte
   212  	var p common.Poly
   213  	for i := 0; i < b.N; i++ {
   214  		PolyDeriveUniformLeGamma1(&p, &seed, uint16(i))
   215  	}
   216  }