Fix i64 conversion (#110)

* Failing test

* Fixed implementation
This commit is contained in:
Rob Knight 2025-03-05 15:46:27 +01:00 committed by GitHub
parent 9d60b0ec3a
commit 77f3f347e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -91,7 +91,7 @@ impl TryInto<i64> for Value {
{ {
Err(anyhow!("Value not an element of the i64 embedding.")) Err(anyhow!("Value not an element of the i64 embedding."))
} else { } else {
Ok((value[0].to_canonical_u64() + value[1].to_canonical_u64() << 32) as i64) Ok((value[0].to_canonical_u64() | (value[1].to_canonical_u64() << 32)) as i64)
} }
} }
} }
@ -201,3 +201,29 @@ pub fn hash_str(s: &str) -> Hash {
.collect(); .collect();
Hash(PoseidonHash::hash_no_pad(&input).elements) Hash(PoseidonHash::hash_no_pad(&input).elements)
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_i64_value_roundtrip() {
let test_cases = vec![
0i64,
1,
-1,
i64::MAX,
i64::MIN,
42,
-42,
1 << 32,
-(1 << 32),
];
for &original in test_cases.iter() {
let value = Value::from(original);
let roundtrip: i64 = value.try_into().unwrap();
assert_eq!(original, roundtrip, "Failed roundtrip for {}", original);
}
}
}