github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/crypto/bn256/cloudflare/gfp_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 24 // Tests that negation works the same way on both assembly-optimized and pure Go 25 // implementation. 26 func TestGFpNeg(t *testing.T) { 27 n := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} 28 w := &gfP{0xfedcba9876543211, 0x0123456789abcdef, 0x2152411021524110, 0x0114251201142512} 29 h := &gfP{} 30 31 gfpNeg(h, n) 32 if *h != *w { 33 t.Errorf("negation mismatch: have %#x, want %#x", *h, *w) 34 } 35 } 36 37 // Tests that addition works the same way on both assembly-optimized and pure Go 38 // implementation. 39 func TestGFpAdd(t *testing.T) { 40 a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} 41 b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} 42 w := &gfP{0xc3df73e9278302b8, 0x687e956e978e3572, 0x254954275c18417f, 0xad354b6afc67f9b4} 43 h := &gfP{} 44 45 gfpAdd(h, a, b) 46 if *h != *w { 47 t.Errorf("addition mismatch: have %#x, want %#x", *h, *w) 48 } 49 } 50 51 // Tests that subtraction works the same way on both assembly-optimized and pure Go 52 // implementation. 53 func TestGFpSub(t *testing.T) { 54 a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} 55 b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} 56 w := &gfP{0x02468acf13579bdf, 0xfdb97530eca86420, 0xdfc1e401dfc1e402, 0x203e1bfe203e1bfd} 57 h := &gfP{} 58 59 gfpSub(h, a, b) 60 if *h != *w { 61 t.Errorf("subtraction mismatch: have %#x, want %#x", *h, *w) 62 } 63 } 64 65 // Tests that multiplication works the same way on both assembly-optimized and pure Go 66 // implementation. 67 func TestGFpMul(t *testing.T) { 68 a := &gfP{0x0123456789abcdef, 0xfedcba9876543210, 0xdeadbeefdeadbeef, 0xfeebdaedfeebdaed} 69 b := &gfP{0xfedcba9876543210, 0x0123456789abcdef, 0xfeebdaedfeebdaed, 0xdeadbeefdeadbeef} 70 w := &gfP{0xcbcbd377f7ad22d3, 0x3b89ba5d849379bf, 0x87b61627bd38b6d2, 0xc44052a2a0e654b2} 71 h := &gfP{} 72 73 gfpMul(h, a, b) 74 if *h != *w { 75 t.Errorf("multiplication mismatch: have %#x, want %#x", *h, *w) 76 } 77 }