test.js (4802B)
1 const { expect } = require('chai') 2 const jsc = require('jsverify') 3 const ob = require('urbit-ob') 4 5 const mtg = require('../src') 6 7 // mocha, jsverify stuff 8 9 // needs to be required explicitly 10 global.crypto = require('crypto') 11 12 const ticket_bytes = jsc.elements([8, 16, 24, 32, 40, 48, 56, 64]) 13 14 // tests 15 16 describe('gen_ticket_simple', () => { 17 context("when no additional entropy provided", () => { 18 it('produces tickets of the correct bitlength', () => { 19 let prop = jsc.forall(ticket_bytes, bytes => { 20 let bits = bytes * 8 21 let ticket = mtg.gen_ticket_simple(bits) 22 let hex = ob.patq2hex(ticket) 23 let nbits = hex.length * 4 24 return (bits === nbits) 25 }) 26 27 jsc.assert(prop) 28 }) 29 }) 30 31 context("when additional entropy provided", () => { 32 it('produces tickets of the correct bitlength', () => { 33 let prop = jsc.forall(ticket_bytes, bytes => { 34 let bits = bytes * 8 35 let addl = Buffer.from("look at all this entropy") 36 let ticket = mtg.gen_ticket_simple(bits, addl) 37 let hex = ob.patq2hex(ticket) 38 let nbits = hex.length * 4 39 return (bits === nbits) 40 }) 41 42 jsc.assert(prop) 43 }) 44 }) 45 46 }) 47 48 describe('gen_ticket_more', () => { 49 50 context("when no additional entropy provided", () => { 51 it('produces tickets of the correct bitlength', async () => { 52 let given_bits = 384 53 let ticket = await mtg.gen_ticket_more(given_bits) 54 let hex = ob.patq2hex(ticket) 55 let expected_bits = hex.length * 4 56 expect(expected_bits).to.equal(given_bits) 57 }) 58 }) 59 60 context("when additional entropy provided", () => { 61 it('produces tickets of the correct bitlength', async () => { 62 let given_bits = 384 63 let addl = Buffer.from("you'll never predict this") 64 let ticket = await mtg.gen_ticket_more(given_bits, addl) 65 let hex = ob.patq2hex(ticket) 66 let expected_bits = hex.length * 4 67 expect(expected_bits).to.equal(given_bits) 68 }) 69 }) 70 71 }) 72 73 describe('gen_ticket_drbg', () => { 74 75 context("when no additional entropy provided", () => { 76 it('produces tickets of the correct bitlength', async () => { 77 let given_bits = 384 78 let ticket = await mtg.gen_ticket_drbg(given_bits) 79 let hex = ob.patq2hex(ticket) 80 let expected_bits = hex.length * 4 81 expect(expected_bits).to.equal(given_bits) 82 }) 83 }) 84 85 context("when additional entropy provided", () => { 86 it('produces tickets of the correct bitlength', async () => { 87 let given_bits = 384 88 let addl = Buffer.from("you'll really never predict this") 89 let ticket = await mtg.gen_ticket_drbg(given_bits, addl) 90 let hex = ob.patq2hex(ticket) 91 let expected_bits = hex.length * 4 92 expect(expected_bits).to.equal(given_bits) 93 }) 94 }) 95 96 }) 97 98 describe('shard', () => { 99 100 it('throws on non-@q input', () => { 101 expect(() => mtg.shard('flubblub', 3, 2)).to.throw() 102 }) 103 104 it("using 3/2 sharding, produces shards that are recombinable", () => { 105 let prop = jsc.forall(ticket_bytes, bytes => { 106 let bits = bytes * 8 107 let ticket = mtg.gen_ticket_simple(bits) 108 let shards = mtg.shard(ticket, 3, 2) 109 let combined0 = mtg.combine(shards.slice(0, 2)) 110 let combined1 = mtg.combine(shards.slice(1, 3)) 111 let combined2 = mtg.combine([shards[0], shards[2]]) 112 let combined3 = mtg.combine(shards) 113 114 return ( 115 (ticket === combined0) && 116 (ticket === combined1) && 117 (ticket === combined2) && 118 (ticket === combined3) 119 ) 120 }) 121 122 jsc.assert(prop) 123 }) 124 125 it("using 5/3 sharding, produces shards that are recombinable", () => { 126 let prop = jsc.forall(ticket_bytes, bytes => { 127 let bits = bytes * 8 128 let ticket = mtg.gen_ticket_simple(bits) 129 let shards = mtg.shard(ticket, 5, 3) 130 131 let combined0 = mtg.combine([shards[0], shards[1], shards[2]]) 132 let combined1 = mtg.combine([shards[0], shards[1], shards[3]]) 133 let combined2 = mtg.combine([shards[0], shards[1], shards[4]]) 134 135 let combined3 = mtg.combine([shards[0], shards[2], shards[3]]) 136 let combined4 = mtg.combine([shards[0], shards[2], shards[4]]) 137 138 let combined5 = mtg.combine([shards[1], shards[2], shards[3]]) 139 let combined6 = mtg.combine([shards[1], shards[2], shards[4]]) 140 141 let combined7 = mtg.combine([shards[2], shards[3], shards[4]]) 142 143 let combined8 = mtg.combine(shards) 144 145 return ( 146 (ticket === combined0) && 147 (ticket === combined1) && 148 (ticket === combined2) && 149 (ticket === combined3) && 150 (ticket === combined4) && 151 (ticket === combined5) && 152 (ticket === combined6) && 153 (ticket === combined7) && 154 (ticket === combined8) 155 ) 156 }) 157 158 jsc.assert(prop) 159 }) 160 161 }) 162 163