@nori-zk/o1js-zk-utils
Version:
o1js-zk-utils supporting Nori Bridge
88 lines • 5 kB
JavaScript
import { Logger, LogPrinter } from 'esm-iso-logger';
import { ContractDepositAttestorInput, ContractDepositAttestor, buildContractDepositLeaves, getContractDepositWitness, ContractDeposit, provableStorageSlotLeafHash, } from './ContractDepositAttestor.js';
import { sp1ConsensusMPTPlonkProof } from './test-examples/sp1-mpt-proof/sp1ProofMessage.js';
import { Bytes32 } from './types.js';
import { computeMerkleTreeDepthAndSize, foldMerkleLeft, getMerkleZeros, } from './merkle-attestor/merkleTree.js';
import { Field, UInt64 } from 'o1js';
import { createTimer, decodeConsensusMptProof, fieldToHexLE, uint8ArrayToBigIntBE, } from './utils.js';
const logger = new Logger('ContractDepositAttestor');
new LogPrinter('TestO1JsZkUtils');
describe('Contract Storage Slot Deposit Attestor Test', () => {
test('contract_deposit_pipeline', async () => {
// Analyse zk program
const contractDepositAttestorAnalysis = await ContractDepositAttestor.analyzeMethods();
logger.log(`ContractDepositAttestor analyze methods gates length '${contractDepositAttestorAnalysis.compute.gates.length}'.`);
// Build zk program
const { verificationKey } = await ContractDepositAttestor.compile({
forceRecompile: true,
});
logger.log(`ContractDepositAttestor contract compiled vk: '${verificationKey.hash}'.`);
// Build contractStorageSlot from sp1 mpt message.
const contractStorageSlots = sp1ConsensusMPTPlonkProof.contract_storage_slots.map((slot) => {
const codeChallenge = Bytes32.fromHex(slot.slot_key_code_challenge
.slice(2)
.padStart(64, '0'));
const valuePad = slot.value.slice(2).padStart(64, '0');
console.log('padded value', valuePad);
const value = Bytes32.fromHex(valuePad);
return new ContractDeposit({
codeChallenge,
value,
});
});
// Build leaves
const leaves = buildContractDepositLeaves(contractStorageSlots);
// Pick an index
let index = sp1ConsensusMPTPlonkProof.contract_storage_slots.length - 1;
// Find Value
const slotToFind = contractStorageSlots.find((_, idx) => idx === index);
if (!slotToFind)
throw new Error(`Slot at ${index} not found`);
console.log('provableStorageSlotLeafHash', provableStorageSlotLeafHash(slotToFind).toBigInt().toString(16));
// Compute path
const path = getContractDepositWitness([...leaves], index);
//console.log('path', path._dummyMask());
// Compute root
const { depth, paddedSize } = computeMerkleTreeDepthAndSize(leaves.length);
const rootHash = foldMerkleLeft(leaves, paddedSize, depth, getMerkleZeros(depth));
// Build ZK input
const input = new ContractDepositAttestorInput({
path,
index: UInt64.from(index),
value: slotToFind,
});
logger.log(`Generated input ${JSON.stringify(input)}`);
// Prove deposit with sample data.
const timer = createTimer();
const output = await ContractDepositAttestor.compute(input);
logger.log(`ContractDepositAttestor.compute took ${timer()}`);
const decodedProof = decodeConsensusMptProof(sp1ConsensusMPTPlonkProof.proof);
const decodedProofContractDepositRootBigInt = uint8ArrayToBigIntBE(// Could do LE without the reverse FIXME
decodedProof.verifiedContractDepositsRoot.toBytes().reverse());
console.log(decodedProofContractDepositRootBigInt, //.toString(16),
output.proof.publicOutput.toBigInt(), //.toString(16),
rootHash.toBigInt());
expect(output.proof.publicOutput.toBigInt()).toBe(rootHash.toBigInt());
expect(decodedProofContractDepositRootBigInt).toEqual(output.proof.publicOutput.toBigInt());
});
test('attestation_hash_calculation', () => {
const dummyAttestationField = new Field(101);
// Convert this field into words
let dummyAttestationHex = fieldToHexLE(dummyAttestationField);
console.log('dummyAttestationHex', dummyAttestationHex);
});
test('attestation_hash_calculation_2', () => {
const dummyAttestationField = new Field(1000000500000000);
// Convert this field into words
let dummyAttestationHex = fieldToHexLE(dummyAttestationField);
console.log('dummyAttestationHex', dummyAttestationHex);
});
test('attestation_hash_calculation_3', () => {
const dummyAttestationField = new Field(21664331661517759511819594545016066304824539159186122091565436915814825459759n);
// Convert this field into words
let dummyAttestationHex = fieldToHexLE(dummyAttestationField);
console.log('dummyAttestationHex', dummyAttestationHex);
// 0x2f000000000000000000000000000000000000000000000000038d7ec293e52f
});
});
//# sourceMappingURL=ContractDepositAttestor.spec.js.map