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

Foreign curve and ECDSA #1007

Merged
merged 113 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
a637e7c
fix example
mitschabaude Jun 28, 2023
1febfbe
initial minimal foreign curve, doesn't work yet
mitschabaude Jun 28, 2023
e012ba6
initial work on test
mitschabaude Jun 28, 2023
addf84c
working bare bones ec with add
mitschabaude Jun 29, 2023
578f444
make curve provable
mitschabaude Jun 29, 2023
4fb98e6
expose common ec methods
mitschabaude Jun 29, 2023
7a228c3
add remaining provable methods
mitschabaude Jun 29, 2023
e241042
correct comments
mitschabaude Jun 29, 2023
6f0a888
minor tweaks
mitschabaude Jun 30, 2023
44779a1
improve api
mitschabaude Jun 30, 2023
26affb1
stub out ecdsa factory
mitschabaude Jun 30, 2023
81b9118
expose ecdsa methods
mitschabaude Jun 30, 2023
1dae1dc
from hex
mitschabaude Jun 30, 2023
005a2aa
start writing test for eth signature
mitschabaude Jun 30, 2023
b237f9c
ecdsa test
mitschabaude Jun 30, 2023
36ad895
minor
mitschabaude Jul 5, 2023
f4e59b2
Merge branch 'feature/foreign-field' into feature/ecdsa
mitschabaude Jul 12, 2023
9b5cf28
bindings
mitschabaude Jul 12, 2023
b530803
renamings
mitschabaude Jul 12, 2023
afce065
add most constant curve impls
mitschabaude Jul 13, 2023
6b43f92
scale
mitschabaude Jul 13, 2023
5781049
constant subgroup check
mitschabaude Jul 13, 2023
5ad6b49
getting non provable curve ops to run
mitschabaude Jul 13, 2023
78c635b
start writing doc comments
mitschabaude Jul 13, 2023
bf9968a
more doc comments, unsafe parameter, proper check()
mitschabaude Jul 13, 2023
61bb485
fox typo and foreign curve check
mitschabaude Jul 13, 2023
499928d
ecdsa doc comments
mitschabaude Jul 13, 2023
f2460f1
implement ecdsa in ts for non-provable
mitschabaude Jul 13, 2023
07a45f7
minor
mitschabaude Jul 13, 2023
6c01efe
subgroup check in constant verification
mitschabaude Jul 13, 2023
2dce057
web compatible benchmark
mitschabaude Jul 13, 2023
3437479
foreign field benchmark
mitschabaude Jul 14, 2023
c033828
Merge branch 'feature/foreign-field' into feature/ecdsa
mitschabaude Nov 30, 2023
45fb41f
Merge branch 'feature/ecdsa-new' into feature/ecdsa
mitschabaude Nov 30, 2023
616e47a
Merge branch 'main' into feature/ecdsa
mitschabaude Nov 30, 2023
5384087
make foreign field struct-friendly
mitschabaude Nov 30, 2023
b9205c4
bindings
mitschabaude Nov 30, 2023
c6989dd
fix foreign curve compilation except the class return
mitschabaude Nov 30, 2023
321fca0
fixup foreign field
mitschabaude Nov 30, 2023
8459851
delete duplicate curve params
mitschabaude Nov 30, 2023
56795cf
foreign field: remove duplicate private method
mitschabaude Nov 30, 2023
bb61f3d
remove private
mitschabaude Nov 30, 2023
c0d8867
compiles
mitschabaude Nov 30, 2023
c85c5ea
tests compile
mitschabaude Nov 30, 2023
6385b9a
Merge branch 'feature/foreign-field' into feature/ecdsa
mitschabaude Nov 30, 2023
d9353d4
remove unnecessary helper
mitschabaude Dec 1, 2023
6f3413a
bindings
mitschabaude Dec 1, 2023
e9d17de
move foreign curve class outside class factory
mitschabaude Dec 1, 2023
13081cc
minor
mitschabaude Dec 1, 2023
190f3e3
negate
mitschabaude Dec 1, 2023
8c09689
support message in foreign field assertMul
mitschabaude Dec 1, 2023
cd9193e
assert on curve
mitschabaude Dec 1, 2023
722cfd6
scale gadget
mitschabaude Dec 1, 2023
a7fc887
Merge branch 'feature/foreign-field' into feature/ecdsa
mitschabaude Dec 4, 2023
e1fa376
Merge branch 'feature/ecdsa-new' into feature/ecdsa
mitschabaude Dec 4, 2023
da21b19
fixup merge
mitschabaude Dec 4, 2023
132d879
support a!=0 in assert on curve
mitschabaude Dec 4, 2023
d953a70
implement subgroup check
mitschabaude Dec 4, 2023
d7707dc
fix assert on curve
mitschabaude Dec 4, 2023
ec7131e
not so slow anymore
mitschabaude Dec 4, 2023
1f7e291
expose cs summary
mitschabaude Dec 4, 2023
0122785
delete bad example
mitschabaude Dec 4, 2023
4b5af0b
remove curve parameter limitations from foreign curve class
mitschabaude Dec 4, 2023
e1282d1
cleanup comments, move redundant unit test to example
mitschabaude Dec 4, 2023
1348779
examples fixup
mitschabaude Dec 4, 2023
6100817
make ecdsa class work
mitschabaude Dec 4, 2023
ecdeea7
move ecdsa class outside factory
mitschabaude Dec 4, 2023
9fe6e59
bindings
mitschabaude Dec 4, 2023
6290202
make type pure
mitschabaude Dec 4, 2023
9dea77f
add ForeignField.random()
mitschabaude Dec 4, 2023
a3577b0
add Ecdsa.sign()
mitschabaude Dec 4, 2023
b35cc61
rewrite ecdsa example using high level API
mitschabaude Dec 4, 2023
5095fac
fix constraint system test flakiness
mitschabaude Dec 4, 2023
6ce500e
Merge branch 'feature/ecdsa-new' into feature/ecdsa
mitschabaude Dec 4, 2023
3e2990c
collateral damage from using higher-level API
mitschabaude Dec 4, 2023
e4ffb48
Merge branch 'feature/ecdsa-new' into feature/ecdsa
mitschabaude Dec 4, 2023
e9c21b0
update benchmark
mitschabaude Dec 4, 2023
a668916
move ecdsa example to where it makes more sense
mitschabaude Dec 4, 2023
8cc376f
doccomment tweaks
mitschabaude Dec 4, 2023
4333762
revert unnecessary change
mitschabaude Dec 4, 2023
c6c39cc
save constraints
mitschabaude Dec 4, 2023
9590840
add ec gadgets
mitschabaude Dec 4, 2023
49b98c5
adapt constant case in multiscalarmul, remove the one in scale
mitschabaude Dec 4, 2023
cbd335f
remove testing code
mitschabaude Dec 4, 2023
9ebd5f3
start writing ec gadgets tests
mitschabaude Dec 4, 2023
07a1fde
simplify build scripts
mitschabaude Dec 5, 2023
502a497
equivalent: add verbose option and onlyif combinator
mitschabaude Dec 5, 2023
578a8ad
fix ec test
mitschabaude Dec 5, 2023
a7f997b
add assert on curve
mitschabaude Dec 5, 2023
7e83c43
minor
mitschabaude Dec 5, 2023
43eadf7
minor console formatting
mitschabaude Dec 5, 2023
78e0c63
standardize ec gadgets inputs
mitschabaude Dec 5, 2023
d32ad75
make record forward provable
mitschabaude Dec 5, 2023
24d3a32
add missing label
mitschabaude Dec 5, 2023
c07857f
verbose ecdsa test
mitschabaude Dec 5, 2023
80d10a6
catch missing provable
mitschabaude Dec 5, 2023
d29a9b5
fix ecdsa unit test
mitschabaude Dec 5, 2023
40cb374
remove use of build:node
mitschabaude Dec 5, 2023
ce82667
remove use of build:node
mitschabaude Dec 5, 2023
81d0047
improve comments
mitschabaude Dec 5, 2023
818046c
flesh out doccomments
mitschabaude Dec 5, 2023
3d14a4e
move dangerous foreign field equals
mitschabaude Dec 5, 2023
dfc7e97
change to safe equals() methods in foreign field, rename assertCanonical
mitschabaude Dec 5, 2023
0403912
add safe ec addition
mitschabaude Dec 5, 2023
abe4d56
signature to bigint
mitschabaude Dec 5, 2023
3a3cce3
adapt unit test
mitschabaude Dec 5, 2023
9bcb6fd
delete ec and ecdsa gadgets namespaces
mitschabaude Dec 5, 2023
31282ea
changelog
mitschabaude Dec 5, 2023
db8f63a
Revert "remove ecdsa from vk test for now"
mitschabaude Dec 6, 2023
120ae94
Merge branch 'tmp' into feature/ecdsa
mitschabaude Dec 6, 2023
0a96141
Merge branch 'main' into feature/ecdsa
mitschabaude Dec 7, 2023
62091b8
dump vks
mitschabaude Dec 7, 2023
5430dc4
Merge branch 'main' into feature/ecdsa
mitschabaude Dec 12, 2023
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
16 changes: 8 additions & 8 deletions .github/actions/live-tests-shared/action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: "Shared steps for live testing jobs"
description: "Shared steps for live testing jobs"
name: 'Shared steps for live testing jobs'
description: 'Shared steps for live testing jobs'
inputs:
mina-branch-name:
description: "Mina branch name in use by service container"
description: 'Mina branch name in use by service container'
required: true
runs:
using: "composite"
using: 'composite'
steps:
- name: Wait for Mina network readiness
uses: o1-labs/wait-for-mina-network-action@v1
Expand All @@ -16,15 +16,15 @@ runs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
node-version: '20'
- name: Build o1js and execute tests
env:
TEST_TYPE: "Live integration tests"
USE_CUSTOM_LOCAL_NETWORK: "true"
TEST_TYPE: 'Live integration tests'
USE_CUSTOM_LOCAL_NETWORK: 'true'
run: |
git submodule update --init --recursive
npm ci
npm run build:node
npm run build
touch profiling.md
sh run-ci-tests.sh
cat profiling.md >> $GITHUB_STEP_SUMMARY
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: |
git submodule update --init --recursive
npm ci
npm run build:node
npm run build
touch profiling.md
sh run-ci-tests.sh
cat profiling.md >> $GITHUB_STEP_SUMMARY
Expand Down Expand Up @@ -91,7 +91,7 @@ jobs:
run: |
git submodule update --init --recursive
npm ci
npm run build:node
npm run build
- name: Publish to NPM if version has changed
uses: JS-DevTools/npm-publish@v1
if: github.ref == 'refs/heads/main'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased](https://github.com/o1-labs/o1js/compare/7acf19d0d...HEAD)

### Added

- Non-native elliptic curve operations exposed through `createForeignCurve()` class factory https://github.com/o1-labs/o1js/pull/1007
- **ECDSA signature verification** exposed through `createEcdsa()` class factory https://github.com/o1-labs/o1js/pull/1240 https://github.com/o1-labs/o1js/pull/1007
- For an example, see `./src/examples/crypto/ecdsa`

## [0.15.0](https://github.com/o1-labs/o1js/compare/1ad7333e9e...7acf19d0d)

### Breaking changes
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,17 @@
"node": ">=16.4.0"
},
"scripts": {
"dev": "npx tsc -p tsconfig.node.json && node src/build/copy-to-dist.js",
"dev": "npx tsc -p tsconfig.test.json && node src/build/copy-to-dist.js",
"make": "make -C ../../.. snarkyjs",
"make:no-types": "npm run clean && make -C ../../.. snarkyjs_no_types",
"wasm": "./src/bindings/scripts/update-wasm-and-types.sh",
"bindings": "cd ../../.. && ./scripts/update-snarkyjs-bindings.sh && cd src/lib/snarkyjs",
"build": "node src/build/copy-artifacts.js && rimraf ./dist/node && npm run dev && node src/build/buildNode.js",
"build:test": "npx tsc -p tsconfig.test.json && cp src/snarky.d.ts dist/node/snarky.d.ts",
"build:node": "npm run build",
"build:web": "rimraf ./dist/web && node src/build/buildWeb.js",
"build:examples": "npm run build && rimraf ./dist/examples && npx tsc -p tsconfig.examples.json",
"build:docs": "npx typedoc --tsconfig ./tsconfig.web.json",
"prepublish:web": "NODE_ENV=production node src/build/buildWeb.js",
"prepublish:node": "npm run build && NODE_ENV=production node src/build/buildNode.js",
"prepublish:node": "node src/build/copy-artifacts.js && rimraf ./dist/node && npx tsc -p tsconfig.node.json && node src/build/copy-to-dist.js && NODE_ENV=production node src/build/buildNode.js",
"prepublishOnly": "npm run prepublish:web && npm run prepublish:node",
"dump-vks": "./run tests/vk-regression/vk-regression.ts --bundle --dump",
"format": "prettier --write --ignore-unknown **/*",
Expand Down
3 changes: 1 addition & 2 deletions run-unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
set -e
shopt -s globstar # to expand '**' into nested directories./

# run the build:test
npm run build:test
npm run build

# find all unit tests in dist/node and run them
# TODO it would be nice to make this work on Mac
Expand Down
2 changes: 1 addition & 1 deletion src/bindings
2 changes: 1 addition & 1 deletion src/examples/api_exploration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ console.assert(!signature.verify(pubKey, msg1).toBoolean());

/* You can initialize elements as literals as follows: */
let g0 = Group.from(-1, 2);
let g1 = new Group({ x: -2, y: 2 });
let g1 = new Group({ x: -1, y: 2 });

/* There is also a predefined generator. */
let g2 = Group.generator;
Expand Down
31 changes: 31 additions & 0 deletions src/examples/benchmarks/foreign-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Crypto, Provable, createForeignField } from 'o1js';

