Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Feb 12, 2025
1 parent ef9a1b4 commit cc0ff82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 92 deletions.
5 changes: 2 additions & 3 deletions src/examples/zkapps/hashing/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HashStorage } from './hash.js';
import { Mina, PrivateKey, AccountUpdate, Bytes } from 'o1js';
import { Mina, AccountUpdate, Bytes } from 'o1js';

let txn;
let proofsEnabled = true;
Expand Down Expand Up @@ -29,8 +29,7 @@ txn = await Mina.transaction(feePayer, async () => {
});
await txn.sign([feePayer.key, contractAccount.key]).send();

const initialState =
Mina.getAccount(contractAccount).zkapp?.appState?.[0].toString();
const initialState = Mina.getAccount(contractAccount).zkapp?.appState?.[0].toString();

let currentState;
console.log('Initial State', initialState);
Expand Down
111 changes: 22 additions & 89 deletions src/lib/provable/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Provable } from './provable.js';
import * as RangeCheck from './gadgets/range-check.js';
import * as Bitwise from './gadgets/bitwise.js';
import { addMod32, addMod64 } from './gadgets/arithmetic.js';
import type { Gadgets } from './gadgets/gadgets.js';
import { withMessage } from './field.js';
import { FieldVar } from './core/fieldvar.js';
import { CircuitValue, prop } from './types/circuit-value.js';
Expand Down Expand Up @@ -101,11 +100,7 @@ class UInt64 extends CircuitValue {
*/
toUInt32Clamped() {
let max = (1n << 32n) - 1n;
let field = Provable.if(
this.greaterThan(UInt64.from(max)),
Field.from(max),
this.value
);
let field = Provable.if(this.greaterThan(UInt64.from(max)), Field.from(max), this.value);
return UInt32.Unsafe.fromField(field);
}

Expand Down Expand Up @@ -135,9 +130,7 @@ class UInt64 extends CircuitValue {
if (!x.isConstant()) return x;
let xBig = x.toBigInt();
if (xBig < 0n || xBig >= 1n << BigInt(this.NUM_BITS)) {
throw Error(
`UInt64: Expected number between 0 and 2^64 - 1, got ${xBig}`
);
throw Error(`UInt64: Expected number between 0 and 2^64 - 1, got ${xBig}`);
}
return x;
}
Expand Down Expand Up @@ -186,10 +179,7 @@ class UInt64 extends CircuitValue {

y_ = y_.seal();

let q = Provable.witness(
Field,
() => new Field(x.toBigInt() / y_.toBigInt())
);
let q = Provable.witness(Field, () => new Field(x.toBigInt() / y_.toBigInt()));

RangeCheck.rangeCheckN(UInt64.NUM_BITS, q);

Expand Down Expand Up @@ -445,10 +435,7 @@ class UInt64 extends CircuitValue {
assertLessThanOrEqual(y: UInt64, message?: string) {
if (this.value.isConstant() && y.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y.value.toBigInt()];
return assert(
x0 <= y0,
message ?? `UInt64.assertLessThanOrEqual: expected ${x0} <= ${y0}`
);
return assert(x0 <= y0, message ?? `UInt64.assertLessThanOrEqual: expected ${x0} <= ${y0}`);
}
assertLessThanOrEqualGeneric(this.value, y.value, (v) =>
RangeCheck.rangeCheckN(UInt64.NUM_BITS, v, message)
Expand All @@ -474,10 +461,7 @@ class UInt64 extends CircuitValue {
assertLessThan(y: UInt64, message?: string) {
if (this.value.isConstant() && y.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y.value.toBigInt()];
return assert(
x0 < y0,
message ?? `UInt64.assertLessThan: expected ${x0} < ${y0}`
);
return assert(x0 < y0, message ?? `UInt64.assertLessThan: expected ${x0} < ${y0}`);
}
assertLessThanGeneric(this.value, y.value, (v) =>
RangeCheck.rangeCheckN(UInt64.NUM_BITS, v, message)
Expand Down Expand Up @@ -516,9 +500,7 @@ class UInt64 extends CircuitValue {
return x.value.toBigInt();
}

static fromValue<T extends AnyConstructor>(
x: number | bigint | UInt64
): InstanceType<T> {
static fromValue<T extends AnyConstructor>(x: number | bigint | UInt64): InstanceType<T> {
return UInt64.from(x) as any;
}

Expand Down Expand Up @@ -634,9 +616,7 @@ class UInt32 extends CircuitValue {
if (!x.isConstant()) return x;
let xBig = x.toBigInt();
if (xBig < 0n || xBig >= 1n << BigInt(this.NUM_BITS)) {
throw Error(
`UInt32: Expected number between 0 and 2^32 - 1, got ${xBig}`
);
throw Error(`UInt32: Expected number between 0 and 2^32 - 1, got ${xBig}`);
}
return x;
}
Expand Down Expand Up @@ -686,10 +666,7 @@ class UInt32 extends CircuitValue {

y_ = y_.seal();

let q = Provable.witness(
Field,
() => new Field(x.toBigInt() / y_.toBigInt())
);
let q = Provable.witness(Field, () => new Field(x.toBigInt() / y_.toBigInt()));

RangeCheck.rangeCheck32(q);

Expand Down Expand Up @@ -943,10 +920,7 @@ class UInt32 extends CircuitValue {
assertLessThanOrEqual(y: UInt32, message?: string) {
if (this.value.isConstant() && y.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y.value.toBigInt()];
return assert(
x0 <= y0,
message ?? `UInt32.assertLessThanOrEqual: expected ${x0} <= ${y0}`
);
return assert(x0 <= y0, message ?? `UInt32.assertLessThanOrEqual: expected ${x0} <= ${y0}`);
}
assertLessThanOrEqualGeneric(this.value, y.value, (v) =>
RangeCheck.rangeCheckN(UInt32.NUM_BITS, v, message)
Expand All @@ -971,10 +945,7 @@ class UInt32 extends CircuitValue {
assertLessThan(y: UInt32, message?: string) {
if (this.value.isConstant() && y.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y.value.toBigInt()];
return assert(
x0 < y0,
message ?? `UInt32.assertLessThan: expected ${x0} < ${y0}`
);
return assert(x0 < y0, message ?? `UInt32.assertLessThan: expected ${x0} < ${y0}`);
}
assertLessThanGeneric(this.value, y.value, (v) =>
RangeCheck.rangeCheckN(UInt32.NUM_BITS, v, message)
Expand Down Expand Up @@ -1013,9 +984,7 @@ class UInt32 extends CircuitValue {
return x.value.toBigInt();
}

static fromValue<T extends AnyConstructor>(
x: number | bigint | UInt32
): InstanceType<T> {
static fromValue<T extends AnyConstructor>(x: number | bigint | UInt32): InstanceType<T> {
return UInt32.from(x) as any;
}

Expand Down Expand Up @@ -1073,9 +1042,7 @@ class Sign extends CircuitValue {
if (x.neg().toString() === '1') return 'Negative';
throw Error(`Invalid Sign: ${x}`);
}
static fromJSON<T extends AnyConstructor>(
x: 'Positive' | 'Negative'
): InstanceType<T> {
static fromJSON<T extends AnyConstructor>(x: 'Positive' | 'Negative'): InstanceType<T> {
return (x === 'Positive' ? new Sign(Field(1)) : new Sign(Field(-1))) as any;
}
neg() {
Expand All @@ -1099,9 +1066,7 @@ class Sign extends CircuitValue {
return x.value.toBigInt() as TypesBigint.Sign;
}

static fromValue<T extends AnyConstructor>(
x: number | bigint | Sign
): InstanceType<T> {
static fromValue<T extends AnyConstructor>(x: number | bigint | Sign): InstanceType<T> {
if (x instanceof Sign) return x as any;
return new Sign(Field(x)) as any;
}
Expand Down Expand Up @@ -1228,10 +1193,7 @@ class Int64 extends CircuitValue implements BalanceChange {
},
};

fromObject(obj: {
magnitude: UInt64 | number | string | bigint;
sgn: Sign | bigint;
}) {
fromObject(obj: { magnitude: UInt64 | number | string | bigint; sgn: Sign | bigint }) {
return Int64.create(UInt64.from(obj.magnitude), Sign.fromValue(obj.sgn));
}

Expand Down Expand Up @@ -1389,11 +1351,7 @@ class Int64 extends CircuitValue implements BalanceChange {
let y_ = UInt64.from(y);
let rest = this.magnitude.divMod(y_).rest.value;
let isNonNegative = this.isNonNegative();
rest = Provable.if(
isNonNegative.or(rest.equals(0)),
rest,
y_.value.sub(rest)
);
rest = Provable.if(isNonNegative.or(rest.equals(0)), rest, y_.value.sub(rest));
return new Int64(new UInt64(rest.value));
}

Expand All @@ -1407,10 +1365,7 @@ class Int64 extends CircuitValue implements BalanceChange {
/**
* Asserts that two values are equal.
*/
assertEquals(
y: Int64 | number | string | bigint | UInt64 | UInt32,
message?: string
) {
assertEquals(y: Int64 | number | string | bigint | UInt64 | UInt32, message?: string) {
let y_ = Int64.from(y);
this.toField().assertEquals(y_.toField(), message);
}
Expand Down Expand Up @@ -1452,9 +1407,7 @@ class Int64 extends CircuitValue implements BalanceChange {

// check unique representation of 0: we can't have magnitude = 0 and sgn = -1
// magnitude + sign != -1 (this check works because magnitude >= 0)
magnitude.value
.add(sgn.value)
.assertNotEquals(-1, 'Int64: 0 must have positive sign');
magnitude.value.add(sgn.value).assertNotEquals(-1, 'Int64: 0 must have positive sign');
}
}

Expand Down Expand Up @@ -1637,12 +1590,7 @@ class UInt8 extends Struct({
if (this.value.isConstant() && y_.value.isConstant()) {
return Bool(this.toBigInt() <= y_.toBigInt());
}
return lessThanOrEqualGeneric(
this.value,
y_.value,
1n << 8n,
RangeCheck.rangeCheck8
);
return lessThanOrEqualGeneric(this.value, y_.value, 1n << 8n, RangeCheck.rangeCheck8);
}

/**
Expand All @@ -1659,12 +1607,7 @@ class UInt8 extends Struct({
if (this.value.isConstant() && y_.value.isConstant()) {
return Bool(this.toBigInt() < y_.toBigInt());
}
return lessThanGeneric(
this.value,
y_.value,
1n << 8n,
RangeCheck.rangeCheck8
);
return lessThanGeneric(this.value, y_.value, 1n << 8n, RangeCheck.rangeCheck8);
}

/**
Expand All @@ -1679,10 +1622,7 @@ class UInt8 extends Struct({
let y_ = UInt8.from(y);
if (this.value.isConstant() && y_.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y_.value.toBigInt()];
return assert(
x0 < y0,
message ?? `UInt8.assertLessThan: expected ${x0} < ${y0}`
);
return assert(x0 < y0, message ?? `UInt8.assertLessThan: expected ${x0} < ${y0}`);
}
try {
// 2^16 < p - 2^8, so we satisfy the assumption of `assertLessThanGeneric`
Expand All @@ -1704,18 +1644,11 @@ class UInt8 extends Struct({
let y_ = UInt8.from(y);
if (this.value.isConstant() && y_.value.isConstant()) {
let [x0, y0] = [this.value.toBigInt(), y_.value.toBigInt()];
return assert(
x0 <= y0,
message ?? `UInt8.assertLessThanOrEqual: expected ${x0} <= ${y0}`
);
return assert(x0 <= y0, message ?? `UInt8.assertLessThanOrEqual: expected ${x0} <= ${y0}`);
}
try {
// 2^16 < p - 2^8, so we satisfy the assumption of `assertLessThanOrEqualGeneric`
assertLessThanOrEqualGeneric(
this.value,
y_.value,
RangeCheck.rangeCheck16
);
assertLessThanOrEqualGeneric(this.value, y_.value, RangeCheck.rangeCheck16);
} catch (err) {
throw withMessage(err, message);
}
Expand Down

0 comments on commit cc0ff82

Please sign in to comment.