# Fix #14 — Replace predictable-entropy referral code generation with Keccak256 cryptographic hashing - #64
Closed
Phantomcall wants to merge 4 commits into
Conversation
…ssue Riddlrealm#15) Use checked_mul/checked_add for reward scaling and accumulation; abort with a new Error::RewardOverflow variant on overflow instead of silently wrapping. Add a #[should_panic] regression test and cite it in SECURE_CODING_GUIDELINES.
…ic hash Removes env.ledger().timestamp() from referral code generation path. Uses triple-layer Keccak256 hash of user address, nonce, and contract address to provide ≥128 bits of entropy. Adds uniqueness test over 100k codes. Documents randomness source in ADR-0031. Closes Riddlrealm#14
The merge of main dropped the xdr::ToXdr trait import that the predictable-entropy code relies on, and initialize() still persisted CodeCounter as u32 while generate_referral_code reads it as u64, causing Error(Value, UnexpectedType) at runtime.
Author
|
all checks have passed now please consider the merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why it matters
Predictable-entropy referral codes enable a referrer to mass-generate codes ahead of
referees, hijack referral attribution, or grind for codes whose reverse-lookup collides
with a known referrer once
CodeOwner(code)is public-facing. The original implementationmixed only 4 bytes of counter + 8 bytes of timestamp, providing ~96 bits of surface but
<40 bits of effective entropy after the alphanumeric reduction step. No random/Oracle
calls were used, and
env.ledger().timestamp()served as the primary entropy source.Technical context
env.ledger().timestamp()(8 bytes) + counter (4 bytes)= ~96 bits surface, <40 bits effective after alphanumeric reduction
generate_referral_code()incontracts/referral/src/lib.rsoracle_price_feednororacle_integrationwas invokedWhat changed
contracts/referral/src/lib.rsenv.ledger().timestamp()from the code path entirelyxdr::ToXdrimport for deterministic address serializationuser_hash(32 bytes)contract_hash(32 bytes)user_hash || nonce || contract_hash→code_hash(32 bytes)code_hashfor alphanumeric code generationCodeCountertype fromu32tou64for larger nonce spaceCodeOwner(String)key formatSecurity properties achieved:
contracts/referral/src/test.rstest_referral_code_uniqueness_over_100ktest:Map<String, bool>trackerdocs/adr/0031-randomness-source.md(new)Verification
cargo check --package referralsucceeds (compiles cleanly)cargo testfails due to pre-existingsoroban-env-host 21.2.1dependency issue (ed25519-dalek 3.0.0rand_core 0.10vsrand 0.8.7rand_core 0.6skew). This is arepo-wide infra break unrelated to this change.
Acceptance criteria checklist
env.ledger().timestamp()-derived keystream in the code pathCodeOwner(String)keys (Issue Rewritescripts/check-missing-permissions.shas AST-based #42)docs/adr/0031-randomness-source.mdLabels
area:security,kind:bug,priority:P0,contract:referralDependencies
emergency_pauseis a single point of failure without quorum #26 (Result-typed API) — future coordination for error handlingPuzzleVerificationtoResult<T, PuzzleError>(ADR-0005) #32 (event schema) — event emission patternsscripts/check-missing-permissions.shas AST-based #42 (migration) — existing code format preserved, no migration neededFiles changed
contracts/referral/src/lib.rs— core entropy fixcontracts/referral/src/test.rs— uniqueness testdocs/adr/0031-randomness-source.md— ADR documentationcloses #14