class ForeignScalar extends createForeignField(
Crypto.CurveParams.Secp256k1.modulus
) {}

function main() {
let s = Provable.witness(
ForeignScalar.Canonical.provable,
ForeignScalar.random
);
let t = Provable.witness(
ForeignScalar.Canonical.provable,
ForeignScalar.random
);
s.mul(t);
}

console.time('running constant version');
main();
console.timeEnd('running constant version');

console.time('running witness generation & checks');
Provable.runAndCheck(main);
console.timeEnd('running witness generation & checks');

console.time('creating constraint system');
let cs = Provable.constraintSystem(main);
console.timeEnd('creating constraint system');

console.log(cs.summary());
1 change: 1 addition & 0 deletions src/examples/crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
These examples show how to use some of the crypto primitives that are supported in provable o1js code.

- Non-native field arithmetic: `foreign-field.ts`
- Non-native ECDSA verification: `ecdsa.ts`
22 changes: 22 additions & 0 deletions src/examples/crypto/ecdsa/ecdsa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ZkProgram, Crypto, createEcdsa, createForeignCurve, Bool } from 'o1js';

export { ecdsaProgram, Secp256k1, Ecdsa };

class Secp256k1 extends createForeignCurve(Crypto.CurveParams.Secp256k1) {}
class Scalar extends Secp256k1.Scalar {}
class Ecdsa extends createEcdsa(Secp256k1) {}

