mirror of
https://github.com/arnaucube/go-iden3-crypto.git
synced 2026-02-07 11:36:41 +01:00
Compare commits
4 Commits
feature/up
...
feature/ex
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71dbddb5f1 | ||
|
|
0a5c6acba3 | ||
|
|
a366175021 | ||
|
|
a2015adb2f |
@@ -222,13 +222,26 @@ func (p *Point) Compress() [32]byte {
|
||||
// Decompress a compressed Point into p, and also returns the decompressed
|
||||
// Point. Returns error if the compressed Point is invalid.
|
||||
func (p *Point) Decompress(leBuf [32]byte) (*Point, error) {
|
||||
var sign bool
|
||||
sign, p.Y = CompressedPointToSignAndY(leBuf)
|
||||
return PointFromSignAndY(sign, p.Y)
|
||||
}
|
||||
|
||||
// CompressedPointToSignAndY returns the sign and coordinate Y from a given
|
||||
// compressed point. This method does not check that the Point belongs to the
|
||||
// BabyJubJub curve, thus does not return error in such case. This method is
|
||||
// intended to obtain the sign and the Y coordinate without checking if the
|
||||
// point belongs to the curve, if the objective is to uncompress a point
|
||||
// Decompress method should be used instead.
|
||||
func CompressedPointToSignAndY(leBuf [32]byte) (bool, *big.Int) {
|
||||
sign := false
|
||||
y := big.NewInt(0)
|
||||
if (leBuf[31] & 0x80) != 0x00 { //nolint:gomnd
|
||||
sign = true
|
||||
leBuf[31] = leBuf[31] & 0x7F //nolint:gomnd
|
||||
}
|
||||
utils.SetBigIntFromLEBytes(p.Y, leBuf[:])
|
||||
return PointFromSignAndY(sign, p.Y)
|
||||
utils.SetBigIntFromLEBytes(y, leBuf[:])
|
||||
return sign, y
|
||||
}
|
||||
|
||||
// PointFromSignAndY returns a Point from a Sign and the Y coordinate
|
||||
|
||||
@@ -354,3 +354,21 @@ func (pk PublicKey) Value() (driver.Value, error) {
|
||||
comp := pk.Compress()
|
||||
return comp[:], nil
|
||||
}
|
||||
|
||||
// Scan implements Scanner for database/sql.
|
||||
func (pkComp *PublicKeyComp) Scan(src interface{}) error {
|
||||
srcB, ok := src.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("can't scan %T into PublicKeyComp", src)
|
||||
}
|
||||
if len(srcB) != 32 {
|
||||
return fmt.Errorf("can't scan []byte of len %d into PublicKeyComp, want %d", len(srcB), 32)
|
||||
}
|
||||
copy(pkComp[:], srcB)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements valuer for database/sql.
|
||||
func (pkComp PublicKeyComp) Value() (driver.Value, error) {
|
||||
return pkComp[:], nil
|
||||
}
|
||||
|
||||
@@ -160,7 +160,7 @@ func TestSignatureScannerValuer(t *testing.T) {
|
||||
assert.Equal(t, value, scan)
|
||||
}
|
||||
|
||||
func TestPubKeyScannerValuer(t *testing.T) {
|
||||
func TestPublicKeyScannerValuer(t *testing.T) {
|
||||
privKValue := NewRandPrivKey()
|
||||
pubKValue := privKValue.Public()
|
||||
privKScan := NewRandPrivKey()
|
||||
@@ -175,6 +175,21 @@ func TestPubKeyScannerValuer(t *testing.T) {
|
||||
assert.Equal(t, value, scan)
|
||||
}
|
||||
|
||||
func TestPublicKeyCompScannerValuer(t *testing.T) {
|
||||
privKValue := NewRandPrivKey()
|
||||
pubKCompValue := privKValue.Public().Compress()
|
||||
privKScan := NewRandPrivKey()
|
||||
pubKCompScan := privKScan.Public().Compress()
|
||||
var value driver.Valuer
|
||||
var scan sql.Scanner
|
||||
value = &pubKCompValue
|
||||
scan = &pubKCompScan
|
||||
fromDB, err := value.Value()
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, scan.Scan(fromDB))
|
||||
assert.Equal(t, value, scan)
|
||||
}
|
||||
|
||||
func BenchmarkBabyjubEddsa(b *testing.B) {
|
||||
var k PrivateKey
|
||||
_, err := hex.Decode(k[:],
|
||||
|
||||
Reference in New Issue
Block a user