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

feat: validate state registrations for all 27 states - #7

Merged
jeferson0306 merged 1 commit into
masterfrom
feat/inscricao-estadual
Sep 4, 2026
Merged

jeferson0306 merged 1 commit into
masterfrom
feat/inscricao-estadual

Conversation

@jeferson0306

Copy link
Copy Markdown
Collaborator

GET /validate?ie=0100482300112&uf=AC

Inscriçã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:

"CE": trailingMod11(9, 9, "06"),
"MA": trailingMod11(9, 9, "12"),
"PI": trailingMod11(9, 9),

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:

Bahia stores its check digits in the opposite order to the one they are computed in, and selects module 10 or 11 from the leading digit — the first digit for eight-digit registrations, the second for nine-digit ones. The state publishes an example for each length; the other implementation rejects the nine-digit one, 100000306. Working it through: DV2 over 1000003 with weights 8…2 gives 14, and 10−4 = 6 ✓; DV1 over 10000036 with weights 9…2 gives 30, remainder 0 → 0 ✓.

Rondônia does not fold a difference of 10 or 11 onto zero the way most states do. Its document says "subtrai-se 10 da mesma", so a sum divisible by eleven yields 1, not 0. My first version used the common rule and was wrong — caught by this differential, then settled by the state's example, which now passes in both the pre-2000 nine-digit and the current fourteen-digit formats.

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 uf is an explicit error listing what is supported.

ISENTO is accepted. A business can be registered and exempt from holding a number; rejecting the word would reject a legal answer.

uf qualifies ie and 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.

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.
@jeferson0306
jeferson0306 merged commit f984ebc into master Sep 4, 2026
1 check passed
@jeferson0306
jeferson0306 deleted the feat/inscricao-estadual branch September 4, 2026 08:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant