Skip to content

Commit

Permalink
Uncompressed G1 group elements (#19684)
Browse files Browse the repository at this point in the history
## Description 

Currently, group elements are represented in compressed form and must be
decompressed before each operation. This is a rather expensive operation
to do, in particular if many elements are used.

This PR introduces a new uncompressed representation of BLS12381-G1
elements and a function to add a vector of uncompressed elements. For
500 terms, computing a sum will be about 100x faster to compute than
using compressed representation.

## Test plan 

Unit tests.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [x] Protocol: Introduce an uncompressed representation of BLS12381-G1
group elements for faster addition of group elements.
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:

---------

Co-authored-by: benr-ml <[email protected]>
  • Loading branch information
jonas-lj and benr-ml authored Oct 21, 2024
1 parent 4e79cd1 commit df0cb67
Show file tree
Hide file tree
Showing 18 changed files with 1,607 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ move-abstract-interpreter = { path = "external-crates/move/crates/move-abstract-
move-abstract-stack = { path = "external-crates/move/crates/move-abstract-stack" }
move-analyzer = { path = "external-crates/move/crates/move-analyzer" }

fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "c050ffc78b93739328af5d59b05f90e0e26b1b7e" }
fastcrypto-tbls = { git = "https://github.com/MystenLabs/fastcrypto", rev = "c050ffc78b93739328af5d59b05f90e0e26b1b7e" }
fastcrypto-zkp = { git = "https://github.com/MystenLabs/fastcrypto", rev = "c050ffc78b93739328af5d59b05f90e0e26b1b7e", package = "fastcrypto-zkp" }
fastcrypto-vdf = { git = "https://github.com/MystenLabs/fastcrypto", rev = "c050ffc78b93739328af5d59b05f90e0e26b1b7e", features = [
fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "2f502fd8570fe4e9cff36eea5bbd6fef22002898" }
fastcrypto-tbls = { git = "https://github.com/MystenLabs/fastcrypto", rev = "2f502fd8570fe4e9cff36eea5bbd6fef22002898" }
fastcrypto-zkp = { git = "https://github.com/MystenLabs/fastcrypto", rev = "2f502fd8570fe4e9cff36eea5bbd6fef22002898", package = "fastcrypto-zkp" }
fastcrypto-vdf = { git = "https://github.com/MystenLabs/fastcrypto", rev = "2f502fd8570fe4e9cff36eea5bbd6fef22002898", features = [
"experimental",
] }
passkey-types = { version = "0.2.0" }
Expand Down
117 changes: 117 additions & 0 deletions crates/sui-framework/docs/sui-framework/bls12381.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Group operations of BLS12-381.
- [Struct `G1`](#0x2_bls12381_G1)
- [Struct `G2`](#0x2_bls12381_G2)
- [Struct `GT`](#0x2_bls12381_GT)
- [Struct `UncompressedG1`](#0x2_bls12381_UncompressedG1)
- [Constants](#@Constants_0)
- [Function `bls12381_min_sig_verify`](#0x2_bls12381_bls12381_min_sig_verify)
- [Function `bls12381_min_pk_verify`](#0x2_bls12381_bls12381_min_pk_verify)
Expand All @@ -32,6 +33,7 @@ Group operations of BLS12-381.
- [Function `g1_neg`](#0x2_bls12381_g1_neg)
- [Function `hash_to_g1`](#0x2_bls12381_hash_to_g1)
- [Function `g1_multi_scalar_multiplication`](#0x2_bls12381_g1_multi_scalar_multiplication)
- [Function `g1_to_uncompressed_g1`](#0x2_bls12381_g1_to_uncompressed_g1)
- [Function `g2_from_bytes`](#0x2_bls12381_g2_from_bytes)
- [Function `g2_identity`](#0x2_bls12381_g2_identity)
- [Function `g2_generator`](#0x2_bls12381_g2_generator)
Expand All @@ -50,6 +52,8 @@ Group operations of BLS12-381.
- [Function `gt_div`](#0x2_bls12381_gt_div)
- [Function `gt_neg`](#0x2_bls12381_gt_neg)
- [Function `pairing`](#0x2_bls12381_pairing)
- [Function `uncompressed_g1_to_g1`](#0x2_bls12381_uncompressed_g1_to_g1)
- [Function `uncompressed_g1_sum`](#0x2_bls12381_uncompressed_g1_sum)


<pre><code><b>use</b> <a href="group_ops.md#0x2_group_ops">0x2::group_ops</a>;
Expand Down Expand Up @@ -149,6 +153,33 @@ Group operations of BLS12-381.



<details>
<summary>Fields</summary>


<dl>
<dt>
<code>dummy_field: bool</code>
</dt>
<dd>

</dd>
</dl>


</details>

<a name="0x2_bls12381_UncompressedG1"></a>

## Struct `UncompressedG1`



<pre><code><b>struct</b> <a href="bls12381.md#0x2_bls12381_UncompressedG1">UncompressedG1</a>
</code></pre>



<details>
<summary>Fields</summary>

Expand Down Expand Up @@ -278,6 +309,15 @@ Group operations of BLS12-381.



<a name="0x2_bls12381_UNCOMPRESSED_G1_TYPE"></a>



<pre><code><b>const</b> <a href="bls12381.md#0x2_bls12381_UNCOMPRESSED_G1_TYPE">UNCOMPRESSED_G1_TYPE</a>: u8 = 4;
</code></pre>



<a name="0x2_bls12381_bls12381_min_sig_verify"></a>

## Function `bls12381_min_sig_verify`
Expand Down Expand Up @@ -835,6 +875,31 @@ Aborts with <code>EInputTooLong</code> if the vectors are larger than 32 (may in



</details>

<a name="0x2_bls12381_g1_to_uncompressed_g1"></a>

## Function `g1_to_uncompressed_g1`

Convert an <code>Element&lt;<a href="bls12381.md#0x2_bls12381_G1">G1</a>&gt;</code> to uncompressed form.


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_g1_to_uncompressed_g1">g1_to_uncompressed_g1</a>(e: &<a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_G1">bls12381::G1</a>&gt;): <a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">bls12381::UncompressedG1</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_g1_to_uncompressed_g1">g1_to_uncompressed_g1</a>(e: &Element&lt;<a href="bls12381.md#0x2_bls12381_G1">G1</a>&gt;): Element&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">UncompressedG1</a>&gt; {
<a href="group_ops.md#0x2_group_ops_convert">group_ops::convert</a>(<a href="bls12381.md#0x2_bls12381_G1_TYPE">G1_TYPE</a>, <a href="bls12381.md#0x2_bls12381_UNCOMPRESSED_G1_TYPE">UNCOMPRESSED_G1_TYPE</a>, e)
}
</code></pre>



</details>

<a name="0x2_bls12381_g2_from_bytes"></a>
Expand Down Expand Up @@ -1280,4 +1345,56 @@ Returns e2 / e1, fails if scalar is zero.



</details>

<a name="0x2_bls12381_uncompressed_g1_to_g1"></a>

## Function `uncompressed_g1_to_g1`

UncompressedG1 group operations ///
Create a <code>Element&lt;<a href="bls12381.md#0x2_bls12381_G1">G1</a>&gt;</code> from its uncompressed form.


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_uncompressed_g1_to_g1">uncompressed_g1_to_g1</a>(e: &<a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">bls12381::UncompressedG1</a>&gt;): <a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_G1">bls12381::G1</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_uncompressed_g1_to_g1">uncompressed_g1_to_g1</a>(e: &Element&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">UncompressedG1</a>&gt;): Element&lt;<a href="bls12381.md#0x2_bls12381_G1">G1</a>&gt; {
<a href="group_ops.md#0x2_group_ops_convert">group_ops::convert</a>(<a href="bls12381.md#0x2_bls12381_UNCOMPRESSED_G1_TYPE">UNCOMPRESSED_G1_TYPE</a>, <a href="bls12381.md#0x2_bls12381_G1_TYPE">G1_TYPE</a>, e)
}
</code></pre>



</details>

<a name="0x2_bls12381_uncompressed_g1_sum"></a>

## Function `uncompressed_g1_sum`

Compute the sum of a list of uncompressed elements.
This is significantly faster and cheaper than summing the elements.


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_uncompressed_g1_sum">uncompressed_g1_sum</a>(terms: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">bls12381::UncompressedG1</a>&gt;&gt;): <a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">bls12381::UncompressedG1</a>&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="bls12381.md#0x2_bls12381_uncompressed_g1_sum">uncompressed_g1_sum</a>(terms: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;Element&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">UncompressedG1</a>&gt;&gt;): Element&lt;<a href="bls12381.md#0x2_bls12381_UncompressedG1">UncompressedG1</a>&gt; {
<a href="group_ops.md#0x2_group_ops_sum">group_ops::sum</a>(<a href="bls12381.md#0x2_bls12381_UNCOMPRESSED_G1_TYPE">UNCOMPRESSED_G1_TYPE</a>, terms)
}
</code></pre>



</details>
96 changes: 96 additions & 0 deletions crates/sui-framework/docs/sui-framework/group_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Generic Move and native functions for group operations.
- [Function `hash_to`](#0x2_group_ops_hash_to)
- [Function `multi_scalar_multiplication`](#0x2_group_ops_multi_scalar_multiplication)
- [Function `pairing`](#0x2_group_ops_pairing)
- [Function `convert`](#0x2_group_ops_convert)
- [Function `sum`](#0x2_group_ops_sum)
- [Function `internal_validate`](#0x2_group_ops_internal_validate)
- [Function `internal_add`](#0x2_group_ops_internal_add)
- [Function `internal_sub`](#0x2_group_ops_internal_sub)
Expand All @@ -25,6 +27,8 @@ Generic Move and native functions for group operations.
- [Function `internal_hash_to`](#0x2_group_ops_internal_hash_to)
- [Function `internal_multi_scalar_mul`](#0x2_group_ops_internal_multi_scalar_mul)
- [Function `internal_pairing`](#0x2_group_ops_internal_pairing)
- [Function `internal_convert`](#0x2_group_ops_internal_convert)
- [Function `internal_sum`](#0x2_group_ops_internal_sum)
- [Function `set_as_prefix`](#0x2_group_ops_set_as_prefix)


Expand Down Expand Up @@ -364,6 +368,54 @@ Aborts with <code><a href="group_ops.md#0x2_group_ops_EInputTooLong">EInputTooLo



</details>

<a name="0x2_group_ops_convert"></a>

## Function `convert`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="group_ops.md#0x2_group_ops_convert">convert</a>&lt;From, To&gt;(from_type_: u8, to_type_: u8, e: &<a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;From&gt;): <a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;To&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<a href="package.md#0x2_package">package</a>) <b>fun</b> <a href="group_ops.md#0x2_group_ops_convert">convert</a>&lt;From, To&gt;(from_type_: u8, to_type_: u8, e: &<a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;From&gt;): <a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;To&gt; {
<a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;To&gt; { bytes: <a href="group_ops.md#0x2_group_ops_internal_convert">internal_convert</a>(from_type_, to_type_, &e.bytes) }
}
</code></pre>



</details>

<a name="0x2_group_ops_sum"></a>

## Function `sum`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="group_ops.md#0x2_group_ops_sum">sum</a>&lt;G&gt;(type_: u8, terms: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;G&gt;&gt;): <a href="group_ops.md#0x2_group_ops_Element">group_ops::Element</a>&lt;G&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<a href="package.md#0x2_package">package</a>) <b>fun</b> <a href="group_ops.md#0x2_group_ops_sum">sum</a>&lt;G&gt;(type_: u8, terms: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;G&gt;&gt;): <a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;G&gt; {
<a href="group_ops.md#0x2_group_ops_Element">Element</a>&lt;G&gt; { bytes: <a href="group_ops.md#0x2_group_ops_internal_sum">internal_sum</a>(type_, &(*terms).map!(|x| x.bytes)) }
}
</code></pre>



</details>

<a name="0x2_group_ops_internal_validate"></a>
Expand Down Expand Up @@ -544,6 +596,50 @@ Aborts with <code><a href="group_ops.md#0x2_group_ops_EInputTooLong">EInputTooLo



</details>

<a name="0x2_group_ops_internal_convert"></a>

## Function `internal_convert`



<pre><code><b>fun</b> <a href="group_ops.md#0x2_group_ops_internal_convert">internal_convert</a>(from_type_: u8, to_type_: u8, e: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="group_ops.md#0x2_group_ops_internal_convert">internal_convert</a>(from_type_: u8, to_type_: u8, e: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;): <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

<a name="0x2_group_ops_internal_sum"></a>

## Function `internal_sum`



<pre><code><b>fun</b> <a href="group_ops.md#0x2_group_ops_internal_sum">internal_sum</a>(type_: u8, e: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;): <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>native</b> <b>fun</b> <a href="group_ops.md#0x2_group_ops_internal_sum">internal_sum</a>(type_: u8, e: &<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;<a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;&gt;): <a href="../move-stdlib/vector.md#0x1_vector">vector</a>&lt;u8&gt;;
</code></pre>



</details>

<a name="0x2_group_ops_set_as_prefix"></a>
Expand Down
Loading

0 comments on commit df0cb67

Please sign in to comment.