|
1 | 1 | #pragma once
|
2 | 2 |
|
3 | 3 | #include "../bigfield/bigfield.hpp"
|
| 4 | +#include "../bigfield/goblin_field.hpp" |
4 | 5 | #include "../byte_array/byte_array.hpp"
|
5 | 6 | #include "../circuit_builders/circuit_builders_fwd.hpp"
|
6 | 7 | #include "../field/field.hpp"
|
|
9 | 10 | #include "barretenberg/ecc/curves/bn254/g1.hpp"
|
10 | 11 | #include "barretenberg/ecc/curves/secp256k1/secp256k1.hpp"
|
11 | 12 | #include "barretenberg/ecc/curves/secp256r1/secp256r1.hpp"
|
| 13 | +#include "barretenberg/stdlib/primitives/biggroup/biggroup_goblin.hpp" |
12 | 14 |
|
13 |
| -// TODO(https://github.com/AztecProtocol/barretenberg/issues/707) If using a a circuit builder with Goblin, which is |
14 |
| -// designed to have efficient bb::g1 operations, a developer might accidentally write inefficient circuits |
15 |
| -// using biggroup functions that do not use the OpQueue. We use this concept to prevent compilation of such functions. |
16 |
| -template <typename Builder, typename NativeGroup> |
17 |
| -concept IsNotGoblinInefficiencyTrap = !(IsMegaBuilder<Builder> && std::same_as<NativeGroup, bb::g1>); |
18 |
| - |
19 |
| -namespace bb::stdlib { |
| 15 | +namespace bb::stdlib::element_default { |
20 | 16 |
|
21 | 17 | // ( ͡° ͜ʖ ͡°)
|
22 | 18 | template <class Builder, class Fq, class Fr, class NativeGroup> class element {
|
23 | 19 | public:
|
24 | 20 | using bool_ct = stdlib::bool_t<Builder>;
|
25 | 21 | using biggroup_tag = element; // Facilitates a constexpr check IsBigGroup
|
26 |
| - |
| 22 | + using BaseField = Fq; |
27 | 23 | struct secp256k1_wnaf {
|
28 | 24 | std::vector<field_t<Builder>> wnaf;
|
29 | 25 | field_t<Builder> positive_skew;
|
@@ -177,22 +173,13 @@ template <class Builder, class Fq, class Fr, class NativeGroup> class element {
|
177 | 173 | * We can chain repeated point additions together, where we only require 2 non-native field multiplications per
|
178 | 174 | * point addition, instead of 3
|
179 | 175 | **/
|
180 |
| - static chain_add_accumulator chain_add_start(const element& p1, const element& p2) |
181 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
182 |
| - static chain_add_accumulator chain_add(const element& p1, const chain_add_accumulator& accumulator) |
183 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
184 |
| - static element chain_add_end(const chain_add_accumulator& accumulator) |
185 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
186 |
| - |
187 |
| - element montgomery_ladder(const element& other) const |
188 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
189 |
| - element montgomery_ladder(const chain_add_accumulator& accumulator) |
190 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
191 |
| - element multiple_montgomery_ladder(const std::vector<chain_add_accumulator>& to_add) const |
192 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
193 |
| - |
194 |
| - element quadruple_and_add(const std::vector<element>& to_add) const |
195 |
| - requires(IsNotGoblinInefficiencyTrap<Builder, NativeGroup>); |
| 176 | + static chain_add_accumulator chain_add_start(const element& p1, const element& p2); |
| 177 | + static chain_add_accumulator chain_add(const element& p1, const chain_add_accumulator& accumulator); |
| 178 | + static element chain_add_end(const chain_add_accumulator& accumulator); |
| 179 | + element montgomery_ladder(const element& other) const; |
| 180 | + element montgomery_ladder(const chain_add_accumulator& accumulator); |
| 181 | + element multiple_montgomery_ladder(const std::vector<chain_add_accumulator>& to_add) const; |
| 182 | + element quadruple_and_add(const std::vector<element>& to_add) const; |
196 | 183 |
|
197 | 184 | typename NativeGroup::affine_element get_value() const
|
198 | 185 | {
|
@@ -222,12 +209,6 @@ template <class Builder, class Fq, class Fr, class NativeGroup> class element {
|
222 | 209 | const size_t max_num_bits = 0,
|
223 | 210 | const bool with_edgecases = false);
|
224 | 211 |
|
225 |
| - // TODO(https://github.com/AztecProtocol/barretenberg/issues/707) max_num_bits is unused; could implement and use |
226 |
| - // this to optimize other operations. |
227 |
| - static element goblin_batch_mul(const std::vector<element>& points, |
228 |
| - const std::vector<Fr>& scalars, |
229 |
| - const size_t max_num_bits = 0); |
230 |
| - |
231 | 212 | // we want to conditionally compile this method iff our curve params are the BN254 curve.
|
232 | 213 | // This is a bit tricky to do with `std::enable_if`, because `bn254_endo_batch_mul` is a member function of a class
|
233 | 214 | // template
|
@@ -938,16 +919,31 @@ template <class Builder, class Fq, class Fr, class NativeGroup> class element {
|
938 | 919 | typename std::conditional<HasPlookup<Builder>, batch_lookup_table_plookup<>, batch_lookup_table_base>::type;
|
939 | 920 | };
|
940 | 921 |
|
941 |
| -template <typename T> |
942 |
| -concept IsBigGroup = std::is_same_v<typename T::biggroup_tag, T>; |
943 |
| - |
944 | 922 | template <typename C, typename Fq, typename Fr, typename G>
|
945 | 923 | inline std::ostream& operator<<(std::ostream& os, element<C, Fq, Fr, G> const& v)
|
946 | 924 | {
|
947 | 925 | return os << "{ " << v.x << " , " << v.y << " }";
|
948 | 926 | }
|
949 |
| -} // namespace bb::stdlib |
| 927 | +} // namespace bb::stdlib::element_default |
950 | 928 |
|
| 929 | +namespace bb::stdlib { |
| 930 | +template <typename T> |
| 931 | +concept IsBigGroup = std::is_same_v<typename T::biggroup_tag, T>; |
| 932 | + |
| 933 | +template <typename Builder, class Fq, class Fr, class NativeGroup> |
| 934 | +concept IsGoblinBigGroup = |
| 935 | + IsMegaBuilder<Builder> && std::same_as<Fq, bb::stdlib::bigfield<Builder, bb::Bn254FqParams>> && |
| 936 | + std::same_as<Fr, bb::stdlib::field_t<Builder>> && std::same_as<NativeGroup, bb::g1>; |
| 937 | + |
| 938 | +/** |
| 939 | + * @brief element wraps either element_default::element or element_goblin::goblin_element depending on parametrisation |
| 940 | + * @details if C = MegaBuilder, G = bn254, Fq = bigfield<C, bb::Bn254FqParams>, Fr = field_t then we're cooking |
| 941 | + */ |
| 942 | +template <typename C, typename Fq, typename Fr, typename G> |
| 943 | +using element = std::conditional_t<IsGoblinBigGroup<C, Fq, Fr, G>, |
| 944 | + element_goblin::goblin_element<C, goblin_field<C>, Fr, G>, |
| 945 | + element_default::element<C, Fq, Fr, G>>; |
| 946 | +} // namespace bb::stdlib |
951 | 947 | #include "biggroup_batch_mul.hpp"
|
952 | 948 | #include "biggroup_bn254.hpp"
|
953 | 949 | #include "biggroup_goblin.hpp"
|
|
0 commit comments