| 1 |
// Copyright 2022 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 bbig |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/internal/boring" |
| 9 |
"math/big" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
func Enc(b *big.Int) boring.BigInt { |
| 14 |
if b == nil { |
| 15 |
return nil |
| 16 |
} |
| 17 |
x := b.Bits() |
| 18 |
if len(x) == 0 { |
| 19 |
return boring.BigInt{} |
| 20 |
} |
| 21 |
return unsafe.Slice((*uint)(&x[0]), len(x)) |
| 22 |
} |
| 23 |
|
| 24 |
func Dec(b boring.BigInt) *big.Int { |
| 25 |
if b == nil { |
| 26 |
return nil |
| 27 |
} |
| 28 |
if len(b) == 0 { |
| 29 |
return new(big.Int) |
| 30 |
} |
| 31 |
x := unsafe.Slice((*big.Word)(&b[0]), len(b)) |
| 32 |
return new(big.Int).SetBits(x) |
| 33 |
} |