This repository was archived by the owner on Oct 12, 2026. It is now read-only.
Repository navigation
feat: validate state registrations for all 27 states - #7
Merged
Merged
Conversation
Inscrição estadual is twenty-seven separate algorithms — the states never agreed on one — so this is the largest single addition here, and the one where being merely close is worthless. Structure: the arithmetic they share lives in ie_arithmetic.go, and most states are a table entry rather than a function. Ten of them are the same nine-digit, weights-nine-down-to-two, mod-11 shape and differ only in whether the opening digits are fixed. The thirteen states that genuinely do something else get a function and a comment saying what. Verification, because "transcribed from the specification" is not evidence: - Every state's own worked example, taken from the roteiro de crítica it publishes through SINTEGRA. Nineteen states print one; all nineteen pass. - A differential run against an existing Go implementation, 20,000 generated numbers per state per length. Twenty-five of twenty-seven agree exactly. - The two that disagree were worked through by hand against the published documents, and in both the other implementation is wrong: Bahia stores its check digits in the opposite order to the one they are computed in, and picks module 10 or 11 from the leading digit — the first for eight-digit registrations, the second for nine. The state publishes an example for each length and the other implementation rejects the nine-digit one. Rondônia does not fold a difference of 10 or 11 onto zero the way most states do. Its document says to subtract ten, so a sum divisible by eleven yields 1, not 0. The first version here used the common rule and was wrong; the state's example survives the change in both the pre-2000 and current formats. ISENTO is accepted: a business can be registered and exempt from holding a number, and rejecting the word would reject a legal answer. The state is a required argument rather than an inference. The same digits can be valid in one state and nonsense in another, so a service that guessed would be answering a question it was not asked. `uf` qualifies `ie` and does not count toward the one-validation-per-request rule.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
GET /validate?ie=0100482300112&uf=ACInscrição estadual is twenty-seven separate algorithms — the states never agreed on one. It is the largest addition here and the one where being merely close is worthless, so most of the work went into verification rather than into the code.
Structure
The shared arithmetic lives in
ie_arithmetic.go: the descending weight series the SINTEGRA documents print above the digits, the weighted sum, and the three ways a remainder becomes a check digit.Most states are then a table entry, not a function. Ten are the same nine-digit, weights-nine-down-to-two, mod-11 shape and differ only in whether their opening digits are fixed:
The thirteen that genuinely do something else get a function and a comment saying what.
Verification
"Transcribed from the specification" is not evidence, so there are three layers:
1. Each state's own worked example. Taken from the roteiro de crítica the state publishes through SINTEGRA. Nineteen states print one; all nineteen pass.
2. A differential run against an existing Go implementation — 20,000 generated numbers per state per length. Twenty-five of twenty-seven agree exactly.
3. The two that disagree, worked through by hand against the published documents. In both, the other implementation is wrong:
Two design decisions
The state is a required argument, not an inference. The same digits can be valid in one state and nonsense in another. A service that guessed would be answering a question it was not asked, so a missing or unknown
ufis an explicit error listing what is supported.ISENTOis accepted. A business can be registered and exempt from holding a number; rejecting the word would reject a legal answer.ufqualifiesieand does not count toward the one-validation-per-request rule — otherwise every state registration request would look like two validations.Reference
The differential was run against dilowagner/singo-ie-validation (MIT), whose repository also carries the SINTEGRA documents the examples come from. No code was copied; it was used as a second opinion, and the two places it disagrees are documented above.