mirror of
https://github.com/logos-storage/rust-poseidon-bn254-pure.git
synced 2026-02-09 18:53:26 +00:00
add functions to convert vectors of field elements between standard and Montgomery representation
This commit is contained in:
parent
ee15a60f89
commit
702fa34c34
@ -26,17 +26,9 @@ fn initial_vector() -> [Felt; 3] {
|
||||
}
|
||||
|
||||
pub fn poseidon1_permute_felt(input: [Felt; 3]) -> [Felt; 3] {
|
||||
let mut state: [Mont; 3] =
|
||||
[ Felt::to_mont(input[0])
|
||||
, Felt::to_mont(input[1])
|
||||
, Felt::to_mont(input[2])
|
||||
];
|
||||
let mut state: [Mont; 3] = Felt::to_mont_vec(input);
|
||||
state = poseidon::permutation::permute_mont_T3(state);
|
||||
let out: [Felt; 3] =
|
||||
[ Felt::from_mont(state[0])
|
||||
, Felt::from_mont(state[1])
|
||||
, Felt::from_mont(state[2])
|
||||
];
|
||||
let out: [Felt; 3] = Felt::from_mont_vec(state);
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
@ -118,7 +118,12 @@ impl Felt {
|
||||
BigInt::is_lt_prime(felt.0)
|
||||
}
|
||||
|
||||
pub fn to_decimal_string(input: Felt) -> String {
|
||||
BigInt::to_decimal_string(input.0)
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
// conversion to/from bytes
|
||||
|
||||
pub fn to_le_bytes(felt: Felt) -> [u8; 32] {
|
||||
BigInt::to_le_bytes(felt.0)
|
||||
@ -136,9 +141,12 @@ impl Felt {
|
||||
Felt(BigInt::from_be_bytes(buf))
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
// conversion to/from Montgomery
|
||||
|
||||
// convert to Montgomery representation
|
||||
pub fn to_mont(fld: Felt) -> Mont {
|
||||
Mont::unsafe_convert_from_big( fld.0 )
|
||||
pub fn to_mont(felt: Felt) -> Mont {
|
||||
Mont::unsafe_convert_from_big( felt.0 )
|
||||
}
|
||||
|
||||
// convert from Montgomery representation
|
||||
@ -146,12 +154,17 @@ impl Felt {
|
||||
Felt(Mont::convert_to_big(mont))
|
||||
}
|
||||
|
||||
pub fn to_decimal_string(input: Felt) -> String {
|
||||
BigInt::to_decimal_string(input.0)
|
||||
pub fn to_mont_vec<const T: usize>(felts: [Felt; T]) -> [Mont; T] {
|
||||
felts.map( |x| Felt::to_mont(x) )
|
||||
}
|
||||
|
||||
pub fn from_mont_vec<const T: usize>(monts: [Mont;T] ) -> [Felt; T] {
|
||||
monts.map( |x| Felt::from_mont(x) )
|
||||
}
|
||||
|
||||
//------------------------------------
|
||||
|
||||
// basic operations
|
||||
|
||||
pub fn zero() -> Felt {
|
||||
Felt(BigInt::zero())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user