Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
anstylian committed Jan 9, 2025
1 parent 7e18078 commit 07be85a
Showing 1 changed file with 59 additions and 65 deletions.
124 changes: 59 additions & 65 deletions synthesizer/program/src/logic/instruction/operation/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,85 +242,79 @@ fn to_console_array<N: Network>(hash: &[bool], len: usize, literal_type: Literal
hash.len()
);

let mut out = vec![];
for element in hash.chunks(chunk_size) {
let mut value = 0;
for (i, bit) in element.iter().enumerate() {
if *bit {
value |= 1 << i;
}
}
let value = match literal_type {
LiteralType::U8 => {
Plaintext::Literal(Literal::U8(console::types::U8::new(value.try_into()?)), Default::default())
}
LiteralType::U16 => {
Plaintext::Literal(Literal::U16(console::types::U16::new(value.try_into()?)), Default::default())
}
_ => bail!("Unsupported literal type: {literal_type} for array"),
};
out.push(value);
}
ensure!(len == out.len(), "Expected to produce {len} elements, but produced {}", out.len());
Ok(Plaintext::Array(out, Default::default()))
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()))
}
LiteralType::U16 => Ok(Plaintext::Literal(
Literal::U16(console::types::U16::new(value.try_into()?)),
Default::default(),
)),
_ => bail!("Unsupported literal type: {literal_type} for array"),
}
})
.collect::<Result<Vec<_>, _>>()?;

ensure!(len == hash.len(), "Expected to produce {len} elements, but produced {}", hash.len());

Ok(Plaintext::Array(hash, Default::default()))
}

fn to_circuit_array_u8<A: circuit::Aleo>(hash: &[circuit::Boolean<A>], len: usize) -> Result<circuit::Plaintext<A>> {
use circuit::{Eject, Inject};

ensure!(hash.len() % 8 == 0, "Expected hash length to be a multiple of 8, but found {}", hash.len());

let mut out = vec![];

for element in hash.chunks(8) {
let mut value = 0;
for (i, bit) in element.iter().enumerate() {
if bit.eject_value() {
value |= 1 << i;
}
}

let value = console::types::U8::new(value);
let value = circuit::Plaintext::Literal(
circuit::Literal::U8(circuit::types::U8::new(element.eject_mode(), value)),
Default::default(),
);

out.push(value);
}

ensure!(len == out.len(), "Expected to produce {len} elements, but produced {}", out.len());

Ok(circuit::Plaintext::Array(out, Default::default()))
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 = console::types::U8::new(value);
circuit::Plaintext::Literal(
circuit::Literal::U8(circuit::types::U8::new(element.eject_mode(), value)),
Default::default(),
)
})
.collect();

ensure!(len == hash.len(), "Expected to produce {len} elements, but produced {}", hash.len());

Ok(circuit::Plaintext::Array(hash, Default::default()))
}

fn to_circuit_array_u16<A: circuit::Aleo>(hash: &[circuit::Boolean<A>], len: usize) -> Result<circuit::Plaintext<A>> {
use circuit::{Eject, Inject};

ensure!(hash.len() % 16 == 0, "Expected hash length to be a multiple of 16, but found {}", hash.len());

let mut out = vec![];

for element in hash.chunks(16) {
let mut value = 0;
for (i, bit) in element.iter().enumerate() {
if bit.eject_value() {
value |= 1 << i;
}
}

let value = console::types::U16::new(value);
let value = circuit::Plaintext::Literal(
circuit::Literal::U16(circuit::types::U16::new(element.eject_mode(), value)),
Default::default(),
);

out.push(value);
}

ensure!(len == out.len(), "Expected to produce {len} elements, but produced {}", out.len());

Ok(circuit::Plaintext::Array(out, Default::default()))
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 = console::types::U16::new(value);
circuit::Plaintext::Literal(
circuit::Literal::U16(circuit::types::U16::new(element.eject_mode(), value)),
Default::default(),
)
})
.collect();

ensure!(len == hash.len(), "Expected to produce {len} elements, but produced {}", hash.len());

Ok(circuit::Plaintext::Array(hash, Default::default()))
}

impl<N: Network, const VARIANT: u8> HashInstruction<N, VARIANT> {
Expand Down

0 comments on commit 07be85a

Please sign in to comment.