-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
refactor: utility methods into separate internal package for reuse in std library
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package limbs | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
) | ||
|
||
// Recompose takes the limbs in inputs and combines them into res. It errors if | ||
// inputs is uninitialized or zero-length and if the result is uninitialized. | ||
// | ||
// The following holds | ||
// | ||
// res = \sum_{i=0}^{len(inputs)} inputs[i] * 2^{nbBits * i} | ||
func Recompose(inputs []*big.Int, nbBits uint, res *big.Int) error { | ||
if len(inputs) == 0 { | ||
return fmt.Errorf("zero length slice input") | ||
} | ||
if res == nil { | ||
return fmt.Errorf("result not initialized") | ||
} | ||
res.SetUint64(0) | ||
for i := range inputs { | ||
res.Lsh(res, nbBits) | ||
res.Add(res, inputs[len(inputs)-i-1]) | ||
} | ||
// we do not mod-reduce here as the result is mod-reduced by the caller if | ||
// needed. In some places we need non-reduced results. | ||
return nil | ||
} | ||
|
||
// Decompose decomposes the input into res as integers of width nbBits. It | ||
// errors if the decomposition does not fit into res or if res is uninitialized. | ||
// | ||
// The following holds | ||
// | ||
// input = \sum_{i=0}^{len(res)} res[i] * 2^{nbBits * i} | ||
func Decompose(input *big.Int, nbBits uint, res []*big.Int) error { | ||
// limb modulus | ||
if input.BitLen() > len(res)*int(nbBits) { | ||
return fmt.Errorf("decomposed integer does not fit into res") | ||
} | ||
for _, r := range res { | ||
if r == nil { | ||
return fmt.Errorf("result slice element uninitalized") | ||
} | ||
} | ||
base := new(big.Int).Lsh(big.NewInt(1), nbBits) | ||
tmp := new(big.Int).Set(input) | ||
for i := 0; i < len(res); i++ { | ||
res[i].Mod(tmp, base) | ||
tmp.Rsh(tmp, nbBits) | ||
} | ||
return nil | ||
} |