const ecdsaProgram = ZkProgram({
name: 'ecdsa',
publicInput: Scalar.provable,
publicOutput: Bool,

methods: {
verifyEcdsa: {
privateInputs: [Ecdsa.provable, Secp256k1.provable],
method(msgHash: Scalar, signature: Ecdsa, publicKey: Secp256k1) {
return signature.verify(msgHash, publicKey);
},
},
},
});
33 changes: 33 additions & 0 deletions src/examples/crypto/ecdsa/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Secp256k1, Ecdsa, ecdsaProgram } from './ecdsa.js';
import assert from 'assert';

// create an example ecdsa signature

let privateKey = Secp256k1.Scalar.random();
let publicKey = Secp256k1.generator.scale(privateKey);

// TODO use an actual keccak hash
let messageHash = Secp256k1.Scalar.random();

let signature = Ecdsa.sign(messageHash.toBigInt(), privateKey.toBigInt());

// investigate the constraint system generated by ECDSA verify

console.time('ecdsa verify (build constraint system)');
let cs = ecdsaProgram.analyzeMethods().verifyEcdsa;
console.timeEnd('ecdsa verify (build constraint system)');

console.log(cs.summary());

// compile and prove

console.time('ecdsa verify (compile)');
await ecdsaProgram.compile();
console.timeEnd('ecdsa verify (compile)');

