From 702fa34c343aade8280c8e4d43e98c321d424160 Mon Sep 17 00:00:00 2001 From: Balazs Komuves Date: Thu, 29 Jan 2026 11:40:54 +0100 Subject: [PATCH] add functions to convert vectors of field elements between standard and Montgomery representation --- benches/iterated_perm.rs | 12 ++---------- src/bn254/field.rs | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/benches/iterated_perm.rs b/benches/iterated_perm.rs index d826c6f..e46af17 100644 --- a/benches/iterated_perm.rs +++ b/benches/iterated_perm.rs @@ -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 } diff --git a/src/bn254/field.rs b/src/bn254/field.rs index 52efb6d..72f9819 100644 --- a/src/bn254/field.rs +++ b/src/bn254/field.rs @@ -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(felts: [Felt; T]) -> [Mont; T] { + felts.map( |x| Felt::to_mont(x) ) + } + + pub fn from_mont_vec(monts: [Mont;T] ) -> [Felt; T] { + monts.map( |x| Felt::from_mont(x) ) } //------------------------------------ - + // basic operations + pub fn zero() -> Felt { Felt(BigInt::zero()) }