Skip to content

Commit

Permalink
bindings/go: add EIP-2537 serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Jun 18, 2024
1 parent 06c6820 commit 9b570f2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
50 changes: 42 additions & 8 deletions bindings/go/blst.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ const BLST_SCALAR_BYTES = 256 / 8
const BLST_FP_BYTES = 384 / 8
const BLST_P1_COMPRESS_BYTES = BLST_FP_BYTES
const BLST_P1_SERIALIZE_BYTES = BLST_FP_BYTES * 2
const BLST_P1_SERIALIZE_EIP2537_BYTES = 64 * 2
const BLST_P2_COMPRESS_BYTES = BLST_FP_BYTES * 2
const BLST_P2_SERIALIZE_BYTES = BLST_FP_BYTES * 4
const BLST_P2_SERIALIZE_EIP2537_BYTES = 128 * 2

type Scalar = C.blst_scalar
type Fp = C.blst_fp
Expand Down Expand Up @@ -1705,11 +1707,22 @@ func (p1 *P1Affine) Serialize() []byte {
return out[:]
}

func (p1 *P1Affine) SerializeEip2537() []byte {
var out [BLST_P1_SERIALIZE_EIP2537_BYTES]byte
C.blst_p1_affine_serialize_eip2537((*C.byte)(&out[0]), p1)
return out[:]
}

func (p1 *P1Affine) Deserialize(in []byte) *P1Affine {
if len(in) != BLST_P1_SERIALIZE_BYTES {
return nil
}
if C.blst_p1_deserialize(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
if len(in) == BLST_P1_SERIALIZE_BYTES {
if C.blst_p1_deserialize(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else if len(in) == BLST_P1_SERIALIZE_EIP2537_BYTES {
if C.blst_p1_deserialize_eip2537(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else {
return nil
}
return p1
Expand Down Expand Up @@ -1797,6 +1810,11 @@ func (p1 *P1) Serialize() []byte {
C.blst_p1_serialize((*C.byte)(&out[0]), p1)
return out[:]
}
func (p1 *P1) SerializeEip2537() []byte {
var out [BLST_P1_SERIALIZE_EIP2537_BYTES]byte
C.blst_p1_serialize_eip2537((*C.byte)(&out[0]), p1)
return out[:]
}
func (p1 *P1) Compress() []byte {
var out [BLST_P1_COMPRESS_BYTES]byte
C.blst_p1_compress((*C.byte)(&out[0]), p1)
Expand Down Expand Up @@ -2367,11 +2385,22 @@ func (p2 *P2Affine) Serialize() []byte {
return out[:]
}

func (p2 *P2Affine) SerializeEip2537() []byte {
var out [BLST_P2_SERIALIZE_EIP2537_BYTES]byte
C.blst_p2_affine_serialize_eip2537((*C.byte)(&out[0]), p2)
return out[:]
}

func (p2 *P2Affine) Deserialize(in []byte) *P2Affine {
if len(in) != BLST_P2_SERIALIZE_BYTES {
return nil
}
if C.blst_p2_deserialize(p2, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
if len(in) == BLST_P2_SERIALIZE_BYTES {
if C.blst_p2_deserialize(p2, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else if len(in) == BLST_P2_SERIALIZE_EIP2537_BYTES {
if C.blst_p2_deserialize_eip2537(p2, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else {
return nil
}
return p2
Expand Down Expand Up @@ -2459,6 +2488,11 @@ func (p2 *P2) Serialize() []byte {
C.blst_p2_serialize((*C.byte)(&out[0]), p2)
return out[:]
}
func (p2 *P2) SerializeEip2537() []byte {
var out [BLST_P2_SERIALIZE_EIP2537_BYTES]byte
C.blst_p2_serialize_eip2537((*C.byte)(&out[0]), p2)
return out[:]
}
func (p2 *P2) Compress() []byte {
var out [BLST_P2_COMPRESS_BYTES]byte
C.blst_p2_compress((*C.byte)(&out[0]), p2)
Expand Down
2 changes: 2 additions & 0 deletions bindings/go/blst.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ const BLST_SCALAR_BYTES = 256 / 8
const BLST_FP_BYTES = 384 / 8
const BLST_P1_COMPRESS_BYTES = BLST_FP_BYTES
const BLST_P1_SERIALIZE_BYTES = BLST_FP_BYTES * 2
const BLST_P1_SERIALIZE_EIP2537_BYTES = 64 * 2
const BLST_P2_COMPRESS_BYTES = BLST_FP_BYTES * 2
const BLST_P2_SERIALIZE_BYTES = BLST_FP_BYTES * 4
const BLST_P2_SERIALIZE_EIP2537_BYTES = 128 * 2

type Scalar = C.blst_scalar
type Fp = C.blst_fp
Expand Down
24 changes: 20 additions & 4 deletions bindings/go/blst_px.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,22 @@ func (p1 *P1Affine) Serialize() []byte {
return out[:]
}

func (p1 *P1Affine) SerializeEip2537() []byte {
var out [BLST_P1_SERIALIZE_EIP2537_BYTES]byte
C.blst_p1_affine_serialize_eip2537((*C.byte)(&out[0]), p1)
return out[:]
}

func (p1 *P1Affine) Deserialize(in []byte) *P1Affine {
if len(in) != BLST_P1_SERIALIZE_BYTES {
return nil
}
if C.blst_p1_deserialize(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
if len(in) == BLST_P1_SERIALIZE_BYTES {
if C.blst_p1_deserialize(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else if len(in) == BLST_P1_SERIALIZE_EIP2537_BYTES {
if C.blst_p1_deserialize_eip2537(p1, (*C.byte)(&in[0])) != C.BLST_SUCCESS {
return nil
}
} else {
return nil
}
return p1
Expand Down Expand Up @@ -137,6 +148,11 @@ func (p1 *P1) Serialize() []byte {
C.blst_p1_serialize((*C.byte)(&out[0]), p1)
return out[:]
}
func (p1 *P1) SerializeEip2537() []byte {
var out [BLST_P1_SERIALIZE_EIP2537_BYTES]byte
C.blst_p1_serialize_eip2537((*C.byte)(&out[0]), p1)
return out[:]
}
func (p1 *P1) Compress() []byte {
var out [BLST_P1_COMPRESS_BYTES]byte
C.blst_p1_compress((*C.byte)(&out[0]), p1)
Expand Down
2 changes: 1 addition & 1 deletion bindings/go/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def remap(fout, fin, mapping, dont_touch, removeImports):

# These are strings that overlap with the mapping names but we don't
# actually want to change. The second value should be a unique string.
dont_touch = (('Fp12', 'foo1234'),)
dont_touch = (('Fp12', 'foo1234'), ('2537', 'bar1234'))

# We're going to swap these names to get from min-pk to min-sig
mapping = [('P1', 'P2'),
Expand Down

0 comments on commit 9b570f2

Please sign in to comment.