console.time('ecdsa verify (prove)');
let proof = await ecdsaProgram.verifyEcdsa(messageHash, signature, publicKey);
console.timeEnd('ecdsa verify (prove)');

proof.publicOutput.assertTrue('signature verifies');
assert(await ecdsaProgram.verify(proof), 'proof verifies');
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export {
AlmostForeignField,
CanonicalForeignField,
} from './lib/foreign-field.js';
export { createForeignCurve, ForeignCurve } from './lib/foreign-curve.js';
export { createEcdsa, EcdsaSignature } from './lib/foreign-ecdsa.js';
export { Poseidon, TokenSymbol } from './lib/hash.js';
export * from './lib/signature.js';
export type {
Expand Down
6 changes: 0 additions & 6 deletions src/lib/circuit_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export {
cloneCircuitValue,
circuitValueEquals,
toConstant,
isConstant,
InferProvable,
HashInput,
InferJson,
Expand Down Expand Up @@ -693,8 +692,3 @@ function toConstant<T>(type: Provable<T>, value: T): T {
type.toAuxiliary(value)
);
}

function isConstant<T>(type: FlexibleProvable<T>, value: T): boolean;
function isConstant<T>(type: Provable<T>, value: T): boolean {
return type.toFields(value).every((x) => x.isConstant());
}
Loading