The best kittens, technology, and video games blog in the world.

Saturday, March 30, 2019

stringify-any

a185975 jewel-2 by The.Rohit from flickr (CC-NC)

So here's my second interesting Javascript npm package - stringify-any.

Javascript is a basically very low quality language where everything only kinda sorta mostly works. In this case I ran into problems of inspecting objects.

Problem this package is targeting

If you want to inspect obj in a browser, you'd usually just do:

console.log(obj)

And the browser will generate appropriate interactive widget you can click around to look around.

That's of course not possible outside the browser. So what if you want to print the object?

console.log(`${obj}`)

The most obvious way will fail with [object Object].

JSON sort of helps, and we can do this:

console.log(JSON.stringify(obj))

It's hard to read due to bad indentation, but copy and pasting JSON into pretty printer is a basic web development skill, so it's kinda tolerable.

And then you try some modern Javascript, and somewhere in that obj there's a nested new Set([1, 2, 3]), which quietly turns into {}, making you want to drop Javascript and just code Ruby in browser or Clojurescript or something.

What you can do now

Just get the package, import stringify function, and call it:

import "stringify-any" as stringify
console.log(stringify(obj))

It will do the right thing.

It handles all cases I could think of, like undefined, -0, infinities, NaN, DateMapSet.

It also sort of handles WeakMapWeakSetFunction - we can't show what's inside, but we can at least show the type.

Because Javascript is Javascript I'm sure I missed something. Please report, github issues are the best place.

I couldn't find any other package that does this correctly (found a lot which tried but failed), but then it's totally possible something like this already exists.

Friday, March 15, 2019

How democratic are countries really

BOY was that EXHAUSTING!! by Lisa Zins from flickr (CC-BY)

I mentioned many times how "democratic" countries are actually not all that democratic. It's time to quantify that.

I have no time to data mine all the Tanzanias and Liechtensteins of the world, so I limited this analysis to OECD member countries plus 3 extra not-yet-OECD EU members. Also excluded is Switzerland, as it has a completely unique system of government.

Limitations

This ranking only covers how representative elections are, and only for Lower House, Upper House, Head of State, and Head of Government, as most countries either have those four roles, or a single entity covers two of those roles in a way that's easy to score.

This excludes all other elections, such as regional elections, super-national elections, possibly judicial elections, and such.

This also excludes any issues such as term limits, mandatory retirement age, campaigning, party financing, relative division of power between those branches, any rule of law issues and such. There's a lot to cover beyond this list, but it's very country-specific and difficult to score objectively.

It's all about how will of the voters is translated into results. If votes are thrown away (by non-proportional system), or voters are forced to vote for their non-preferred candidates (by FPTP and such), it gets lower scores.

If a very insignificant number of seats are reserved (like current VP, former presidents, ethnic minority groups), I'm not counting that either way.

I compiled the information from Wikipedia, and it's possible that I made a few mistakes.

Scoring

Power is divided differently between branches of government in different countries, but for some reasonable baseline the following mix is used:
  • Lower House - 40%
  • Upper House - 15%
  • Head of Government - 30%
  • Head of State - 15%
If there's no Upper House, Lower House is counted for both roles.

If elected Head of State is also Head of Government, they're counted for both roles. 

If parliament elects Head of Government and/or Head of State, Lower House is counted for both roles. This also includes all cases where technically Head of State nominates Head of Government, but they need some kind of parliamentary confirmation, as that's in practice the same as direct election by the parliament.

Scoring for parliamentary elections

  • Proportional representation (with threshold less 4%) - 100%
  • Proportional representation (with threshold 4% or higher) - 80%
  • Single Transferable Vote - 80%
  • Mixed Member Representation - 60% - there's a lot of variety here, so it's a non-committal scoring
  • Proportional representation (with threshold 7% or higher) - 60%
  • FPTP with runoff - 60%
  • Proportional representation (with threshold 10% or higher) - 40%
  • Proportional representation with majority bonus - 40%
  • FPTP - 40%
  • FPTP with vastly disproportional sized districts (like US states) - 20%
  • Unelected - 0%
Proportional representation always has some threshold. For example a no-threshold elections with single national list for 100-member parliament basically forces de facto 1% threshold. And any kind of regional lists de facto results in low thresholds, even without any distortionary intend.

Medium-threshold systems are distortionary in practice. For example in Poland's electoral threshold alone was responsible for overturning results of 1993 and 2015 elections.

High threshold systems and bonus for largest party are intentional distortions targeted against minor parties.

FPTP is extreme distortionary in theory and practice, and it only somewhat works if districting just so happen to be random and not too biased. In practice every single FPTP system in the world has severe bias against certain demographies and parties.

FPTP with vastly disproportional sized districts is borderline if it should count as elections at all.

Unelected Upper House obviously gets no points.

STV and Mixed Member Representation systems cover various systems which are generally less representative than pure proportional representation would be, but it's difficult to score them properly.

