github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/crypto/elliptic/internal/fiat/p521_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fiat_test 6 7 import ( 8 "crypto/elliptic/internal/fiat" 9 "crypto/rand" 10 "testing" 11 ) 12 13 func p521Random(t *testing.T) *fiat.P521Element { 14 buf := make([]byte, 66) 15 if _, err := rand.Read(buf); err != nil { 16 t.Fatal(err) 17 } 18 buf[65] &= 1 19 e, err := new(fiat.P521Element).SetBytes(buf) 20 if err != nil { 21 t.Fatal(err) 22 } 23 return e 24 } 25 26 func TestP521Invert(t *testing.T) { 27 a := p521Random(t) 28 inv := new(fiat.P521Element).Invert(a) 29 one := new(fiat.P521Element).Mul(a, inv) 30 if new(fiat.P521Element).One().Equal(one) != 1 { 31 t.Errorf("a * 1/a != 1; got %x for %x", one.Bytes(), a.Bytes()) 32 } 33 inv.Invert(new(fiat.P521Element)) 34 if new(fiat.P521Element).Equal(inv) != 1 { 35 t.Errorf("1/0 != 0; got %x", inv.Bytes()) 36 } 37 }