@starkom/bluesky 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +12 -2
- package/contracts/BlueSky.sol +204 -0
- package/contracts/Poseidon1.sol +6049 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# BlueSky.sol
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/libernet-xyz/bluesky-sol/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@starkom/bluesky-sol)
|
|
5
|
+
[](https://github.com/libernet-xyz/bluesky-sol/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
The Ethereum smartcontracts in this module implement the [BlueSky prime field][bluesky] used in
|
|
10
|
+
Starkom and several Poseidon1 instances over the same field with state widths T=3, T=4, and T=5,
|
|
11
|
+
each with both the x^5 and the x^7 S-box.
|
|
12
|
+
|
|
13
|
+
[bluesky]: https://crates.io/crates/starkom-bluesky
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
pragma solidity ^0.8.34;
|
|
3
|
+
|
|
4
|
+
/// @title BlueSky
|
|
5
|
+
/// @notice Arithmetic library for the 255-bit BlueSky prime field.
|
|
6
|
+
/// @dev The prime is p = 0x7ffffffffffffffffffffffffffffffe0673ddf29e9b5547c000000000000001.
|
|
7
|
+
/// @dev All inputs and outputs are canonical integers in [0, p).
|
|
8
|
+
/// @dev Functions that call the `modexp` precompile (address 0x05) are marked `view` rather than
|
|
9
|
+
/// `pure`.
|
|
10
|
+
library BlueSky {
|
|
11
|
+
/// @dev Reverted when zero is passed to `inv`.
|
|
12
|
+
error ZeroDivisor();
|
|
13
|
+
|
|
14
|
+
/// @notice The BlueSky prime p.
|
|
15
|
+
uint256 constant P = 0x7ffffffffffffffffffffffffffffffe0673ddf29e9b5547c000000000000001;
|
|
16
|
+
|
|
17
|
+
/// @dev p - 2, the exponent used by Fermat's little theorem for inversion:
|
|
18
|
+
/// a^(p-2) ≡ a^(-1) (mod p) for any a ≠ 0.
|
|
19
|
+
uint256 constant P_MINUS_2 = 0x7ffffffffffffffffffffffffffffffe0673ddf29e9b5547bfffffffffffffff;
|
|
20
|
+
|
|
21
|
+
/// @dev (P+1)/2 mod P — the multiplicative inverse of 2.
|
|
22
|
+
uint256 constant TWO_INV = 0x3fffffffffffffffffffffffffffffff0339eef94f4daaa3e000000000000001;
|
|
23
|
+
|
|
24
|
+
/// @dev The primitive 2^62-th root of unity in the BlueSky field.
|
|
25
|
+
uint256 constant TWO_ADIC_ROOT_OF_UNITY =
|
|
26
|
+
0x2772569d549e1249ca6891eceba43568f6e0a747a2afe898b3977ca1a5bbfc9c;
|
|
27
|
+
|
|
28
|
+
/// @dev Inverse of the primitive 2^62-th root of unity.
|
|
29
|
+
uint256 constant TWO_ADIC_ROOT_OF_UNITY_INV =
|
|
30
|
+
0x76def406f046ef5ee7eeecd2c4e6ecd7cdedc4e2bcf6b19f1420121cd00b4cdb;
|
|
31
|
+
|
|
32
|
+
/// @dev The 2-adicity of P-1: P-1 = 2^62 * m for odd m.
|
|
33
|
+
uint256 constant S = 62;
|
|
34
|
+
|
|
35
|
+
/// @dev The additive identity.
|
|
36
|
+
uint256 constant ZERO = 0;
|
|
37
|
+
|
|
38
|
+
/// @dev The multiplicative identity.
|
|
39
|
+
uint256 constant ONE = 1;
|
|
40
|
+
|
|
41
|
+
/// @dev The largest field element, p - 1 (that is, -1).
|
|
42
|
+
uint256 constant MAX = 0x7ffffffffffffffffffffffffffffffe0673ddf29e9b5547c000000000000000;
|
|
43
|
+
|
|
44
|
+
/// @dev The multiplicative generator of the field, which is also a quadratic non-residue.
|
|
45
|
+
uint256 constant MULTIPLICATIVE_GENERATOR = 5;
|
|
46
|
+
|
|
47
|
+
/// @dev The generator raised to 2^S, i.e. 5^(2^62). It generates the subgroup of odd order
|
|
48
|
+
/// (P-1)/2^S.
|
|
49
|
+
uint256 constant DELTA = 0x232680e97f4b6251c95edefd145053d6dea23905b503a1002987caddeb54cdce;
|
|
50
|
+
|
|
51
|
+
/// @dev The exponent of the S-box x -> x^ALPHA used by the Poseidon hash over this field.
|
|
52
|
+
uint256 constant ALPHA = 5;
|
|
53
|
+
|
|
54
|
+
/// @dev The 3-adicity of P-1: P-1 = 3^39 * m for m not divisible by 3.
|
|
55
|
+
uint256 constant T = 39;
|
|
56
|
+
|
|
57
|
+
/// @dev The multiplicative inverse of 3.
|
|
58
|
+
uint256 constant THREE_INV = 0x55555555555555555555555555555554044d3ea1bf1238da8000000000000001;
|
|
59
|
+
|
|
60
|
+
/// @dev The primitive 3^39-th root of unity in the BlueSky field.
|
|
61
|
+
uint256 constant THREE_ADIC_ROOT_OF_UNITY =
|
|
62
|
+
0x1b6292b3a6f8a32da98705fee2d66e8fe35f48642a417a72f1a4ca414adfd9e6;
|
|
63
|
+
|
|
64
|
+
/// @dev Inverse of the primitive 3^39-th root of unity.
|
|
65
|
+
uint256 constant THREE_ADIC_ROOT_OF_UNITY_INV =
|
|
66
|
+
0x4d369d8e1ff761fca979a0ffb5545471ceae8164a047dff8b5679e11c5dfdf72;
|
|
67
|
+
|
|
68
|
+
/// @notice Returns the sum of two field elements.
|
|
69
|
+
/// @param a First operand, in [0, p).
|
|
70
|
+
/// @param b Second operand, in [0, p).
|
|
71
|
+
/// @return The field element (a + b) mod p.
|
|
72
|
+
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
73
|
+
return addmod(a, b, P);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// @notice Returns the difference of two field elements.
|
|
77
|
+
/// @param a Minuend, in [0, p).
|
|
78
|
+
/// @param b Subtrahend, in [0, p).
|
|
79
|
+
/// @return The field element (a - b) mod p.
|
|
80
|
+
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
81
|
+
// P - b is in (0, P] and addmod uses infinite-precision addition,
|
|
82
|
+
// so this is correct even when b == 0 (addmod(a, P, P) == a).
|
|
83
|
+
return addmod(a, P - b, P);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// @notice Returns the product of two field elements.
|
|
87
|
+
/// @param a First operand, in [0, p).
|
|
88
|
+
/// @param b Second operand, in [0, p).
|
|
89
|
+
/// @return The field element (a * b) mod p.
|
|
90
|
+
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
91
|
+
return mulmod(a, b, P);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/// @notice Returns the additive inverse of a field element.
|
|
95
|
+
/// @param a The operand, in [0, p).
|
|
96
|
+
/// @return The field element (-a) mod p.
|
|
97
|
+
function neg(uint256 a) internal pure returns (uint256) {
|
|
98
|
+
return a == 0 ? 0 : P - a;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// @notice Returns the square of a field element.
|
|
102
|
+
/// @param a The operand, in [0, p).
|
|
103
|
+
/// @return The field element a^2 mod p.
|
|
104
|
+
function square(uint256 a) internal pure returns (uint256) {
|
|
105
|
+
return mulmod(a, a, P);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// @notice Returns twice a field element.
|
|
109
|
+
/// @param a The operand, in [0, p).
|
|
110
|
+
/// @return The field element 2a mod p.
|
|
111
|
+
function double(uint256 a) internal pure returns (uint256) {
|
|
112
|
+
return addmod(a, a, P);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/// @notice Returns the cube of a field element.
|
|
116
|
+
/// @param a The operand, in [0, p).
|
|
117
|
+
/// @return The field element a^3 mod p.
|
|
118
|
+
function cube(uint256 a) internal pure returns (uint256) {
|
|
119
|
+
return mulmod(mulmod(a, a, P), a, P);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// @notice Returns a raised to an integer power.
|
|
123
|
+
/// @dev Calls the `modexp` precompile (EIP-198, address 0x05).
|
|
124
|
+
/// @dev Follows the precompile's convention: 0^0 = 1.
|
|
125
|
+
/// @param a The base, in [0, p).
|
|
126
|
+
/// @param exponent The exponent (any uint256; implicitly reduced mod p-1 by Fermat).
|
|
127
|
+
/// @return The field element a^exponent mod p.
|
|
128
|
+
function pow(uint256 a, uint256 exponent) internal view returns (uint256) {
|
|
129
|
+
return _modexp(a, exponent, P);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// @notice Returns the multiplicative inverse of a field element.
|
|
133
|
+
/// @dev Computed via Fermat's little theorem: a^(p-2) mod p. Calls the `modexp` precompile
|
|
134
|
+
/// (EIP-198, address 0x05).
|
|
135
|
+
/// @param a The operand, in [0, p). Must be non-zero.
|
|
136
|
+
/// @return The field element a^(-1) mod p.
|
|
137
|
+
function inv(uint256 a) internal view returns (uint256) {
|
|
138
|
+
if (a == 0) {
|
|
139
|
+
revert ZeroDivisor();
|
|
140
|
+
}
|
|
141
|
+
return _modexp(a, P_MINUS_2, P);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/// @notice Returns the quotient of two field elements.
|
|
145
|
+
/// @dev Calls the `modexp` precompile through `inv`.
|
|
146
|
+
/// @param a The dividend, in [0, p).
|
|
147
|
+
/// @param b The divisor, in [0, p). Must be non-zero.
|
|
148
|
+
/// @return The field element a * b^(-1) mod p.
|
|
149
|
+
function div(uint256 a, uint256 b) internal view returns (uint256) {
|
|
150
|
+
return mulmod(a, inv(b), P);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/// @notice Inverts all the provided values in place using Montgomery batch inversion.
|
|
154
|
+
/// @dev Costs a single call to the `modexp` precompile plus three multiplications per value,
|
|
155
|
+
/// instead of one precompile call per value. Reverts with `ZeroDivisor` if any value is zero.
|
|
156
|
+
/// @dev Only worth it for two or more values: for a single value it costs more than `inv`
|
|
157
|
+
/// because of the extra array allocation and loop. Measured with the optimizer enabled
|
|
158
|
+
/// under the Osaka gas schedule (EIP-7883 `modexp` pricing), where `inv` costs about 4.6k
|
|
159
|
+
/// gas: one value costs 5.4k with `invBatch` versus 4.6k with `inv`, while two values cost
|
|
160
|
+
/// 6.2k versus 9.2k. The break-even point is therefore N = 2, and the saving grows to about
|
|
161
|
+
/// 60% at N = 4 and about 80% at N = 256. Under the Prague schedule (EIP-2565 pricing) `inv`
|
|
162
|
+
/// costs about 1.9k gas, so the break-even is still N = 2 but the margin is thin there (3.5k
|
|
163
|
+
/// versus 3.8k). Results depend on the optimizer and on the chain's `modexp` pricing.
|
|
164
|
+
/// @param values The field elements to invert, each in [0, p). Overwritten with their inverses.
|
|
165
|
+
function invBatch(uint256[] memory values) internal view {
|
|
166
|
+
uint256 length = values.length;
|
|
167
|
+
if (length == 0) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
uint256[] memory partialProducts = new uint256[](length);
|
|
171
|
+
uint256 accumulator = 1;
|
|
172
|
+
for (uint256 i = 0; i < length; i++) {
|
|
173
|
+
partialProducts[i] = accumulator;
|
|
174
|
+
accumulator = mulmod(accumulator, values[i], P);
|
|
175
|
+
}
|
|
176
|
+
uint256 inverse = inv(accumulator);
|
|
177
|
+
for (uint256 i = length; i > 0; i--) {
|
|
178
|
+
uint256 input = values[i - 1];
|
|
179
|
+
values[i - 1] = mulmod(partialProducts[i - 1], inverse, P);
|
|
180
|
+
inverse = mulmod(inverse, input, P);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// @dev Calls the `modexp` precompile (EIP-198) for 256-bit inputs.
|
|
185
|
+
function _modexp(
|
|
186
|
+
uint256 base,
|
|
187
|
+
uint256 exponent,
|
|
188
|
+
uint256 modulus
|
|
189
|
+
) internal view returns (uint256 result) {
|
|
190
|
+
assembly {
|
|
191
|
+
let ptr := mload(0x40)
|
|
192
|
+
mstore(ptr, 0x20)
|
|
193
|
+
mstore(add(ptr, 0x20), 0x20)
|
|
194
|
+
mstore(add(ptr, 0x40), 0x20)
|
|
195
|
+
mstore(add(ptr, 0x60), base)
|
|
196
|
+
mstore(add(ptr, 0x80), exponent)
|
|
197
|
+
mstore(add(ptr, 0xa0), modulus)
|
|
198
|
+
if iszero(staticcall(gas(), 5, ptr, 0xc0, ptr, 0x20)) {
|
|
199
|
+
revert(0, 0)
|
|
200
|
+
}
|
|
201
|
+
result := mload(ptr)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|