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

Tests differential schnorr verify #44

Merged
merged 2 commits into from
Aug 13, 2024
Merged
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
2 changes: 1 addition & 1 deletion docs/Management.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $ forge script \
--password "$KEYSTORE_PASSWORD" \
--broadcast \
--rpc-url "$RPC_URL" \
--sig $(cast calldata "lift(address,uint,uint,uint8,bytes32,bytes32)" "$SCRIBE" "$PUBLIC_KEY_X_COORDINATE" "$PUBLIC_KEY_Y_COORDINATE" "$ECDSA_V $ECDSA_R" "$ECDSA_S") \
--sig $(cast calldata "lift(address,uint,uint,uint8,bytes32,bytes32)" "$SCRIBE" "$PUBLIC_KEY_X_COORDINATE" "$PUBLIC_KEY_Y_COORDINATE" "$ECDSA_V" "$ECDSA_R" "$ECDSA_S") \
-vvv \
script/${SCRIBE_FLAVOUR}.s.sol:${SCRIBE_FLAVOUR}Script
```
Expand Down
34 changes: 34 additions & 0 deletions script/libs/LibOracleSuite.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity ^0.8.16;

import {Vm} from "forge-std/Vm.sol";

import {LibSecp256k1} from "src/libs/LibSecp256k1.sol";

/**
* @title LibOracleSuite
*
Expand Down Expand Up @@ -42,6 +44,38 @@ library LibOracleSuite {
return (signature, commitment);
}

/// @dev Verifies public key `pubKey` signs via `signature` and `commitment`
/// message `message`.
///
/// Verified via:
/// ```bash
/// $ ./bin/schnorr verify \
/// <message> \
/// <pubKey.x> \
/// <pubKey.y> \
/// <signature> \
/// <commitment>
/// ```
function verify(
LibSecp256k1.Point memory pubKey,
bytes32 message,
bytes32 signature,
address commitment
) internal returns (bool) {
string[] memory inputs = new string[](7);
inputs[0] = "bin/schnorr";
inputs[1] = "verify";
inputs[2] = vm.toString(message);
inputs[3] = vm.toString(pubKey.x);
inputs[4] = vm.toString(pubKey.y);
inputs[5] = vm.toString(signature);
inputs[6] = vm.toString(commitment);

uint result = abi.decode(vm.ffi(inputs), (uint));

return result == 1;
}

/// @dev Constructs poke message for `wat` with value `val` and age `age`.
///
/// Constructed via:
Expand Down
51 changes: 38 additions & 13 deletions test/LibSchnorrTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,46 @@ abstract contract LibSchnorrTest is Test {
}

// Create signature via oracle-suite.
uint signature;
address commitment;
(signature, commitment) = LibOracleSuite.sign(privKeys, message);

// IMPORTANT: Don't do anything if signature is invalid.
if (signature == 0) {
console2.log("Signature is zero");
return;
}
uint signatureSuite;
address commitmentSuite;
(signatureSuite, commitmentSuite) =
LibOracleSuite.sign(privKeys, message);

// Create signature via LibSchnorrExtended.
uint signatureLibSchnorr;
address commitmentLibSchnorr;
(signatureLibSchnorr, commitmentLibSchnorr) =
LibSchnorrExtended.signMessage(privKeys, message);

// Expect both signatures to be verifiable via LibSchnorr.
assertTrue(
LibSchnorr.verifySignature(
aggPubKey, message, bytes32(signatureSuite), commitmentSuite
)
);
assertTrue(
LibSchnorr.verifySignature(
aggPubKey,
message,
bytes32(signatureLibSchnorr),
commitmentLibSchnorr
)
);

// Expect oracle-suite's signature to be verifiable.
bool ok = LibSchnorr.verifySignature(
aggPubKey, message, bytes32(signature), commitment
// Expect both signatures to be verifiable via oracle-suite.
assertTrue(
LibOracleSuite.verify(
aggPubKey, message, bytes32(signatureSuite), commitmentSuite
)
);
assertTrue(
LibOracleSuite.verify(
aggPubKey,
message,
bytes32(signatureLibSchnorr),
commitmentLibSchnorr
)
);
assertTrue(ok);
}

function testFuzz_verifySignature_SingleSigner(
Expand Down
Loading