Skip to content
This repository was archived by the owner on Jun 10, 2026. It is now read-only.
This repository was archived by the owner on Jun 10, 2026. It is now read-only.

sample_bits() introduces 1/128 per-element bias for KoalaBear field #4

Description

@jiayaoqijia

Summary

The sample() method in DuplexChallenger returns raw sponge state elements that are canonical field elements in [0, P). When used as challenges in WHIR's ChallengeSampler::sample(), the distribution over the extension field is uniform modulo the field order, introducing a bias of ~(2^31 - P) / 2^31 per base field element. For KoalaBear (P = 2^31 - 2^24 + 1), this bias is 2^24/2^31 = 1/128.

Severity

HIGH -- Materially weakens security guarantees in adversarial settings.

Location

src/duplex_challenger.rs:55-70 -- sample_in_range uses bitwise masking which is correct for power-of-2 ranges, but sample() (line 45-52) returns elements that are already canonical field elements in [0, P), not uniform over [0, 2^31).

Impact

The 1/128 per-element bias accumulates across multiple challenge rounds. For 100+ challenges in a typical WHIR proof, this could reduce security by ~1-2 bits in adversarial settings.

Suggested Fix

Document clearly that sample() returns elements uniform in F_p (not in [0, 2^31)), and verify that all security proofs account for this. If field-element uniformity is insufficient (e.g., for grinding), add a rejection sampling path:

pub fn sample_uniform_bits(&mut self, bits: usize) -> usize {
    loop {
        let raw = self.sample()[0].as_canonical_u64() as usize;
        let candidate = raw & ((1 << bits) - 1);
        if raw < ((F::ORDER_U64 as usize) >> bits) << bits {
            return candidate;
        }
        self.duplexing(None);
    }
}

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions