github.com/cloudflare/circl@v1.5.0/zk/qndleq/qndleq_test.go (about) 1 package qndleq_test 2 3 import ( 4 "crypto/rand" 5 "math/big" 6 "testing" 7 8 "github.com/cloudflare/circl/internal/test" 9 "github.com/cloudflare/circl/zk/qndleq" 10 ) 11 12 func TestProve(t *testing.T) { 13 const testTimes = 1 << 8 14 const SecParam = 128 15 one := big.NewInt(1) 16 max := new(big.Int).Lsh(one, 256) 17 18 for i := 0; i < testTimes; i++ { 19 N, _ := rand.Int(rand.Reader, max) 20 if N.Bit(0) == 0 { 21 N.Add(N, one) 22 } 23 x, _ := rand.Int(rand.Reader, N) 24 g, err := qndleq.SampleQn(rand.Reader, N) 25 test.CheckNoErr(t, err, "failed to sampleQn") 26 h, err := qndleq.SampleQn(rand.Reader, N) 27 test.CheckNoErr(t, err, "failed to sampleQn") 28 gx := new(big.Int).Exp(g, x, N) 29 hx := new(big.Int).Exp(h, x, N) 30 31 proof, err := qndleq.Prove(rand.Reader, x, g, gx, h, hx, N, SecParam) 32 test.CheckNoErr(t, err, "failed to generate proof") 33 test.CheckOk(proof.Verify(g, gx, h, hx, N), "failed to verify", t) 34 } 35 } 36 37 func TestSampleQn(t *testing.T) { 38 const testTimes = 1 << 7 39 one := big.NewInt(1) 40 max := new(big.Int).Lsh(one, 256) 41 42 for i := 0; i < testTimes; i++ { 43 N, _ := rand.Int(rand.Reader, max) 44 if N.Bit(0) == 0 { 45 N.Add(N, one) 46 } 47 a, err := qndleq.SampleQn(rand.Reader, N) 48 test.CheckNoErr(t, err, "failed to sampleQn") 49 jac := big.Jacobi(a, N) 50 test.CheckOk(jac == 1, "Jacoby symbol should be one", t) 51 gcd := new(big.Int).GCD(nil, nil, a, N) 52 test.CheckOk(gcd.Cmp(one) == 0, "should be coprime to N", t) 53 } 54 } 55 56 func Benchmark_qndleq(b *testing.B) { 57 const SecParam = 128 58 one := big.NewInt(1) 59 max := new(big.Int).Lsh(one, 256) 60 61 N, _ := rand.Int(rand.Reader, max) 62 if N.Bit(0) == 0 { 63 N.Add(N, one) 64 } 65 x, _ := rand.Int(rand.Reader, N) 66 g, _ := qndleq.SampleQn(rand.Reader, N) 67 h, _ := qndleq.SampleQn(rand.Reader, N) 68 gx := new(big.Int).Exp(g, x, N) 69 hx := new(big.Int).Exp(h, x, N) 70 71 proof, _ := qndleq.Prove(rand.Reader, x, g, gx, h, hx, N, SecParam) 72 73 b.Run("Prove", func(b *testing.B) { 74 for i := 0; i < b.N; i++ { 75 _, _ = qndleq.Prove(rand.Reader, x, g, gx, h, hx, N, SecParam) 76 } 77 }) 78 79 b.Run("Verify", func(b *testing.B) { 80 for i := 0; i < b.N; i++ { 81 _ = proof.Verify(g, gx, h, hx, N) 82 } 83 }) 84 }