Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLAKE2b gadget #1767

Open
wants to merge 43 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d357853
add blake2b
boray Jul 18, 2024
cb93722
add or gadget and improvements
boray Jul 22, 2024
31abb6e
Merge branch 'feature/blake2b' of github.com:o1-labs/o1js into featur…
boray Jul 23, 2024
2846396
add comments and tests
boray Jul 23, 2024
9dd79aa
fix bitwise test
boray Jul 24, 2024
24c743a
add comments to gadgets
boray Jul 24, 2024
044cbe1
dump vks
boray Jul 24, 2024
81bc5a9
add doccoments and test
boray Jul 26, 2024
58a07a0
add yoni's fix #1763
boray Jul 26, 2024
72877d7
Merge remote-tracking branch 'origin/main' into feature/blake2b
boray Jul 26, 2024
10836c9
add UInt32.or()
boray Jul 26, 2024
00cdb75
Update src/lib/provable/gadgets/bitwise.ts
boray Jul 28, 2024
5db6974
update vk-regression.json
boray Jul 28, 2024
63b939c
fixes and styling
boray Jul 29, 2024
e6317f1
remove question marks
boray Jul 30, 2024
95a71d3
Merge remote-tracking branch 'origin/feature/divmod32-quotientbits-bo…
boray Jul 30, 2024
90daf62
optimize g function additions
boray Aug 21, 2024
1938405
hardcode IV as UInt64
boray Aug 21, 2024
5e2b28a
dump vks
boray Aug 21, 2024
a86a106
simplify or
boray Aug 22, 2024
c38118e
fix formatting
boray Aug 22, 2024
5bba739
fix divMod64 range check
boray Aug 22, 2024
e9c5a29
Merge remote-tracking branch 'origin/v2' into feature/blake2b
boray Aug 22, 2024
555070b
fix elliptic curve to match v2
boray Aug 23, 2024
d071e7b
fix formatting
boray Aug 23, 2024
9b32c4d
style(gadgets.ts): wrap comments for readability
boray Aug 29, 2024
70169e3
fix(bitwise.unit-test.ts): correct typo
boray Sep 5, 2024
acd32bb
perf: optimize divmod rangechecks
boray Sep 5, 2024
ec5b6a7
fix: remove incorrect optimization
boray Sep 5, 2024
b741027
fix(arithmetic.ts): fix range assertion
boray Sep 6, 2024
a34d8c2
docs: clarify output description
boray Sep 6, 2024
6ba2968
fix(blake2b.ts): add range check for digestLength
boray Sep 7, 2024
d841a51
fix(blake2b.ts): range check input byte length
boray Sep 7, 2024
d999c09
test: add test vector
boray Sep 9, 2024
e5bd574
feat: add last block flag and state type
boray Sep 9, 2024
1c5f3bd
fix: counter logic
boray Sep 9, 2024
7f650b1
chore: update reference
boray Sep 9, 2024
e545799
docs: add comments
boray Sep 9, 2024
368ae88
docs: add more comments
boray Sep 10, 2024
cb80ccf
perf: reduce row number
boray Sep 20, 2024
20a0eac
fix: uint64 construction
boray Sep 20, 2024
cceeef5
test: remove in-circuit equivalence tests
boray Sep 20, 2024
4304c0f
test: add in-circuit tests for blake2b
boray Sep 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/examples/crypto/blake2b/blake2b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Bytes, Gadgets, ZkProgram } from 'o1js';

export { BLAKE2BProgram, Bytes12 };

class Bytes12 extends Bytes(12) {}

let BLAKE2BProgram = ZkProgram({
name: 'blake2b',
publicOutput: Bytes(32),
methods: {
blake2b: {
privateInputs: [Bytes12],
async method(xs: Bytes12) {
return Gadgets.BLAKE2B.hash(xs, 32);
},
},
},
});
23 changes: 23 additions & 0 deletions src/examples/crypto/blake2b/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Bytes12, BLAKE2BProgram } from './blake2b.js';

console.time('compile');
await BLAKE2BProgram.compile();
console.timeEnd('compile');

let preimage = Bytes12.fromString('hello world!');

