Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jun 21, 2017
0 parents commit 3b99cd8
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Mathias Buus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# xsalsa20

WIP - nothing to see here

```
npm install xsalsa20
```

## Usage

``` js
var xsalsa20 = require('xsalsa20')
```

## License

MIT
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var fs = require('fs')
var toUint8Array = require('base64-to-uint8array')

var WASM = toUint8Array(fs.readFileSync(__dirname + '/xsalsa20.wasm', 'base64'))
var mod
var mem
var rdy

exports.WASM_SUPPORTED = typeof WebAssembly !== 'undefined'

function ready (cb) {
if (!cb) cb = noop
if (!exports.WASM_SUPPORTED) return cb()
if (!rdy) rdy = WebAssembly.instantiate(WASM).then(setup)
return rdy.then(cb).catch(cb)
}

function noop () {}

function setup (w) {
mod = w.instance.exports
mem = new Uint8Array(w.instance.exports.memory.buffer)
}

ready(function () {
mod.core_hsalsa20(0, 32, 48)
console.log(new Buffer(mem.slice(0, 32)))
})
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "xsalsa20",
"version": "0.0.0",
"description": "XSalsa20 implemented in Javascript and WebAssembly",
"main": "index.js",
"dependencies": {
"base64-to-uint8array": "^1.0.0"
},
"devDependencies": {},
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/xsalsa20.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://github.com/mafintosh/xsalsa20/issues"
},
"homepage": "https://github.com/mafintosh/xsalsa20"
}
Binary file added xsalsa20.wasm
Binary file not shown.
96 changes: 96 additions & 0 deletions xsalsa20.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
(module
(memory (export "memory") 10 10)

(func (export "core_hsalsa20") (param $out_ptr i32) (param $in_ptr i32) (param $key_ptr i32)
;; uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8,
;; x9, x10, x11, x12, x13, x14, x15;
;; int i;

(local $x0 i32)
(local $x1 i32)
(local $x2 i32)
(local $x3 i32)
(local $x4 i32)
(local $x5 i32)
(local $x6 i32)
(local $x7 i32)
(local $x8 i32)
(local $x9 i32)
(local $x10 i32)
(local $x11 i32)
(local $x12 i32)
(local $x13 i32)
(local $x14 i32)
(local $x15 i32)
(local $i i32)

(set_local $x0 (i32.const 0x61707865))
(set_local $x5 (i32.const 0x3320646e))
(set_local $x10 (i32.const 0x79622d32))
(set_local $x15 (i32.const 0x6b206574))

(set_local $x1 (i32.load (get_local $key_ptr)))
(set_local $x2 (i32.load (i32.add (get_local $key_ptr) (i32.const 4))))
(set_local $x3 (i32.load (i32.add (get_local $key_ptr) (i32.const 8))))
(set_local $x4 (i32.load (i32.add (get_local $key_ptr) (i32.const 12))))
(set_local $x11 (i32.load (i32.add (get_local $key_ptr) (i32.const 16))))
(set_local $x12 (i32.load (i32.add (get_local $key_ptr) (i32.const 20))))
(set_local $x13 (i32.load (i32.add (get_local $key_ptr) (i32.const 24))))
(set_local $x14 (i32.load (i32.add (get_local $key_ptr) (i32.const 28))))
(set_local $x6 (i32.load (get_local $in_ptr)))
(set_local $x7 (i32.load (i32.add (get_local $in_ptr) (i32.const 4))))
(set_local $x8 (i32.load (i32.add (get_local $in_ptr) (i32.const 8))))
(set_local $x9 (i32.load (i32.add (get_local $in_ptr) (i32.const 12))))

;; rounds
(set_local $i (i32.const 20))
(block $end_loop
(loop $start_loop
(br_if $end_loop (i32.eq (get_local $i) (i32.const 0)))
(set_local $x4 (i32.xor (get_local $x4) (i32.rotl (i32.add (get_local $x0) (get_local $x12)) (i32.const 7))))
(set_local $x8 (i32.xor (get_local $x8) (i32.rotl (i32.add (get_local $x4) (get_local $x0)) (i32.const 9))))
(set_local $x12 (i32.xor (get_local $x12) (i32.rotl (i32.add (get_local $x8) (get_local $x4)) (i32.const 13))))
(set_local $x0 (i32.xor (get_local $x0) (i32.rotl (i32.add (get_local $x12) (get_local $x8)) (i32.const 18))))
(set_local $x9 (i32.xor (get_local $x9) (i32.rotl (i32.add (get_local $x5) (get_local $x1)) (i32.const 7))))
(set_local $x13 (i32.xor (get_local $x13) (i32.rotl (i32.add (get_local $x9) (get_local $x5)) (i32.const 9))))
(set_local $x1 (i32.xor (get_local $x1) (i32.rotl (i32.add (get_local $x13) (get_local $x9)) (i32.const 13))))
(set_local $x5 (i32.xor (get_local $x5) (i32.rotl (i32.add (get_local $x1) (get_local $x13)) (i32.const 18))))
(set_local $x14 (i32.xor (get_local $x14) (i32.rotl (i32.add (get_local $x10) (get_local $x6)) (i32.const 7))))
(set_local $x2 (i32.xor (get_local $x2) (i32.rotl (i32.add (get_local $x14) (get_local $x10)) (i32.const 9))))
(set_local $x6 (i32.xor (get_local $x6) (i32.rotl (i32.add (get_local $x2) (get_local $x14)) (i32.const 13))))
(set_local $x10 (i32.xor (get_local $x10) (i32.rotl (i32.add (get_local $x6) (get_local $x2)) (i32.const 18))))
(set_local $x3 (i32.xor (get_local $x3) (i32.rotl (i32.add (get_local $x15) (get_local $x11)) (i32.const 7))))
(set_local $x7 (i32.xor (get_local $x7) (i32.rotl (i32.add (get_local $x3) (get_local $x15)) (i32.const 9))))
(set_local $x11 (i32.xor (get_local $x11) (i32.rotl (i32.add (get_local $x7) (get_local $x3)) (i32.const 13))))
(set_local $x15 (i32.xor (get_local $x15) (i32.rotl (i32.add (get_local $x11) (get_local $x7)) (i32.const 18))))
(set_local $x1 (i32.xor (get_local $x1) (i32.rotl (i32.add (get_local $x0) (get_local $x3)) (i32.const 7))))
(set_local $x2 (i32.xor (get_local $x2) (i32.rotl (i32.add (get_local $x1) (get_local $x0)) (i32.const 9))))
(set_local $x3 (i32.xor (get_local $x3) (i32.rotl (i32.add (get_local $x2) (get_local $x1)) (i32.const 13))))
(set_local $x0 (i32.xor (get_local $x0) (i32.rotl (i32.add (get_local $x3) (get_local $x2)) (i32.const 18))))
(set_local $x6 (i32.xor (get_local $x6) (i32.rotl (i32.add (get_local $x5) (get_local $x4)) (i32.const 7))))
(set_local $x7 (i32.xor (get_local $x7) (i32.rotl (i32.add (get_local $x6) (get_local $x5)) (i32.const 9))))
(set_local $x4 (i32.xor (get_local $x4) (i32.rotl (i32.add (get_local $x7) (get_local $x6)) (i32.const 13))))
(set_local $x5 (i32.xor (get_local $x5) (i32.rotl (i32.add (get_local $x4) (get_local $x7)) (i32.const 18))))
(set_local $x11 (i32.xor (get_local $x11) (i32.rotl (i32.add (get_local $x10) (get_local $x9)) (i32.const 7))))
(set_local $x8 (i32.xor (get_local $x8) (i32.rotl (i32.add (get_local $x11) (get_local $x10)) (i32.const 9))))
(set_local $x9 (i32.xor (get_local $x9) (i32.rotl (i32.add (get_local $x8) (get_local $x11)) (i32.const 13))))
(set_local $x10 (i32.xor (get_local $x10) (i32.rotl (i32.add (get_local $x9) (get_local $x8)) (i32.const 18))))
(set_local $x12 (i32.xor (get_local $x12) (i32.rotl (i32.add (get_local $x15) (get_local $x14)) (i32.const 7))))
(set_local $x13 (i32.xor (get_local $x13) (i32.rotl (i32.add (get_local $x12) (get_local $x15)) (i32.const 9))))
(set_local $x14 (i32.xor (get_local $x14) (i32.rotl (i32.add (get_local $x13) (get_local $x12)) (i32.const 13))))
(set_local $x15 (i32.xor (get_local $x15) (i32.rotl (i32.add (get_local $x14) (get_local $x13)) (i32.const 18))))
(set_local $i (i32.sub (get_local $i) (i32.const 2)))
(br $start_loop)
)
)

(i32.store (get_local $out_ptr) (get_local $x0))
(i32.store (i32.add (get_local $out_ptr) (i32.const 4)) (get_local $x5))
(i32.store (i32.add (get_local $out_ptr) (i32.const 8)) (get_local $x10))
(i32.store (i32.add (get_local $out_ptr) (i32.const 12)) (get_local $x15))
(i32.store (i32.add (get_local $out_ptr) (i32.const 16)) (get_local $x6))
(i32.store (i32.add (get_local $out_ptr) (i32.const 20)) (get_local $x7))
(i32.store (i32.add (get_local $out_ptr) (i32.const 24)) (get_local $x8))
(i32.store (i32.add (get_local $out_ptr) (i32.const 28)) (get_local $x9))
)
)

0 comments on commit 3b99cd8

Please sign in to comment.