Skip to content

Commit 42c1f0b

Browse files
tideofwordsax0
andauthored
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>
1 parent 2864ef2 commit 42c1f0b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/backends/plonky2/basetypes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl PartialOrd for Hash {
153153
impl fmt::Display for Hash {
154154
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155155
let v0 = self.0[0].to_canonical_u64();
156-
for i in 0..4 {
156+
for i in 0..HASH_SIZE {
157157
write!(f, "{:02x}", (v0 >> (i * 8)) & 0xff)?;
158158
}
159159
write!(f, "…")
@@ -168,8 +168,8 @@ impl FromHex for Hash {
168168
// In little endian
169169
let bytes = <[u8; 32]>::from_hex(hex)?;
170170
let mut buf: [u8; 8] = [0; 8];
171-
let mut inner = [F::ZERO; 4];
172-
for i in 0..4 {
171+
let mut inner = [F::ZERO; HASH_SIZE];
172+
for i in 0..HASH_SIZE {
173173
buf.copy_from_slice(&bytes[8 * i..8 * (i + 1)]);
174174
inner[i] = F::from_canonical_u64(u64::from_le_bytes(buf));
175175
}

src/middleware/custom.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{fmt, hash as h, iter::zip};
55
use anyhow::{anyhow, Result};
66
use plonky2::field::types::Field;
77

8+
use crate::backends::plonky2::basetypes::HASH_SIZE;
89
use crate::util::hashmap_insert_no_dupe;
910

1011
use super::{
@@ -51,12 +52,13 @@ impl ToFields for HashOrWildcard {
5152
match self {
5253
HashOrWildcard::Hash(h) => h.to_fields(_params),
5354
HashOrWildcard::Wildcard(w) => {
54-
let usizes: Vec<usize> = vec![0, 0, 0, *w];
55+
let mut usizes: Vec<usize> = vec![0; HASH_SIZE - 1];
56+
usizes.push(*w);
5557
let fields: Vec<F> = usizes
5658
.iter()
5759
.map(|x| F::from_canonical_u64(*x as u64))
5860
.collect();
59-
(fields, 4)
61+
(fields, HASH_SIZE)
6062
}
6163
}
6264
}
@@ -99,8 +101,7 @@ impl ToFields for StatementTmplArg {
99101
// Key(hash_or_wildcard1, hash_or_wildcard2)
100102
// => (2, [hash_or_wildcard1], [hash_or_wildcard2])
101103
// In all three cases, we pad to 2 * hash_size + 1 = 9 field elements
102-
let hash_size = 4;
103-
let statement_tmpl_arg_size = 2 * hash_size + 1;
104+
let statement_tmpl_arg_size = 2 * HASH_SIZE + 1;
104105
match self {
105106
StatementTmplArg::None => {
106107
let fields: Vec<F> = std::iter::repeat_with(|| F::from_canonical_u64(0))
@@ -111,7 +112,7 @@ impl ToFields for StatementTmplArg {
111112
StatementTmplArg::Literal(v) => {
112113
let fields: Vec<F> = std::iter::once(F::from_canonical_u64(1))
113114
.chain(v.to_fields(_params).0)
114-
.chain(std::iter::repeat_with(|| F::from_canonical_u64(0)).take(hash_size))
115+
.chain(std::iter::repeat_with(|| F::from_canonical_u64(0)).take(HASH_SIZE))
115116
.collect();
116117
(fields, statement_tmpl_arg_size)
117118
}

0 commit comments

Comments
 (0)