Replace constant 4 with HASH_SIZE (#119)

* Replace constant 4 with HASH_SIZE

* cargo fmt

* More 4 change to HASH_SIZE

---------

Co-authored-by: Ahmad <root@ahmadafuni.com>
This commit is contained in:
tideofwords 2025-03-07 06:10:09 -08:00 committed by GitHub
parent 2864ef22d4
commit 42c1f0b0f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 8 deletions

View file

@ -153,7 +153,7 @@ impl PartialOrd for Hash {
impl fmt::Display for Hash { impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let v0 = self.0[0].to_canonical_u64(); let v0 = self.0[0].to_canonical_u64();
for i in 0..4 { for i in 0..HASH_SIZE {
write!(f, "{:02x}", (v0 >> (i * 8)) & 0xff)?; write!(f, "{:02x}", (v0 >> (i * 8)) & 0xff)?;
} }
write!(f, "") write!(f, "")
@ -168,8 +168,8 @@ impl FromHex for Hash {
// In little endian // In little endian
let bytes = <[u8; 32]>::from_hex(hex)?; let bytes = <[u8; 32]>::from_hex(hex)?;
let mut buf: [u8; 8] = [0; 8]; let mut buf: [u8; 8] = [0; 8];
let mut inner = [F::ZERO; 4]; let mut inner = [F::ZERO; HASH_SIZE];
for i in 0..4 { for i in 0..HASH_SIZE {
buf.copy_from_slice(&bytes[8 * i..8 * (i + 1)]); buf.copy_from_slice(&bytes[8 * i..8 * (i + 1)]);
inner[i] = F::from_canonical_u64(u64::from_le_bytes(buf)); inner[i] = F::from_canonical_u64(u64::from_le_bytes(buf));
} }

View file

@ -5,6 +5,7 @@ use std::{fmt, hash as h, iter::zip};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use plonky2::field::types::Field; use plonky2::field::types::Field;
use crate::backends::plonky2::basetypes::HASH_SIZE;
use crate::util::hashmap_insert_no_dupe; use crate::util::hashmap_insert_no_dupe;
use super::{ use super::{
@ -51,12 +52,13 @@ impl ToFields for HashOrWildcard {
match self { match self {
HashOrWildcard::Hash(h) => h.to_fields(_params), HashOrWildcard::Hash(h) => h.to_fields(_params),
HashOrWildcard::Wildcard(w) => { HashOrWildcard::Wildcard(w) => {
let usizes: Vec<usize> = vec![0, 0, 0, *w]; let mut usizes: Vec<usize> = vec![0; HASH_SIZE - 1];
usizes.push(*w);
let fields: Vec<F> = usizes let fields: Vec<F> = usizes
.iter() .iter()
.map(|x| F::from_canonical_u64(*x as u64)) .map(|x| F::from_canonical_u64(*x as u64))
.collect(); .collect();
(fields, 4) (fields, HASH_SIZE)
} }
} }
} }
@ -99,8 +101,7 @@ impl ToFields for StatementTmplArg {
// Key(hash_or_wildcard1, hash_or_wildcard2) // Key(hash_or_wildcard1, hash_or_wildcard2)
// => (2, [hash_or_wildcard1], [hash_or_wildcard2]) // => (2, [hash_or_wildcard1], [hash_or_wildcard2])
// In all three cases, we pad to 2 * hash_size + 1 = 9 field elements // In all three cases, we pad to 2 * hash_size + 1 = 9 field elements
let hash_size = 4; let statement_tmpl_arg_size = 2 * HASH_SIZE + 1;
let statement_tmpl_arg_size = 2 * hash_size + 1;
match self { match self {
StatementTmplArg::None => { StatementTmplArg::None => {
let fields: Vec<F> = std::iter::repeat_with(|| F::from_canonical_u64(0)) let fields: Vec<F> = std::iter::repeat_with(|| F::from_canonical_u64(0))
@ -111,7 +112,7 @@ impl ToFields for StatementTmplArg {
StatementTmplArg::Literal(v) => { StatementTmplArg::Literal(v) => {
let fields: Vec<F> = std::iter::once(F::from_canonical_u64(1)) let fields: Vec<F> = std::iter::once(F::from_canonical_u64(1))
.chain(v.to_fields(_params).0) .chain(v.to_fields(_params).0)
.chain(std::iter::repeat_with(|| F::from_canonical_u64(0)).take(hash_size)) .chain(std::iter::repeat_with(|| F::from_canonical_u64(0)).take(HASH_SIZE))
.collect(); .collect();
(fields, statement_tmpl_arg_size) (fields, statement_tmpl_arg_size)
} }