github.com/amazechain/amc@v0.1.3/common/crypto/bls12381/utils.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package bls12381
    18  
    19  import (
    20  	"errors"
    21  	"github.com/holiman/uint256"
    22  	"math/big"
    23  )
    24  
    25  func bigFromHex(hex string) *big.Int {
    26  	i256, err := uint256.FromHex(hex)
    27  	if nil != err {
    28  		return uint256.NewInt(0).ToBig()
    29  	}
    30  	return i256.ToBig()
    31  }
    32  
    33  // decodeFieldElement expects 64 byte input with zero top 16 bytes,
    34  // returns lower 48 bytes.
    35  func decodeFieldElement(in []byte) ([]byte, error) {
    36  	if len(in) != 64 {
    37  		return nil, errors.New("invalid field element length")
    38  	}
    39  	// check top bytes
    40  	for i := 0; i < 16; i++ {
    41  		if in[i] != byte(0x00) {
    42  			return nil, errors.New("invalid field element top bytes")
    43  		}
    44  	}
    45  	out := make([]byte, 48)
    46  	copy(out, in[16:])
    47  	return out, nil
    48  }