Skip to content

Commit

Permalink
updating parseUtils.ts and adding test to risc0 proof
Browse files Browse the repository at this point in the history
  • Loading branch information
luiz-lvj committed Oct 25, 2024
1 parent 1c85ee9 commit fb984e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ interface ReceiptClaim {
tagDigest?: Uint8Array;
}

export class KeyPatternNotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = "KeyPatternNotFoundError";
}
}



Expand Down Expand Up @@ -188,17 +194,24 @@ export const parseGroth16ProofFromObject = (data: any, publicInputsData?: bigint

try{

console.log("AQUI BEFORE")

const sealHex = toHexStr(findItemFromKeyPatterns(proof, ['seal']));
const imageIdHex = toHexStr(findItemFromKeyPatterns(proof, [ 'image_id']));
const journalHex = toHexStr(findItemFromKeyPatterns(proof, ['journal']));

console.log("journalHex: ", journalHex);

console.log("LATER: ", journalHex);

const sealBytes = hexStringToBytes(sealHex);
const imageIdBytes = hexStringToBytes(imageIdHex);
const journalBytes = hexStringToBytes(journalHex);

return createGroth16ProofFromRisc0(sealBytes, imageIdBytes, journalBytes)

} catch(err){
console.log("ERROR AQUI: ", err);

}

Expand All @@ -217,7 +230,12 @@ export const parseGroth16ProofFromObject = (data: any, publicInputsData?: bigint
throw new Error(`Invalid public inputs format: ${publicInputsData}`);
}
} else{
publicInputs = findItemFromKeyPatterns(data, ['public']);

try {
publicInputs = findItemFromKeyPatterns(data, ['public']);
} catch(err){
throw new Error(`Error: ${err}`);
}
}
const a = tryParseG1PointFromKey(proof, ['a'], curveId);
const b = tryParseG2PointFromKey(proof, ['b'], curveId);
Expand Down Expand Up @@ -246,14 +264,17 @@ export const parseGroth16ProofFromObject = (data: any, publicInputsData?: bigint
export const createGroth16ProofFromRisc0 = (seal: Uint8Array, imageId: Uint8Array, journalBytes: Uint8Array,
controlRoot: bigint = RISC0_CONTROL_ROOT, bn254ControlId: bigint = RISC0_BN254_CONTROL_ID): Groth16Proof => {

if(imageId.length <= 32){
if(imageId.length > 32){
throw new Error("imageId must be 32 bytes")
}

const [constrolRoot0, controlRoot1] = splitDigest(controlRoot);

const proof = seal.slice(4);


const journal = createHash("sha256").update(journalBytes).digest();

const claimDigest = digestReceiptClaim(ok(imageId, journal));

const [claim0, claim1] = splitDigest(claimDigest);
Expand Down Expand Up @@ -330,6 +351,11 @@ export const checkGroth16VerifyingKey = (vk: Groth16VerifyingKey): boolean => {
const digestReceiptClaim = (receipt: ReceiptClaim): Uint8Array => {
const { tagDigest, input, preStateDigest, postStateDigest, output, exitCode } = receipt;

console.log("receipt: ", receipt);
console.log("tagDigest: ", tagDigest);



// Concatenating all parts into one Buffer
const data = Buffer.concat([
tagDigest!,
Expand Down Expand Up @@ -362,6 +388,7 @@ function ok(imageId: Uint8Array, journalDigest: Uint8Array): ReceiptClaim {

// Create and return the ReceiptClaim object
return {
tagDigest: createHash('sha256').update(Buffer.from("risc0.ReceiptClaim")).digest(),
preStateDigest: imageId,
postStateDigest: SYSTEM_STATE_ZERO_DIGEST,
exitCode,
Expand Down Expand Up @@ -494,7 +521,7 @@ const findItemFromKeyPatterns = (data: { [key: string]: any }, keyPatterns: stri
if(bestMatch){
return bestMatch;
}
throw new Error(`No key found with patterns ${keyPatterns}`);
throw new KeyPatternNotFoundError(`No key found with patterns ${keyPatterns}`);
}

export const getPFromCurveId = (curveId: CurveId): bigint => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseGroth16ProofFromJson, parseGroth16VerifyingKeyFromJson } from "../../src/node/starknet/groth16ContractGenerator/parsingUtils";
import { parseGroth16VerifyingKeyFromJson, parseGroth16ProofFromJson } from "../../src/node/starknet/groth16ContractGenerator/parsingUtils";

const PATH = '../../../hydra/garaga/starknet/groth16_contract_generator/examples';

Expand All @@ -22,6 +22,7 @@ describe('Groth16 Parsing Tests', () => {
const proofPaths = [
`${PATH}/proof_bn254.json`,
`${PATH}/proof_bls.json`,
`${PATH}/proof_risc0.json`,
];

test.each(proofPaths)('should parse proof from %s', (proofPath) => {
Expand Down

0 comments on commit fb984e9

Please sign in to comment.