Skip to content

Commit fe42a00

Browse files
nonam3eLeonHibnik
andauthored
[FEAT] Add Koalabear field support (#706)
Co-authored-by: Leon Hibnik <[email protected]>
1 parent f309d6f commit fe42a00

File tree

60 files changed

+3425
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3425
-21
lines changed

.github/workflows/cpp-golang-rust.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ jobs:
178178
build_args: -DEXT_FIELD=OFF
179179
- name: m31
180180
build_args: -DEXT_FIELD=ON
181+
- name: koalabear
182+
build_args: -DEXT_FIELD=ON
181183
steps:
182184
- name: Checkout Repo
183185
uses: actions/checkout@v4
@@ -283,7 +285,7 @@ jobs:
283285
fi
284286
done
285287
286-
- name: Run Golang field Tests (babybear only)
288+
- name: Run Golang field Tests
287289
working-directory: ./wrappers/golang/fields
288290
if: needs.check-changed-files.outputs.golang == 'true' || needs.check-changed-files.outputs.cpp == 'true'
289291
# if: matrix.field.name == 'babybear' #&& matrix.field.name == 'babybear'

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ chmod +x build.sh
130130
```
131131
132132
> [!NOTE]
133-
> To specify the field, use the flag -field=<field>, where <field> can be one of the following: babybear, stark252, m31.
133+
> To specify the field, use the flag -field=<field>, where <field> can be one of the following: babybear, stark252, m31, koalabear.
134134
> To specify a curve, use the flag -curve=<curve>, where <curve> can be one of the following: bn254, bls12_377, bls12_381, bw6_761, grumpkin.
135135
136136
Once ICICLE has been built, you can add specific packages when you need them in your application:
@@ -163,7 +163,7 @@ cmake -S icicle -B build -DFIELD=babybear
163163
```
164164
165165
> [!NOTE]
166-
> To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31.
166+
> To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31, koalabear.
167167
> To specify a curve, use the flag -DCURVE=curve, where curve can be one of the following: bn254, bls12_377, bls12_381, bw6_761, grumpkin.
168168
169169
**Build the project:**
@@ -333,6 +333,7 @@ See [LICENSE-MIT][LMIT] for details.
333333
[babybear]: ./icicle/fields/
334334
[stark252]: ./icicle/fields/
335335
[m31]: ./icicle/fields/
336+
[koalabear]: ./icicle/fields/
336337
[LMIT]: ./LICENSE
337338
[DISCORD]: https://discord.gg/Y4SkbDf2Ff
338339
[googletest]: https://github.com/google/googletest/

docs/docs/icicle/build_from_source.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ICICLE can be built and tested in C++ using CMake. The build process is straight
2424
```
2525

2626
:::info
27-
To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31.
27+
To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31, koalabear.
2828

2929
To specify a curve, use the flag -DCURVE=curve, where curve can be one of the following: bn254, bls12_377, bls12_381, bw6_761, grumpkin.
3030
:::

docs/docs/icicle/libraries.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ Each library has a corresponding crate. See [programmers guide](./programmers_gu
3737

3838
#### Supported fields and operations
3939

40-
| Operation\Field | [babybear](https://eprint.iacr.org/2023/824.pdf) | [Stark252](https://docs.starknet.io/documentation/architecture_and_concepts/Cryptography/p-value/) | m31 |
41-
| ----------------------------------------- | :----------------------------------------------: | :------------------------------------------------------------------------------------------------: | :---: |
42-
| [Vector operations](./primitives/vec_ops) ||||
43-
| [Polynomials](./polynomials/overview) ||||
44-
| [NTT](primitives/ntt) ||||
45-
| Extension Field ||||
46-
| [Poseidon](primitives/hash#poseidon) ||||
47-
| [Poseidon2](primitives/hash#poseidon2) ||||
40+
| Operation\Field | [babybear](https://eprint.iacr.org/2023/824.pdf) | [Stark252](https://docs.starknet.io/documentation/architecture_and_concepts/Cryptography/p-value/) | m31 | Koalabear |
41+
| ----------------------------------------- | :----------------------------------------------: | :------------------------------------------------------------------------------------------------: | :---: | :---: |
42+
| [Vector operations](./primitives/vec_ops) ||| ||
43+
| [Polynomials](./polynomials/overview) ||| ||
44+
| [NTT](primitives/ntt) ||| ||
45+
| Extension Field ||| ||
46+
| [Poseidon](primitives/hash#poseidon) ||| ||
47+
| [Poseidon2](primitives/hash#poseidon2) ||| ||
4848

4949
### Misc
5050

docs/docs/icicle/primitives/poseidon2.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For ICICLE supported curves/fields
3232
* BN254 $\alpha = 5$
3333
* Grumpkin $\alpha = 5$
3434
* Stark252 $\alpha=3$
35+
* Koalabear $\alpha=3$
3536

3637
### MDS matrix structure
3738

icicle/backend/cpu/src/hash/cpu_poseidon.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ using namespace poseidon_constants_babybear;
2727
#elif FIELD_ID == STARK_252
2828
#include "icicle/hash/poseidon_constants/constants/stark252_poseidon.h"
2929
using namespace poseidon_constants_stark252;
30+
#elif FIELD_ID == KOALA_BEAR
31+
#include "icicle/hash/poseidon_constants/constants/koalabear_poseidon.h"
32+
using namespace poseidon_constants_koalabear;
3033
#endif
3134

3235
namespace icicle {

icicle/backend/cpu/src/hash/cpu_poseidon2.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ using namespace poseidon2_constants_babybear;
2727
#elif FIELD_ID == STARK_252
2828
#include "icicle/hash/poseidon2_constants/constants/stark252_poseidon2.h"
2929
using namespace poseidon2_constants_stark252;
30+
#elif FIELD_ID == KOALA_BEAR
31+
#include "icicle/hash/poseidon2_constants/constants/koalabear_poseidon2.h"
32+
using namespace poseidon2_constants_koalabear;
3033
#endif
3134

3235
namespace icicle {

icicle/cmake/fields_and_curves.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(ICICLE_FIELDS
55
1001:babybear:NTT,EXT_FIELD,POSEIDON,POSEIDON2
66
1002:stark252:NTT,POSEIDON,POSEIDON2
77
1003:m31:EXT_FIELD,POSEIDON,POSEIDON2
8+
1004:koalabear:NTT,EXT_FIELD,POSEIDON,POSEIDON2
89
)
910

1011
# Define available curves with an index and their supported features

icicle/include/icicle/api/koalabear.h

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// WARNING: This file is auto-generated by a script.
2+
// Any changes made to this file may be overwritten.
3+
// Please modify the code generation script instead.
4+
// Path to the code generation script: scripts/gen_c_api.py
5+
6+
#pragma once
7+
8+
#include "icicle/fields/stark_fields/koalabear.h"
9+
#include "icicle/ntt.h"
10+
#include "icicle/vec_ops.h"
11+
12+
extern "C" void koalabear_extension_generate_scalars(koalabear::extension_t* scalars, int size);
13+
14+
extern "C" eIcicleError koalabear_extension_scalar_convert_montgomery(
15+
const koalabear::extension_t* input,
16+
uint64_t size,
17+
bool is_into,
18+
const VecOpsConfig* config,
19+
koalabear::extension_t* output);
20+
21+
extern "C" eIcicleError
22+
koalabear_ntt_init_domain(koalabear::scalar_t* primitive_root, const NTTInitDomainConfig* config);
23+
24+
extern "C" eIcicleError koalabear_ntt(
25+
const koalabear::scalar_t* input,
26+
int size,
27+
NTTDir dir,
28+
const NTTConfig<koalabear::scalar_t>* config,
29+
koalabear::scalar_t* output);
30+
31+
extern "C" eIcicleError koalabear_ntt_release_domain();
32+
33+
extern "C" eIcicleError koalabear_extension_vector_mul(
34+
const koalabear::extension_t* vec_a,
35+
const koalabear::extension_t* vec_b,
36+
uint64_t n,
37+
const VecOpsConfig* config,
38+
koalabear::extension_t* result);
39+
40+
extern "C" eIcicleError koalabear_extension_vector_add(
41+
const koalabear::extension_t* vec_a,
42+
const koalabear::extension_t* vec_b,
43+
uint64_t n,
44+
const VecOpsConfig* config,
45+
koalabear::extension_t* result);
46+
47+
extern "C" eIcicleError koalabear_extension_vector_sub(
48+
const koalabear::extension_t* vec_a,
49+
const koalabear::extension_t* vec_b,
50+
uint64_t n,
51+
const VecOpsConfig* config,
52+
koalabear::extension_t* result);
53+
54+
extern "C" eIcicleError koalabear_extension_matrix_transpose(
55+
const koalabear::extension_t* input,
56+
uint32_t nof_rows,
57+
uint32_t nof_cols,
58+
const VecOpsConfig* config,
59+
koalabear::extension_t* output);
60+
61+
extern "C" eIcicleError koalabear_extension_bit_reverse(
62+
const koalabear::extension_t* input, uint64_t n, const VecOpsConfig* config, koalabear::extension_t* output);
63+
64+
extern "C" eIcicleError koalabear_extension_ntt(
65+
const koalabear::extension_t* input,
66+
int size,
67+
NTTDir dir,
68+
const NTTConfig<koalabear::scalar_t>* config,
69+
koalabear::extension_t* output);
70+
71+
extern "C" void koalabear_generate_scalars(koalabear::scalar_t* scalars, int size);
72+
73+
extern "C" void koalabear_scalar_convert_montgomery(
74+
const koalabear::scalar_t* input,
75+
uint64_t size,
76+
bool is_into,
77+
const VecOpsConfig* config,
78+
koalabear::scalar_t* output);
79+
80+
extern "C" eIcicleError koalabear_vector_mul(
81+
const koalabear::scalar_t* vec_a,
82+
const koalabear::scalar_t* vec_b,
83+
uint64_t n,
84+
const VecOpsConfig* config,
85+
koalabear::scalar_t* result);
86+
87+
extern "C" eIcicleError koalabear_vector_add(
88+
const koalabear::scalar_t* vec_a,
89+
const koalabear::scalar_t* vec_b,
90+
uint64_t n,
91+
const VecOpsConfig* config,
92+
koalabear::scalar_t* result);
93+
94+
extern "C" eIcicleError koalabear_vector_sub(
95+
const koalabear::scalar_t* vec_a,
96+
const koalabear::scalar_t* vec_b,
97+
uint64_t n,
98+
const VecOpsConfig* config,
99+
koalabear::scalar_t* result);
100+
101+
extern "C" eIcicleError koalabear_matrix_transpose(
102+
const koalabear::scalar_t* input,
103+
uint32_t nof_rows,
104+
uint32_t nof_cols,
105+
const VecOpsConfig* config,
106+
koalabear::scalar_t* output);
107+
108+
extern "C" eIcicleError koalabear_bit_reverse(
109+
const koalabear::scalar_t* input, uint64_t n, const VecOpsConfig* config, koalabear::scalar_t* output);

icicle/include/icicle/fields/field_config.h

+3
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ namespace field_config = stark252;
3434
#elif FIELD_ID == M31
3535
#include "icicle/fields/stark_fields/m31.h"
3636
namespace field_config = m31;
37+
#elif FIELD_ID == KOALA_BEAR
38+
#include "icicle/fields/stark_fields/koalabear.h"
39+
namespace field_config = koalabear;
3740
#endif

icicle/include/icicle/fields/id.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#define BW6_761 4
99
#define GRUMPKIN 5
1010

11-
#define BABY_BEAR 1001
12-
#define STARK_252 1002
13-
#define M31 1003
11+
#define BABY_BEAR 1001
12+
#define STARK_252 1002
13+
#define M31 1003
14+
#define KOALA_BEAR 1004
1415

1516
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include "icicle/fields/storage.h"
4+
#include "icicle/fields/field.h"
5+
#include "icicle/fields/quartic_extension.h"
6+
#include "icicle/fields/params_gen.h"
7+
8+
namespace koalabear {
9+
struct fp_config {
10+
static constexpr storage<1> modulus = {0x7f000001};
11+
PARAMS(modulus)
12+
13+
static constexpr storage<1> rou = {0x6ac49f88};
14+
TWIDDLES(modulus, rou)
15+
16+
// nonresidue to generate the extension field
17+
static constexpr uint32_t nonresidue = 3;
18+
// true if nonresidue is negative.
19+
static constexpr bool nonresidue_is_negative = false;
20+
};
21+
22+
/**
23+
* Scalar field. Is always a prime field.
24+
*/
25+
typedef Field<fp_config> scalar_t;
26+
27+
/**
28+
* Quartic extension field of `scalar_t` enabled if `-DEXT_FIELD` env variable is.
29+
*/
30+
typedef QuarticExtensionField<fp_config, scalar_t> q_extension_t;
31+
32+
/**
33+
* The default extension type
34+
*/
35+
typedef q_extension_t extension_t;
36+
} // namespace koalabear

0 commit comments

Comments
 (0)