Skip to content

Commit

Permalink
helpers: skip partial sha if ignoreBodyHashCheck is true
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Mar 28, 2024
1 parent dca512e commit e56d39a
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions packages/helpers/src/input-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type CircuitInput = {
in_body_padded?: string[];
in_body_len_padded_bytes?: string;
body_hash_idx?: string;
}
};

export function generateCircuitInputs(params: {
body: Buffer;
Expand All @@ -124,7 +124,7 @@ export function generateCircuitInputs(params: {
maxMessageLength: number;
maxBodyLength: number;
ignoreBodyHashCheck?: boolean;
}) : CircuitInput {
}): CircuitInput {
const {
rsaSignature,
rsaPublicKey,
Expand All @@ -143,33 +143,32 @@ export function generateCircuitInputs(params: {
maxMessageLength
);

// 65 comes from the 64 at the end and the 1 bit in the start, then 63 comes from the formula to round it up to the nearest 64.
// see sha256algorithm.com for a more full explanation of padding length
const bodySHALength = Math.floor((body.length + 63 + 65) / 64) * 64;
const [bodyPadded, bodyPaddedLen] = sha256Pad(
body,
Math.max(maxBodyLength, bodySHALength)
);

const { precomputedSha, bodyRemaining, bodyRemainingLength } =
generatePartialSHA({
body: bodyPadded,
bodyLength: bodyPaddedLen,
selectorString: shaPrecomputeSelector,
maxRemainingBodyLength: maxBodyLength,
});


const circuitInputs : CircuitInput = {
const circuitInputs: CircuitInput = {
in_padded: Uint8ArrayToCharArray(messagePadded), // Packed into 1 byte signals
pubkey: toCircomBigIntBytes(rsaPublicKey),
signature: toCircomBigIntBytes(rsaSignature),
in_len_padded_bytes: messagePaddedLen.toString(),
};

if (!ignoreBodyHashCheck) {
if (!ignoreBodyHashCheck) {
const bodyHashIndex = message.toString().indexOf(bodyHash);

// 65 comes from the 64 at the end and the 1 bit in the start, then 63 comes from the formula to round it up to the nearest 64.
// see sha256algorithm.com for a more full explanation of padding length
const bodySHALength = Math.floor((body.length + 63 + 65) / 64) * 64;
const [bodyPadded, bodyPaddedLen] = sha256Pad(
body,
Math.max(maxBodyLength, bodySHALength)
);

const { precomputedSha, bodyRemaining, bodyRemainingLength } =
generatePartialSHA({
body: bodyPadded,
bodyLength: bodyPaddedLen,
selectorString: shaPrecomputeSelector,
maxRemainingBodyLength: maxBodyLength,
});

circuitInputs.precomputed_sha = Uint8ArrayToCharArray(precomputedSha);
circuitInputs.body_hash_idx = bodyHashIndex.toString();
circuitInputs.in_body_padded = Uint8ArrayToCharArray(bodyRemaining);
Expand Down

0 comments on commit e56d39a

Please sign in to comment.