From d3db0e405b14bbc6bc29b2d498d41ed6c1404d15 Mon Sep 17 00:00:00 2001 From: Angelos Stylianidis Date: Tue, 21 Jan 2025 23:40:01 +0200 Subject: [PATCH] use from_bits_le --- .../src/logic/instruction/operation/hash.rs | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/synthesizer/program/src/logic/instruction/operation/hash.rs b/synthesizer/program/src/logic/instruction/operation/hash.rs index 88a154fdc2..5ecff63dbe 100644 --- a/synthesizer/program/src/logic/instruction/operation/hash.rs +++ b/synthesizer/program/src/logic/instruction/operation/hash.rs @@ -245,16 +245,15 @@ fn to_console_array(hash: &[bool], len: usize, literal_type: Literal let hash = hash .chunks(chunk_size) .map(|element| { - let value = element.iter().enumerate().fold(0, |acc, (i, bit)| if *bit { acc | (1 << i) } else { acc }); - match literal_type { LiteralType::U8 => { - Ok(Plaintext::Literal(Literal::U8(console::types::U8::new(value.try_into()?)), Default::default())) + let value = console::types::U8::from_bits_le(element)?; + Ok(Plaintext::Literal(Literal::U8(value), Default::default())) } - LiteralType::U16 => Ok(Plaintext::Literal( - Literal::U16(console::types::U16::new(value.try_into()?)), - Default::default(), - )), + LiteralType::U16 => { + let value = console::types::U16::from_bits_le(element)?; + Ok(Plaintext::Literal(Literal::U16(value), Default::default())) + }, _ => bail!("Unsupported literal type: {literal_type} for array"), } }) @@ -266,21 +265,17 @@ fn to_console_array(hash: &[bool], len: usize, literal_type: Literal } fn to_circuit_array_u8(hash: &[circuit::Boolean], len: usize) -> Result> { - use circuit::{Eject, Inject}; + use circuit::traits::FromBits; ensure!(hash.len() % 8 == 0, "Expected hash length to be a multiple of 8, but found {}", hash.len()); let hash: Vec<_> = hash .chunks(8) .map(|element| { - let value = element - .iter() - .enumerate() - .fold(0, |acc, (i, bit)| if bit.eject_value() { acc | (1 << i) } else { acc }); + let value = circuit::types::U8::::from_bits_le(element); - let value = console::types::U8::new(value); circuit::Plaintext::Literal( - circuit::Literal::U8(circuit::types::U8::new(element.eject_mode(), value)), + circuit::Literal::U8(value), Default::default(), ) }) @@ -292,21 +287,17 @@ fn to_circuit_array_u8(hash: &[circuit::Boolean], len: usiz } fn to_circuit_array_u16(hash: &[circuit::Boolean], len: usize) -> Result> { - use circuit::{Eject, Inject}; + use circuit::traits::FromBits; ensure!(hash.len() % 16 == 0, "Expected hash length to be a multiple of 16, but found {}", hash.len()); let hash: Vec<_> = hash .chunks(16) .map(|element| { - let value = element - .iter() - .enumerate() - .fold(0, |acc, (i, bit)| if bit.eject_value() { acc | (1 << i) } else { acc }); + let value = circuit::types::U16::::from_bits_le(element); - let value = console::types::U16::new(value); circuit::Plaintext::Literal( - circuit::Literal::U16(circuit::types::U16::new(element.eject_mode(), value)), + circuit::Literal::U16(value), Default::default(), ) })