Skip to content

Instantly share code, notes, and snippets.

@woudsma
Forked from tylerburdsall/lazy-cartesian-product.py
Last active June 15, 2020 01:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woudsma/654f822037cc65da4fc7e5b8dc912007 to your computer and use it in GitHub Desktop.
Save woudsma/654f822037cc65da4fc7e5b8dc912007 to your computer and use it in GitHub Desktop.
import math
from bigfloat import *
class LazyCartesianProduct:
def __init__(self, sets, context):
self.sets = sets
self.context = context
self.divs = []
self.mods = []
self.maxSize = BigFloat.exact(1)
self.precompute()
def precompute(self):
for i in self.sets:
self.maxSize = mul(self.maxSize, BigFloat.exact(len(i)), context=self.context)
factor = BigFloat.exact(1)
for i in range((len(self.sets) - 1), -1, -1):
items = BigFloat.exact(str(len(self.sets[i])), precision=self.context.precision)
self.divs.insert(0, factor)
self.mods.insert(0, items)
factor = mul(factor, items, context=self.context)
def entryAt(self, n):
if less(n, BigFloat.exact(0)) or greaterequal(n, self.maxSize):
raise IndexError
combination = []
for i in range(0, len(self.sets)):
d = div(n, self.divs[i], context=self.context)
m = mod(floor(d, context=self.context), self.mods[i], context=self.context)
combination.append(int(m))
return combination
@woudsma
Copy link
Author

woudsma commented Nov 11, 2018

Example main.py

from bigfloat import *
from random import getrandbits
from LazyCartesianProduct import LazyCartesianProduct

bits = 2
size = 32
sets = [(range(bits)) for x in range(size ** 2)]
total = BigFloat.exact(bits ** (size ** 2))
p = total.precision
context = precision(p)
cp = LazyCartesianProduct(sets, context)

# get last index
index = sub(total, BigFloat.exact(1), context=context)
# get arbitrary index
index = sub(total, BigFloat.exact('2e99', precision=p), context=context)
# get random index
index = mul(total, BigFloat.exact('0.' + str(getrandbits(p)), precision=p), context=context)

print cp.entryAt(index)

@tylerburdsall
Copy link

I did notice a bug in this gist, on line 24 of lazy-cartesian-product.py it should be:

if less(n, BigFloat.exact(0)) or greaterequal(n, self.maxSize):

That way a user can check for the 0th index, which is a valid index.

@woudsma
Copy link
Author

woudsma commented Nov 23, 2018

I did notice a bug in this gist, on line 24 of lazy-cartesian-product.py it should be:

if less(n, BigFloat.exact(0)) or greaterequal(n, self.maxSize):

That way a user can check for the 0th index, which is a valid index.

Thanks! Updated.

@practical-engelbart
Copy link

I use this to automate the steps, part of a cloud-init script I made.

#!/bin/bash
apt-update
apt-get upgrade -yq > /dev/null 2>&1
apt-get install -yq apt-transport-https ca-certificates curl gnupg-agent software-properties-common > /dev/null 2>&1
apt-get install -yq apache2-utils wget libnss3-tools ccze jq > /dev/null 2>&1

mkdir -p /etc/systemd/system/docker.service.d/
mkdir /root/.docker
mkdir /etc/docker/certs.d/

curl -L https://github.com/cloudflare/cfssl/releases/download/v1.4.1/cfssl_1.4.1_linux_amd64 -o cfssl
chmod +x cfssl
mv cfssl /usr/local/bin/cfssl
curl -L https://github.com/cloudflare/cfssl/releases/download/v1.4.1/cfssljson_1.4.1_linux_amd64 -o cfssljson
chmod +x cfssljson
mv cfssljson /usr/local/bin/cfssljson
curl -L https://github.com/cloudflare/cfssl/releases/download/v1.4.1/cfssl-certinfo_1.4.1_linux_amd64 -o cfssl-certinfo
chmod +x cfssl-certinfo
mv cfssl-certinfo /usr/local/bin/cfssl-certinfo

cat > /root/.docker/ca-config.json <<EOF
{
    "signing": {
        "default": {
            "expiry": "8760h"
        },
        "profiles": {
            "server": {
                "expiry": "8760h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "server auth"
                ]
            },
            "client": {
                "expiry": "8760h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "client auth"
                ]
            }
        }
    }
}
EOF

cat > /root/.docker/ca-csr.json <<EOF
{
    "CN": "localdomain",
    "hosts": [
        "localhost",
        "localhost.localdomain"
    ],
    "key": {
        "algo": "ecdsa",
        "size": 521
    },
    "names": [
        {
            "C": "US",
            "ST": "CA",
            "L": "San Francisco",
            "OU": "CA",
            "O": "<organization>"
        }
    ]
}
EOF

cd /root/.docker
cfssl gencert -initca ca-csr.json | cfssljson -bare ca

cat > /root/.docker/server-csr.json <<EOF
{
    "CN": "server",
    "hosts": [
        "localhost.localdomain",
        "localhost",
        "docker.internal",
        "127.0.0.1",
        "127.0.1.1",
        "172.17.0.1",
        "172.20.20.1"
    ],
    "key": {
        "algo": "ecdsa",
        "size": 521
    },
    "names": [
        {
            "C": "US",
            "ST": "CA",
            "L": "San Francisco",
            "OU": "Server",
            "O": "<organization"
        }
    ]
}
EOF

cat > /root/.docker/client-csr.json <<EOF
{
    "CN": "client",
    "hosts": [""],
    "key": {
        "algo": "ecdsa",
        "size": 521
    },
    "names": [
        {
            "C": "US",
            "ST": "CA",
            "L": "San Francisco",
            "OU": "Client",
            "O": "<organization>"
        }
    ]
}
EOF

cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
--config=ca-config.json \
-profile=server \
server-csr.json | cfssljson -bare server

cfssl gencert \
-ca=ca.pem \
-ca-key=ca-key.pem \
-config=ca-config.json \
-profile=client \
client-csr.json | cfssljson -bare client

cp ca.pem /etc/docker/certs.d/ca.crt
cp client.pem /etc/docker/certs.d/client.pem
cp client-key.pem /etc/docker/certs.d/client.key
chmod -R 600 /etc/docker/certs.d/
chmod 444 /etc/docker/certs.d/ca.crt
chmod 444 /etc/docker/certs.d/client.pem
chmod 400 /etc/docker/certs.d/client.key
chown -R root:docker /etc/docker/certs.d/
chmod 400 ca-key.pem
chmod 444 ca.pem
chmod 400 server-key.pem
chmod 444 server.pem
chmod 400 client-key.pem
chmod 444 client.pem

cp ca.pem /usr/local/share/ca-certificates/localhost.crt
update-ca-certificates

cat > /etc/systemd/system/docker.service.d/override.conf <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 --tlsverify --tlscacert /root/.docker/ca.pem --tlscert /root/.docker/server.pem --tlskey /root/.docker/server-key.pem -H unix:///var/run/docker.sock
 
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

echo 'export DOCKER_CERT_PATH=/etc/docker/certs.d/' >> /etc/profile
echo 'export DOCKER_HOST=tcp://127.0.0.1:2376'>> /etc/profile 
echo 'export DOCKER_TLS_VERIFY=1' >> /etc/profile

systemctl daemon-reload
systemctl restart docker

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