Skip to content

Commit

Permalink
apply prettier fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
querolita committed Feb 12, 2025
1 parent cc0ff82 commit 27dddf2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 112 deletions.
11 changes: 1 addition & 10 deletions src/examples/zkapps/hashing/hash.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
Hash,
Field,
SmartContract,
state,
State,
method,
Permissions,
Bytes,
} from 'o1js';
import { Hash, Field, SmartContract, state, State, method, Permissions, Bytes } from 'o1js';

let initialCommitment = Field(0);
class Bytes32 extends Bytes(32) {}
Expand Down
18 changes: 9 additions & 9 deletions src/lib/provable/crypto/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ const Hash = {
/**
* The SHA2 hash function with an output length of 224 | 256 | 384 | 512 bits.
*/
SHA2: {
/**
* Hashes the given bytes using SHA2.
*
* This is an alias for `Gadgets.SHA2.hash(length,bytes)`.\
* See {@link Gadgets.SHA2.hash} for details and usage examples.
*/
hash: Gadgets.SHA2.hash,
},
SHA2: {
/**
* Hashes the given bytes using SHA2.
*
* This is an alias for `Gadgets.SHA2.hash(length,bytes)`.\
* See {@link Gadgets.SHA2.hash} for details and usage examples.
*/
hash: Gadgets.SHA2.hash,
},

/**
* The SHA3 hash function with an output length of 256 bits.
Expand Down
6 changes: 1 addition & 5 deletions src/lib/provable/gadgets/gadgets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import {
leftShift32,
} from './bitwise.js';
import { Field } from '../wrapped.js';
import {
ForeignField,
Field3,
Sum as ForeignFieldSum,
} from './foreign-field.js';
import { ForeignField, Field3, Sum as ForeignFieldSum } from './foreign-field.js';
import { divMod32, addMod32, divMod64, addMod64 } from './arithmetic.js';
import { SHA2 } from './sha2.js';
import { SHA256 } from './sha256.js';
Expand Down
63 changes: 19 additions & 44 deletions src/lib/provable/gadgets/sha2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ export { SHA2 };

// constants for SHA2-224 and SHA2-256 §4.2.2
const K224_256 = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];

// constants for SHA2-384 and SHA2-512 §4.2.3
Expand Down Expand Up @@ -141,13 +138,13 @@ const SHA2Constants = {
H: {
// SHA2-224 §5.3.2
224: [
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511,
0x64f98fa7, 0xbefa4fa4,
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7,
0xbefa4fa4,
],
// SHA-256 §5.3.3
256: [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19,
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
],
// SHA2-384 §5.3.4
384: [
Expand Down Expand Up @@ -263,10 +260,7 @@ const SHA2 = {
// The only difference between the padding used in SHA2-224/256 and SHA2-384/512
// is the size of the word (32bit vs 64bit). In the first case, UInt32[][] is
// returned, in the second case UInt64[][] is returned.
function padding<T extends UInt32 | UInt64>(
length: Length,
data: FlexibleBytes
): T[][] {
function padding<T extends UInt32 | UInt64>(length: Length, data: FlexibleBytes): T[][] {
// create a provable Bytes instance from the input data
// the Bytes class will be static sized according to the length of the input data
let message = Bytes.from(data);
Expand Down Expand Up @@ -319,10 +313,7 @@ function padding<T extends UInt32 | UInt64>(
* or the 1024-bit message block (16-element array of UInt64).
* @returns The message schedule (64-element array of UInt32 or 80-element array of UInt64).
*/
function messageSchedule<T extends UInt32 | UInt64>(
length: Length,
M: T[]
): T[] {
function messageSchedule<T extends UInt32 | UInt64>(length: Length, M: T[]): T[] {
// §6.2.2.1 and §6.4.2.1

// Declare W as an empty array of type T[] (generic array)
Expand Down Expand Up @@ -357,11 +348,7 @@ function messageSchedule<T extends UInt32 | UInt64>(
*
* @returns The updated intermediate hash values after compression.
*/
function compression<T extends UInt32 | UInt64>(
length: Length,
[...H]: T[],
W: T[]
) {
function compression<T extends UInt32 | UInt64>(length: Length, [...H]: T[], W: T[]) {
let numWords = SHA2Constants.NUM_WORDS[length];

let k = SHA2Constants.K[length];
Expand Down Expand Up @@ -390,9 +377,7 @@ function compression<T extends UInt32 | UInt64>(
.seal();

// T2 is also unreduced
const unreducedT2 = SigmaZero(length, a).value.add(
Maj(length, a, b, c).value
);
const unreducedT2 = SigmaZero(length, a).value.add(Maj(length, a, b, c).value);

h = g;
g = f;
Expand Down Expand Up @@ -444,11 +429,7 @@ function createChunks<T extends UInt32 | UInt64>(
return chunks;
}

function intermediateHash<T extends UInt32 | UInt64>(
length: Length,
variables: T[],
H: T[]
) {
function intermediateHash<T extends UInt32 | UInt64>(length: Length, variables: T[], H: T[]) {
if (isShort(length)) {
for (let i = 0; i < 8; i++) {
H[i] = (variables[i] as UInt32).addMod32(H[i] as UInt32) as T;
Expand Down Expand Up @@ -546,8 +527,7 @@ function sigma<T extends UInt32 | UInt64>(
bits: TupleN<number, 3>,
firstShifted = false
): T {
if (u.isConstant() || !isShort(length))
return sigmaSimple(length, u, bits, firstShifted);
if (u.isConstant() || !isShort(length)) return sigmaSimple(length, u, bits, firstShifted);

// When T is UInt64, 64-bit rotation is natively supported in the gadgets.
// However, 32-bit rotation is not natively supported, thus the following:
Expand All @@ -563,12 +543,7 @@ function sigma<T extends UInt32 | UInt64>(
// decompose x into 4 chunks of size d0, d1, d2, d3
let [x0, x1, x2, x3] = exists(4, () => {
let xx = x.toBigInt();
return [
bitSlice(xx, 0, d0),
bitSlice(xx, r0, d1),
bitSlice(xx, r1, d2),
bitSlice(xx, r2, d3),
];
return [bitSlice(xx, 0, d0), bitSlice(xx, r0, d1), bitSlice(xx, r1, d2), bitSlice(xx, r2, d3)];
});

// range check each chunk
Expand Down
37 changes: 12 additions & 25 deletions src/lib/provable/gadgets/sha256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,18 @@ export { SHA256 };
const SHA256Constants = {
// constants §4.2.2
K: [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
],
// initial hash values §5.3.3
H: [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19,
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
],
};

Expand Down Expand Up @@ -176,12 +172,7 @@ function sigma(u: UInt32, bits: TupleN<number, 3>, firstShifted = false) {
// decompose x into 4 chunks of size d0, d1, d2, d3
let [x0, x1, x2, x3] = exists(4, () => {
let xx = x.toBigInt();
return [
bitSlice(xx, 0, d0),
bitSlice(xx, r0, d1),
bitSlice(xx, r1, d2),
bitSlice(xx, r2, d3),
];
return [bitSlice(xx, 0, d0), bitSlice(xx, r0, d1), bitSlice(xx, r1, d2), bitSlice(xx, r2, d3)];
});

// range check each chunk
Expand Down Expand Up @@ -268,15 +259,11 @@ function sha256Compression([...H]: UInt32[], W: UInt32[]) {
h = g;
g = f;
f = e;
e = UInt32.Unsafe.fromField(
divMod32(d.value.add(unreducedT1), 48).remainder
); // mod 32bit the unreduced field element
e = UInt32.Unsafe.fromField(divMod32(d.value.add(unreducedT1), 48).remainder); // mod 32bit the unreduced field element
d = c;
c = b;
b = a;
a = UInt32.Unsafe.fromField(
divMod32(unreducedT2.add(unreducedT1), 48).remainder
); // mod 32bit
a = UInt32.Unsafe.fromField(divMod32(unreducedT2.add(unreducedT1), 48).remainder); // mod 32bit
}

// new intermediate hash value
Expand Down
19 changes: 4 additions & 15 deletions src/lib/provable/test/sha2.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
import { ZkProgram } from '../../proof-system/zkprogram.js';
import { Bytes } from '../wrapped-classes.js';
import { Gadgets } from '../gadgets/gadgets.js';
import {
sha256 as nobleSha256,
sha224 as nobleSha224,
} from '@noble/hashes/sha256';
import {
sha384 as nobleSha384,
sha512 as nobleSha512,
} from '@noble/hashes/sha512';
import { sha256 as nobleSha256, sha224 as nobleSha224 } from '@noble/hashes/sha256';
import { sha384 as nobleSha384, sha512 as nobleSha512 } from '@noble/hashes/sha512';
import { bytes } from './test-utils.js';
import {
equivalentAsync,
equivalentProvable,
} from '../../testing/equivalent.js';
import { equivalentAsync, equivalentProvable } from '../../testing/equivalent.js';
import { Random, sample } from '../../testing/random.js';
import { expect } from 'expect';
import { Provable } from 'o1js';
Expand All @@ -39,9 +30,7 @@ for (const fn of TestFunctions) {

async function sha2(length: 224 | 256 | 384 | 512) {
let cs = await Provable.constraintSystem(() => {
Provable.witness(Bytes(length / 8), () =>
Gadgets.SHA2.hash(length, Bytes.fromString('abc'))
);
Provable.witness(Bytes(length / 8), () => Gadgets.SHA2.hash(length, Bytes.fromString('abc')));
});
assert(cs.rows == rows[lengths.indexOf(length)]);
}
Expand Down
5 changes: 1 addition & 4 deletions tests/vk-regression/plain-constraint-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ const CryptoCS = constraintSystem('Crypto', {

// mock ZkProgram API for testing

function constraintSystem(
name: string,
obj: { [K: string]: (...args: any) => void }
) {
function constraintSystem(name: string, obj: { [K: string]: (...args: any) => void }) {
let methodKeys = Object.keys(obj);

return {
Expand Down

0 comments on commit 27dddf2

Please sign in to comment.