-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,50 @@ | ||
import { Field, Provable, Bool } from 'o1js'; | ||
import { PackingPlant } from '../PackingPlant.js'; | ||
|
||
const L = 254; // 254 1-byte uints fit in one Field | ||
const SIZE_IN_BITS = 1n; | ||
|
||
export function PackedBoolFactory(l: number) { | ||
class PackedBool_ extends PackingPlant(Bool, l, SIZE_IN_BITS) { | ||
static toAuxiliary(value?: { packed: Field } | undefined): Array<Bool> { | ||
const auxiliary = Provable.witness(Provable.Array(Bool, l), () => { | ||
let bools_ = new Array(l); | ||
bools_.fill(0n); | ||
let packedN; | ||
if (value && value.packed) { | ||
packedN = value.packed.toBigInt(); | ||
} else { | ||
throw new Error('No Packed Value Provided'); | ||
} | ||
for (let i = 0; i < l; i++) { | ||
bools_[i] = packedN & ((1n << SIZE_IN_BITS) - 1n); | ||
packedN >>= SIZE_IN_BITS; | ||
} | ||
return bools_.map((x) => Bool.fromJSON(Boolean(x))); | ||
}); | ||
return auxiliary; | ||
export class PackedBool extends PackingPlant(Bool, L, SIZE_IN_BITS) { | ||
static pack(aux: Array<Bool>): Field { | ||
let f = aux[0].toField(); | ||
const n = Math.min(aux.length, L); | ||
for (let i = 1; i < n; i++) { | ||
const c = Field((2n ** SIZE_IN_BITS) ** BigInt(i)); | ||
f = f.add(aux[i].toField().mul(c)); | ||
} | ||
return f; | ||
} | ||
|
||
static pack(aux: Array<Bool>): Field { | ||
let f = aux[0].toField(); | ||
const n = Math.min(aux.length, l); | ||
for (let i = 1; i < n; i++) { | ||
const c = Field((2n ** SIZE_IN_BITS) ** BigInt(i)); | ||
f = f.add(aux[i].toField().mul(c)); | ||
static unpack(f: Field): Bool[] { | ||
const unpacked = Provable.witness(Provable.Array(Bool, L), () => { | ||
let bools_ = new Array(L); | ||
bools_.fill(0n); | ||
let packedN; | ||
if (f) { | ||
packedN = f.toBigInt(); | ||
} else { | ||
throw new Error('No Packed Value Provided'); | ||
} | ||
return f; | ||
} | ||
for (let i = 0; i < L; i++) { | ||
bools_[i] = packedN & ((1n << SIZE_IN_BITS) - 1n); | ||
packedN >>= SIZE_IN_BITS; | ||
} | ||
return bools_.map((x) => Bool.fromJSON(Boolean(x))); | ||
}); | ||
return unpacked; | ||
} | ||
|
||
static fromAuxiliary(aux: Array<Bool>): PackedBool_ { | ||
const packed = PackedBool_.pack(aux); | ||
return new PackedBool_(packed, aux); | ||
} | ||
static fromBools(bools: Array<Bool>): PackedBool { | ||
const packed = PackedBool.pack(bools); | ||
return new PackedBool(packed); | ||
} | ||
|
||
static fromBooleans(bigints: Array<boolean>): PackedBool_ { | ||
const uint32s = bigints.map((x) => Bool(x)); | ||
return this.fromAuxiliary(uint32s); | ||
} | ||
static fromBooleans(booleans: Array<boolean>): PackedBool { | ||
const bools = booleans.map((x) => Bool(x)); | ||
return PackedBool.fromBools(bools); | ||
} | ||
|
||
toBooleans(): Array<boolean> { | ||
return PackedBool_.unpack(this.packed).map((x) => x.toBoolean()); | ||
} | ||
toBooleans(): Array<boolean> { | ||
return PackedBool.unpack(this.packed).map((x) => x.toBoolean()); | ||
} | ||
return PackedBool_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,50 @@ | ||
import { Field, Provable, UInt32 } from 'o1js'; | ||
import { PackingPlant } from '../PackingPlant.js'; | ||
|
||
const L = 7; // 7 32-byte uints fit in one Field | ||
const SIZE_IN_BITS = 32n; | ||
|
||
export function PackedUInt32Factory(l: number) { | ||
class PackedUInt32_ extends PackingPlant(UInt32, l, SIZE_IN_BITS) { | ||
static toAuxiliary(value?: { packed: Field } | undefined): Array<UInt32> { | ||
const auxiliary = Provable.witness(Provable.Array(UInt32, l), () => { | ||
let uints_ = new Array(l); | ||
uints_.fill(0n); | ||
let packedN; | ||
if (value && value.packed) { | ||
packedN = value.packed.toBigInt(); | ||
} else { | ||
throw new Error('No Packed Value Provided'); | ||
} | ||
for (let i = 0; i < l; i++) { | ||
uints_[i] = packedN & ((1n << SIZE_IN_BITS) - 1n); | ||
packedN >>= SIZE_IN_BITS; | ||
} | ||
return uints_.map((x) => UInt32.from(x)); | ||
}); | ||
return auxiliary; | ||
export class PackedUInt32 extends PackingPlant(UInt32, L, SIZE_IN_BITS) { | ||
static pack(unpacked: Array<UInt32>): Field { | ||
let f = unpacked[0].value; | ||
const n = Math.min(unpacked.length, L); | ||
for (let i = 1; i < n; i++) { | ||
const c = Field((2n ** SIZE_IN_BITS) ** BigInt(i)); | ||
f = f.add(unpacked[i].value.mul(c)); | ||
} | ||
return f; | ||
} | ||
|
||
static pack(aux: Array<UInt32>): Field { | ||
let f = aux[0].value; | ||
const n = Math.min(aux.length, l); | ||
for (let i = 1; i < n; i++) { | ||
const c = Field((2n ** SIZE_IN_BITS) ** BigInt(i)); | ||
f = f.add(aux[i].value.mul(c)); | ||
static unpack(f: Field): UInt32[] { | ||
const auxiliary = Provable.witness(Provable.Array(UInt32, L), () => { | ||
let uints_ = new Array(L); | ||
uints_.fill(0n); | ||
let packedN; | ||
if (f) { | ||
packedN = f.toBigInt(); | ||
} else { | ||
throw new Error('No Packed Value Provided'); | ||
} | ||
return f; | ||
} | ||
for (let i = 0; i < L; i++) { | ||
uints_[i] = packedN & ((1n << SIZE_IN_BITS) - 1n); | ||
packedN >>= SIZE_IN_BITS; | ||
} | ||
return uints_.map((x) => UInt32.from(x)); | ||
}); | ||
return auxiliary; | ||
} | ||
|
||
static fromAuxiliary(aux: Array<UInt32>): PackedUInt32_ { | ||
const packed = PackedUInt32_.pack(aux); | ||
return new PackedUInt32_(packed, aux); | ||
} | ||
static fromUInt32s(uint32s: Array<UInt32>): PackedUInt32 { | ||
const packed = PackedUInt32.pack(uint32s); | ||
return new PackedUInt32(packed); | ||
} | ||
|
||
static fromBigInts(bigints: Array<bigint>): PackedUInt32_ { | ||
const uint32s = bigints.map((x) => UInt32.from(x)); | ||
return this.fromAuxiliary(uint32s); | ||
} | ||
static fromBigInts(bigints: Array<bigint>): PackedUInt32 { | ||
const uint32s = bigints.map((x) => UInt32.from(x)); | ||
return PackedUInt32.fromUInt32s(uint32s); | ||
} | ||
|
||
toBigInts(): Array<bigint> { | ||
return PackedUInt32_.unpack(this.packed).map((x) => x.toBigint()); | ||
} | ||
toBigInts(): Array<bigint> { | ||
return PackedUInt32.unpack(this.packed).map((x) => x.toBigint()); | ||
} | ||
return PackedUInt32_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.