-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT(stark252 field): Adds Stark252 curve (#494)
## Describe the changes Adds support for the stark252 base field.
- Loading branch information
Showing
15 changed files
with
860 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// WARNING: This file is auto-generated by a script. | ||
// Any changes made to this file may be overwritten. | ||
// Please modify the code generation script instead. | ||
// Path to the code generation script: scripts/gen_c_api.py | ||
|
||
#pragma once | ||
#ifndef STARK252_API_H | ||
#define STARK252_API_H | ||
|
||
#include <cuda_runtime.h> | ||
#include "gpu-utils/device_context.cuh" | ||
#include "fields/stark_fields/stark252.cuh" | ||
#include "ntt/ntt.cuh" | ||
#include "vec_ops/vec_ops.cuh" | ||
|
||
extern "C" cudaError_t stark252_mul_cuda( | ||
stark252::scalar_t* vec_a, stark252::scalar_t* vec_b, int n, vec_ops::VecOpsConfig& config, stark252::scalar_t* result); | ||
|
||
extern "C" cudaError_t stark252_add_cuda( | ||
stark252::scalar_t* vec_a, stark252::scalar_t* vec_b, int n, vec_ops::VecOpsConfig& config, stark252::scalar_t* result); | ||
|
||
extern "C" cudaError_t stark252_sub_cuda( | ||
stark252::scalar_t* vec_a, stark252::scalar_t* vec_b, int n, vec_ops::VecOpsConfig& config, stark252::scalar_t* result); | ||
|
||
extern "C" cudaError_t stark252_transpose_matrix_cuda( | ||
const stark252::scalar_t* input, | ||
uint32_t row_size, | ||
uint32_t column_size, | ||
stark252::scalar_t* output, | ||
device_context::DeviceContext& ctx, | ||
bool on_device, | ||
bool is_async); | ||
|
||
extern "C" void stark252_generate_scalars(stark252::scalar_t* scalars, int size); | ||
|
||
extern "C" cudaError_t stark252_scalar_convert_montgomery( | ||
stark252::scalar_t* d_inout, size_t n, bool is_into, device_context::DeviceContext& ctx); | ||
|
||
extern "C" cudaError_t stark252_initialize_domain( | ||
stark252::scalar_t* primitive_root, device_context::DeviceContext& ctx, bool fast_twiddles_mode); | ||
|
||
extern "C" cudaError_t stark252_ntt_cuda( | ||
const stark252::scalar_t* input, int size, ntt::NTTDir dir, ntt::NTTConfig<stark252::scalar_t>& config, stark252::scalar_t* output); | ||
|
||
extern "C" cudaError_t stark252_release_domain(device_context::DeviceContext& ctx); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
#define GRUMPKIN 5 | ||
|
||
#define BABY_BEAR 1001 | ||
#define STARK_252 1002 | ||
|
||
#endif |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "icicle-stark252" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
description = "Rust wrapper for the CUDA implementation of stark252 prime field by Ingonyama" | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
icicle-core = { workspace = true } | ||
icicle-cuda-runtime = { workspace = true } | ||
|
||
[build-dependencies] | ||
cmake = "0.1.50" | ||
|
||
[dev-dependencies] | ||
lambdaworks-math = "0.6.0" | ||
|
||
[features] | ||
default = [] | ||
devmode = ["icicle-core/devmode"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use cmake::Config; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-env-changed=CXXFLAGS"); | ||
println!("cargo:rerun-if-changed=../../../../icicle"); | ||
|
||
// Base config | ||
let mut config = Config::new("../../../../icicle/"); | ||
config | ||
.define("FIELD", "stark252") | ||
.define("CMAKE_BUILD_TYPE", "Release") | ||
.define("EXT_FIELD", "OFF"); | ||
|
||
// Build | ||
let out_dir = config | ||
.build_target("icicle_field") | ||
.build(); | ||
|
||
println!("cargo:rustc-link-search={}/build/lib", out_dir.display()); | ||
|
||
println!("cargo:rustc-link-lib=ingo_field_stark252"); | ||
println!("cargo:rustc-link-lib=stdc++"); | ||
println!("cargo:rustc-link-lib=cudart"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use icicle_core::field::{Field, MontgomeryConvertibleField}; | ||
use icicle_core::traits::{FieldConfig, FieldImpl, GenerateRandom}; | ||
use icicle_core::{impl_field, impl_scalar_field}; | ||
use icicle_cuda_runtime::device::check_device; | ||
use icicle_cuda_runtime::device_context::DeviceContext; | ||
use icicle_cuda_runtime::error::CudaError; | ||
use icicle_cuda_runtime::memory::{DeviceSlice, HostOrDeviceSlice}; | ||
|
||
pub(crate) const SCALAR_LIMBS: usize = 8; | ||
|
||
impl_scalar_field!("stark252", stark252, SCALAR_LIMBS, ScalarField, ScalarCfg, Fr); | ||
#[cfg(test)] | ||
mod tests { | ||
use super::ScalarField; | ||
use icicle_core::impl_field_tests; | ||
use icicle_core::tests::*; | ||
|
||
impl_field_tests!(ScalarField); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod field; | ||
pub mod ntt; | ||
pub mod polynomials; | ||
pub mod vec_ops; |
60 changes: 60 additions & 0 deletions
60
wrappers/rust/icicle-fields/icicle-stark252/src/ntt/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::field::{ScalarCfg, ScalarField}; | ||
|
||
use icicle_core::error::IcicleResult; | ||
use icicle_core::ntt::{NTTConfig, NTTDir, NTTDomain, NTT}; | ||
use icicle_core::traits::IcicleResultWrap; | ||
use icicle_core::{impl_ntt, impl_ntt_without_domain}; | ||
use icicle_cuda_runtime::device_context::DeviceContext; | ||
use icicle_cuda_runtime::error::CudaError; | ||
use icicle_cuda_runtime::memory::HostOrDeviceSlice; | ||
|
||
impl_ntt!("stark252", stark252, ScalarField, ScalarCfg); | ||
|
||
#[cfg(test)] | ||
pub(crate) mod tests { | ||
use super::ScalarField; | ||
use icicle_core::{ | ||
ntt::{initialize_domain, ntt_inplace, NTTConfig, NTTDir}, | ||
traits::{FieldImpl, GenerateRandom}, | ||
}; | ||
use icicle_cuda_runtime::{device_context::DeviceContext, memory::HostSlice}; | ||
use lambdaworks_math::{ | ||
field::{ | ||
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField, traits::IsFFTField, | ||
}, | ||
polynomial::Polynomial, | ||
traits::ByteConversion, | ||
}; | ||
|
||
pub type FE = FieldElement<Stark252PrimeField>; | ||
|
||
#[test] | ||
fn test_against_lambdaworks() { | ||
let log_sizes = [15, 20]; | ||
let ctx = DeviceContext::default(); | ||
let lw_root_of_unity = Stark252PrimeField::get_primitive_root_of_unity(log_sizes[log_sizes.len() - 1]).unwrap(); | ||
initialize_domain(ScalarField::from_bytes_le(&lw_root_of_unity.to_bytes_le()), &ctx, false).unwrap(); | ||
for log_size in log_sizes { | ||
let ntt_size = 1 << log_size; | ||
|
||
let mut scalars: Vec<ScalarField> = <ScalarField as FieldImpl>::Config::generate_random(ntt_size); | ||
let scalars_lw: Vec<FE> = scalars | ||
.iter() | ||
.map(|x| FieldElement::from_bytes_le(&x.to_bytes_le()).unwrap()) | ||
.collect(); | ||
|
||
let ntt_cfg: NTTConfig<'_, ScalarField> = NTTConfig::default(); | ||
ntt_inplace(HostSlice::from_mut_slice(&mut scalars[..]), NTTDir::kForward, &ntt_cfg).unwrap(); | ||
|
||
let poly = Polynomial::new(&scalars_lw[..]); | ||
let evaluations = Polynomial::evaluate_fft::<Stark252PrimeField>(&poly, 1, None).unwrap(); | ||
|
||
for (s1, s2) in scalars | ||
.iter() | ||
.zip(evaluations.iter()) | ||
{ | ||
assert_eq!(s1.to_bytes_le(), s2.to_bytes_le()); | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
wrappers/rust/icicle-fields/icicle-stark252/src/polynomials/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use crate::field::{ScalarCfg, ScalarField}; | ||
use icicle_core::impl_univariate_polynomial_api; | ||
|
||
impl_univariate_polynomial_api!("stark252", stark252, ScalarField, ScalarCfg); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use icicle_core::impl_polynomial_tests; | ||
impl_polynomial_tests!(stark252, ScalarField); | ||
} |
20 changes: 20 additions & 0 deletions
20
wrappers/rust/icicle-fields/icicle-stark252/src/vec_ops/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::field::{ScalarCfg, ScalarField}; | ||
|
||
use icicle_core::error::IcicleResult; | ||
use icicle_core::impl_vec_ops_field; | ||
use icicle_core::traits::IcicleResultWrap; | ||
use icicle_core::vec_ops::{VecOps, VecOpsConfig}; | ||
use icicle_cuda_runtime::device_context::DeviceContext; | ||
use icicle_cuda_runtime::error::CudaError; | ||
use icicle_cuda_runtime::memory::HostOrDeviceSlice; | ||
|
||
impl_vec_ops_field!("stark252", stark252, ScalarField, ScalarCfg); | ||
|
||
#[cfg(test)] | ||
pub(crate) mod tests { | ||
use crate::field::ScalarField; | ||
use icicle_core::impl_vec_add_tests; | ||
use icicle_core::vec_ops::tests::*; | ||
|
||
impl_vec_add_tests!(ScalarField); | ||
} |