From b280a5252c6bd5ea9b50de632de434173ae57718 Mon Sep 17 00:00:00 2001 From: M Alghazwi Date: Fri, 13 Dec 2024 16:37:26 +0300 Subject: [PATCH] add select hash helper fn --- codex-plonky2-circuits/src/circuits/utils.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/codex-plonky2-circuits/src/circuits/utils.rs b/codex-plonky2-circuits/src/circuits/utils.rs index 43bd66c..a9ff178 100644 --- a/codex-plonky2-circuits/src/circuits/utils.rs +++ b/codex-plonky2-circuits/src/circuits/utils.rs @@ -89,4 +89,20 @@ pub fn add_assign_hash_out_target< /// Reads the contents of the specified file and returns them as a vector of bytes using `std::fs::read`. pub fn read_bytes_from_file>(path: P) -> io::Result> { fs::read(path) +} + +/// select hash helper method +/// Computes `if b { h0 } else { h1 }`. +pub fn select_hash< + F: RichField + Extendable + Poseidon2, + const D: usize, +>( + builder: &mut CircuitBuilder, + b: BoolTarget, + h0: HashOutTarget, + h1: HashOutTarget, +) -> HashOutTarget { + HashOutTarget { + elements: core::array::from_fn(|i| builder.select(b, h0.elements[i], h1.elements[i])), + } } \ No newline at end of file