Skip to content

Commit

Permalink
big prime contraints (#10)
Browse files Browse the repository at this point in the history
* big_prime_constraints

* prime field element place holder

* fmt
  • Loading branch information
thor314 committed Dec 19, 2023
1 parent 9310acc commit 5b807ae
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Rust ###
# ref: https://github.com/github/gitignore/blob/main/Rust.gitignore
.vscode

# exclude compiled files and binaries
debug/
Expand Down
2 changes: 2 additions & 0 deletions algebra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ edition="2021"

[dependencies]
ark-ff="0.4.2"
num-bigint="0.4.4"
lazy_static="1.4.0"
2 changes: 2 additions & 0 deletions algebra/src/fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod big_prime_constants;
mod prime_field_element;
67 changes: 67 additions & 0 deletions algebra/src/fields/big_prime_constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::str::FromStr;

use lazy_static::lazy_static;
use num_bigint::BigInt;

pub trait BigPrimeConstants {
const NBITS: u32;
const INDEX: u32;
type ValueType: Sized;

// Define associated functions instead of constants for complex types
fn modulus() -> Self::ValueType;
fn montgomery_r() -> Self::ValueType;
fn montgomery_r_squared() -> Self::ValueType;
fn montgomery_r_cubed() -> Self::ValueType;
fn factors() -> &'static [Self::ValueType];
fn montgomery_m_prime() -> Self::ValueType;
fn generator() -> Self::ValueType;
fn max_divisible() -> Self::ValueType;
}

struct BigPrimeConstants252_0;

lazy_static! {
static ref FACTORS_252_0: [BigInt; 5] = [
BigInt::from_str("3").unwrap(),
BigInt::from_str("5").unwrap(),
BigInt::from_str("7").unwrap(),
BigInt::from_str("5e2430d").unwrap(),
BigInt::from_str("9f1e667").unwrap(),
];
}

impl BigPrimeConstants for BigPrimeConstants252_0 {
type ValueType = BigInt;

const INDEX: u32 = 0;
const NBITS: u32 = 252;

fn modulus() -> Self::ValueType {
BigInt::from_str("800000000000011000000000000000000000000000000000000000000000001").unwrap()
}

fn montgomery_r() -> Self::ValueType {
BigInt::from_str("7fffffffffffdf0ffffffffffffffffffffffffffffffffffffffffffffffe1").unwrap()
}

fn montgomery_r_squared() -> Self::ValueType {
BigInt::from_str("7ffd4ab5e008810ffffffffff6f800000000001330ffffffffffd737e000401").unwrap()
}

fn montgomery_r_cubed() -> Self::ValueType {
BigInt::from_str("38e5f79873c0a6df47d84f8363000187545706677ffcc06cc7177d1406df18e").unwrap()
}

fn factors() -> &'static [Self::ValueType] { &*FACTORS_252_0 }

fn montgomery_m_prime() -> Self::ValueType {
BigInt::from(u64::MAX) // Equivalent to ~uint64_t(0)
}

fn generator() -> Self::ValueType { BigInt::from(3) }

fn max_divisible() -> Self::ValueType {
BigInt::from_str("f80000000000020f00000000000000000000000000000000000000000000001f").unwrap()
}
}
22 changes: 22 additions & 0 deletions algebra/src/fields/prime_field_element.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::marker::PhantomData;

// use super::big_prime_constants::BigPrimeConstants;

// Define a struct representing a prime field element.
// The NBits and Index are generic parameters.
// todo: probably get rid of this and use Arkworks PrimeFieldElement
pub struct PrimeFieldElement<NBits, Index> {
_nbits: PhantomData<NBits>,
_index: PhantomData<Index>,
}

impl<NBits, Index> From<usize> for PrimeFieldElement<NBits, Index> {
fn from(val: usize) -> Self {
// Self{val}
todo!()
}
}

// impl<NBits, Index> PrimeFieldElement<NBits, Index> {
// // ... other methods as needed
// }
4 changes: 4 additions & 0 deletions algebra/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
// todo
mod fields;
mod polymorphic;

pub use polymorphic::{ConstFieldElementSpan, FieldElementSpan, FieldElementVector};

0 comments on commit 5b807ae

Please sign in to comment.