Scoring for presidential elections

  • Single Transferable Vote - 100%
  • Majority with runoff - 80%
  • Plurality without runoff - 60%
  • Electoral College - 40%
  • Monarch - 0%
This should be fairly obvious. There's no way to elect a single position proportionally, so whichever system most reasonably approximates Condorcet winner is best.

None of the countries currently hold Prime Minister elections (Israel had them for a while), so either Lower House score or Head of State score counts.

Results

  • 100 Israel
  • 97 Finland
  • 97 Portugal
  • 94 Iceland
  • 91 Romania
  • 85 Denmark
  • 85 Luxembourg
  • 80 Bulgaria
  • 80 Croatia
  • 80 Estonia
  • 80 Latvia
  • 80 Slovakia
  • 80 Slovenia
  • 79 Chile
  • 76 Spain
  • 74 Czech Republic
  • 74 Poland
  • 71 Ireland
  • 70 Netherlands
  • 68 Australia
  • 68 Austria
  • 68 Norway
  • 68 Sweden
  • 63 France
  • 63 Lithuania
  • 60 Hungary
  • 60 Italy
  • 60 Mexico
  • 60 South Korea
  • 58 Turkey
  • 56 Belgium
  • 51 Germany
  • 51 Japan
  • 51 New Zealand
  • 40 Greece
  • 37 United States
  • 28 Canada
  • 28 United Kingdom

Raw data


CountryLower HouseUpper HouseHead of GovernmentHead of State
AustraliaSTVSTVParliamentMonarch
Austria4%UnelectedParliamentRunoff
Belgium5%UnelectedParliamentMonarch
Bulgaria4%noneParliamentRunoff
CanadaFPTPUnelectedParliamentMonarch
Chile0%2 per statePresidentRunoff
Croatia5%noneParliamentRunoff
Czech Republic5%FPTPParliamentRunoff
Denmark2%noneParliamentMonarch
Estonia5%noneParliamentParliament
Finland0%noneParliamentRunoff
FranceRunoff FPTPMixedParliamentRunoff
GermanyMixedUnelectedParliamentParliament
Greece3%+bonusnoneParliamentParliament
HungaryMixednoneParliamentParliament
Iceland0%noneParliamentPlurality
IrelandSTVUnelectedParliamentSTV
Israel3.25%noneParliamentParliament
ItalyMixedMixedParliamentParliament
JapanMixedMixedParliamentMonarch
Latvia5%noneParliamentParliament
LithuaniaMixednoneParliamentRunoff
Luxembourg0%noneParliamentMonarch
MexicoMixedMixedPresidentPlurality
Netherlands0%UnelectedParliamentMonarch
New ZealandMixednoneParliamentMonarch
Norway4%noneParliamentMonarch
Poland5%FPTPParliamentRunoff
Portugal0%noneParliamentRunoff
Romania0%MixedParliamentRunoff
Slovakia5%noneParliamentRunoff
Slovenia4%noneParliamentRunoff
South KoreaMixednonePresidentPlurality
Spain3%FPTPParliamentMonarch
Sweden4%noneParliamentMonarch
Turkey10%nonePresidentRunoff
United KingdomFPTPUnelectedParliamentMonarch
United StatesFPTP2 per statePresidentElectoral College

Commentary

There's a lot of "democratic" countries with very questionable scores. It's usually not quite as bad, as the general pattern is that more representative branches of government have more power than less representative branches.

So monarchs tend to have a lot less power than presidents, unelected Upper Houses tend to have a lot less power than elected Upper Houses and so on. Notably Emperor of Japan (sort of Head of State) has no role whatsoever, and British House of Lords (Upper House) is basically meaningless.

If this list was weighted by actual power, it would look a bit better.

The main exception to this pattern is US, which has it exactly backwards, and the (relatively speaking, none of them are actually good) most representative House of Representatives (Lower House) is least powerful, while the even less representative Senate (Upper House) and Presidency have almost all power between them.

Consequences

It all might have seemed like a purely theoretical issue a decade ago, but right now of the bottom 4 countries, 3 are in a major unending political crisis, and Canada's luck might run out just as it did for UK and US.

Of 13 countries scored 60% or lower, only Canada, Mexico, and New Zealand avoided major political crises recently, unless there were some that I missed. On top of the list, such situations are fairly rare.

One thing that's not directly connected with electoral systems, but would greatly improve quality of democracy, is implementing strict term limits, especially for top positions like presidents and prime ministers. Eternal prime ministers cause all sorts of problems, which could be easily avoided if their party just picked someone else for the role.

Emojiful ls in your terminal

lsd is a really sweet looking ls replacement, unfortunately it's awkward to install, so here are the steps for OSX:

Install lsd and font with emoji


brew tap caskroom/fonts
brew cask install font-hack-nerd-font
brew install lsd

Change font in your terminal

