github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/ctcrypto/crypto/ringct/bulletproof_regulation_test.go (about)

     1  package ringct
     2  
     3  import (
     4  	"math/rand"
     5  	"os"
     6  	"runtime/pprof"
     7  	"testing"
     8  
     9  	"github.com/ethereum/go-ethereum/ctcrypto/crypto"
    10  )
    11  
    12  func TestEdgeBulletProofWithRegulation(t *testing.T) {
    13  	random_gamma := crypto.SkGen()
    14  	sL, sR, rho, S := GenRegulationParaForBulletproof()
    15  	if S == nil {
    16  		t.Fatalf("GenRegulationParaForBulletproof test failed")
    17  	}
    18  	b0 := BULLETPROOF_Prove_Amount_WithRegulation(0, &random_gamma, sL, sR, rho)
    19  
    20  	if !b0.BULLETPROOF_Verify() {
    21  		t.Fatalf("BulletProof 0 amount test failed")
    22  	}
    23  	if !b0.BULLETPROOF_Verify_fast() {
    24  		t.Fatalf("BulletProof fast 0 amount test failed")
    25  	}
    26  
    27  	if !b0.BULLETPROOF_Verify_ultrafast() {
    28  		t.Fatalf("BulletProof ultra fast 0 amount test failed")
    29  	}
    30  
    31  	bmax := BULLETPROOF_Prove_Amount_WithRegulation(0xffffffffffffffff, &random_gamma, sL, sR, rho)
    32  
    33  	if !bmax.BULLETPROOF_Verify() {
    34  		t.Fatalf("BulletProof 0xffffffffffffffff max amount test failed")
    35  	}
    36  	if !bmax.BULLETPROOF_Verify_fast() {
    37  		t.Fatalf("BulletProof fast 0xffffffffffffffff max amount test failed")
    38  	}
    39  	if !bmax.BULLETPROOF_Verify_ultrafast() {
    40  		t.Fatalf("BulletProof ultrafast 0xffffffffffffffff max amount test failed")
    41  	}
    42  
    43  	invalid_8 := crypto.Zero
    44  	invalid_8[8] = 1
    45  
    46  	binvalid8 := BULLETPROOF_Prove_WithRegulation(&invalid_8, &random_gamma, sL, sR, rho)
    47  
    48  	if binvalid8.BULLETPROOF_Verify() {
    49  		t.Fatalf("BulletProof invalid 8 test failed")
    50  	}
    51  
    52  	if binvalid8.BULLETPROOF_Verify_fast() {
    53  		t.Fatalf("BulletProof invalid 8 test failed")
    54  	}
    55  	if binvalid8.BULLETPROOF_Verify_ultrafast() {
    56  		t.Fatalf("BulletProof invalid 8 test failed")
    57  	}
    58  
    59  	invalid_31 := crypto.Zero
    60  	invalid_31[31] = 1
    61  
    62  	binvalid31 := BULLETPROOF_Prove_WithRegulation(&invalid_31, &random_gamma, sL, sR, rho)
    63  
    64  	if binvalid31.BULLETPROOF_Verify() {
    65  		t.Fatalf("BulletProof invalid 31 test failed")
    66  	}
    67  	if binvalid31.BULLETPROOF_Verify_fast() {
    68  		t.Fatalf("BulletProof invalid 31 fast test failed")
    69  	}
    70  	if binvalid31.BULLETPROOF_Verify_ultrafast() {
    71  		t.Fatalf("BulletProof invalid 31 fast test failed")
    72  	}
    73  
    74  }
    75  
    76  func TestEdgeBulletProofWithRegulationRaw(t *testing.T) {
    77  	random_gamma := crypto.SkGen()
    78  	sL, sR, rho, S := GenRegulationParaForBulletproof()
    79  	b00 := BULLETPROOF_Prove_Amount_WithRegulation(0, &random_gamma, sL, sR, rho)
    80  
    81  	if !b00.BULLETPROOF_Verify() {
    82  		t.Fatalf("BulletProof 0 amount test failed")
    83  	}
    84  	if S == nil {
    85  		t.Fatalf("GenRegulationParaForBulletproof test failed")
    86  	}
    87  	b0 := BULLETPROOF_Prove_Amount_WithRegulation_Raw(0, &random_gamma, sL, sR, rho)
    88  
    89  	if !b0.BULLETPROOF_Verify_Raw_ultrafast() {
    90  		t.Fatalf("BulletProof ultra fast 0 amount test failed")
    91  	}
    92  	amount := b0.ExtractAmount(sL)
    93  	if *d2h(0) != amount {
    94  		t.Fatalf("BulletProof ultrafast 0 ExtractAmount failed")
    95  	}
    96  
    97  	bmax := BULLETPROOF_Prove_Amount_WithRegulation_Raw(0xffffffffffffffff, &random_gamma, sL, sR, rho)
    98  
    99  	if !bmax.BULLETPROOF_Verify_Raw_ultrafast() {
   100  		t.Fatalf("BulletProof ultrafast 0xffffffffffffffff max amount test failed")
   101  	}
   102  	amount = bmax.ExtractAmount(sL)
   103  	if *d2h(0xffffffffffffffff) != amount {
   104  		t.Fatalf("BulletProof ultrafast 0xffffffffffffffff ExtractAmount failed")
   105  	}
   106  
   107  	invalid_8 := crypto.Zero
   108  	invalid_8[8] = 1
   109  
   110  	binvalid8 := BULLETPROOF_Prove_WithRegulation_Raw(&invalid_8, &random_gamma, sL, sR, rho)
   111  
   112  	if binvalid8.BULLETPROOF_Verify_Raw_ultrafast() {
   113  		t.Fatalf("BulletProof invalid 8 test failed")
   114  	}
   115  
   116  	invalid_31 := crypto.Zero
   117  	invalid_31[31] = 1
   118  
   119  	binvalid31 := BULLETPROOF_Prove_WithRegulation_Raw(&invalid_31, &random_gamma, sL, sR, rho)
   120  
   121  	if binvalid31.BULLETPROOF_Verify_Raw_ultrafast() {
   122  		t.Fatalf("BulletProof invalid 31 fast test failed")
   123  	}
   124  
   125  }
   126  
   127  func TestEdgeBulletProofWithRegulationOpt(t *testing.T) {
   128  	random_gamma := crypto.SkGen()
   129  	sL, sR, rho, S := GenRegulationParaForBulletproof()
   130  	b00 := BULLETPROOF_Prove_Amount_WithRegulation(0, &random_gamma, sL, sR, rho)
   131  
   132  	if !b00.BULLETPROOF_Verify() {
   133  		t.Fatalf("BulletProof 0 amount test failed")
   134  	}
   135  	if S == nil {
   136  		t.Fatalf("GenRegulationParaForBulletproof test failed")
   137  	}
   138  	b0 := BULLETPROOF_Prove_Amount_WithRegulation_Raw(0, &random_gamma, sL, sR, rho)
   139  	proof := []BulletProof{*b0}
   140  	if !b0.BULLETPROOF_Verify_Raw_ultrafast() {
   141  		t.Fatalf("BulletProof ultra fast 0 amount test failed")
   142  	}
   143  
   144  	amount := b0.ExtractAmount(sL)
   145  	if *d2h(0) != amount {
   146  		t.Fatalf("BulletProof ultrafast 0 ExtractAmount failed")
   147  	}
   148  
   149  	bmax := BULLETPROOF_Prove_Amount_WithRegulation_Raw(0xffffffffffffffff, &random_gamma, sL, sR, rho)
   150  
   151  	if !bmax.BULLETPROOF_Verify_Raw_ultrafast() {
   152  		t.Fatalf("BulletProof ultrafast 0xffffffffffffffff max amount test failed")
   153  	}
   154  	amount = bmax.ExtractAmount(sL)
   155  	if *d2h(0xffffffffffffffff) != amount {
   156  		t.Fatalf("BulletProof ultrafast 0xffffffffffffffff ExtractAmount failed")
   157  	}
   158  	count := 2
   159  	_masks := make([]crypto.Key, count)
   160  	_amount := make([]crypto.Key, count)
   161  	for i := range _masks {
   162  		_masks[i] = crypto.SkGen()
   163  		_amount[i] = *d2h(rand.Uint64())
   164  	}
   165  	sL2, sR2, rho, S := GenRegulationParaForBulletproof2(uint32(count))
   166  	proof0 := BULLETPROOF_Prove2_raw(_amount, _masks)
   167  	if !BULLETPROOF_Verify2_Optimized_WithRegulation([]BulletProof{*proof0}) {
   168  		t.Fatalf("BulletProof BULLETPROOF_Verify2_Optimized_WithRegulation test failed")
   169  	}
   170  
   171  	proof = []BulletProof{*BULLETPROOF_Prove2_WithRegulation(_amount, _masks, sL2, sR2, *rho)}
   172  
   173  	if !BULLETPROOF_Verify2_Optimized_WithRegulation(proof) {
   174  		t.Fatalf("BulletProof BULLETPROOF_Verify2_Optimized_WithRegulation test failed")
   175  	}
   176  }
   177  
   178  func BenchmarkBulletproofVerifyultrafastRaw(b *testing.B) {
   179  
   180  	cpufile, err := os.Create("/tmp/bp_cpuprofile_fast.prof")
   181  	if err != nil {
   182  
   183  	}
   184  	if err := pprof.StartCPUProfile(cpufile); err != nil {
   185  	}
   186  	defer pprof.StopCPUProfile()
   187  
   188  	random_gamma := crypto.SkGen()
   189  	sL, sR, rho, _ := GenRegulationParaForBulletproof()
   190  	bp := BULLETPROOF_Prove_Amount_WithRegulation_Raw(0, &random_gamma, sL, sR, rho)
   191  	b.ResetTimer()
   192  	for n := 0; n < b.N; n++ {
   193  		if !bp.BULLETPROOF_Verify_Raw_ultrafast() {
   194  			b.Fatalf("BulletProof verification failed")
   195  		}
   196  	}
   197  }
   198  
   199  func BenchmarkBulletProofRawWithMultipleV(b *testing.B) {
   200  	count := 2
   201  	proofCount := 5
   202  	var proofs []BulletProof
   203  	for i := 0; i < proofCount; i++ {
   204  		_masks := make([]crypto.Key, count)
   205  		_amount := make([]crypto.Key, count)
   206  		for i := range _masks {
   207  			_masks[i] = crypto.SkGen()
   208  			_amount[i] = *d2h(rand.Uint64())
   209  		}
   210  		sL2, sR2, rho, _ := GenRegulationParaForBulletproof2(uint32(count))
   211  		proofs = append(proofs, *BULLETPROOF_Prove2_WithRegulation(_amount, _masks, sL2, sR2, *rho))
   212  	}
   213  
   214  	b.ResetTimer()
   215  	for n := 0; n < b.N; n++ {
   216  		if !BULLETPROOF_Verify2_Optimized_WithRegulation(proofs) {
   217  			b.Fatalf("BULLETPROOF_Verify2_Optimized failed")
   218  		}
   219  	}
   220  }