Skip to content

Commit

Permalink
Fix noir ecdsa verification in acc contract.
Browse files Browse the repository at this point in the history
Fix minor errors.
  • Loading branch information
suyash67 committed Aug 1, 2023
1 parent 584b70f commit fdc8dab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 22 deletions.
18 changes: 3 additions & 15 deletions yarn-project/circuits.js/src/barretenberg/crypto/ecdsa/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
import { numToUInt32BE } from '@aztec/foundation/serialize';
import { IWasmModule } from '@aztec/foundation/wasm';

import { secp256k1 } from '@noble/curves/secp256k1';

import { CircuitsWasm, PrivateKey } from '../../../index.js';
import { Signer } from '../index.js';
import { EcdsaSignature } from './signature.js';
Expand Down Expand Up @@ -47,18 +43,10 @@ export class Ecdsa implements Signer {
this.wasm.writeMemory(mem, msg);
this.wasm.call('ecdsa__construct_signature', mem, msg.length, 0, 32, 64, 96);

// TODO(#913): Understand why this doesn't work
// const sig = new EcdsaSignature(
// Buffer.from(this.wasm.getMemorySlice(32, 64)),
// Buffer.from(this.wasm.getMemorySlice(64, 96)),
// Buffer.from(this.wasm.getMemorySlice(96, 97)),
// );

const signature = secp256k1.sign(msg, privateKey.value);
return new EcdsaSignature(
toBufferBE(signature.r, 32),
toBufferBE(signature.s, 32),
numToUInt32BE(signature.recovery!).subarray(3, 4),
Buffer.from(this.wasm.getMemorySlice(32, 64)),
Buffer.from(this.wasm.getMemorySlice(64, 96)),
Buffer.from(this.wasm.getMemorySlice(96, 97)),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ contract EcdsaAccount {
let public_key = storage.public_key.get_note(&mut context);

// Verify payload signature using Ethereum's signing scheme
// Note that noir expects the hash of the message/challenge as input to the ECDSA verification.
let payload_bytes: [u8; entrypoint::ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] = payload.to_be_bytes();
let challenge: [u8; 32] = std::hash::sha256(payload_bytes);
let verification = std::ecdsa_secp256k1::verify_signature(public_key.x, public_key.y, signature, challenge);
let hashed_challenge: [u8; 32] = std::hash::sha256(challenge);
let verification = std::ecdsa_secp256k1::verify_signature(public_key.x, public_key.y, signature, hashed_challenge);
assert(verification == true);

// debug_log::debug_log_format("Verification result is {0}", [verification as Field]);
// debug_log::debug_log_array_with_prefix("public_key.x", public_key.x);
// debug_log::debug_log_array_with_prefix("public_key.y", public_key.y);
// debug_log::debug_log_array_with_prefix("challenge", challenge);
// debug_log::debug_log_array_with_prefix("signature", signature);

payload.execute_calls(&mut context);

context.finish()
Expand Down

0 comments on commit fdc8dab

Please sign in to comment.