ZenCode

Reference Material

Getting started:

Install ZenRoom

Docker Version

// Install ZenRoom DockerHub image
$ docker pull decodeproject/zenroom

// Run it
$ docker container start -it zenroom

Bin code on Linux

Follow https://dev.zenroom.org/#/pages/zencode-cookbook-intro

// Download Binary on https://zenroom.org/#downloads -> if Raspberry, use ARM
$ sudo cp /home/pi/Downloads/zenroom /bin
$ sudo chmod 777 ./zenroom

// go on your project directory and test
$ zenroom
//output to say it is working
[*] Zenroom v1.0.0+6798e47 - secure crypto language VM
 .  Zenroom is Copyright (C) 2017-2020 by the Dyne.org foundation
 .  For the original source code and documentation go to https://zenroom.org
 .  Zenroom is free software: you can redistribute it and/or modify
 .  it under the terms of the GNU Affero General Public License as
 .  published by the Free Software Foundation, either version 3 of the
 .  License, or (at your option) any later version.
 .  Zenroom is distributed in the hope that it will be useful,
 .  but WITHOUT ANY WARRANTY; without even the implied warranty of
 .  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 .  GNU Affero General Public License for more details.
 .  You should have received a copy of the GNU Affero General Public License
 .  along with this program.  If not, see http://www.gnu.org/licenses/
 .  using default configuration
 .  System memory manager in use
 .  ECDH curve is SECP256K1
 .  Memory in use: 300 KB
 .  reading Zencode from stdin

// To execude a zencode "arrayGenerator.zen" with following code
    Given nothing
    When I create the array of '2' random objects of '8' bits
    And I rename the 'array' to 'myTinyArray'
    And I create the array of '4' random objects of '32' bits
    And I rename the 'array' to 'myAverageArray'
    And I create the array of '8' random objects of '128' bits
    And I rename the 'array' to 'myBigFatArray'
    Then print all data 

$ zenroom -z arrayGenerator.zen | tee myFirstRandomArray.json
$ nano myFirstRandomeArra.json
// You should see the random number generated

Node.JS Module import of zenroom

// Create the project
$ mkdir test
$ npm init
$ nano index.js //put content of zenroom code as here under in "viaJavascript"
//Install zenroom node package
$ npm install zenroom
// You can run your javascrip
$ node index.js

Detail Javascript for Zenroom

Reference material:

Testing Zen Script code

On CLI

Check the sample code here under: https://apiroom.net for all zencode script source here under

Via Javascript

Sample 1 - Hello World (Lua Script)

Reference material: https://dev.zenroom.org/#/pages/how-to-embed

// save index.js (and make you npm init, plus npm install zenroom)
const {zenroom_exec} = require("zenroom");
const script = `print("Hello World!")`
zenroom_exec(script).then(({result}) => console.log(result)) //=> "Hello World!"

Sample 2 - Generate Key Pair

How to use ZenCode with node.js Javascript https://dev.zenroom.org/#/pages/javascript?id=💾-install

// Create index.js
const { zenroom_exec, zencode_exec } = require('zenroom')

// Zencode Hello World!

const zencode = `
Scenario 'ecdh': Create the keypair
Given that I am known as 'Alice'
When I create the keypair
Then print my data
    `  
	
zencode_exec(zencode)
	.then((result) => {
		console.log(result.result);
	})
	.catch((error) => {
		throw new Error(error);
    });
// Execude the code
$ node index.js

 .  System memory manager in use
{"Alice":{"keypair":{"private_key":"VDhxszbEdlW9TgfwmNqeyIlfdFA1/FQ9W23RrRstC18=","public_key":"BB3ia/5go2wIulhJKIYvEfGBsMdiTdH09D5yZ+WuUsfGZIzS05ReFW3utHveV4bmjoTrPBC7q496G9zlKROjXlE="}}}

Sample 3 - Encrypt with Password

// Create your NPM project
$ npm init
$ npm install zenroom

// create the index.js file
const { zenroom_exec, zencode_exec } = require('zenroom')

const zencode=`
Scenario 'ecdh': Encrypt a message with the password 
Given that I have a 'string' named 'password' 
Given that I have a 'string' named 'header' 
Given that I have a 'string' named 'message' 
When I encrypt the secret message 'message' with 'password' 
Then print the 'secret message'
`
const input=`
{
	"myName": "User123456"
}
`
const inputkey=`
{
	"password": "myVerySecretPassword"
}
`
const inputdata=`
{
	"header": "A very important secret",
	"message": "Dear Bob, your name is too short, goodbye - Alice."
}
`
zencode_exec(zencode,{keys:inputkey, data:inputdata})
	.then((result) => {
		console.log(result.result);
	})
	.catch((error) => {
		throw new Error(error);
    });

Sample 4: Sign with Eliptic Curve Keys (ECDH Signature)

Reference: https://dev.zenroom.org/#/pages/zencode-scenarios-ecdh?id=create-the-signature-of-an-object

They use EDSA Secp256k1, more explanation on https://cryptobook.nakov.com/digital-signatures/ecdsa-sign-verify-messages

//index.js
const { zenroom_exec, zencode_exec } = require('zenroom')

