Skip to content

Commit 30bcb23

Browse files
authored
fix: Fr::from_u64 big-endian encoding to match C++ msgpack format (#22233)
## Summary Fixes bb-rs test failure introduced by #22311 (graceful failures in verifier code paths). `Fr::from_u64` was storing values in **little-endian**, but the C++ `field::msgpack_unpack` expects **big-endian** (network byte order). This was masked before because the C++ side silently reduced non-canonical values via `to_montgomery_form_reduced()`. After #22311 added strict non-canonical field validation (throwing for values >= modulus), values like `Fr::from_u64(123)` produce `0x7B<<248` which exceeds the BN254 Fr modulus (`0x30644e72...`) and cause a C++ exception that propagates through the FFI boundary, aborting the Rust test process with "Rust cannot catch foreign exceptions". ## Fix One-line change: store the u64 value in bytes 24..32 as big-endian instead of bytes 0..8 as little-endian. ## Test plan - [x] Reproduced failure locally — `test_pedersen_commit_deterministic` crashes with SIGABRT - [x] Applied fix — all 70 bb-rs tests pass (3 perf tests ignored) - [ ] `./bootstrap.sh ci` running
1 parent 5f09c55 commit 30bcb23

File tree

1 file changed

+2
-2
lines changed
  • barretenberg/rust/barretenberg-rs/src

1 file changed

+2
-2
lines changed

barretenberg/rust/barretenberg-rs/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use serde::{Deserialize, Serialize};
77
pub struct Fr(pub [u8; 32]);
88

99
impl Fr {
10-
/// Create a new field element from a u64 value (little-endian encoding)
10+
/// Create a new field element from a u64 value (big-endian encoding, matching C++ msgpack format)
1111
pub fn from_u64(value: u64) -> Self {
1212
let mut bytes = [0u8; 32];
13-
bytes[0..8].copy_from_slice(&value.to_le_bytes());
13+
bytes[24..32].copy_from_slice(&value.to_be_bytes());
1414
Fr(bytes)
1515
}
1616

0 commit comments

Comments
 (0)