github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/main_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package bn256
    19  
    20  import (
    21  	"testing"
    22  
    23  	"crypto/rand"
    24  )
    25  
    26  func TestRandomG2Marshal(t *testing.T) {
    27  	for i := 0; i < 10; i++ {
    28  		n, g2, err := RandomG2(rand.Reader)
    29  		if err != nil {
    30  			t.Error(err)
    31  			continue
    32  		}
    33  		t.Logf("%v: %x\n", n, g2.Marshal())
    34  	}
    35  }
    36  
    37  func TestPairings(t *testing.T) {
    38  	a1 := new(G1).ScalarBaseMult(bigFromBase10("1"))
    39  	a2 := new(G1).ScalarBaseMult(bigFromBase10("2"))
    40  	a37 := new(G1).ScalarBaseMult(bigFromBase10("37"))
    41  	an1 := new(G1).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    42  
    43  	b0 := new(G2).ScalarBaseMult(bigFromBase10("0"))
    44  	b1 := new(G2).ScalarBaseMult(bigFromBase10("1"))
    45  	b2 := new(G2).ScalarBaseMult(bigFromBase10("2"))
    46  	b27 := new(G2).ScalarBaseMult(bigFromBase10("27"))
    47  	b999 := new(G2).ScalarBaseMult(bigFromBase10("999"))
    48  	bn1 := new(G2).ScalarBaseMult(bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495616"))
    49  
    50  	p1 := Pair(a1, b1)
    51  	pn1 := Pair(a1, bn1)
    52  	np1 := Pair(an1, b1)
    53  	if pn1.String() != np1.String() {
    54  		t.Error("Pairing mismatch: e(a, -b) != e(-a, b)")
    55  	}
    56  	if !PairingCheck([]*G1{a1, an1}, []*G2{b1, b1}) {
    57  		t.Error("MultiAte check gave false negative!")
    58  	}
    59  	p0 := new(GT).Add(p1, pn1)
    60  	p0_2 := Pair(a1, b0)
    61  	if p0.String() != p0_2.String() {
    62  		t.Error("Pairing mismatch: e(a, b) * e(a, -b) != 1")
    63  	}
    64  	p0_3 := new(GT).ScalarMult(p1, bigFromBase10("21888242871839275222246405745257275088548364400416034343698204186575808495617"))
    65  	if p0.String() != p0_3.String() {
    66  		t.Error("Pairing mismatch: e(a, b) has wrong order")
    67  	}
    68  	p2 := Pair(a2, b1)
    69  	p2_2 := Pair(a1, b2)
    70  	p2_3 := new(GT).ScalarMult(p1, bigFromBase10("2"))
    71  	if p2.String() != p2_2.String() {
    72  		t.Error("Pairing mismatch: e(a, b * 2) != e(a * 2, b)")
    73  	}
    74  	if p2.String() != p2_3.String() {
    75  		t.Error("Pairing mismatch: e(a, b * 2) != e(a, b) ** 2")
    76  	}
    77  	if p2.String() == p1.String() {
    78  		t.Error("Pairing is degenerate!")
    79  	}
    80  	if PairingCheck([]*G1{a1, a1}, []*G2{b1, b1}) {
    81  		t.Error("MultiAte check gave false positive!")
    82  	}
    83  	p999 := Pair(a37, b27)
    84  	p999_2 := Pair(a1, b999)
    85  	if p999.String() != p999_2.String() {
    86  		t.Error("Pairing mismatch: e(a * 37, b * 27) != e(a, b * 999)")
    87  	}
    88  }