const zencode=`
Scenario 'ecdh': create the signature of an object 
Given I am 'Alice' 
Given I have my 'keypair' 
Given that I have a 'string' named 'myMessage' inside 'mySecretStuff' 
Given I have a 'string array' named 'myStringArray' 
When I create the signature of 'myStringArray' 
When I rename the 'signature' to 'myStringArray.signature' 
When I create the signature of 'keypair' 
When I rename the 'signature' to 'keypair.signature' 
When I create the signature of 'myMessage' 
When I rename the 'signature' to 'myMessage.signature' 
Then print the 'myMessage' 
Then print the 'myMessage.signature' 
Then print the 'myStringArray' 
Then print the 'myStringArray.signature' 
Then print the 'keypair' 
Then print the 'keypair.signature'
`
const inputkey=`
{
	"Alice": {
		"keypair": {
			"private_key": "Aku7vkJ7K01gQehKELav3qaQfTeTMZKgK+5VhaR3Ui0=",
			"public_key": "BBCQg21VcjsmfTmNsg+I+8m1Cm0neaYONTqRnXUjsJLPa8075IYH+a9w2wRO7rFM1cKmv19Igd7ntDZcUvLq3xI="
		}
	}
}
`
const inputdata=`
{
	"mySecretStuff": {
		"myMessage": "Dear Bob, your name is too short, goodbye - Alice."
	},
	"myStringArray": [
		"Hello World! This is my string array, element [0]",
		"Hello World! This is my string array, element [1]",
		"Hello World! This is my string array, element [2]"
	]
}
`
zencode_exec(zencode,{keys:inputkey, data:inputdata})
	.then((result) => {
		console.log(result.result);
	})
	.catch((error) => {
		throw new Error(error);
    });
// run it: node index.js
// output
$ {
   "keypair": {
      "private_key": "Aku7vkJ7K01gQehKELav3qaQfTeTMZKgK+5VhaR3Ui0=",
      "public_key": "BBCQg21VcjsmfTmNsg+I+8m1Cm0neaYONTqRnXUjsJLPa8075IYH+a9w2wRO7rFM1cKmv19Igd7ntDZcUvLq3xI="
   },
   "keypair.signature": {
      "r": "O9Nz+GuH6PS0w584gPEQtFDJ9cH58k5DqIXTrPmf3WA=",
      "s": "ltvX/t7A1kk34K3e8xWSeQqaUuCsDpbTnHlQQ9g2PCY="
   },
   "myMessage": "Dear Bob, your name is too short, goodbye - Alice.",
   "myMessage.signature": {
      "r": "gYq1PqrQp7GpccZXDgTJVN1feSPcHe+OS9aGiuuw/pU=",
      "s": "ArPkzbPfAw9r2a4KuNXHPL8DM/r7SELdPAfP4WVco3U="
   },
   "myStringArray": [
      "Hello World! This is my string array, element [0]",
      "Hello World! This is my string array, element [1]",
      "Hello World! This is my string array, element [2]"
   ],
   "myStringArray.signature": {
      "r": "BzfOASY6gFsrdRbOiP2g6I9IgNbKcTiPkmf/5KnA0xw=",
      "s": "by+IOVBwdOQ64/9H8SH3Iq2+6gQC6FsoXEyldsfJ6Qo="
   }
} 

Sample 5: Verify Signature

// index.js
const { zenroom_exec, zencode_exec } = require('zenroom')

const zencode=`
rule check version 1.0.0 
Scenario 'ecdh': Bob verifies the signature from Alice 
Given that I am known as 'Bob' 
Given I have a 'public key' from 'Alice' 
Given I have a 'string' named 'myMessage' 
Given I have a 'signature' named 'myMessage.signature' 
Given I have a 'string array' named 'myStringArray' 
Given I have a 'signature' named 'myStringArray.signature' 
When I verify the 'myMessage' has a signature in 'myMessage.signature' by 'Alice' 
When I verify the 'myStringArray' has a signature in 'myStringArray.signature' by 'Alice' 
Then print 'Zenroom certifies that signatures are all correct!' as 'string' 
Then print the 'myMessage' 
`
const inputkey=`
{
	"Alice": {
		"public_key": "BBCQg21VcjsmfTmNsg+I+8m1Cm0neaYONTqRnXUjsJLPa8075IYH+a9w2wRO7rFM1cKmv19Igd7ntDZcUvLq3xI="
	}
}
`
const inputdata=`
{
	"keypair": {
		"public_key": "BBCQg21VcjsmfTmNsg+I+8m1Cm0neaYONTqRnXUjsJLPa8075IYH+a9w2wRO7rFM1cKmv19Igd7ntDZcUvLq3xI="
	},
	"keypair.signature": {
		"r": "UlrY1tYFXt0ClYncu51upl/wPPgGthEajxAxs8Ia/g4=",
		"s": "rkpKyzEJ6Sg3bgyyYDqYwZH7gDnbMBO6W0+yzpmej0o="
	},
	"myMessage": "Dear Bob, your name is too short, goodbye - Alice.",
	"myMessage.signature": {
		"r": "vWerszPubruWexUib69c7IU8Dxy1iisUmMGC7h7arDw=",
		"s": "nSjxT+JAP56HMRJjrLwwB6kP+mluYySeZcG8JPBGcpY="
	},
	"myStringArray": [
		"Hello World! This is my string array, element [0]",
		"Hello World! This is my string array, element [1]",
		"Hello World! This is my string array, element [2]"
	],
	"myStringArray.signature": {
		"r": "B8qrQqYSWaTf5Q16mBCjY1tfsD4Cf6ZSMJTHCCV8Chg=",
		"s": "S1/Syca6+XozVr5P9fQ6/AkQ+fJTMfwc063sbKmZ5B4="
	}
}
`
zencode_exec(zencode,{keys:inputkey, data:inputdata})
	.then((result) => {
		console.log(result.result);
	})
	.catch((error) => {
		throw new Error(error);
    });
    
 // run it: node index.js
 // output
 $ {"myMessage":"Dear Bob, your name is too short, goodbye - Alice.","output":"Zenroom_certifies_that_signatures_are_all_correct!"}

Last updated