-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AIP-20][Discussion] Generic Cryptography Algebra and BLS12-381 Implementation #94
Comments
That sounds gas heavy, I wont use this until it able to inline on any function.
if you could native function these thing, and make sure low execution gas fee, I could may use these. |
@e99243506bigplay In general, cryptographic operations are computationally expensive. E.g. the overhead of a bls12381 pairing is currently equivalent to ~300,000,000 units of internal gas (the unit of measurement your numbers are using). I don't think the intrinsic move costs you listed above will be your biggest concern (if by "inline" you mean to define functions like |
I agree that a special discount for the gas cost will make those cryptographic operations in stdlib more useful, if needed. |
@e99243506bigplay, can you expand more on why you think this is too gas heavy? Here's what I'm thinking:
|
I don't get you, and I dont good at mathematic. if every operation like (add mul div sub pow sqrt) has be to call a function to done it, the sum of equation won't result at least 4x than inlined version of gas fee?
Results:
Did you see line of 0 to 23 that not necessary instruction? That is the inlined move function looks like.
Personally I like to do same thing without not necessary instructions and functions to result submitted transaction with less gas fee. |
I am not sure I fully follow your concerns. If you are trying to say that there is gas overhead involved in calling a native function, yes, there is. Unfortunately, there is no other easier way for us to implement complex cryptographic operations. It seems like you are proposing to implement these operations directly as Move bytecode. That too would be very expensive & very complex to implement. |
eh, no, don't implement stuff in Move bytecode, I wish there is WASM so I could implement some native function on WASM, then share value to MoveVM. |
AIP Discussion
Discussion and feedback thread for AIP.
Link to AIP: #86
Summary
This AIP proposes the support of generic cryptography algebra operations in Aptos standard library.
The initial list of the supported generic operations includes group/field element serialization/deserialization, basic arithmetic, pairing, hash-to-structure, casting.
The initial list of supported algebraic structures includes groups/fields used in BLS12-381, a popular pairing-friendly curve as described here.
Either the operation list or the structure list can be extended by future AIPs.
Motivation
Algebraic structures are fundamental building blocks for many cryptographic schemes, but also hard to implement efficiently in pure Move.
This change should allow Move developers to implement generic constructions of those schemes, then get different instantiations by only switching the type parameter(s).
For example, if BLS12-381 groups and BN254 groups are supported, one can implement a generic Groth16 proof verifier construction, then be able to use both BLS12-381-based Groth16 proof verifier and BN254-based Groth16 proof verifier.
BLS12-381-based Groth16 proof verifier has been implemented this way as part of the reference implementation.
Rationale
An alternative non-generic approach is to expose instantiated schemes directly in aptos_stdlib.
For example, we can define a Groth16 proof verification function
0x1::groth16_<curve>::verify_proof(vk, proof, public_inputs): bool
for every pairing-friendly elliptic curve
<curve>
.For ECDSA signatures which require a hash function and a group, we can define
0x1::ecdsa_<hash>_<group>::verify_signature(pk, msg, sig):bool
for each pair of proper hash function
<hash>
and group<group>
.Compared with the proposed approach, the alternative approach saves the work of constructing the schemes for Move developers. However, the size of aptos_stdlib can multiply too fast in the future.
Furthermore, the non-generic approach is not scalable from a development standpoint: a new native is needed for every combination of cryptosystem and its underlying algebraic structure (e.g., elliptic curve).
To keep the Aptos stdlib concise while still covering as many use cases as possible, the proposed generic approach should be chosen over the alternative approach.
Specifications
Generic Operations
Structs and Functions
Module
aptos_std::crypto_algebra
is designed to have the following definitions.Element<S>
that represents an element of algebraic structureS
.Below is the full specification in pseudo-Move.
In general, every structure implements basic operations like (de)serialization, equality check, random sampling.
For example, A group may also implement the following operations. (Additive notions are used.)
order()
for getting the group order.zero()
for getting the group identity.one()
for getting the group generator (if exists).neg()
for group element inversion.add()
for basic group operation.sub()
for group element subtraction.double()
for efficient group element doubling.scalar_mul()
for group scalar multiplication.multi_scalar_mul()
for efficient group multi-scalar multiplication.hash_to()
for hash-to-group.As another example, a field may also implement the following operations.
order()
for getting the field order.zero()
for the field additive identity.one()
for the field multiplicative identity.add()
for field addition.sub()
for field subtraction.mul()
for field multiplication.div()
for field division.neg()
for field negation.inv()
for field inversion.sqr()
for efficient field element squaring.from_u64()
for quick conversion from u64 to field element.Similarly, for 3 groups
G1
,G2
,Gt
that admit a bilinear map,pairing<G1, G2, Gt>()
andmulti_pairing<G1, G2, Gt>()
may be implemented.For a subset/superset relationship between 2 structures,
upcast()
anddowncast()
may be implemented.E.g., in BLS12-381
Gt
is a multiplicative subgroup fromFq12
so upcasting fromGt
toFq12
and downcasting fromFq12
toGt
can be supported.Shared Scalar Fields
Some groups share the same group order,
and an ergonomic design for this is to
allow multiple groups to share the same scalar field
(mainly for the purpose of scalar multiplication),
if they have the same order.
In other words, the following should be supported.
Handling Incorrect Type Parameter(s)
There is currently no easy way to ensure type safety for the generic operations.
E.g.,
pairing<A,B,C>(a,b,c)
can compile even if groupsA
,B
andC
do not admin a pairing.Therefore, the backend should handle the type checks at runtime.
For example, if a group operation that takes 2+ type parameters is invoked with incompatible type parameters, it must abort.
For example,
scalar_mul<GroupA, ScalarB>()
whereGroupA
andScalarB
have different orders, will abort with a “not implemented” error.Invoking operation functions with user-defined types should also abort with a “not implemented” error.
For example,
zero<std::option::Option<u64>>()
will abort.Implementation of BLS12-381 structures
To support a wide-enough variety of BLS12-381 operations using the
aptos_std::crypto_algebra
API,we implement several marker types for the relevant groups of order
r
(forG1
,G2
andGt
) and fields (e.g.,Fr
,Fq12
).We also implement marker types for popular serialization formats and hash-to-group suites.
Below, we describe all possible marker types we could implement for BLS12-381
and mark the ones that we actually implement as "implemented".
These, we believe, should be sufficient to support most BLS12-381 applications.
Fq
The finite field$F_q$ used in BLS12-381 curves with a prime order $q$ equal to
0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab.
FormatFqLsb
A serialization format for
Fq
elements,where an element is represented by a byte array
b[]
of size 48 with the least significant byte (LSB) coming first.FormatFqMsb
A serialization format for
Fq
elements,where an element is represented by a byte array
b[]
of size 48 with the most significant byte (MSB) coming first.Fq2
The finite field$F_{q^2}$ used in BLS12-381 curves,$F_{q^2}=F_q[u]/(u^2+1)$ .
which is an extension field of
Fq
, constructed asFormatFq2LscLsb
A serialization format for$(c_0+c_1\cdot u)$ is represented by a byte array
Fq2
elements,where an element in the form
b[]
of size 96,which is a concatenation of its coefficients serialized, with the least significant coefficient (LSC) coming first:
b[0..48]
isFormatFqLsb
.b[48..96]
isFormatFqLsb
.FormatFq2MscMsb
A serialization format for$(c_0+c_1\cdot u)$ is represented by a byte array
Fq2
elements,where an element in the form
b[]
of size 96,which is a concatenation of its coefficients serialized, with the most significant coefficient (MSC) coming first:
b[0..48]
isFormatFqLsb
.b[48..96]
isFormatFqLsb
.Fq6
The finite field$F_{q^6}$ used in BLS12-381 curves,$F_{q^6}=F_{q^2}[v]/(v^3-u-1)$ .
which is an extension field of
Fq2
, constructed asFormatFq6LscLsb
A serialization scheme for$(c_0+c_1\cdot v+c_2\cdot v^2)$ is represented by a byte array
Fq6
elements,where an element in the form
b[]
of size 288,which is a concatenation of its coefficients serialized, with the least significant coefficient (LSC) coming first:
b[0..96]
isFormatFq2LscLsb
.b[96..192]
isFormatFq2LscLsb
.b[192..288]
isFormatFq2LscLsb
.Fq12
(implemented)The finite field$F_{q^12}$ used in BLS12-381 curves,$F_{q^12}=F_{q^6}[w]/(w^2-v)$ .
which is an extension field of
Fq6
, constructed asFormatFq12LscLsb
(implemented)A serialization scheme for$(c_0+c_1\cdot w)$ is represented by a byte array
Fq12
elements,where an element
b[]
of size 576,which is a concatenation of its coefficients serialized, with the least significant coefficient (LSC) coming first.
b[0..288]
isFormatFq6LscLsb
.b[288..576]
isFormatFq6LscLsb
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
G1Full
A group constructed by the points on the BLS12-381 curve$E(F_q): y^2=x^3+4$ and the point at infinity,$G_1$ used in pairing.
under the elliptic curve point addition.
It contains the prime-order subgroup
G1
(implemented)The group$G_1$ in BLS12-381-based pairing $G_1 \times G_2 \rightarrow G_t$ .$r$
It is a subgroup of
G1Full
with a prime orderequal to 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001.
(so
Fr
is the associated scalar field).FormatG1Uncompr
(implemented)A serialization scheme for
G1
elements derived from https://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#name-zcash-serialization-format-.Below is the serialization procedure that takes a
G1
elementp
and outputs a byte array of size 96.(x,y)
be the coordinates ofp
ifp
is on the curve, or(0,0)
otherwise.x
andy
intob_x[]
andb_y[]
respectively usingFormatFqMsb
.b_x[]
andb_y[]
intob[]
.p
is the point at infinity, set the infinity bit:b[0]: = b[0] | 0x40
.b[]
.Below is the deserialization procedure that takes a byte array
b[]
and outputs either aG1
element or none.b[]
is not 96, return none.b[0] & 0x80 != 0
.b[0] & 0x40 != 0
.[b[0] & 0x1f, b[1], ..., b[47]]
tox
usingFormatFqMsb
. Ifx
is none, return none.[b[48], ..., b[95]]
toy
usingFormatFqMsb
. Ify
is none, return none.(x,y)
is on curveE
. If not, return none.(x,y)
is in the subgroup of orderr
. If not, return none.(x,y)
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
FormatG1Compr
(implemented)A serialization scheme for
G1
elements derived from https://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#name-zcash-serialization-format-.Below is the serialization procedure that takes a
G1
elementp
and outputs a byte array of size 48.(x,y)
be the coordinates ofp
ifp
is on the curve, or(0,0)
otherwise.x
intob[]
usingFormatFqMsb
.b[0] := b[0] | 0x80
.p
is the point at infinity, set the infinity bit:b[0]: = b[0] | 0x40
.y > -y
, set the lexicographical flag:b[0] := b[0] | 0x20
.b[]
.Below is the deserialization procedure that takes a byte array
b[]
and outputs either aG1
element or none.b[]
is not 48, return none.b[0] & 0x80 != 0
.b[0] & 0x40 != 0
.b[0] & 0x20 != 0
.[b[0] & 0x1f, b[1], ..., b[47]]
tox
usingFormatFqMsb
. Ifx
is none, return none.x
fory
. If no suchy
exists, return none.y'
bemax(y,-y)
if the lexicographical flag is set, ormin(y,-y)
otherwise.(x,y')
is in the subgroup of orderr
. If not, return none.(x,y')
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
G2Full
A group constructed by the points on a curve$E'(F_{q^2}): y^2=x^3+4(u+1)$ and the point at infinity,$G_2$ used in pairing.
under the elliptic curve point addition.
It contains the prime-order subgroup
G2
(implemented)The group$G_2$ in BLS12-381-based pairing $G_1 \times G_2 \rightarrow G_t$ .$r$ equal to
It is a subgroup of
G2Full
with a prime order0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001.
(so
Fr
is the scalar field).FormatG2Uncompr
(implemented)A serialization scheme for
G2
elements derived fromhttps://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#name-zcash-serialization-format-.
Below is the serialization procedure that takes a
G2
elementp
and outputs a byte array of size 192.(x,y)
be the coordinates ofp
ifp
is on the curve, or(0,0)
otherwise.x
andy
intob_x[]
andb_y[]
respectively usingFormatFq2MscMsb
.b_x[]
andb_y[]
intob[]
.p
is the point at infinity, set the infinity bit inb[]
:b[0]: = b[0] | 0x40
.b[]
.Below is the deserialization procedure that takes a byte array
b[]
and outputs either aG2
element or none.b[]
is not 192, return none.b[0] & 0x80 != 0
.b[0] & 0x40 != 0
.[b[0] & 0x1f, ..., b[95]]
tox
usingFormatFq2MscMsb
. Ifx
is none, return none.[b[96], ..., b[191]]
toy
usingFormatFq2MscMsb
. Ify
is none, return none.(x,y)
is on the curveE'
. If not, return none.(x,y)
is in the subgroup of orderr
. If not, return none.(x,y)
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
FormatG2Compr
(implemented)A serialization scheme for
G2
elements derived fromhttps://www.ietf.org/archive/id/draft-irtf-cfrg-pairing-friendly-curves-11.html#name-zcash-serialization-format-.
Below is the serialization procedure that takes a
G2
elementp
and outputs a byte array of size 96.(x,y)
be the coordinates ofp
ifp
is on the curve, or(0,0)
otherwise.x
intob[]
usingFormatFq2MscMsb
.b[0] := b[0] | 0x80
.p
is the point at infinity, set the infinity bit:b[0]: = b[0] | 0x40
.y > -y
, set the lexicographical flag:b[0] := b[0] | 0x20
.b[]
.Below is the deserialization procedure that takes a byte array
b[]
and outputs either aG2
element or none.b[]
is not 96, return none.b[0] & 0x80 != 0
.b[0] & 0x40 != 0
.b[0] & 0x20 != 0
.[b[0] & 0x1f, b[1], ..., b[95]]
tox
usingFormatFq2MscMsb
. Ifx
is none, return none.x
fory
. If no suchy
exists, return none.y'
bemax(y,-y)
if the lexicographical flag is set, ormin(y,-y)
otherwise.(x,y')
is in the subgroup of orderr
. If not, return none.(x,y')
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
Gt
(implemented)The group$G_t$ in BLS12-381-based pairing $G_1 \times G_2 \rightarrow G_t$ .$r$ equal to 0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001.
It is a multiplicative subgroup of
Fq12
,with a prime order
(so
Fr
is the scalar field).The identity of
Gt
is 1.FormatGt
(implemented)A serialization scheme for
Gt
elements.To serialize, it treats a
Gt
elementp
as anFq12
element and serialize it usingFormatFq12LscLsb
.To deserialize, it uses
FormatFq12LscLsb
to try deserializing to anFq12
element then test the membership inGt
.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0.
Fr
(implemented)The finite field$F_r$ that can be used as the scalar fields$G_1$ , $G_2$ , $G_t$ in BLS12-381-based pairing.
associated with the groups
FormatFrLsb
(implemented)A serialization format for
Fr
elements,where an element is represented by a byte array
b[]
of size 32 with the least significant byte (LSB) coming first.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0, blst-0.3.7.
FormatFrMsb
(implemented)A serialization scheme for
Fr
elements,where an element is represented by a byte array
b[]
of size 32 with the most significant byte (MSB) coming first.NOTE: other implementation(s) using this format: ark-bls12-381-0.4.0, blst-0.3.7.
HashG1XmdSha256SswuRo
(implemented)The hash-to-curve suite
BLS12381G1_XMD:SHA-256_SSWU_RO_
that hashes a byte array intoG1
elements.Full specification is defined in https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#name-bls12-381-g1.
HashG2XmdSha256SswuRo
(implemented)The hash-to-curve suite
BLS12381G2_XMD:SHA-256_SSWU_RO_
that hashes a byte array intoG2
elements.Full specification is defined in https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-16#name-bls12-381-g2.
Reference Implementation
https://github.com/aptos-labs/aptos-core/pull/6550/files
Risks and Drawbacks
Developing cryptographic schemes, whether in Move or in any other language, is very difficult due to the inherent mathematic complexity of such schemes, as well as the difficulty of using cryptographic libraries securely.
As a result, we caution Move application developers that
implementing cryptographic schemes using
crypto_algebra.move
and/or thebls12381_algebra.move
modules will be error prone and could result in vulnerable applications.That being said, the
crypto_algebra.move
and thebls12381_algebra.move
Move modules have been designed with safety in mind.First, we offer a minimal, hard-to-misuse abstraction for algebraic structures like groups and fields.
Second, our Move modules are type safe (e.g., inversion in a group G returns an Option).
Third, our BLS12-381 implementation always performs prime-order subgroup checks when deserializing group elements, to avoid serious implementation bugs.
Future Potential
The
crypto_algebra.move
Move module can be extended to support more structures (e.g., new elliptic curves) and operations (e.g., batch inversion of field elements), This will:Once the Move language is upgraded with support for some kind of interfaces,
it can be use to rewrite the Move side specifications to ensure type safety at compile time.
Suggested implementation timeline
The change should be available on devnet in April 2023.
The text was updated successfully, but these errors were encountered: