github.com/braveheart12/insolar-09-08-19@v0.8.7/platformpolicy/internal/sign/serialization.go (about)

     1  /*
     2   *    Copyright 2019 Insolar Technologies
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package sign
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  const (
    27  	expectedBigIntBytesLength = 32
    28  	sizeBytesLength           = 2
    29  	TwoBigIntBytesLength      = (expectedBigIntBytesLength * 2) + sizeBytesLength
    30  )
    31  
    32  func SerializeTwoBigInt(one, two *big.Int) []byte {
    33  	oneBytes := one.Bytes()
    34  	twoBytes := two.Bytes()
    35  
    36  	oneBytesLen := len(oneBytes)
    37  	twoBytesLen := len(twoBytes)
    38  
    39  	if oneBytesLen > expectedBigIntBytesLength || twoBytesLen > expectedBigIntBytesLength {
    40  		err := fmt.Sprintf(
    41  			"[ serializeTwoBigInt ] wrong one, two length. one: %d; two: %d; needed: %d. One was: %s, Two was: %s",
    42  			oneBytesLen,
    43  			twoBytesLen,
    44  			expectedBigIntBytesLength,
    45  			one.String(),
    46  			two.String(),
    47  		)
    48  		panic(err)
    49  	}
    50  
    51  	var serialized [TwoBigIntBytesLength]byte
    52  
    53  	serialized[0] = uint8(oneBytesLen)
    54  	serialized[1] = uint8(twoBytesLen)
    55  
    56  	oneStartPos := sizeBytesLength
    57  	oneEndPos := oneStartPos + oneBytesLen
    58  	copy(serialized[oneStartPos:oneEndPos], oneBytes)
    59  
    60  	twoStartPos := sizeBytesLength + expectedBigIntBytesLength
    61  	twoEndPos := twoStartPos + twoBytesLen
    62  	copy(serialized[twoStartPos:twoEndPos], twoBytes)
    63  
    64  	return serialized[:]
    65  }
    66  
    67  func DeserializeTwoBigInt(data []byte) (*big.Int, *big.Int, error) {
    68  	if len(data) != TwoBigIntBytesLength {
    69  		return nil, nil, errors.Errorf("[ DeserializeTwoBigInt ] wrong data length: %d", len(data))
    70  	}
    71  
    72  	var one, two big.Int
    73  
    74  	oneBytesLen := int(data[0])
    75  	twoBytesLen := int(data[1])
    76  
    77  	if oneBytesLen > expectedBigIntBytesLength || twoBytesLen > expectedBigIntBytesLength {
    78  		return nil, nil, errors.Errorf("[ DeserializeTwoBigInt ] wrong data to parse one len: %d, two len: %d", oneBytesLen, twoBytesLen)
    79  	}
    80  
    81  	oneStartPos := sizeBytesLength
    82  	oneEndPos := oneStartPos + oneBytesLen
    83  
    84  	twoStartPos := sizeBytesLength + expectedBigIntBytesLength
    85  	twoEndPos := twoStartPos + twoBytesLen
    86  
    87  	one.SetBytes(data[oneStartPos:oneEndPos])
    88  	two.SetBytes(data[twoStartPos:twoEndPos])
    89  	return &one, &two, nil
    90  }