console.log('blake2b rows:', (await BLAKE2BProgram.analyzeMethods()).blake2b.rows);

console.time('prove');
let proof = await BLAKE2BProgram.blake2b(preimage);
console.timeEnd('prove');
let isValid = await BLAKE2BProgram.verify(proof);

console.log('digest:', proof.publicOutput.toHex());

if (
proof.publicOutput.toHex() !==
'4fccfb4d98d069558aa93e9565f997d81c33b080364efd586e77a433ddffc5e2'
)
throw new Error('Invalid blake2b digest!');
if (!isValid) throw new Error('Invalid proof');
6 changes: 6 additions & 0 deletions src/lib/provable/crypto/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ const Hash = {
return Keccak.preNist(512, bytes);
},
},

BLAKE2B: {
hash(bytes: Bytes) {
return Gadgets.BLAKE2B.hash(bytes);
},
},
};
58 changes: 56 additions & 2 deletions src/lib/provable/gadgets/arithmetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { provableTuple } from '../types/struct.js';
import { Field } from '../wrapped.js';
import { assert } from '../../util/errors.js';
import { Provable } from '../provable.js';
import { rangeCheck32, rangeCheckN } from './range-check.js';
import { rangeCheck32, rangeCheck64, rangeCheckN } from './range-check.js';

export { divMod32, addMod32 };
export { divMod32, addMod32, divMod64, addMod64 };

function divMod32(n: Field, nBits = 64) {
assert(
Expand Down Expand Up @@ -56,3 +56,57 @@ function divMod32(n: Field, nBits = 64) {
function addMod32(x: Field, y: Field) {
return divMod32(x.add(y), 33).remainder;
}

function divMod64(n: Field, nBits = 128) {
assert(
nBits >= 0 && nBits < 255,
`nBits must be in the range [0, 255), got ${nBits}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 255 not included because of Pasta curves?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why we don't include 255. I followed the range checking practice used in divMod32. Maybe @mitschabaude knows the reason?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the field size is less than 2^255, there are 255 bit integers that have two different representations in the field (in fact that's true for almost all 255 bit integers since the field size is just slightly over 2^254)

That non-uniqueness would make the divmod gadget unsound: the prover could pick between two different quotient/remainder splits, one of which is wrong.

);

// calculate the number of bits allowed for the quotient to avoid overflow
const quotientBits = Math.max(0, nBits - 64);

if (n.isConstant()) {
assert(
n.toBigInt() < 1n << BigInt(nBits),
`n needs to fit into ${nBits} bits, but got ${n.toBigInt()}`
);
let nBigInt = n.toBigInt();
let q = nBigInt >> 64n;
let r = nBigInt - (q << 64n);
return {
querolita marked this conversation as resolved.
Show resolved Hide resolved
remainder: new Field(r),
quotient: new Field(q),
};
}

let [quotient, remainder] = Provable.witness(
provableTuple([Field, Field]),
() => {
let nBigInt = n.toBigInt();
let q = nBigInt >> 64n;
let r = nBigInt - (q << 64n);
return [q, r] satisfies [bigint, bigint];
}
);

if (quotientBits === 1) {
quotient.assertBool();
querolita marked this conversation as resolved.
Show resolved Hide resolved
} else if (quotientBits === 64) {
rangeCheck64(quotient);
} else {
rangeCheckN(quotientBits, quotient);
}
rangeCheck64(remainder);

n.assertEquals(quotient.mul(1n << 64n).add(remainder));

return {
remainder,
quotient,
};
}

function addMod64(x: Field, y: Field) {
return divMod64(x.add(y), 65).remainder;
}
5 changes: 5 additions & 0 deletions src/lib/provable/gadgets/bitwise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
rotate64,
rotate32,
and,
or,
rightShift64,
leftShift64,
leftShift32,
Expand Down Expand Up @@ -173,6 +174,10 @@ function and(a: Field, b: Field, length: number) {
return outputAnd;
}

function or(a: Field, b: Field, length: number) {
boray marked this conversation as resolved.
Show resolved Hide resolved
return not(and(not(a, length), not(b, length), length), length);
}

function rotate64(
field: Field,
bits: number,
Expand Down
Loading
Loading