Skip to content

Commit

Permalink
use from_bits_le
Browse files Browse the repository at this point in the history
  • Loading branch information
anstylian committed Jan 21, 2025
1 parent 28c795a commit d3db0e4
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions synthesizer/program/src/logic/instruction/operation/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,15 @@ fn to_console_array<N: Network>(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"),
}
})
Expand All @@ -266,21 +265,17 @@ fn to_console_array<N: Network>(hash: &[bool], len: usize, literal_type: Literal
}

fn to_circuit_array_u8<A: circuit::Aleo>(hash: &[circuit::Boolean<A>], len: usize) -> Result<circuit::Plaintext<A>> {
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::<A>::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(),
)
})
Expand All @@ -292,21 +287,17 @@ fn to_circuit_array_u8<A: circuit::Aleo>(hash: &[circuit::Boolean<A>], len: usiz
}

fn to_circuit_array_u16<A: circuit::Aleo>(hash: &[circuit::Boolean<A>], len: usize) -> Result<circuit::Plaintext<A>> {
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::<A>::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(),
)
})
Expand Down

0 comments on commit d3db0e4

Please sign in to comment.