Skip to content

Instantly share code, notes, and snippets.

@emilbayes
Created June 19, 2019 16:14
Show Gist options
  • Save emilbayes/380fa5ba23f7a12d4acf3e8c285db6bb to your computer and use it in GitHub Desktop.
Save emilbayes/380fa5ba23f7a12d4acf3e8c285db6bb to your computer and use it in GitHub Desktop.
const accounts = 1
const bits = 42
function forin (n, exp) {
res = []
for (var i = 0; i < n; i++) {
res.push(exp(n))
}
return res
}
const args = [
forin(accounts, (n) => `private field commitment_${n}`).join(', '),
forin(accounts, (n) => `private field[256] amount_${n}`).join(', '),
forin(accounts, (n) => `private field amounts_${n}`).join(', '),
forin(accounts, (n) => `private field[256] blinding_factor_${n}`).join(', '),
`private field[${bits * accounts}] bits`,
`public field total_amount`
].join(', ')
const HexpBits = BigInt('2417296792044260459589534796306265266991365743098572196690216026133643768250').toString(2).padStart(256, '0').split('')
const script = `
import "ecc/edwardsAdd.code" as add
import "ecc/edwardsScalarMult.code" as scalarMult
import "ecc/babyjubjubParams.code" as jubjub
// ACCOUNTS = ${accounts}
// BITS = ${bits}
def main (${args}) -> (bool):
G = jubjub()
H = scalarMult([${HexpBits.join()}], [G[4], G[5]], G)
field sum_amount = 0
field res = 0
${forin(accounts, (j) => `
commitment_${j} == add(scalarMult(amount_${j}, [G[4], G[5]], G), scalarMult(blinding_factor_${j}, H, G), G)[0]
res = 0
for field i in 0..${bits} do
field bit = bits[i + j* ${bits}]
bit * bit == bit
res = res + bit * (2 ** i)
endfor
amounts[${j}] == res
sum_amount = sum_amount + res
return total_amount == sum_amount
`)}`
console.log(script)
import "ecc/edwardsAdd.code" as add
import "ecc/edwardsScalarMult.code" as scalarMult
import "ecc/babyjubjubParams.code" as jubjub
// ACCOUNTS = 1
// BITS = 42
def main (private field commitment_1, private field[256] amount_1, private field amounts_1, private field[256] blinding_factor_1, private field[42] bits, public field total_amount) -> (bool):
G = jubjub()
H = scalarMult([0,0,0,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,0,0,1,0,0,0,0,1,1,1,0,0,1,0,1,0,0,1,0,1,0,0,1,1,1,0,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,0,1,0,1,0,1,0,0,0,1,0,0,1,1,0,0,0,0,1,0,0,1,0,1,1,0,0,1,1,0,0,1,1,1,1,0,0,1,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,0,1,0,1,1,1,0,1,1,1,0,0,1,1,1,0,1,1,1,1,0,0,1,1,1,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,0,1,1,0,1,1,1,0,0,1,1,1,1,0,0,0,1,0,0,0,1,1,1,0,1,0,0,1,0,0,1,0,1,0,0,0,1,0,0,1,0,0,1,0,1,0,1,1,0,1,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,1,1,1,0,1,0], [G[4], G[5]], G)
field sum_amount = 0
field res = 0
commitment_1 == add(scalarMult(amount_1, [G[4], G[5]], G), scalarMult(blinding_factor_1, H, G), G)
res = 0
for field i in 0..42 do
field bit = bits[i + j* 42]
bit * bit == bit
res = res + bit * (2 ** i)
endfor
amounts[1] == res
sum_amount = sum_amount + res
return total_amount == sum_amount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment