Skip to content

Commit

Permalink
Merge branch 'arkworks-compatibility' of github.com:penumbra-zone/dec…
Browse files Browse the repository at this point in the history
…af377 into arkworks-compatibility
  • Loading branch information
cronokirby committed Jan 12, 2024
2 parents 5bc8d88 + abaf119 commit 031a44a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
41 changes: 41 additions & 0 deletions src/fields/fp/u32/arkworks_constants.rs
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
use super::Fp;

pub const NUM_LIMBS: usize = 12;

pub const MODULUS_LIMBS: [u32; 12] = [
1, 2231943168, 805306368, 386620740, 3121170432, 519266863, 16061327, 438491635, 1822509371,
3325756864, 398790890, 28195398,
];

pub const MODULUS_MINUS_ONE_DIV_TWO_LIMBS: [u32; 12] = [
0, 1115971584, 402653184, 193310370, 3708068864, 2407117079, 2155514311, 2366729465, 911254685,
1662878432, 199395445, 14097699,
];

pub const MODULUS_BIT_SIZE: u32 = 0x179;

pub const TRACE_LIMBS: [u32; 12] = [
136227, 1964032000, 536894509, 2294212645, 1312586701, 1741423572, 619473035, 385987205,
1135286508, 3910688532, 1720, 0,
];

pub const TRACE_MINUS_ONE_DIV_TWO_LIMBS: [u32; 12] = [
68113, 3129499648, 2415930902, 3294589970, 656293350, 3018195434, 2457220165, 192993602,
567643254, 1955344266, 860, 0,
];

pub const TWO_ADICITY: u32 = 0x2e;

pub const QUADRATIC_NON_RESIDUE_TO_TRACE: Fp = Fp::from_montgomery_limbs([
2343670258, 1761113770, 2792500817, 625887104, 529141423, 2820205081, 495800407, 739929555,
2699006747, 3437600971, 3369043093, 12220676,
]);

pub const MULTIPLICATIVE_GENERATOR: Fp = Fp::from_montgomery_limbs([
4294965012, 367984639, 3221224285, 1721492387, 892444466, 3700424240, 1970634519, 3507452915,
3506873522, 1768468278, 3989639519, 26220195,
]);

pub const TWO_ADIC_ROOT_OF_UNITY: Fp = Fp::from_montgomery_limbs([
2031790878, 3754616354, 3204826524, 1913374628, 226001622, 631062918, 2984565398, 3626713688,
1739907172, 3086590412, 1450066569, 16622719,
]);
35 changes: 27 additions & 8 deletions src/fields/fp/u32/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ impl PartialEq for Fp {

impl Eq for Fp {}

impl zeroize::Zeroize for Fp {
fn zeroize(&mut self) {
self.0 .0.zeroize()
}
}

impl Fp {
pub fn from_bytes(bytes: &[u8; 48]) -> Self {
pub fn from_le_limbs(limbs: [u32; 12]) -> Fp {
let x_non_monty = fiat::FpNonMontgomeryDomainFieldElement(limbs);
let mut x = fiat::FpMontgomeryDomainFieldElement([0; 12]);
fiat::fp_to_montgomery(&mut x, &x_non_monty);
Self(x)
}

pub fn from_bytes(bytes: &[u8; 48]) -> Fp {
let mut x_non_montgomery = fiat::FpNonMontgomeryDomainFieldElement([0; 12]);
let mut x = fiat::FpMontgomeryDomainFieldElement([0; 12]);

Expand All @@ -25,27 +38,33 @@ impl Fp {
Self(x)
}

pub fn to_bytes(&self) -> [u8; 48] {
pub fn to_le_limbs(&self) -> [u32; 12] {
let mut x_non_montgomery = fiat::FpNonMontgomeryDomainFieldElement([0; 12]);
let mut bytes = [0u8; 48];

fiat::fp_from_montgomery(&mut x_non_montgomery, &self.0);
fiat::fp_to_bytes(&mut bytes, &x_non_montgomery.0);
x_non_montgomery.0
}

pub fn to_bytes_le(&self) -> [u8; 48] {
let mut bytes = [0u8; 48];
fiat::fp_to_bytes(&mut bytes, &self.to_le_limbs());
bytes
}

pub fn zero() -> Self {
pub const fn from_montgomery_limbs(limbs: [u32; 12]) -> Fp {
Self(fiat::FpMontgomeryDomainFieldElement(limbs))
}

pub fn zero() -> Fp {
Self(fiat::FpMontgomeryDomainFieldElement([0; 12]))
}

pub fn one() -> Self {
pub fn one() -> Fp {
let mut one = Self::zero();
fiat::fp_set_one(&mut one.0);
one
}

pub fn square(&self) -> Self {
pub fn square(&self) -> Fp {
let mut result = fiat::FpMontgomeryDomainFieldElement([0; 12]);
fiat::fp_square(&mut result, &self.0);
Self(result)
Expand Down

0 comments on commit 031a44a

Please sign in to comment.