For iTerm2, it's: Preferences > Profiles > Text.
Then for both Font and Non-ASCII Font, choose "Hack Regular Nerd Font Complete".
It's a bit smaller than default font so I increased point size from 12pt to 14pt, but it's up to you.


For builtin Terminal app, it's Preferences > Profiles > Font.


Because this setting is per-profile, if you use multiple profiles, or some non-default profile, you might need to set it in multiple places.

Make it default

Depending on your setup, add this to your ~/.bashrc or ~/.profile or ~/.bash_profile:

alias ls="lsd"

Or depending If you use another shell, edit ~/.zshrc or such file.

Tuesday, March 12, 2019

Map mode for Hearts of Iron 4 resistance and suppression system for 1.6


I previously published this data for HoI4 1.0-1.5, and simply updated that post each release, as there were minimal differences.

1.6 changes thing a lot, so it's best to just publish new maps.

For some theory behind this map, check out this post as nothing really changed. It's still a horrible system that forces endless tedious and boring micromanagement on the player, and which completely cripples the AI.

If Paradox wants to do resistance, it should be possible to just put units anywhere in some big region like "France", and they'd apply to all its states, not this nonsense of individually placing every damn 1cav brigade by alt-tabbing out of the game and hoping you clicked things right. Or just remove it completely, the game already applies flat static penalty to occupied lands.

EU4 figured that out, why can't HoI4? Also contrary to romantic visions of La Résistance, all that resistance was historically close to irrelevant minor hassle, except maybe in Yugoslavia.

Anyway, here are the maps. Click for full resolution version.

Gentlest:


Gentle:

Harsh:

Harshest:

Friday, March 01, 2019

Challenges for February 2019 SecTalks London

Snow Leopard Cub Looking Curious by Eric Kilby from flickr (CC-SA)

I ran another round of London SecTalks CTF.

There were 12 regular challenges, and 1 super-hard bonus challenge. Only 10/13 got at least one solve during the event, so maybe difficulty or number of challenges were a bit too high.

Challenge files and code used to generate them available on github.

There are no answers below, but some serious hints which might make it too easy.

For previous rounds, see posts about September 2017, November 2017, May 2018, July 2018, and October 2018 CTFs.

from_future_import_flag (5 points)

It was a simple program which could tell you the flag if you run it in year 2020.

Censored SVG (10 points)

A simple HTML file with SVG image on it. The image contains flag covered with some censoring square. SVG image is base64 encoded for one small layer.

Hello RISC-V (15 points)

A simple binary which will give you the flag if you run it. It only runs on RISC-V, so you'll need to figure out a way to do so.

Crystal (20 points)

Password validator binary compiled from Crystal. Binary contains a lot of extra stuff, but validating function shouldn't look all that strange.

Powerpoint (25 points)

PowerPoint PPTX file encrypted with XOR. Flag is on the slides.

RISC-V Verify (30 points)

RISC-V binary validating password. It might be easier to run static analysis, but you should still understand basics of RISC-V assembly. Or you could run gdb on RISC-V emulator.

OCaml (35 points)

Password validator binary compiled from OCaml. OCaml uses unusual integer representation, so it's extra layer of confusion.

It had zero solves during the event.

LOLHEX (40 points)

English text encoded by hex encoding, but which of 16 digits is which is scrambled. Simple statistical analysis should do here.

Imba (45 points)

Password validator written in Imba. It will tell you if you get your flag right. It can be solved by static analysis (data driving logic is easy to see), or in-browser, possibly by by attaching DOM breakpoints.

It had zero solves during the event.

Catzip (50 points)

For mandatory meme challenge. It's a zip format made by gzipping something, then turning it into a PNG containing cat emojis. There's two cat emojis, 8 per row, so you can probably guess where it's going.

FPGA (55 points)

A slight variant of challenge from previous time. It's a small circuit, and you need to find inputs which give positive result. This time circuit has NAND, AND, NOR, OR, and XOR gates.

It's highly advisable to use Z3 or similar for it.

Monoalphabetische Chiffrierung (60 points)

German text encoded with monoalphabetic cipher, and then turned into Unicode Fraktur.

It's actually very easy, as punctuation, capitalization, digits etc. were not really scrambled.

LOL64 (100 points)

For super hard bonus challenge, it's English text encoded by Base64 variant, but which of 64 digits is which is scrambled.

Understandably, it had zero solves during the event.

It's definitely solvable in principle, but I'm not really sure how to write such solver. It can be seen as breaking a key of 64 6-bit parts, so 512 bits total. Knowing that it's all ASCII and simple statistical methods get a lot of those bits, but from that point on it would take some creative statistical analysis. Big difficulty is just size of the key, and how a single error in decoding can mess up all following statistical analysis, and cost a lot of time.

I'm sure there are also some ways to circumvent this whole process, and find exact encoded text from some simple statistical properties, but I didn't try it this way as it's less fun.