Skip to content

Commit

Permalink
Merge pull request #1339 from o1-labs/merge-rampup-main
Browse files Browse the repository at this point in the history
Merge rampup -> main
  • Loading branch information
mitschabaude authored Dec 20, 2023
2 parents 776992a + 9ae2dfe commit d389fee
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bindings
10 changes: 10 additions & 0 deletions src/examples/benchmarks/keccak-witness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Hash, Bytes, Provable } from 'o1js';

let Bytes32 = Bytes(32);

console.time('keccak witness');
Provable.runAndCheck(() => {
let bytes = Provable.witness(Bytes32.provable, () => Bytes32.random());
Hash.Keccak256.hash(bytes);
});
console.timeEnd('keccak witness');
26 changes: 13 additions & 13 deletions src/lib/gadgets/bitwise.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ let Bitwise = ZkProgram({
leftShift32: {
privateInputs: [Field],
method(a: Field) {
Gadgets.rangeCheck32(a);
return Gadgets.leftShift32(a, 12);
},
},
Expand Down Expand Up @@ -138,7 +139,9 @@ await Bitwise.compile();
);
});

await equivalentAsync({ from: [uint(64), uint(64)], to: field }, { runs: 3 })(
const runs = 2;

await equivalentAsync({ from: [uint(64), uint(64)], to: field }, { runs })(
(x, y) => {
return x ^ y;
},
Expand All @@ -148,7 +151,7 @@ await equivalentAsync({ from: [uint(64), uint(64)], to: field }, { runs: 3 })(
}
);

await equivalentAsync({ from: [maybeField], to: field }, { runs: 3 })(
await equivalentAsync({ from: [maybeField], to: field }, { runs })(
(x) => {
return Fp.not(x, 254);
},
Expand All @@ -157,7 +160,7 @@ await equivalentAsync({ from: [maybeField], to: field }, { runs: 3 })(
return proof.publicOutput;
}
);
await equivalentAsync({ from: [maybeField], to: field }, { runs: 3 })(
await equivalentAsync({ from: [maybeField], to: field }, { runs })(
(x) => {
if (x > 2n ** 254n) throw Error('Does not fit into 254 bit');
return Fp.not(x, 254);
Expand All @@ -168,10 +171,7 @@ await equivalentAsync({ from: [maybeField], to: field }, { runs: 3 })(
}
);

await equivalentAsync(
{ from: [maybeField, maybeField], to: field },
{ runs: 3 }
)(
await equivalentAsync({ from: [maybeField, maybeField], to: field }, { runs })(
(x, y) => {
if (x >= 2n ** 64n || y >= 2n ** 64n)
throw Error('Does not fit into 64 bits');
Expand All @@ -183,7 +183,7 @@ await equivalentAsync(
}
);

await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
await equivalentAsync({ from: [field], to: field }, { runs })(
(x) => {
if (x >= 2n ** 64n) throw Error('Does not fit into 64 bits');
return Fp.rot(x, 12n, 'left');
Expand All @@ -194,7 +194,7 @@ await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
}
);

await equivalentAsync({ from: [uint(32)], to: uint(32) }, { runs: 30 })(
await equivalentAsync({ from: [uint(32)], to: uint(32) }, { runs })(
(x) => {
return Fp.rot(x, 12n, 'left', 32n);
},
Expand All @@ -204,7 +204,7 @@ await equivalentAsync({ from: [uint(32)], to: uint(32) }, { runs: 30 })(
}
);

await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
await equivalentAsync({ from: [field], to: field }, { runs })(
(x) => {
if (x >= 2n ** 64n) throw Error('Does not fit into 64 bits');
return Fp.leftShift(x, 12);
Expand All @@ -215,9 +215,9 @@ await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
}
);

await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
await equivalentAsync({ from: [field], to: field }, { runs })(
(x) => {
if (x >= 2n ** 64n) throw Error('Does not fit into 64 bits');
if (x >= 1n << 32n) throw Error('Does not fit into 32 bits');
return Fp.leftShift(x, 12, 32);
},
async (x) => {
Expand All @@ -226,7 +226,7 @@ await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
}
);

await equivalentAsync({ from: [field], to: field }, { runs: 3 })(
await equivalentAsync({ from: [field], to: field }, { runs })(
(x) => {
if (x >= 2n ** 64n) throw Error('Does not fit into 64 bits');
return Fp.rightShift(x, 12);
Expand Down
10 changes: 7 additions & 3 deletions src/lib/keccak.unit-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Keccak } from './keccak.js';
import { ZkProgram } from './proof_system.js';
import { equivalent, equivalentAsync } from './testing/equivalent.js';
import {
equivalentProvable,
equivalent,
equivalentAsync,
} from './testing/equivalent.js';
import {
keccak_224,
keccak_256,
Expand Down Expand Up @@ -47,13 +51,13 @@ for (let length of lengths) {
let inputBytes = bytes(preimageLength);
let outputBytes = bytes(length / 8);

equivalent({ from: [inputBytes], to: outputBytes, verbose: true })(
equivalentProvable({ from: [inputBytes], to: outputBytes, verbose: true })(
testImplementations.sha3[length],
(x) => Keccak.nistSha3(length, x),
`sha3 ${length}`
);

equivalent({ from: [inputBytes], to: outputBytes, verbose: true })(
equivalentProvable({ from: [inputBytes], to: outputBytes, verbose: true })(
testImplementations.preNist[length],
(x) => Keccak.preNist(length, x),
`keccak ${length}`
Expand Down
9 changes: 9 additions & 0 deletions src/lib/provable-types/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { assert } from '../gadgets/common.js';
import { chunkString } from '../util/arrays.js';
import { Provable } from '../provable.js';
import { UInt8 } from '../int.js';
import { randomBytes } from '../../bindings/crypto/random.js';

export { Bytes, createBytes, FlexibleBytes };

Expand Down Expand Up @@ -64,6 +65,14 @@ class Bytes {
return this.from(bytes);
}

/**
* Create random {@link Bytes} using secure builtin randomness.
*/
static random() {
let bytes = randomBytes(this.size);
return this.from(bytes);
}

/**
* Create {@link Bytes} from a hex string.
*
Expand Down
2 changes: 1 addition & 1 deletion src/mina
Submodule mina updated 101 files

0 comments on commit d389fee

Please sign in to comment.