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

feat: groth16 #12

Draft
wants to merge 3 commits into
base: feat/ecdsa
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@axiom-crypto/tools",
"version": "2.1.1-alpha.2",
"version": "2.1.1-alpha.3",
"description": "Useful data, field, and byte manipulation tools for Axiom.",
"author": "Intrinsic Technologies",
"license": "MIT",
Expand Down Expand Up @@ -41,4 +41,4 @@
"tsup": "^7.2.0",
"typescript": "^5.1.6"
}
}
}
22 changes: 22 additions & 0 deletions src/codec/v2/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
AxiomV2FeeData,
AxiomV2FullQuery,
ECDSASubquery,
Groth16Subquery
} from "./types";

/**
Expand Down Expand Up @@ -215,6 +216,17 @@ export function decodeDataQuery(reader: ByteStringReader): AxiomV2DataQuery | nu
subqueryData: ecdsaSubquery,
});
break;
case DataSubqueryType.Groth16:
const groth16Subquery = decodeGroth16Subquery(reader);
if (!groth16Subquery) {
console.warn(`Unable to decode Groth16 subquery at index ${reader.currentIdx}`);
return null;
}
subqueries.push({
type: DataSubqueryType.Groth16,
subqueryData: groth16Subquery,
});
break;
default:
throw new Error(`Unknown subquery type ${type} at index ${reader.currentIdx}`);
}
Expand Down Expand Up @@ -389,3 +401,13 @@ export function decodeECDSASubquery(reader: ByteStringReader): ECDSASubquery | n
msgHash,
};
}

export function decodeGroth16Subquery(reader: ByteStringReader): Groth16Subquery | null {
let bytes: string[] = [];
for (let i = 0; i < AxiomV2CircuitConstant.MaxSubqueryInputs; i++) {
bytes.push(reader.readBytes(32));
}
return {
bytes,
};
}
14 changes: 14 additions & 0 deletions src/codec/v2/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DataSubquery,
DataSubqueryType,
ECDSASubquery,
Groth16Subquery,
HeaderSubquery,
ReceiptSubquery,
SolidityNestedMappingSubquery,
Expand Down Expand Up @@ -343,6 +344,10 @@ export function encodeDataSubquery(subquery: DataSubquery): string {
const { pubkey, r, s, msgHash } = subquery.subqueryData as ECDSASubquery;
encodedSubquery = encodeECDSASubquery(pubkey, r, s, msgHash);
break;
case DataSubqueryType.Groth16:
const groth16Subquery = subquery.subqueryData as Groth16Subquery;
encodedSubquery = encodeGroth16Subquery(groth16Subquery.bytes);
break;
default:
throw new Error("Invalid subquery type");
}
Expand Down Expand Up @@ -560,3 +565,12 @@ export function encodeECDSASubquery(
[pubkey[0], pubkey[1], r, s, msgHash],
);
}

/**
* @param bytes An array of bytes32 strings.
*/
export function encodeGroth16Subquery(bytes: string[]): string {
rpalakkal marked this conversation as resolved.
Show resolved Hide resolved
const types = new Array(AxiomV2CircuitConstant.MaxSubqueryInputs).fill("bytes32");
bytes.forEach(validateBytes32);
return ethers.solidityPacked(types, bytes);
}
6 changes: 6 additions & 0 deletions src/codec/v2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export interface ECDSASubquery extends Subquery {
msgHash: string;
}

export interface Groth16Subquery extends Subquery {
/// An array of bytes32 strings
bytes: string[];
rpalakkal marked this conversation as resolved.
Show resolved Hide resolved
}

export interface BeaconValidatorSubquery extends Subquery {
// WIP
}
Expand All @@ -150,6 +155,7 @@ export enum DataSubqueryType {
Receipt,
SolidityNestedMapping,
ECDSA,
Groth16,
}

export enum HeaderField {
Expand Down
203 changes: 110 additions & 93 deletions test/codec/v2/data/codecData.ts

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions test/codec/v2/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import {
bytes32,
decodeFullQueryV2,
decodeECDSASubquery,
decodeGroth16Subquery,
} from "../../../src";
import {
BLOCK_NUMBER,
GROTH16_SUBQUERY,
MSGHASH,
PUBKEY,
R,
Expand All @@ -49,6 +51,7 @@ import {
encodedEcdsaSubquery,
encodedFullQuery,
encodedFullQueryInvalidVersion,
encodedGroth16Subquery,
encodedHeaderSubquery,
encodedQuery,
encodedQueryNoCompute,
Expand Down Expand Up @@ -256,6 +259,15 @@ describe("Decoder V2", () => {
expect(decodedQuery.msgHash).toEqual(MSGHASH);
});

test("Decoding a Groth16 subquery", () => {
const reader = new ByteStringReader(encodedGroth16Subquery);
const decodedQuery = decodeGroth16Subquery(reader);
if (!decodedQuery) {
throw new Error("Decoding failed");
}
expect(decodedQuery.bytes).toEqual(GROTH16_SUBQUERY);
})

test("Decoding a DataQuery", () => {
const reader = new ByteStringReader(encodedDataQuery);
const dataQuery = decodeDataQuery(reader);
Expand Down
9 changes: 9 additions & 0 deletions test/codec/v2/encode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ import {
ReceiptSubquery,
SolidityNestedMappingSubquery,
encodeECDSASubquery,
encodeGroth16Subquery,
} from "../../../src";
import {
BLOCK_NUMBER,
GROTH16_SUBQUERY,
MSGHASH,
PUBKEY,
R,
Expand All @@ -53,6 +55,7 @@ import {
dataQueryHash,
dataSubqueries,
encodedEcdsaSubquery,
encodedGroth16Subquery,
extraData,
feeData,
k,
Expand Down Expand Up @@ -269,6 +272,12 @@ describe("Encoder V2", () => {
expect(encoded).toEqual(encodedEcdsaSubquery);
});

test("Encoding a Groth16 subquery", () => {
const bytes = GROTH16_SUBQUERY;
const encoded = encodeGroth16Subquery(bytes);
expect(encoded).toEqual(encodedGroth16Subquery);
});

test("Encoding a DataQuery", () => {
const blockNumber = BLOCK_NUMBER;
const fieldIdx = 0;
Expand Down
Loading