Skip to content

Commit

Permalink
wip: recurz
Browse files Browse the repository at this point in the history
  • Loading branch information
chiefbiiko committed Feb 21, 2024
1 parent 8de4d09 commit 61d4618
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const BN254_PRIME =
* @returns {Buffer} Big-endian bytes
*/
function toBytesBE(b, len) {
console.log("toBytesBE b", b)
if (typeof b !== "bigint") {
b = BigInt(b)
}
Expand Down Expand Up @@ -59,8 +60,8 @@ function total(...x) {

/**
* Coerces given (string) scalar(s) to bigint.
* @param {number|string|bigint} x Scalar
* @return {bigint}
* @param {any} x Scalar
* @return {any} Bigint scalar or vector
*/
function toBigInt(x) {
if (Array.isArray(x)) {
Expand All @@ -76,6 +77,23 @@ function toBigInt(x) {
}
}

/**
* Recursicely pushes given inputs as field elements onto the out array.
* @param {any} inputs
* @param {[]number} out
* @returns {[number]} out array populated with all field elements of inputs
*/
function pushFieldElements(prime, inputs, out) {
return inputs.reduce((acc, cur) => {
if (Array.isArray(cur)) {
return pushFieldElements(cur, acc)
} else {
Array.prototype.push.apply(acc, toBytesBE(cur % prime, 32))
return acc
}
}, out)
}

/**
* Serializes given gnark inputs to a binary full witness.
* @param {Object} inputs Must only contain bigint (arrays), no nested objects.
Expand All @@ -94,7 +112,7 @@ export default function serialize(
opts.publicOnly === true || opts.publicOnly === false
? opts.publicOnly
: false
const out = []
let out = []

// sort public/secret inputs
const pubs = []
Expand Down Expand Up @@ -122,30 +140,36 @@ export default function serialize(
toBytesBE(total(pubs, publicOnly ? [] : secs), 4),
)



//TODO mk dis recursive
// push actual field elements for public inputs
for (const pub of pubs) {
if (Array.isArray(pub)) {
for (const p of pub) {
Array.prototype.push.apply(out, toBytesBE(p % prime, 32))
}
} else {
Array.prototype.push.apply(out, toBytesBE(pub % prime, 32))
}
}
out = pushFieldElements(prime, pubs, out)
console.log("out1", out)
// for (const pub of pubs) {
// if (Array.isArray(pub)) {
// for (const p of pub) {
// Array.prototype.push.apply(out, toBytesBE(p % prime, 32))
// }
// } else {
// Array.prototype.push.apply(out, toBytesBE(pub % prime, 32))
// }
// }

//TODO mk dis recursive
// push actual field elements for secret inputs
for (const sec of secs) {
if (Array.isArray(sec)) {
for (const s of sec) {
console.log("typeof s, s", typeof s, s)
Array.prototype.push.apply(out, toBytesBE(s % prime, 32))
}
} else {
Array.prototype.push.apply(out, toBytesBE(sec % prime, 32))
}
}
out = pushFieldElements(prime, secs, out)
console.log("out2", out)
// for (const sec of secs) {
// if (Array.isArray(sec)) {
// for (const s of sec) {
// console.log("typeof s, s", typeof s, s)
// Array.prototype.push.apply(out, toBytesBE(s % prime, 32))
// }
// } else {
// Array.prototype.push.apply(out, toBytesBE(sec % prime, 32))
// }
// }

return Buffer.from(out)
}

0 comments on commit 61d4618

Please sign in to comment.