Skip to content
On this page

BLS12-381

BLS12-381 is a paring-friendly elliptic curve.

Types

Scalar field Fr

An element of the scalar field Fr, used for scalar multiplication on the BLS12-381 curves G1 and G2. Written as raw bytes, using a little-endian encoding.

Curve Points

Points on the BLS12-381 curves G1 and G2, respectively. Written as raw bytes, using a big-endian point encoding, as specified here.

Operations

Convert to int

sp.to_int(x: sp.bls12_381_fr) → sp.int

Convert a sp.bls12_381_fr value to sp.int.

Example

smartpy
fr = sp.bls12_381_fr("0x2ef123703093cbbbd124e15f2054fa5781ed0b8d092ec3c6e5d76b4ca918a221")

x = sp.to_int(fr)

Negation

x: sp.TBls12_381_g1 → sp.TBls12_381_g1
x: sp.TBls12_381_g2 → sp.TBls12_381_g2
x: sp.TBls12_381_fr → sp.TBls12_381_fr

Negate a curve point or field element.

Example

smartpy
g1 = sp.bls12_381_g1("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1")
g2 = sp.bls12_381_g2("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801")
fr = sp.bls12_381_fr("0x2ef123703093cbbbd124e15f2054fa5781ed0b8d092ec3c6e5d76b4ca918a221")

negatedG1 = - g1
negatedG2 = - g2
negatedFr = - fr

Addition

a: sp.TBls12_381_g1  b: sp.TBls12_381_g1 → sp.TBls12_381_g1
a: sp.TBls12_381_g2  b: sp.TBls12_381_g2 → sp.TBls12_381_g2
a: sp.TBls12_381_fr  b: sp.TBls12_381_fr → sp.TBls12_381_fr

Add two curve points or field elements.

Example

smartpy
g1 = sp.bls12_381_g1("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1")
g2 = sp.bls12_381_g2("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801")
fr = sp.bls12_381_fr("0x2ef123703093cbbbd124e15f2054fa5781ed0b8d092ec3c6e5d76b4ca918a221")

resultG1 = g1 + g1
resultG2 = g2 + g2
resultFr = fr + fr

Multiplication

sp.mul(a: sp.bls12_381_g1, b: bls12_381_fr) → sp.bls12_381_g1
sp.mul(a: sp.bls12_381_g2, b: bls12_381_fr) → sp.bls12_381_g2
sp.mul(a: sp.bls12_381_fr, b: bls12_381_fr) → sp.bls12_381_fr
sp.mul(a: sp.nat, b: bls12_381_fr) → sp.bls12_381_fr
sp.mul(a: sp.int, b: bls12_381_fr) → sp.bls12_381_fr

Multiply a curve point or field element by a scalar field element.

Example

smartpy
g1 = sp.bls12_381_g1("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1")
g2 = sp.bls12_381_g2("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801")
fr = sp.bls12_381_fr("0x2ef123703093cbbbd124e15f2054fa5781ed0b8d092ec3c6e5d76b4ca918a221")

resultG1 = sp.mul(g1, fr)
resultG2 = sp.mul(g2, fr)
resultFr1 = sp.mul(fr, fr)
resultFr2 = sp.mul(sp.nat(1), fr)
resultFr3 = sp.mul(sp.int(1), fr)

Pairing check

sp.pairing_check(p: sp.list[sp.pair[sp.bls12_381_g1, sp.bls12_381_g2]]) → sp.bool

Verify that the product of pairings of the given list of points is equal to 1 in Fq12. Returns true if the list is empty.

Example

smartpy
listOfPairs = [
    (
        sp.bls12_381_g1("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1"),
        sp.bls12_381_g2("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801")
    ),
    (
        sp.bls12_381_g1("0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1"),
        sp.bls12_381_g2("0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb80606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801")
    )
]

isValid = sp.pairing_check(listOfPairs)