Skip to content

Commit

Permalink
rename bignum module
Browse files Browse the repository at this point in the history
  • Loading branch information
keks committed Nov 13, 2024
1 parent 05f1652 commit 9caa0ff
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 201 deletions.
2 changes: 1 addition & 1 deletion hacl-rs/src/bignum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod bignum;
pub mod base;
pub mod bignum256;
pub mod bignum256_32;
pub mod bignum32;
Expand Down
200 changes: 100 additions & 100 deletions hacl-rs/src/bignum/bignum.rs → hacl-rs/src/bignum/base.rs

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions hacl-rs/src/bignum/bignum256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ pub fn r#mod(n: &[u64], a: &[u64], res: &mut [u64]) -> bool {
if is_valid_m == 0xFFFFFFFFFFFFFFFFu64 {
let mut r2: [u64; 4] = [0u64; 4usize];
super::bignum256::precompr2(nBits, n, &mut r2);
let mu: u64 = super::bignum::mod_inv_uint64(n[0usize]);
let mu: u64 = super::base::mod_inv_uint64(n[0usize]);
super::bignum256::bn_slow_precomp(n, mu, &r2, a, res)
} else {
(res[0usize..4usize]).copy_from_slice(&[0u64; 4usize])
Expand Down Expand Up @@ -850,15 +850,15 @@ fn exp_consttime_precomp(
fn exp_vartime(nBits: u32, n: &[u64], a: &[u64], bBits: u32, b: &[u64], res: &mut [u64]) {
let mut r2: [u64; 4] = [0u64; 4usize];
super::bignum256::precompr2(nBits, n, &mut r2);
let mu: u64 = super::bignum::mod_inv_uint64(n[0usize]);
let mu: u64 = super::base::mod_inv_uint64(n[0usize]);
super::bignum256::exp_vartime_precomp(n, mu, &r2, a, bBits, b, res)
}

#[inline]
fn exp_consttime(nBits: u32, n: &[u64], a: &[u64], bBits: u32, b: &[u64], res: &mut [u64]) {
let mut r2: [u64; 4] = [0u64; 4usize];
super::bignum256::precompr2(nBits, n, &mut r2);
let mu: u64 = super::bignum::mod_inv_uint64(n[0usize]);
let mu: u64 = super::base::mod_inv_uint64(n[0usize]);
super::bignum256::exp_consttime_precomp(n, mu, &r2, a, bBits, b, res)
}

Expand Down Expand Up @@ -1011,22 +1011,22 @@ Heap-allocate and initialize a montgomery context.
The caller will need to call Hacl_Bignum256_mont_ctx_free on the return value
to avoid memory leaks.
*/
pub fn mont_ctx_init(n: &[u64]) -> Box<[super::bignum::bn_mont_ctx_u64]> {
pub fn mont_ctx_init(n: &[u64]) -> Box<[super::base::bn_mont_ctx_u64]> {
let mut r2: Box<[u64]> = vec![0u64; 4usize].into_boxed_slice();
let mut n1: Box<[u64]> = vec![0u64; 4usize].into_boxed_slice();
let r21: &mut [u64] = &mut r2;
let n11: &mut [u64] = &mut n1;
(n11[0usize..4usize]).copy_from_slice(&n[0usize..4usize]);
let nBits: u32 = 64u32.wrapping_mul(super::bignum_base::bn_get_top_index_u64(4u32, n) as u32);
super::bignum256::precompr2(nBits, n, r21);
let mu: u64 = super::bignum::mod_inv_uint64(n[0usize]);
let res: super::bignum::bn_mont_ctx_u64 = super::bignum::bn_mont_ctx_u64 {
let mu: u64 = super::base::mod_inv_uint64(n[0usize]);
let res: super::base::bn_mont_ctx_u64 = super::base::bn_mont_ctx_u64 {
len: 4u32,
n: (*n11).into(),
mu,
r2: (*r21).into(),
};
let buf: Box<[super::bignum::bn_mont_ctx_u64]> = vec![res].into_boxed_slice();
let buf: Box<[super::base::bn_mont_ctx_u64]> = vec![res].into_boxed_slice();
buf
}

Expand All @@ -1037,7 +1037,7 @@ Write `a mod n` in `res`.
The outparam res is meant to be a 256-bit bignum, i.e. uint64_t[4].
The argument k is a montgomery context obtained through Hacl_Bignum256_mont_ctx_init.
*/
pub fn mod_precomp(k: &[super::bignum::bn_mont_ctx_u64], a: &[u64], res: &mut [u64]) {
pub fn mod_precomp(k: &[super::base::bn_mont_ctx_u64], a: &[u64], res: &mut [u64]) {
let n: &[u64] = &(k[0usize]).n;
let mu: u64 = (k[0usize]).mu;
let r2: &[u64] = &(k[0usize]).r2;
Expand All @@ -1064,7 +1064,7 @@ Write `a ^ b mod n` in `res`.
• a < n
*/
pub fn mod_exp_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u64],
k: &[super::base::bn_mont_ctx_u64],
a: &[u64],
bBits: u32,
b: &[u64],
Expand Down Expand Up @@ -1096,7 +1096,7 @@ Write `a ^ b mod n` in `res`.
• a < n
*/
pub fn mod_exp_consttime_precomp(
k: &[super::bignum::bn_mont_ctx_u64],
k: &[super::base::bn_mont_ctx_u64],
a: &[u64],
bBits: u32,
b: &[u64],
Expand All @@ -1121,7 +1121,7 @@ Write `a ^ (-1) mod n` in `res`.
• a < n
*/
pub fn mod_inv_prime_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u64],
k: &[super::base::bn_mont_ctx_u64],
a: &[u64],
res: &mut [u64],
) {
Expand Down
22 changes: 11 additions & 11 deletions hacl-rs/src/bignum/bignum256_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ pub fn r#mod(n: &[u32], a: &[u32], res: &mut [u32]) -> bool {
if is_valid_m == 0xFFFFFFFFu32 {
let mut r2: [u32; 8] = [0u32; 8usize];
super::bignum256_32::precompr2(nBits, n, &mut r2);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
super::bignum256_32::bn_slow_precomp(n, mu, &r2, a, res)
} else {
(res[0usize..8usize]).copy_from_slice(&[0u32; 8usize])
Expand Down Expand Up @@ -850,15 +850,15 @@ fn exp_consttime_precomp(
fn exp_vartime(nBits: u32, n: &[u32], a: &[u32], bBits: u32, b: &[u32], res: &mut [u32]) {
let mut r2: [u32; 8] = [0u32; 8usize];
super::bignum256_32::precompr2(nBits, n, &mut r2);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
super::bignum256_32::exp_vartime_precomp(n, mu, &r2, a, bBits, b, res)
}

#[inline]
fn exp_consttime(nBits: u32, n: &[u32], a: &[u32], bBits: u32, b: &[u32], res: &mut [u32]) {
let mut r2: [u32; 8] = [0u32; 8usize];
super::bignum256_32::precompr2(nBits, n, &mut r2);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
super::bignum256_32::exp_consttime_precomp(n, mu, &r2, a, bBits, b, res)
}

Expand Down Expand Up @@ -1030,22 +1030,22 @@ Heap-allocate and initialize a montgomery context.
The caller will need to call Hacl_Bignum256_mont_ctx_free on the return value
to avoid memory leaks.
*/
pub fn mont_ctx_init(n: &[u32]) -> Box<[super::bignum::bn_mont_ctx_u32]> {
pub fn mont_ctx_init(n: &[u32]) -> Box<[super::base::bn_mont_ctx_u32]> {
let mut r2: Box<[u32]> = vec![0u32; 8usize].into_boxed_slice();
let mut n1: Box<[u32]> = vec![0u32; 8usize].into_boxed_slice();
let r21: &mut [u32] = &mut r2;
let n11: &mut [u32] = &mut n1;
(n11[0usize..8usize]).copy_from_slice(&n[0usize..8usize]);
let nBits: u32 = 32u32.wrapping_mul(super::bignum_base::bn_get_top_index_u32(8u32, n));
super::bignum256_32::precompr2(nBits, n, r21);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
let res: super::bignum::bn_mont_ctx_u32 = super::bignum::bn_mont_ctx_u32 {
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
let res: super::base::bn_mont_ctx_u32 = super::base::bn_mont_ctx_u32 {
len: 8u32,
n: (*n11).into(),
mu,
r2: (*r21).into(),
};
let buf: Box<[super::bignum::bn_mont_ctx_u32]> = vec![res].into_boxed_slice();
let buf: Box<[super::base::bn_mont_ctx_u32]> = vec![res].into_boxed_slice();
buf
}

Expand All @@ -1056,7 +1056,7 @@ Write `a mod n` in `res`.
The outparam res is meant to be a 256-bit bignum, i.e. uint32_t[8].
The argument k is a montgomery context obtained through Hacl_Bignum256_mont_ctx_init.
*/
pub fn mod_precomp(k: &[super::bignum::bn_mont_ctx_u32], a: &[u32], res: &mut [u32]) {
pub fn mod_precomp(k: &[super::base::bn_mont_ctx_u32], a: &[u32], res: &mut [u32]) {
let n: &[u32] = &(k[0usize]).n;
let mu: u32 = (k[0usize]).mu;
let r2: &[u32] = &(k[0usize]).r2;
Expand All @@ -1083,7 +1083,7 @@ Write `a ^ b mod n` in `res`.
• a < n
*/
pub fn mod_exp_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
bBits: u32,
b: &[u32],
Expand Down Expand Up @@ -1115,7 +1115,7 @@ Write `a ^ b mod n` in `res`.
• a < n
*/
pub fn mod_exp_consttime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
bBits: u32,
b: &[u32],
Expand All @@ -1140,7 +1140,7 @@ Write `a ^ (-1) mod n` in `res`.
• a < n
*/
pub fn mod_inv_prime_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
res: &mut [u32],
) {
Expand Down
52 changes: 26 additions & 26 deletions hacl-rs/src/bignum/bignum32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::fstar;
use crate::lowstar;
use crate::util as lib;

pub type pbn_mont_ctx_u32<'a> = &'a [super::bignum::bn_mont_ctx_u32];
pub type pbn_mont_ctx_u32<'a> = &'a [super::base::bn_mont_ctx_u32];

/**
Write `a + b mod 2 ^ (32 * len)` in `res`.
Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn add_mod(len: u32, n: &[u32], a: &[u32], b: &[u32], res: &mut [u32]) {
let mut b_copy: Box<[u32]> = vec![0u32; len as usize].into_boxed_slice();
((&mut a_copy)[0usize..len as usize]).copy_from_slice(&a[0usize..len as usize]);
((&mut b_copy)[0usize..len as usize]).copy_from_slice(&b[0usize..len as usize]);
super::bignum::bn_add_mod_n_u32(len, n, &a_copy, &b_copy, res)
super::base::bn_add_mod_n_u32(len, n, &a_copy, &b_copy, res)
}

/**
Expand All @@ -101,7 +101,7 @@ Write `(a - b) mod n` in `res`.
- `b < n`
*/
pub fn sub_mod(len: u32, n: &[u32], a: &[u32], b: &[u32], res: &mut [u32]) {
super::bignum::bn_sub_mod_n_u32(len, n, a, b, res)
super::base::bn_sub_mod_n_u32(len, n, a, b, res)
}

/**
Expand All @@ -117,7 +117,7 @@ Write `a * b` in `res`.
*/
pub fn mul(len: u32, a: &[u32], b: &[u32], res: &mut [u32]) {
let mut tmp: Box<[u32]> = vec![0u32; 4u32.wrapping_mul(len) as usize].into_boxed_slice();
super::bignum::bn_karatsuba_mul_uint32(len, a, b, &mut tmp, res)
super::base::bn_karatsuba_mul_uint32(len, a, b, &mut tmp, res)
}

/**
Expand All @@ -130,7 +130,7 @@ Write `a * a` in `res`.
*/
pub fn sqr(len: u32, a: &[u32], res: &mut [u32]) {
let mut tmp: Box<[u32]> = vec![0u32; 4u32.wrapping_mul(len) as usize].into_boxed_slice();
super::bignum::bn_karatsuba_sqr_uint32(len, a, &mut tmp, res)
super::base::bn_karatsuba_sqr_uint32(len, a, &mut tmp, res)
}

#[inline]
Expand All @@ -139,8 +139,8 @@ fn bn_slow_precomp(len: u32, n: &[u32], mu: u32, r2: &[u32], a: &[u32], res: &mu
let mut a1: Box<[u32]> = vec![0u32; len.wrapping_add(len) as usize].into_boxed_slice();
((&mut a1)[0usize..len.wrapping_add(len) as usize])
.copy_from_slice(&a[0usize..len.wrapping_add(len) as usize]);
super::bignum::bn_almost_mont_reduction_u32(len, n, mu, &mut a1, &mut a_mod);
super::bignum::bn_to_mont_u32(len, n, mu, r2, &a_mod, res)
super::base::bn_almost_mont_reduction_u32(len, n, mu, &mut a1, &mut a_mod);
super::base::bn_to_mont_u32(len, n, mu, r2, &a_mod, res)
}

/**
Expand Down Expand Up @@ -178,8 +178,8 @@ pub fn r#mod(len: u32, n: &[u32], a: &[u32], res: &mut [u32]) -> bool {
let nBits: u32 = 32u32.wrapping_mul(super::bignum_base::bn_get_top_index_u32(len, n));
if is_valid_m == 0xFFFFFFFFu32 {
let mut r2: Box<[u32]> = vec![0u32; len as usize].into_boxed_slice();
super::bignum::bn_precomp_r2_mod_n_u32(len, nBits, n, &mut r2);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
super::base::bn_precomp_r2_mod_n_u32(len, nBits, n, &mut r2);
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
super::bignum32::bn_slow_precomp(len, n, mu, &r2, a, res)
} else {
(res[0usize..len as usize]).copy_from_slice(&vec![0u32; len as usize].into_boxed_slice())
Expand Down Expand Up @@ -223,10 +223,10 @@ pub fn mod_exp_vartime(
b: &[u32],
res: &mut [u32],
) -> bool {
let is_valid_m: u32 = super::bignum::bn_check_mod_exp_u32(len, n, a, bBits, b);
let is_valid_m: u32 = super::base::bn_check_mod_exp_u32(len, n, a, bBits, b);
let nBits: u32 = 32u32.wrapping_mul(super::bignum_base::bn_get_top_index_u32(len, n));
if is_valid_m == 0xFFFFFFFFu32 {
super::bignum::bn_mod_exp_vartime_u32(len, nBits, n, a, bBits, b, res)
super::base::bn_mod_exp_vartime_u32(len, nBits, n, a, bBits, b, res)
} else {
(res[0usize..len as usize]).copy_from_slice(&vec![0u32; len as usize].into_boxed_slice())
};
Expand Down Expand Up @@ -269,10 +269,10 @@ pub fn mod_exp_consttime(
b: &[u32],
res: &mut [u32],
) -> bool {
let is_valid_m: u32 = super::bignum::bn_check_mod_exp_u32(len, n, a, bBits, b);
let is_valid_m: u32 = super::base::bn_check_mod_exp_u32(len, n, a, bBits, b);
let nBits: u32 = 32u32.wrapping_mul(super::bignum_base::bn_get_top_index_u32(len, n));
if is_valid_m == 0xFFFFFFFFu32 {
super::bignum::bn_mod_exp_consttime_u32(len, nBits, n, a, bBits, b, res)
super::base::bn_mod_exp_consttime_u32(len, nBits, n, a, bBits, b, res)
} else {
(res[0usize..len as usize]).copy_from_slice(&vec![0u32; len as usize].into_boxed_slice())
};
Expand Down Expand Up @@ -378,7 +378,7 @@ pub fn mod_inv_prime_vartime(len: u32, n: &[u32], a: &[u32], res: &mut [u32]) ->
c0
};
lowstar::ignore::ignore::<u32>(c);
super::bignum::bn_mod_exp_vartime_u32(len, nBits, n, a, 32u32.wrapping_mul(len), &n2, res)
super::base::bn_mod_exp_vartime_u32(len, nBits, n, a, 32u32.wrapping_mul(len), &n2, res)
} else {
(res[0usize..len as usize]).copy_from_slice(&vec![0u32; len as usize].into_boxed_slice())
};
Expand All @@ -399,22 +399,22 @@ Heap-allocate and initialize a montgomery context.
- `n % 2 = 1`
- `1 < n`
*/
pub fn mont_ctx_init(len: u32, n: &[u32]) -> Box<[super::bignum::bn_mont_ctx_u32]> {
pub fn mont_ctx_init(len: u32, n: &[u32]) -> Box<[super::base::bn_mont_ctx_u32]> {
let mut r2: Box<[u32]> = vec![0u32; len as usize].into_boxed_slice();
let mut n1: Box<[u32]> = vec![0u32; len as usize].into_boxed_slice();
let r21: &mut [u32] = &mut r2;
let n11: &mut [u32] = &mut n1;
(n11[0usize..len as usize]).copy_from_slice(&n[0usize..len as usize]);
let nBits: u32 = 32u32.wrapping_mul(super::bignum_base::bn_get_top_index_u32(len, n));
super::bignum::bn_precomp_r2_mod_n_u32(len, nBits, n, r21);
let mu: u32 = super::bignum::mod_inv_uint32(n[0usize]);
let res: super::bignum::bn_mont_ctx_u32 = super::bignum::bn_mont_ctx_u32 {
super::base::bn_precomp_r2_mod_n_u32(len, nBits, n, r21);
let mu: u32 = super::base::mod_inv_uint32(n[0usize]);
let res: super::base::bn_mont_ctx_u32 = super::base::bn_mont_ctx_u32 {
len,
n: (*n11).into(),
mu,
r2: (*r21).into(),
};
let buf: Box<[super::bignum::bn_mont_ctx_u32]> = vec![res].into_boxed_slice();
let buf: Box<[super::base::bn_mont_ctx_u32]> = vec![res].into_boxed_slice();
buf
}

Expand All @@ -427,7 +427,7 @@ Write `a mod n` in `res`.
@param[out] res Points to `len` number of limbs, i.e. `uint32_t[len]`. Must be
disjoint from the memory location of `a`.
*/
pub fn mod_precomp(k: &[super::bignum::bn_mont_ctx_u32], a: &[u32], res: &mut [u32]) {
pub fn mod_precomp(k: &[super::base::bn_mont_ctx_u32], a: &[u32], res: &mut [u32]) {
let len1: u32 = (k[0usize]).len;
let n: &[u32] = &(k[0usize]).n;
let mu: u32 = (k[0usize]).mu;
Expand Down Expand Up @@ -459,7 +459,7 @@ Write `a ^ b mod n` in `res`.
- `a < n`
*/
pub fn mod_exp_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
bBits: u32,
b: &[u32],
Expand All @@ -469,7 +469,7 @@ pub fn mod_exp_vartime_precomp(
let n: &[u32] = &(k[0usize]).n;
let mu: u32 = (k[0usize]).mu;
let r2: &[u32] = &(k[0usize]).r2;
super::bignum::bn_mod_exp_vartime_precomp_u32(len1, n, mu, r2, a, bBits, b, res)
super::base::bn_mod_exp_vartime_precomp_u32(len1, n, mu, r2, a, bBits, b, res)
}

/**
Expand All @@ -496,7 +496,7 @@ Write `a ^ b mod n` in `res`.
- `a < n`
*/
pub fn mod_exp_consttime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
bBits: u32,
b: &[u32],
Expand All @@ -506,7 +506,7 @@ pub fn mod_exp_consttime_precomp(
let n: &[u32] = &(k[0usize]).n;
let mu: u32 = (k[0usize]).mu;
let r2: &[u32] = &(k[0usize]).r2;
super::bignum::bn_mod_exp_consttime_precomp_u32(len1, n, mu, r2, a, bBits, b, res)
super::base::bn_mod_exp_consttime_precomp_u32(len1, n, mu, r2, a, bBits, b, res)
}

/**
Expand All @@ -525,7 +525,7 @@ Write `a ^ (-1) mod n` in `res`.
- `a < n`
*/
pub fn mod_inv_prime_vartime_precomp(
k: &[super::bignum::bn_mont_ctx_u32],
k: &[super::base::bn_mont_ctx_u32],
a: &[u32],
res: &mut [u32],
) {
Expand Down Expand Up @@ -575,7 +575,7 @@ pub fn mod_inv_prime_vartime_precomp(
c0
};
lowstar::ignore::ignore::<u32>(c);
super::bignum::bn_mod_exp_vartime_precomp_u32(
super::base::bn_mod_exp_vartime_precomp_u32(
len1,
n,
mu,
Expand Down
Loading

0 comments on commit 9caa0ff

Please sign in to comment.