-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #418 from iden3/iden232
IDEN-232 get rid of merkletree dependency. Replace merkletree.Hash ty…
- Loading branch information
Showing
9 changed files
with
167 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/hex" | ||
"math/big" | ||
|
||
"github.com/iden3/go-iden3-crypto/utils" | ||
) | ||
|
||
// ElemBytes length is 32 bytes. But not all 32-byte values are valid. | ||
// The value should be not greater than Q constant | ||
// 21888242871839275222246405745257275088548364400416034343698204186575808495617 | ||
type ElemBytes [32]byte | ||
|
||
// ToInt returns *big.Int representation of ElemBytes. | ||
func (el ElemBytes) ToInt() *big.Int { | ||
return new(big.Int).SetBytes(utils.SwapEndianness(el[:])) | ||
} | ||
|
||
// SetInt sets element's data to serialized value of *big.Int in little-endian. | ||
// And checks that the value is valid (fits in Field Q). | ||
// Returns ErrDataOverflow if the value is too large | ||
func (el *ElemBytes) SetInt(value *big.Int) error { | ||
if !utils.CheckBigIntInField(value) { | ||
return ErrDataOverflow | ||
} | ||
|
||
val := utils.SwapEndianness(value.Bytes()) | ||
copy((*el)[:], val) | ||
memset((*el)[len(val):], 0) | ||
return nil | ||
} | ||
|
||
// Hex returns HEX representation of ElemBytes | ||
func (el ElemBytes) Hex() string { | ||
return hex.EncodeToString(el[:]) | ||
} | ||
|
||
// NewElemBytesFromInt creates new ElemBytes from *big.Int. | ||
// Returns error ErrDataOverflow if value is too large to fill the Field Q. | ||
func NewElemBytesFromInt(i *big.Int) (ElemBytes, error) { | ||
var s ElemBytes | ||
bs := i.Bytes() | ||
// may be this check is redundant because of CheckBigIntInField, but just | ||
// in case. | ||
if len(bs) > len(s) { | ||
return s, ErrDataOverflow | ||
} | ||
if !utils.CheckBigIntInField(i) { | ||
return s, ErrDataOverflow | ||
} | ||
copy(s[:], utils.SwapEndianness(bs)) | ||
return s, nil | ||
} | ||
|
||
// ElemBytesToInts converts slice of ElemBytes to slice of *big.Int | ||
func ElemBytesToInts(elements []ElemBytes) []*big.Int { | ||
result := make([]*big.Int, len(elements)) | ||
for i := range elements { | ||
result[i] = elements[i].ToInt() | ||
} | ||
return result | ||
} |
Oops, something went wrong.